Skip to content

Commit a4f31cc

Browse files
authored
[mlir-tblgen] Emit named operand indices (#146839)
An operation's operands are defined by the `arguments` field in the tablegen definition. mlir-tblgen generates accessors for them: `getXYZ()` and `setXYZ(...)` 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: * Given an `mlir::OpOperand`, find out whether it is a specific argument (from the `arguments` field in the `.td` file) * For operation with variable number of operands (variadic, `AttrSizedOperandSegments`), get the value to pass to `getODSOperands` or `getODSOperandIndexAndLength`.
1 parent a178949 commit a4f31cc

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

mlir/test/mlir-tblgen/op-operand.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def OpA : NS_Op<"one_normal_operand_op", []> {
1313
let arguments = (ins I32:$input);
1414
}
1515

16+
// DECL-LABEL: class OpA : {{.*}} {
17+
// DECL: static constexpr int odsIndex_input = 0;
18+
1619
// CHECK-LABEL: OpA definitions
1720

1821
// CHECK: void OpA::build
@@ -28,6 +31,9 @@ def OpB : NS_Op<"one_variadic_operand_op", []> {
2831
let arguments = (ins Variadic<I32>:$input);
2932
}
3033

34+
// DECL-LABEL: class OpB : {{.*}} {
35+
// DECL: static constexpr int odsIndex_input = 0;
36+
3137
// CHECK-LABEL: OpB::build
3238
// CHECK: ::mlir::ValueRange input
3339
// CHECK-NOT: assert
@@ -37,6 +43,11 @@ def OpD : NS_Op<"mix_variadic_and_normal_inputs_op", [SameVariadicOperandSize]>
3743
let arguments = (ins Variadic<AnyTensor>:$input1, AnyTensor:$input2, Variadic<AnyTensor>:$input3);
3844
}
3945

46+
// DECL-LABEL: class OpD : {{.*}} {
47+
// DECL: static constexpr int odsIndex_input1 = 0;
48+
// DECL: static constexpr int odsIndex_input2 = 1;
49+
// DECL: static constexpr int odsIndex_input3 = 2;
50+
4051
// DECL-LABEL: ::mlir::Operation::operand_range getInput1
4152
// DECL-NEXT: return getODSOperands(0);
4253

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,17 @@ generateNamedOperandGetters(const Operator &op, Class &opClass,
22232223
"'SameVariadicOperandSize' traits");
22242224
}
22252225

2226+
// Print the ods names so they don't need to be hardcoded in the source.
2227+
for (int i = 0; i != numOperands; ++i) {
2228+
const auto &operand = op.getOperand(i);
2229+
if (operand.name.empty())
2230+
continue;
2231+
2232+
opClass.declare<Field>("static constexpr int", Twine("odsIndex_") +
2233+
operand.name + " = " +
2234+
Twine(i));
2235+
}
2236+
22262237
// First emit a few "sink" getter methods upon which we layer all nicer named
22272238
// getter methods.
22282239
// If generating for an adaptor, the method is put into the non-templated

0 commit comments

Comments
 (0)