Skip to content

Commit 04575dc

Browse files
authoredSep 17, 2024
[mlir] [tblgen-to-irdl] Add types to tblgen-to-irdl script (#108558)
Adds dialect types to the tblgen-to-irdl script and also allows operations to refer to types by symbol, when possible, and updates tests to do this. The name of the type is exported with an exclamation mark to avoid name clashes.
1 parent 08bba65 commit 04575dc

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed
 

‎mlir/test/tblgen-to-irdl/CMathDialect.td

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ class CMath_Op<string mnemonic, list<Trait> traits = []>
1919
def f32Orf64Type : Or<[CPred<"::llvm::isa<::mlir::F32>">,
2020
CPred<"::llvm::isa<::mlir::F64>">]>;
2121

22+
// CHECK: irdl.type @"!complex"
2223
def CMath_ComplexType : CMath_Type<"ComplexType", "complex"> {
2324
let parameters = (ins f32Orf64Type:$elementType);
25+
let assemblyFormat = "`<` $elementType `>`";
2426
}
2527

2628
// CHECK: irdl.operation @identity {
27-
// CHECK-NEXT: %0 = irdl.base "!cmath.complex"
29+
// CHECK-NEXT: %0 = irdl.base @cmath::@"!complex"
2830
// CHECK-NEXT: irdl.results(%0)
2931
// CHECK-NEXT: }
3032
def CMath_IdentityOp : CMath_Op<"identity"> {
3133
let results = (outs CMath_ComplexType:$out);
3234
}
3335

3436
// CHECK: irdl.operation @mul {
35-
// CHECK-NEXT: %0 = irdl.base "!cmath.complex"
36-
// CHECK-NEXT: %1 = irdl.base "!cmath.complex"
37-
// CHECK-NEXT: %2 = irdl.base "!cmath.complex"
37+
// CHECK-NEXT: %0 = irdl.base @cmath::@"!complex"
38+
// CHECK-NEXT: %1 = irdl.base @cmath::@"!complex"
39+
// CHECK-NEXT: %2 = irdl.base @cmath::@"!complex"
3840
// CHECK-NEXT: irdl.operands(%0, %1)
3941
// CHECK-NEXT: irdl.results(%2)
4042
// CHECK-NEXT: }
@@ -45,7 +47,7 @@ def CMath_MulOp : CMath_Op<"mul"> {
4547

4648
// CHECK: irdl.operation @norm {
4749
// CHECK-NEXT: %0 = irdl.any
48-
// CHECK-NEXT: %1 = irdl.base "!cmath.complex"
50+
// CHECK-NEXT: %1 = irdl.base @cmath::@"!complex"
4951
// CHECK-NEXT: irdl.operands(%0)
5052
// CHECK-NEXT: irdl.results(%1)
5153
// CHECK-NEXT: }

‎mlir/test/tblgen-to-irdl/TestDialect.td

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ class Test_Type<string name, string typeMnemonic, list<Trait> traits = []>
1616
class Test_Op<string mnemonic, list<Trait> traits = []>
1717
: Op<Test_Dialect, mnemonic, traits>;
1818

19+
// CHECK: irdl.type @"!singleton_a"
1920
def Test_SingletonAType : Test_Type<"SingletonAType", "singleton_a"> {}
21+
// CHECK: irdl.type @"!singleton_b"
2022
def Test_SingletonBType : Test_Type<"SingletonBType", "singleton_b"> {}
23+
// CHECK: irdl.type @"!singleton_c"
2124
def Test_SingletonCType : Test_Type<"SingletonCType", "singleton_c"> {}
2225

2326

@@ -26,7 +29,7 @@ def Test_AndOp : Test_Op<"and"> {
2629
let arguments = (ins AllOfType<[Test_SingletonAType, AnyType]>:$in);
2730
}
2831
// CHECK-LABEL: irdl.operation @and {
29-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.base "!test.singleton_a"
32+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.base @test::@"!singleton_a"
3033
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.any
3134
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.all_of(%[[v0]], %[[v1]])
3235
// CHECK-NEXT: irdl.operands(%[[v2]])
@@ -79,9 +82,9 @@ def Test_OrOp : Test_Op<"or"> {
7982
let arguments = (ins AnyTypeOf<[Test_SingletonAType, Test_SingletonBType, Test_SingletonCType]>:$in);
8083
}
8184
// CHECK-LABEL: irdl.operation @or {
82-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.base "!test.singleton_a"
83-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.base "!test.singleton_b"
84-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.base "!test.singleton_c"
85+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.base @test::@"!singleton_a"
86+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.base @test::@"!singleton_b"
87+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.base @test::@"!singleton_c"
8588
// CHECK-NEXT: %[[v3:[^ ]*]] = irdl.any_of(%[[v0]], %[[v1]], %[[v2]])
8689
// CHECK-NEXT: irdl.operands(%[[v3]])
8790
// CHECK-NEXT: }
@@ -114,8 +117,8 @@ def Test_VariadicityOp : Test_Op<"variadicity"> {
114117
Test_SingletonCType:$required);
115118
}
116119
// CHECK-LABEL: irdl.operation @variadicity {
117-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.base "!test.singleton_a"
118-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.base "!test.singleton_b"
119-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.base "!test.singleton_c"
120+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.base @test::@"!singleton_a"
121+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.base @test::@"!singleton_b"
122+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.base @test::@"!singleton_c"
120123
// CHECK-NEXT: irdl.operands(variadic %[[v0]], optional %[[v1]], %[[v2]])
121124
// CHECK-NEXT: }

‎mlir/tools/tblgen-to-irdl/OpDefinitionsGen.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ Value createConstraint(OpBuilder &builder, tblgen::Constraint constraint) {
177177
}
178178

179179
if (predRec.isSubClassOf("TypeDef")) {
180+
auto dialect = predRec.getValueAsDef("dialect")->getValueAsString("name");
181+
if (dialect == selectedDialect) {
182+
std::string combined = ("!" + predRec.getValueAsString("mnemonic")).str();
183+
SmallVector<FlatSymbolRefAttr> nested = {
184+
SymbolRefAttr::get(ctx, combined)};
185+
auto typeSymbol = SymbolRefAttr::get(ctx, dialect, nested);
186+
auto op = builder.create<irdl::BaseOp>(UnknownLoc::get(ctx), typeSymbol);
187+
return op.getOutput();
188+
}
180189
std::string typeName = ("!" + predRec.getValueAsString("typeName")).str();
181190
auto op = builder.create<irdl::BaseOp>(UnknownLoc::get(ctx),
182191
StringAttr::get(ctx, typeName));
@@ -250,6 +259,12 @@ static StringRef getOperatorName(tblgen::Operator &tblgenOp) {
250259
return opName;
251260
}
252261

262+
/// Returns the name of the type without the dialect prefix.
263+
static StringRef getTypeName(tblgen::TypeDef &tblgenType) {
264+
StringRef opName = tblgenType.getDef()->getValueAsString("mnemonic");
265+
return opName;
266+
}
267+
253268
/// Extract an operation to IRDL.
254269
irdl::OperationOp createIRDLOperation(OpBuilder &builder,
255270
tblgen::Operator &tblgenOp) {
@@ -300,6 +315,19 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
300315
return op;
301316
}
302317

318+
irdl::TypeOp createIRDLType(OpBuilder &builder, tblgen::TypeDef &tblgenType) {
319+
MLIRContext *ctx = builder.getContext();
320+
StringRef typeName = getTypeName(tblgenType);
321+
std::string combined = ("!" + typeName).str();
322+
323+
irdl::TypeOp op = builder.create<irdl::TypeOp>(
324+
UnknownLoc::get(ctx), StringAttr::get(ctx, combined));
325+
326+
op.getBody().emplaceBlock();
327+
328+
return op;
329+
}
330+
303331
static irdl::DialectOp createIRDLDialect(OpBuilder &builder) {
304332
MLIRContext *ctx = builder.getContext();
305333
return builder.create<irdl::DialectOp>(UnknownLoc::get(ctx),
@@ -322,6 +350,14 @@ static bool emitDialectIRDLDefs(const RecordKeeper &recordKeeper,
322350
// Set insertion point to start of DialectOp.
323351
builder = builder.atBlockBegin(&dialect.getBody().emplaceBlock());
324352

353+
for (const Record *type :
354+
recordKeeper.getAllDerivedDefinitionsIfDefined("TypeDef")) {
355+
tblgen::TypeDef tblgenType(type);
356+
if (tblgenType.getDialect().getName() != selectedDialect)
357+
continue;
358+
createIRDLType(builder, tblgenType);
359+
}
360+
325361
for (const Record *def :
326362
recordKeeper.getAllDerivedDefinitionsIfDefined("Op")) {
327363
tblgen::Operator tblgenOp(def);

0 commit comments

Comments
 (0)