Skip to content

Commit f7fb4e0

Browse files
committed
init dialect
1 parent f2ecff1 commit f7fb4e0

20 files changed

+542
-23
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode/*
22
build/*
3+
benchmarks/*

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ include(HandleLLVMOptions)
9595

9696
set(COMPIGRA_TOOLS_DIR ${CMAKE_BINARY_DIR})
9797

98+
add_custom_target(compigra-headers)
99+
set_target_properties(compigra-headers PROPERTIES FOLDER "Misc")
100+
98101
include_directories(${LLVM_INCLUDE_DIRS})
99102
include_directories(${CLANG_INCLUDE_DIRS})
100103
include_directories(${MLIR_INCLUDE_DIRS})

include/InitAllPasses.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- InitAllPasses.h - All passes registration -----------------*- C++-*-===//
2+
//
3+
// Compigra is under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines a helper to trigger the registration of all passes defined
10+
// in the Dynamatic tutorials.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef INIT_ALL_PASSES_H
15+
#define INIT_ALL_PASSES_H
16+
17+
// #include "compigra/Passes.h"
18+
19+
namespace mlir {
20+
namespace compigra {
21+
#define GEN_PASS_REGISTRATION
22+
#include "compigra/Passes.h.inc"
23+
24+
inline void registerAllPasses() {
25+
registerPasses();
26+
}
27+
} // namespace compigra
28+
} // namespace mlir
29+
30+
#endif // INIT_ALL_PASSES_H

include/compigra/CMakeLists.txt

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
set(LLVM_TARGET_DEFINITIONS Passes.td)
2-
mlir_tablegen(Passes.h.inc -gen-pass-decls)
3-
add_public_tablegen_target(CompigraPassIncGen)
1+
add_mlir_dialect(CgraOps cgra)
2+
# add_mlir_doc(CgraDialect -gen-dialect-doc CgraDialect compigra/)
3+
# add_mlir_doc(CgraOps -gen-op-doc CgraOps compigra/)
44

5-
add_mlir_doc(Passes CompigraPasses ./ -gen-pass-doc)
5+
set(LLVM_TARGET_DEFINITIONS CgraDialect.td)
6+
7+
mlir_tablegen(CgraEnums.h.inc -gen-enum-decls)
8+
mlir_tablegen(CgraEnums.cpp.inc -gen-enum-defs)
9+
add_public_tablegen_target(MLIRCgraEnumsIncGen)
10+
add_dependencies(compigra-headers MLIRCgraEnumsIncGen)
11+
12+
add_subdirectory(Passes)

include/compigra/CgraDialect.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===- Dialect.h - compigra dialect declaration -----------------*- C++ -*-===//
2+
//
3+
// Compigra under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the Handshake MLIR dialect.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CGRA_DIALECT_H
14+
#define CGRA_DIALECT_H
15+
16+
#include "mlir/IR/Dialect.h"
17+
18+
// Pull in the Dialect definition.
19+
#include "compigra/CgraOpsDialect.h.inc"
20+
21+
// Pull in all enum type definitions, attributes,
22+
// and utility function declarations.
23+
#include "compigra/CgraEnums.h.inc"
24+
25+
#endif // CGRA_DIALECT_H

include/compigra/CgraDialect.td

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===- Cgra.td - Cgra dialect definition ---------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file originates from the CIRCT project (https://github.com/llvm/circt).
10+
// It includes modifications made as part of Dynamatic.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
//
14+
// This is the top level file for the Cgra dialect.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef CGRA_DIALECT_TD
19+
#define CGRA_DIALECT_TD
20+
21+
include "mlir/IR/OpBase.td"
22+
include "mlir/IR/DialectBase.td"
23+
include "mlir/IR/AttrTypeBase.td"
24+
include "mlir/IR/PatternBase.td"
25+
include "mlir/IR/SymbolInterfaces.td"
26+
include "mlir/IR/RegionKindInterface.td"
27+
include "mlir/Interfaces/FunctionInterfaces.td"
28+
include "mlir/Interfaces/CallInterfaces.td"
29+
include "mlir/Interfaces/SideEffectInterfaces.td"
30+
include "compigra/CgraInterfaces.td"
31+
32+
def Cgra_Dialect : Dialect {
33+
let name = "cgra";
34+
let cppNamespace = "::compigra::cgra";
35+
36+
let summary = "Types and operations for the Cgra dialect";
37+
let description = [{This dialect defined the `cgra` dialect, providing branch,
38+
select, and merge Op in CGRA ASM.}];
39+
40+
let useDefaultTypePrinterParser = 1;
41+
let useDefaultAttributePrinterParser = 1;
42+
43+
// Opt-out of properties for now, must migrate by LLVM 19. #5273.
44+
let usePropertiesForAttributes = 0;
45+
}
46+
47+
// include "compigra/CgraOps.td"
48+
#endif // CGRA_DIALECT_TD

include/compigra/CgraInterfaces.td

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//===- CompigraInterfaces.td - cgra interfaces -------------*- tablegen -*-===//
2+
//
3+
// Compigra under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is the definition file for the structured interface for Handshake ops.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CGRA_OP_INTERFACES
14+
#define CGRA_OP_INTERFACES
15+
16+
include "mlir/IR/OpBase.td"
17+
18+
def SOSTInterface : OpInterface<"SOSTInterface"> {
19+
let cppNamespace = "::compigra::cgra";
20+
let description = [{
21+
Sized Operation with Single Type (SOST).
22+
23+
These are operations whose operands all have the same type and which have
24+
an integer size property, be it the number of operation operands (e.g.,
25+
for a merge) or the number of operation results (e.g., for a fork).
26+
}];
27+
28+
let methods = [
29+
InterfaceMethod<[{
30+
Get the data type associated to the operation.
31+
The default implementation of this method simply returns the type of
32+
the first operation operand.
33+
}],
34+
"mlir::Type", "getDataType", (ins), "",
35+
[{
36+
auto concreteOp = mlir::cast<ConcreteOp>($_op);
37+
return concreteOp->getOperands().front().getType();
38+
}]
39+
>,
40+
InterfaceMethod<[{
41+
Get the size associated to the operation.
42+
The default implementation of this method simply returns the number of
43+
operation operands.
44+
}],
45+
"unsigned", "getSize", (ins), "",
46+
[{
47+
auto concreteOp = mlir::cast<ConcreteOp>($_op);
48+
return concreteOp->getNumOperands();
49+
}]
50+
>,
51+
InterfaceMethod<[{
52+
Determine whether the operation is a control operation.
53+
The default implementation of this method assumes that the operation
54+
is a control operation if and only if its associated data type is a
55+
NoneType.
56+
}],
57+
"bool", "sostIsControl", (ins), "",
58+
[{
59+
auto concreteOp = mlir::cast<ConcreteOp>($_op);
60+
// The operation is a control operation if its single data type is a
61+
// NoneType.
62+
return concreteOp.getDataType().template isa<mlir::NoneType>();
63+
}]
64+
>,
65+
InterfaceMethod<[{
66+
Print the "SOST characteristics" of an operation.
67+
If the `explicitSize` parameter is set to true, then the method prints
68+
the operation's size (in the SOST sense) between square brackets before
69+
printing the operation's operands, attributes, and data type.
70+
}],
71+
"void", "sostPrint", (ins
72+
"mlir::OpAsmPrinter &": $printer, "bool": $explicitSize
73+
), "",
74+
[{
75+
auto concreteOp = mlir::cast<ConcreteOp>($_op);
76+
77+
if (explicitSize) {
78+
printer << " [" << concreteOp.getSize() << "]";
79+
}
80+
printer << " " << concreteOp->getOperands();
81+
printer.printOptionalAttrDict(concreteOp->getAttrs());
82+
printer << " : " << concreteOp.getDataType();
83+
}]
84+
>
85+
];
86+
87+
let verify = [{
88+
auto concreteOp = mlir::cast<ConcreteOp>($_op);
89+
90+
// SOST operation's size must be at least one
91+
if (concreteOp.getSize() < 1) {
92+
return concreteOp.emitOpError(
93+
"SOST operation's size must be at least 1, but has size ")
94+
<< concreteOp.getSize();
95+
}
96+
97+
// SOST operation's operands must all have the same type
98+
auto dataType = concreteOp.getDataType();
99+
for (auto operand : concreteOp->getOperands())
100+
if (operand.getType() != dataType)
101+
return concreteOp.emitOpError("SOST operation reports having data type ")
102+
<< dataType << ", but one operand has type " << operand.getType();
103+
104+
return mlir::success();
105+
}];
106+
}
107+
108+
def MergeLikeOpInterface : OpInterface<"MergeLikeOpInterface"> {
109+
let cppNamespace = "::compigra::cgra";
110+
let description = [{
111+
Some handshake operations can have predecessors in other
112+
blocks. This is primarily useful for verification purposes during
113+
lowering from other dialect, such as the standard CDFG dialect.
114+
}];
115+
116+
let methods = [
117+
InterfaceMethod<[{
118+
Returns an operand range over the data signals being merged.
119+
}],
120+
"mlir::OperandRange", "getDataOperands", (ins)
121+
>,
122+
];
123+
124+
let verify = [{
125+
auto concreteOp = mlir::cast<ConcreteOp>($_op);
126+
127+
auto operands = concreteOp.getDataOperands();
128+
129+
if (!operands.size())
130+
return concreteOp.emitOpError("must have at least one data operand");
131+
132+
mlir::Type resultType = $_op->getResult(0).getType();
133+
134+
for (auto operand : operands)
135+
if (operand.getType() != resultType)
136+
return concreteOp.emitOpError("operand has type ") << operand.getType()
137+
<< ", but result has type " << resultType;
138+
139+
return mlir::success();
140+
}];
141+
}
142+
143+
def GeneralOpInterface : OpInterface<"GeneralOpInterface"> {
144+
let cppNamespace = "::compigra::cgra";
145+
let description =
146+
[{"Simulate the Execution of ops. The op takes a set of input values and "
147+
"returns the corresponding outputs assuming the precondition to "
148+
"execute holds."}];
149+
150+
let methods = [
151+
InterfaceMethod<
152+
"Simulate the Execution of the general op with given inputs", "void",
153+
"execute",
154+
(ins "std::vector<llvm::Any> &" : $ins,
155+
"std::vector<llvm::Any> &" : $outs)>,
156+
];
157+
}
158+
159+
#endif // CGRA_OP_INTERFACES

include/compigra/CgraOps.td

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===- Ops.td - Cgra operation definitions -----------------*- tablegen -*-===//
2+
//
3+
// Compigra is under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//============================================================================//
8+
//
9+
// This file define Cgra ops in tablegen.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
include "mlir/IR/EnumAttr.td"
14+
include "mlir/IR/OpAsmInterface.td"
15+
include "mlir/IR/BuiltinTypes.td"
16+
include "mlir/IR/BuiltinAttributeInterfaces.td"
17+
include "mlir/Interfaces/InferTypeOpInterface.td"
18+
include "compigra/CgraDialect.td"
19+
20+
21+
/// Base class for Cgra dialect operations.
22+
class Cgra_Op<string mnemonic, list<Trait> traits = []>
23+
: Op<Cgra_Dialect, mnemonic, traits> {
24+
}
25+
26+
/// Base class for Cgra dialect types.
27+
class Cgra_Type<string name, string typeMnemonic, list<Trait> traits = []>
28+
: TypeDef<Cgra_Dialect, name, traits> {
29+
let mnemonic = typeMnemonic;
30+
}
31+
32+
def MergeOp : Cgra_Op<"merge", [
33+
Pure, SOSTInterface, SameOperandsAndResultType
34+
]> {
35+
let summary = "merge operation";
36+
let description = [{
37+
The merge operation represents a (nondeterministic)
38+
merge operation. Any input is propagated to the single output. The
39+
number of inputs corresponds to the number of predecessor
40+
blocks.
41+
42+
Example:
43+
```
44+
%0 = merge %a, %b, %c : i32
45+
```
46+
}];
47+
48+
let arguments = (ins Variadic<AnyType>:$dataOperands);
49+
let results = (outs AnyType:$result);
50+
let hasCustomAssemblyFormat = 1;
51+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(LLVM_TARGET_DEFINITIONS Passes.td)
2+
mlir_tablegen(Passes.h.inc -gen-pass-decls)
3+
add_public_tablegen_target(CompigraPassIncGen)
4+
5+
add_mlir_doc(Passes CompigraPasses ./ -gen-pass-doc)

include/compigra/CfFixIndexWidth.h include/compigra/Passes/CfFixIndexWidth.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@
99
// This file declares the --fix-index-width pass.
1010
//
1111
//===----------------------------------------------------------------------===//
12-
#ifndef COMPIGRA_SCFFIXINDEXWIDTH_H
13-
#define COMPIGRA_SCFFIXINDEXWIDTH_H
12+
#ifndef COMPIGRA_CFFIXINDEXWIDTH_H
13+
#define COMPIGRA_CFFIXINDEXWIDTH_H
1414

15-
#include "compigra/Passes.h"
15+
#include "compigra/Passes/Passes.h"
1616
#include "mlir/Dialect/Arith/IR/Arith.h"
1717
#include "mlir/IR/BuiltinOps.h"
1818

1919
namespace mlir {
2020
namespace compigra {
2121

22-
23-
2422
std::unique_ptr<mlir::Pass> createCfFixIndexWidth();
2523

2624
} // namespace compigra
2725
} // namespace mlir
2826

29-
#endif // COMPIGRA_SCFFIXINDEXWIDTH_H
27+
#endif // COMPIGRA_CFFIXINDEXWIDTH_H

0 commit comments

Comments
 (0)