Skip to content

Commit

Permalink
[mlir:OpFormatGen] Add Support for $_ctxt in the transformer.
Browse files Browse the repository at this point in the history
This is useful for "build tuple" type ops. In my case, in npcomp, I have
an op:

```
// Result type is `!torch.tuple<!torch.tensor, !torch.tensor>`.
torch.prim.TupleConstruct %0, %1 : !torch.tensor, !torch.tensor
```

and the context is required for the `Torch::TupleType::get` call (for
the case of an empty tuple).

The handling of these FmtContext's in the code is pretty ad-hoc -- I didn't
attempt to rationalize it and just made a targeted fix. As someone
unfamiliar with the code I had a hard time seeing how to more broadly fix
the situation.

Differential Revision: https://reviews.llvm.org/D104274
  • Loading branch information
silvasean committed Jun 15, 2021
1 parent 3120118 commit 853a614
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,15 @@ def FormatTypesMatchAttrOp : TEST_Op<"format_types_match_attr", [
let assemblyFormat = "attr-dict $value";
}

def FormatTypesMatchContextOp : TEST_Op<"format_types_match_context", [
TypesMatchWith<"tuple result type matches operand type", "value", "result",
"::mlir::TupleType::get($_ctxt, $_self)">
]> {
let arguments = (ins AnyType:$value);
let results = (outs AnyType:$result);
let assemblyFormat = "attr-dict $value `:` type($value)";
}

//===----------------------------------------------------------------------===//
// Test SideEffects
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions mlir/test/mlir-tblgen/op-format.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ test.format_infer_variadic_type_from_non_variadic %i64, %i64 : i64

// CHECK: test.format_types_match_attr 1 : i64
%ignored_res5 = test.format_types_match_attr 1 : i64

// CHECK: test.format_types_match_context %[[I64]] : i64
%ignored_res6 = test.format_types_match_context %i64 : i64
1 change: 1 addition & 0 deletions mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ OpEmitter::OpEmitter(const Operator &op,
opClass(op.getCppClassName(), op.getExtraClassDeclaration()),
staticVerifierEmitter(staticVerifierEmitter) {
verifyCtx.withOp("(*this->getOperation())");
verifyCtx.addSubst("_ctxt", "this->getOperation()->getContext()");

genTraits();

Expand Down
1 change: 1 addition & 0 deletions mlir/tools/mlir-tblgen/OpFormatGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ void OperationFormat::genParserTypeResolution(Operator &op,
} else if (const NamedTypeConstraint *var = resolver.getVariable()) {
if (Optional<StringRef> tform = resolver.getVarTransformer()) {
FmtContext fmtContext;
fmtContext.addSubst("_ctxt", "parser.getBuilder().getContext()");
if (var->isVariadic())
fmtContext.withSelf(var->name + "Types");
else
Expand Down

0 comments on commit 853a614

Please sign in to comment.