Skip to content

Commit 5762bd6

Browse files
authored
[mlir] [tblgen-to-irdl] Add region support (#110512)
Adds support to exporting regions.
1 parent 5064c4c commit 5762bd6

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ def AnyRegion : Region<CPred<"true">, "any region">;
206206
// A region with the given number of blocks.
207207
class SizedRegion<int numBlocks> : Region<
208208
CPred<"::llvm::hasNItems($_self, " # numBlocks # ")">,
209-
"region with " # numBlocks # " blocks">;
209+
"region with " # numBlocks # " blocks"> {
210+
int blocks = numBlocks;
211+
}
210212

211213
// A region with at least the given number of blocks.
212214
class MinSizedRegion<int numBlocks> : Region<

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ def Test_OrOp : Test_Op<"or"> {
106106
// CHECK-NEXT: irdl.operands(%[[v3]])
107107
// CHECK-NEXT: }
108108

109+
// Check regions are converted correctly.
110+
def Test_RegionsOp : Test_Op<"regions"> {
111+
let regions = (region AnyRegion:$any_region,
112+
SizedRegion<1>:$single_block_region);
113+
}
114+
// CHECK-LABEL: irdl.operation @regions {
115+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.region
116+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.region with size 1
117+
// CHECK-NEXT: irdl.regions(%[[v0]], %[[v1]])
118+
// CHECK-NEXT: }
119+
109120
// Check that various types are converted correctly.
110121
def Test_TypesOp : Test_Op<"types"> {
111122
let arguments = (ins I32:$a,

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,29 @@ Value createAttrConstraint(OpBuilder &builder, tblgen::Constraint constraint) {
340340
return createPredicate(builder, constraint.getPredicate());
341341
}
342342

343+
Value createRegionConstraint(OpBuilder &builder, tblgen::Region constraint) {
344+
MLIRContext *ctx = builder.getContext();
345+
const Record &predRec = constraint.getDef();
346+
347+
if (predRec.getName() == "AnyRegion") {
348+
ValueRange entryBlockArgs = {};
349+
auto op =
350+
builder.create<irdl::RegionOp>(UnknownLoc::get(ctx), entryBlockArgs);
351+
return op.getResult();
352+
}
353+
354+
if (predRec.isSubClassOf("SizedRegion")) {
355+
ValueRange entryBlockArgs = {};
356+
auto ty = IntegerType::get(ctx, 32);
357+
auto op = builder.create<irdl::RegionOp>(
358+
UnknownLoc::get(ctx), entryBlockArgs,
359+
IntegerAttr::get(ty, predRec.getValueAsInt("blocks")));
360+
return op.getResult();
361+
}
362+
363+
return createPredicate(builder, constraint.getPredicate());
364+
}
365+
343366
/// Returns the name of the operation without the dialect prefix.
344367
static StringRef getOperatorName(tblgen::Operator &tblgenOp) {
345368
StringRef opName = tblgenOp.getDef().getValueAsString("opName");
@@ -406,6 +429,12 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
406429
attrNames.push_back(StringAttr::get(ctx, namedAttr.name));
407430
}
408431

432+
SmallVector<Value> regions;
433+
for (auto namedRegion : tblgenOp.getRegions()) {
434+
regions.push_back(
435+
createRegionConstraint(consBuilder, namedRegion.constraint));
436+
}
437+
409438
// Create the operands and results operations.
410439
if (!operands.empty())
411440
consBuilder.create<irdl::OperandsOp>(UnknownLoc::get(ctx), operands,
@@ -416,6 +445,8 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
416445
if (!attributes.empty())
417446
consBuilder.create<irdl::AttributesOp>(UnknownLoc::get(ctx), attributes,
418447
ArrayAttr::get(ctx, attrNames));
448+
if (!regions.empty())
449+
consBuilder.create<irdl::RegionsOp>(UnknownLoc::get(ctx), regions);
419450

420451
return op;
421452
}

0 commit comments

Comments
 (0)