Skip to content

[mlir] [tblgen-to-irdl] Add region support #110512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mlir/include/mlir/IR/OpBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ def AnyRegion : Region<CPred<"true">, "any region">;
// A region with the given number of blocks.
class SizedRegion<int numBlocks> : Region<
CPred<"::llvm::hasNItems($_self, " # numBlocks # ")">,
"region with " # numBlocks # " blocks">;
"region with " # numBlocks # " blocks"> {
int blocks = numBlocks;
}

// A region with at least the given number of blocks.
class MinSizedRegion<int numBlocks> : Region<
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/tblgen-to-irdl/TestDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ def Test_OrOp : Test_Op<"or"> {
// CHECK-NEXT: irdl.operands(%[[v3]])
// CHECK-NEXT: }

// Check regions are converted correctly.
def Test_RegionsOp : Test_Op<"regions"> {
let regions = (region AnyRegion:$any_region,
SizedRegion<1>:$single_block_region);
}
// CHECK-LABEL: irdl.operation @regions {
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.region
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.region with size 1
// CHECK-NEXT: irdl.regions(%[[v0]], %[[v1]])
// CHECK-NEXT: }

// Check that various types are converted correctly.
def Test_TypesOp : Test_Op<"types"> {
let arguments = (ins I32:$a,
Expand Down
31 changes: 31 additions & 0 deletions mlir/tools/tblgen-to-irdl/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,29 @@ Value createAttrConstraint(OpBuilder &builder, tblgen::Constraint constraint) {
return createPredicate(builder, constraint.getPredicate());
}

Value createRegionConstraint(OpBuilder &builder, tblgen::Region constraint) {
MLIRContext *ctx = builder.getContext();
const Record &predRec = constraint.getDef();

if (predRec.getName() == "AnyRegion") {
ValueRange entryBlockArgs = {};
auto op =
builder.create<irdl::RegionOp>(UnknownLoc::get(ctx), entryBlockArgs);
return op.getResult();
}

if (predRec.isSubClassOf("SizedRegion")) {
ValueRange entryBlockArgs = {};
auto ty = IntegerType::get(ctx, 32);
auto op = builder.create<irdl::RegionOp>(
UnknownLoc::get(ctx), entryBlockArgs,
IntegerAttr::get(ty, predRec.getValueAsInt("blocks")));
return op.getResult();
}

return createPredicate(builder, constraint.getPredicate());
}

/// Returns the name of the operation without the dialect prefix.
static StringRef getOperatorName(tblgen::Operator &tblgenOp) {
StringRef opName = tblgenOp.getDef().getValueAsString("opName");
Expand Down Expand Up @@ -404,6 +427,12 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
attrNames.push_back(StringAttr::get(ctx, namedAttr.name));
}

SmallVector<Value> regions;
for (auto namedRegion : tblgenOp.getRegions()) {
regions.push_back(
createRegionConstraint(consBuilder, namedRegion.constraint));
}

// Create the operands and results operations.
if (!operands.empty())
consBuilder.create<irdl::OperandsOp>(UnknownLoc::get(ctx), operands,
Expand All @@ -414,6 +443,8 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
if (!attributes.empty())
consBuilder.create<irdl::AttributesOp>(UnknownLoc::get(ctx), attributes,
ArrayAttr::get(ctx, attrNames));
if (!regions.empty())
consBuilder.create<irdl::RegionsOp>(UnknownLoc::get(ctx), regions);

return op;
}
Expand Down
Loading