diff --git a/CMakeLists.txt b/CMakeLists.txt index 059cb2337faa..9e9e31f019cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,6 +186,9 @@ option(CIRCT_BUILD_TOOLS "Build the CIRCT tools. If OFF, just generate build tar set(CIRCT_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')") +option(CIRCT_INCLUDE_TESTS + "Generate build targets for the CIRCT unit tests." ON) + list(APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR}/cmake/modules") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") include(AddCIRCT) @@ -618,16 +621,19 @@ endif() # Directory setup #------------------------------------------------------------------------------- +if (CIRCT_INCLUDE_TESTS) + add_definitions(-DCIRCT_INCLUDE_TESTS) + if (CIRCT_GTEST_AVAILABLE) + add_subdirectory(unittests) + endif() + add_subdirectory(test) + add_subdirectory(integration_test) +endif() add_subdirectory(include/circt) add_subdirectory(lib) if(CIRCT_INCLUDE_TOOLS) add_subdirectory(tools) endif() -if (CIRCT_GTEST_AVAILABLE) - add_subdirectory(unittests) -endif() -add_subdirectory(test) -add_subdirectory(integration_test) add_subdirectory(frontends) option(CIRCT_INCLUDE_DOCS "Generate build targets for the CIRCT docs.") diff --git a/include/circt/Dialect/CMakeLists.txt b/include/circt/Dialect/CMakeLists.txt index 150fb9b28b46..8e859b82a196 100644 --- a/include/circt/Dialect/CMakeLists.txt +++ b/include/circt/Dialect/CMakeLists.txt @@ -28,6 +28,9 @@ add_subdirectory(MSFT) add_subdirectory(OM) add_subdirectory(Pipeline) add_subdirectory(RTG) +if (CIRCT_INCLUDE_TESTS) + add_subdirectory(RTGTest) +endif() add_subdirectory(Ibis) add_subdirectory(Seq) add_subdirectory(Sim) diff --git a/include/circt/Dialect/RTGTest/CMakeLists.txt b/include/circt/Dialect/RTGTest/CMakeLists.txt new file mode 100644 index 000000000000..f33061b2d87c --- /dev/null +++ b/include/circt/Dialect/RTGTest/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(IR) diff --git a/include/circt/Dialect/RTGTest/IR/CMakeLists.txt b/include/circt/Dialect/RTGTest/IR/CMakeLists.txt new file mode 100644 index 000000000000..fe85e00050a2 --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/CMakeLists.txt @@ -0,0 +1,6 @@ +add_circt_dialect(RTGTest rtgtest) + +set(LLVM_TARGET_DEFINITIONS RTGTest.td) + +add_circt_dialect_doc(RTGTest rtgtest) + diff --git a/include/circt/Dialect/RTGTest/IR/RTGTest.td b/include/circt/Dialect/RTGTest/IR/RTGTest.td new file mode 100644 index 000000000000..bb4cc711a052 --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTest.td @@ -0,0 +1,26 @@ +//===- RTGTest.td - RTGTest top-level definition -----------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This is the top level file for the RTGTest dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTEST_TD +#define CIRCT_DIALECT_RTGTEST_IR_RTGTEST_TD + +include "mlir/IR/OpBase.td" +include "mlir/Interfaces/SideEffectInterfaces.td" +include "mlir/IR/OpAsmInterface.td" + +include "circt/Dialect/RTG/IR/RTGInterfaces.td" + +include "circt/Dialect/RTGTest/IR/RTGTestDialect.td" +include "circt/Dialect/RTGTest/IR/RTGTestTypes.td" +include "circt/Dialect/RTGTest/IR/RTGTestOps.td" + +#endif // CIRCT_DIALECT_RTGTEST_IR_RTGTEST_TD diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestDialect.h b/include/circt/Dialect/RTGTest/IR/RTGTestDialect.h new file mode 100644 index 000000000000..d9b667e2a4df --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTestDialect.h @@ -0,0 +1,24 @@ +//===- RTGTestDialect.h - RTG Test dialect declaration ----------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the RTGTest MLIR dialect. This dialect defines a minimal +// set of operations to use for testing the RTG dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_H +#define CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_H + +#include "circt/Support/LLVM.h" +#include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/Dialect.h" + +// Pull in the Dialect definition. +#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h.inc" + +#endif // CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_H diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestDialect.td b/include/circt/Dialect/RTGTest/IR/RTGTestDialect.td new file mode 100644 index 000000000000..1c9eaef9a1bc --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTestDialect.td @@ -0,0 +1,36 @@ +//===- RTGTestDialect.td - RTG Test dialect definition -----*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the RTG Test dialect. This dialect provides a minimal +// set of operations to test the RTG dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_TD +#define CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_TD + +include "mlir/IR/DialectBase.td" + +def RTGTestDialect : Dialect { + let name = "rtgtest"; + + let summary = "types and operations for random test generation testing"; + let description = [{ + This dialect defines the `rtgtest` dialect, which provides a set of + operation definitions to test the RTG dialect. + }]; + + let useDefaultTypePrinterParser = 1; + let cppNamespace = "::circt::rtgtest"; + + let extraClassDeclaration = [{ + void registerTypes(); + }]; +} + +#endif // CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_TD diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestOps.h b/include/circt/Dialect/RTGTest/IR/RTGTestOps.h new file mode 100644 index 000000000000..365165bcb853 --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTestOps.h @@ -0,0 +1,30 @@ +//===- RTGTestOps.h - Declare RTGTest dialect operations --------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file declares the operation classes for the RTGTest dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTESTOPS_H +#define CIRCT_DIALECT_RTGTEST_IR_RTGTESTOPS_H + +#include "circt/Dialect/RTG/IR/RTGOpInterfaces.h" +#include "circt/Dialect/RTG/IR/RTGOps.h" +#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h" +#include "circt/Dialect/RTGTest/IR/RTGTestTypes.h" +#include "circt/Support/LLVM.h" +#include "mlir/IR/OpImplementation.h" + +//===----------------------------------------------------------------------===// +// TableGen generated logic. +//===----------------------------------------------------------------------===// + +#define GET_OP_CLASSES +#include "circt/Dialect/RTGTest/IR/RTGTest.h.inc" + +#endif // CIRCT_DIALECT_RTGTEST_IR_RTGTESTOPS_H diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestOps.td b/include/circt/Dialect/RTGTest/IR/RTGTestOps.td new file mode 100644 index 000000000000..401f3d79beae --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTestOps.td @@ -0,0 +1,36 @@ +//===- RTGTestOps.td - RTGTest operations ------------------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This describes the RTGTest MLIR operations. +// +//===----------------------------------------------------------------------===// + +include "mlir/IR/CommonAttrConstraints.td" +include "circt/Dialect/RTG/IR/RTGInterfaces.td" + +// Base class for the operation in this dialect. +class RTGTestOp traits = []> : + Op; + + +def CPUDeclOp : RTGTestOp<"cpu_decl", [ + Pure, + ConstantLike, + DeclareOpInterfaceMethods, +]> { + let summary = "declare a CPU"; + let description = [{ + This operation is used to test the `ContextResourceOpInterface` and passes + taking advantage of it. + }]; + + let arguments = (ins IndexAttr:$id); + let results = (outs CPUType:$cpu); + + let assemblyFormat = "$id attr-dict"; +} diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestTypes.h b/include/circt/Dialect/RTGTest/IR/RTGTestTypes.h new file mode 100644 index 000000000000..7afa98b81e86 --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTestTypes.h @@ -0,0 +1,21 @@ +//===- RTGTestTypes.h - RTG Test dialect types ------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTESTTYPES_H +#define CIRCT_DIALECT_RTGTEST_IR_RTGTESTTYPES_H + +#include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/BuiltinTypes.h" +#include "mlir/IR/Types.h" + +#include "circt/Dialect/RTG/IR/RTGTypeInterfaces.h" + +#define GET_TYPEDEF_CLASSES +#include "circt/Dialect/RTGTest/IR/RTGTestTypes.h.inc" + +#endif // CIRCT_DIALECT_RTGTEST_IR_RTGTESTTYPES_H diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestTypes.td b/include/circt/Dialect/RTGTest/IR/RTGTestTypes.td new file mode 100644 index 000000000000..8351cd8b9661 --- /dev/null +++ b/include/circt/Dialect/RTGTest/IR/RTGTestTypes.td @@ -0,0 +1,35 @@ +//===- RTGTestTypes.td - RTGTest types ---------------------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This describes the RTGTest types. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTESTTYPES_TD +#define CIRCT_DIALECT_RTGTEST_IR_RTGTESTTYPES_TD + +include "circt/Dialect/RTGTest/IR/RTGTestDialect.td" +include "circt/Dialect/RTG/IR/RTGInterfaces.td" +include "mlir/IR/AttrTypeBase.td" + +class RTGTestTypeDef traits = []> + : TypeDef; + +def CPUType : RTGTestTypeDef<"cpu", [ContextResourceTypeInterface]> { + let summary = "handle to a specific CPU"; + let description = [{ + This type implements a specific context resource to test RTG operations + taking context resources as operands (such as `on_context`) and other things + requiring a concrete instance of a `ContextResourceTypeInterface`. + }]; + + let mnemonic = "cpu"; + let assemblyFormat = ""; +} + +#endif // CIRCT_DIALECT_RTGTEST_IR_RTGTESTTYPES_TD diff --git a/include/circt/InitAllDialects.h b/include/circt/InitAllDialects.h index 5030cf5ad6c4..d2ce9697f692 100644 --- a/include/circt/InitAllDialects.h +++ b/include/circt/InitAllDialects.h @@ -38,6 +38,9 @@ #include "circt/Dialect/OM/OMDialect.h" #include "circt/Dialect/Pipeline/PipelineDialect.h" #include "circt/Dialect/RTG/IR/RTGDialect.h" +#ifdef CIRCT_INCLUDE_TESTS +#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h" +#endif #include "circt/Dialect/SMT/SMTDialect.h" #include "circt/Dialect/SSP/SSPDialect.h" #include "circt/Dialect/SV/SVDialect.h" @@ -77,6 +80,9 @@ inline void registerAllDialects(mlir::DialectRegistry ®istry) { om::OMDialect, pipeline::PipelineDialect, rtg::RTGDialect, +#ifdef CIRCT_INCLUDE_TESTS + rtgtest::RTGTestDialect, +#endif seq::SeqDialect, sim::SimDialect, smt::SMTDialect, diff --git a/lib/Dialect/CMakeLists.txt b/lib/Dialect/CMakeLists.txt index 449034f8069c..fb35c1dc049b 100644 --- a/lib/Dialect/CMakeLists.txt +++ b/lib/Dialect/CMakeLists.txt @@ -32,6 +32,9 @@ add_subdirectory(MSFT) add_subdirectory(OM) add_subdirectory(Pipeline) add_subdirectory(RTG) +if (CIRCT_INCLUDE_TESTS) + add_subdirectory(RTGTest) +endif() add_subdirectory(Seq) add_subdirectory(Sim) add_subdirectory(SMT) diff --git a/lib/Dialect/RTGTest/CMakeLists.txt b/lib/Dialect/RTGTest/CMakeLists.txt new file mode 100644 index 000000000000..f33061b2d87c --- /dev/null +++ b/lib/Dialect/RTGTest/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(IR) diff --git a/lib/Dialect/RTGTest/IR/CMakeLists.txt b/lib/Dialect/RTGTest/IR/CMakeLists.txt new file mode 100644 index 000000000000..5d50d2b679f9 --- /dev/null +++ b/lib/Dialect/RTGTest/IR/CMakeLists.txt @@ -0,0 +1,15 @@ +add_circt_dialect_library(CIRCTRTGTestDialect + RTGTestDialect.cpp + RTGTestOps.cpp + RTGTestTypes.cpp + + ADDITIONAL_HEADER_DIRS + ${CIRCT_MAIN_INCLUDE_DIR}/circt/Dialect/RTGTest/IR + + DEPENDS + MLIRRTGTestIncGen + + LINK_LIBS PUBLIC + MLIRIR + CIRCTRTGDialect +) diff --git a/lib/Dialect/RTGTest/IR/RTGTestDialect.cpp b/lib/Dialect/RTGTest/IR/RTGTestDialect.cpp new file mode 100644 index 000000000000..06ee710b5aa1 --- /dev/null +++ b/lib/Dialect/RTGTest/IR/RTGTestDialect.cpp @@ -0,0 +1,36 @@ +//===- RTGTestDialect.cpp - Implement the RTGTest dialect -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements the RTGTest dialect. +// +//===----------------------------------------------------------------------===// + +#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h" +#include "circt/Dialect/RTGTest/IR/RTGTestOps.h" +#include "circt/Dialect/RTGTest/IR/RTGTestTypes.h" +#include "mlir/IR/Builders.h" +#include "mlir/IR/BuiltinTypes.h" +#include "mlir/IR/DialectImplementation.h" + +using namespace circt; +using namespace rtgtest; + +//===----------------------------------------------------------------------===// +// Dialect specification. +//===----------------------------------------------------------------------===// + +void RTGTestDialect::initialize() { + registerTypes(); + // Register operations. + addOperations< +#define GET_OP_LIST +#include "circt/Dialect/RTGTest/IR/RTGTest.cpp.inc" + >(); +} + +#include "circt/Dialect/RTGTest/IR/RTGTestDialect.cpp.inc" diff --git a/lib/Dialect/RTGTest/IR/RTGTestOps.cpp b/lib/Dialect/RTGTest/IR/RTGTestOps.cpp new file mode 100644 index 000000000000..a4a584ba6b56 --- /dev/null +++ b/lib/Dialect/RTGTest/IR/RTGTestOps.cpp @@ -0,0 +1,32 @@ +//===- RTGTestOps.cpp - Implement the RTG operations ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements the RTGTest ops. +// +//===----------------------------------------------------------------------===// + +#include "circt/Dialect/RTGTest/IR/RTGTestOps.h" +#include "mlir/IR/Builders.h" +#include "llvm/ADT/APInt.h" + +using namespace circt; +using namespace rtgtest; + +//===----------------------------------------------------------------------===// +// CPUDeclOp +//===----------------------------------------------------------------------===// + +size_t CPUDeclOp::getIdentifier(size_t idx) { return getId().getZExtValue(); } + +//===----------------------------------------------------------------------===// +// TableGen generated logic. +//===----------------------------------------------------------------------===// + +// Provide the autogenerated implementation guts for the Op classes. +#define GET_OP_CLASSES +#include "circt/Dialect/RTGTest/IR/RTGTest.cpp.inc" diff --git a/lib/Dialect/RTGTest/IR/RTGTestTypes.cpp b/lib/Dialect/RTGTest/IR/RTGTestTypes.cpp new file mode 100644 index 000000000000..629a7b89145a --- /dev/null +++ b/lib/Dialect/RTGTest/IR/RTGTestTypes.cpp @@ -0,0 +1,34 @@ +//===- RTGTestTypes.cpp ---------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "circt/Dialect/RTGTest/IR/RTGTestTypes.h" +#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h" +#include "mlir/IR/Builders.h" +#include "mlir/IR/DialectImplementation.h" +#include "llvm/ADT/TypeSwitch.h" + +using namespace circt; +using namespace rtgtest; + +//===----------------------------------------------------------------------===// +// TableGen generated logic. +//===----------------------------------------------------------------------===// + +#define GET_TYPEDEF_CLASSES +#include "circt/Dialect/RTGTest/IR/RTGTestTypes.cpp.inc" + +//===----------------------------------------------------------------------===// +// Type registration +//===----------------------------------------------------------------------===// + +void circt::rtgtest::RTGTestDialect::registerTypes() { + addTypes< +#define GET_TYPEDEF_LIST +#include "circt/Dialect/RTGTest/IR/RTGTestTypes.cpp.inc" + >(); +} diff --git a/test/Dialect/RTGTest/IR/basic.mlir b/test/Dialect/RTGTest/IR/basic.mlir new file mode 100644 index 000000000000..897bd52dc669 --- /dev/null +++ b/test/Dialect/RTGTest/IR/basic.mlir @@ -0,0 +1,11 @@ +// RUN: circt-opt %s | FileCheck %s + +// TODO: replace this with `rtg.target` because ops implementing +// ContextResourceOpInterface are only allowed in such target operations. +// CHECK-LABEL: @cpus +rtg.sequence @cpus { +// CHECK: !rtgtest.cpu +^bb0(%arg0: !rtgtest.cpu): + // CHECK: %0 = rtgtest.cpu_decl 0 + %0 = rtgtest.cpu_decl 0 +}