-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[mlir-tblgen] Emit named operand indices #146839
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
[mlir-tblgen] Emit named operand indices #146839
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Michael Kruse (Meinersbur) ChangesAn operation's operands are defined by the
Emitting a named constant avoids hardcoding the index in the source as magic constant, such as
Extracted out of #144785 Full diff: https://github.com/llvm/llvm-project/pull/146839.diff 2 Files Affected:
diff --git a/mlir/test/mlir-tblgen/op-operand.td b/mlir/test/mlir-tblgen/op-operand.td
index a2fa1f7046a97..ab8d721ed5427 100644
--- a/mlir/test/mlir-tblgen/op-operand.td
+++ b/mlir/test/mlir-tblgen/op-operand.td
@@ -13,6 +13,9 @@ def OpA : NS_Op<"one_normal_operand_op", []> {
let arguments = (ins I32:$input);
}
+// DECL-LABEL: class OpA : {{.*}} {
+// DECL: static constexpr int odsIndex_input = 0;
+
// CHECK-LABEL: OpA definitions
// CHECK: void OpA::build
@@ -28,6 +31,9 @@ def OpB : NS_Op<"one_variadic_operand_op", []> {
let arguments = (ins Variadic<I32>:$input);
}
+// DECL-LABEL: class OpB : {{.*}} {
+// DECL: static constexpr int odsIndex_input = 0;
+
// CHECK-LABEL: OpB::build
// CHECK: ::mlir::ValueRange input
// CHECK-NOT: assert
@@ -37,6 +43,11 @@ def OpD : NS_Op<"mix_variadic_and_normal_inputs_op", [SameVariadicOperandSize]>
let arguments = (ins Variadic<AnyTensor>:$input1, AnyTensor:$input2, Variadic<AnyTensor>:$input3);
}
+// DECL-LABEL: class OpD : {{.*}} {
+// DECL: static constexpr int odsIndex_input1 = 0;
+// DECL: static constexpr int odsIndex_input2 = 1;
+// DECL: static constexpr int odsIndex_input3 = 2;
+
// DECL-LABEL: ::mlir::Operation::operand_range getInput1
// DECL-NEXT: return getODSOperands(0);
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 6008ed4673d1b..cbb4030f3adb4 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -2223,6 +2223,17 @@ generateNamedOperandGetters(const Operator &op, Class &opClass,
"'SameVariadicOperandSize' traits");
}
+ // Print the ods names so they don't need to be hardcoded in the source.
+ for (int i = 0; i != numOperands; ++i) {
+ const auto &operand = op.getOperand(i);
+ if (operand.name.empty())
+ continue;
+
+ opClass.declare<Field>("static constexpr int", Twine("odsIndex_") +
+ operand.name + " = " +
+ Twine(i));
+ }
+
// First emit a few "sink" getter methods upon which we layer all nicer named
// getter methods.
// If generating for an adaptor, the method is put into the non-templated
|
The method to do this is non-trivial because of variadics. It is possible to emit a method computing this from TableGen (the opposite of |
Is this a question or a statement? Even if we had an oppsite of
It is done in #144785 which uses it for this pupose. In principle: Check whether a given operand index falls into the range of |
This was a statement (such method is possible I believe), with a question in the parenthesis (whether it would be the opposite of ...).
Gotcha, thanks for explaining! Anyway I agree that being able to use getODSOperandIndexAndLength() without a magic constant is a nice improvement. |
Thanks for the review |
An operation's operands are defined by the
arguments
field in the tablegen definition. mlir-tblgen generates accessors for them:getXYZ()
andsetXYZ(...)
to set an operation's operands without knowing the operand's index, but it does not expose the operand index itself. Yet some use cases requires knowing the operand index that is now covered by just getters and setters. For instance:mlir::OpOperand
, find out whether it is a specific argument (from thearguments
field in the.td
file)AttrSizedOperandSegments
), get the value to pass togetODSOperands
orgetODSOperandIndexAndLength
.Emitting a named constant avoids hardcoding the index in the source as magic constant, such as
2
inllvm-project/mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp
Line 244 in d56c06e
Extracted out of #144785