[Mlir-commits] [mlir] [mlir-tblgen] Emit named result indices (PR #210542)
Ryan Thomas Lynch
llvmlistbot at llvm.org
Sat Jul 18 13:29:03 PDT 2026
https://github.com/emosy created https://github.com/llvm/llvm-project/pull/210542
similar to https://github.com/llvm/llvm-project/pull/146839
useful for cases such as being able to programmatically update the result segment sizes or clone an operation via `OperationState` while editing just a few results, programmatically
>From 6ee0b42d88567dad41cd3717dff39f931f59b227 Mon Sep 17 00:00:00 2001
From: Ryan Thomas Lynch <me at ryanlyn.ch>
Date: Sat, 18 Jul 2026 13:27:08 -0700
Subject: [PATCH] [mlir-tblgen] Emit named result indices
similar to https://github.com/llvm/llvm-project/pull/146839
useful for cases such as being able to programmatically update the
result segment sizes or clone an operation via `OperationState` while
editing just a few results, programmatically
---
mlir/test/mlir-tblgen/op-result.td | 11 +++++++++++
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 14 +++++++++++++-
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/mlir/test/mlir-tblgen/op-result.td b/mlir/test/mlir-tblgen/op-result.td
index a4f7af6dbcf1c..ddc1ef077d31c 100644
--- a/mlir/test/mlir-tblgen/op-result.td
+++ b/mlir/test/mlir-tblgen/op-result.td
@@ -14,6 +14,9 @@ def OpA : NS_Op<"one_normal_result_op", []> {
let results = (outs I32:$result);
}
+// DECL-LABEL: class OpA : {{.*}} {
+// DECL: static constexpr int odsIndex_result = 0;
+
// CHECK-LABEL: void OpA::build
// CHECK: ::mlir::TypeRange resultTypes, ::mlir::ValueRange operands
// CHECK: assert(resultTypes.size() == 1u && "mismatched number of return types");
@@ -24,6 +27,10 @@ def OpB : NS_Op<"same_input_output_type_op", [SameOperandsAndResultType]> {
let results = (outs I32:$y);
}
+// DECL-LABEL: class OpB : {{.*}} {
+// DECL: static constexpr int odsIndex_x = 0;
+// DECL: static constexpr int odsIndex_y = 0;
+
// CHECK-LABEL: OpB definitions
// CHECK: void OpB::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type y, ::mlir::Value x)
// CHECK: odsState.addTypes(y);
@@ -39,6 +46,10 @@ def OpC : NS_Op<"three_normal_result_op", []> {
let results = (outs I32:$x, /*unnamed*/I32, I32:$z);
}
+// DECL-LABEL: class OpC : {{.*}} {
+// DECL: static constexpr int odsIndex_x = 0;
+// DECL: static constexpr int odsIndex_z = 2;
+
// CHECK-LABEL: OpC definitions
// CHECK: void OpC::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type x, ::mlir::Type resultType1, ::mlir::Type z)
// CHECK-NEXT: odsState.addTypes(x)
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index deef178e68dc1..4614459abb2b6 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -2175,7 +2175,8 @@ generateNamedOperandGetters(const Operator &op, Class &opClass,
"'SameVariadicOperandSize' traits");
}
- // Print the ods names so they don't need to be hardcoded in the source.
+ // Print the ODS indices of operands 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())
@@ -2389,6 +2390,17 @@ void OpEmitter::genNamedResultGetters() {
"'SameVariadicResultSize' traits");
}
+ // Print the ODS indices of results so they don't need to be hardcoded in the
+ // source.
+ for (int i = 0; i != numResults; ++i) {
+ const auto &result = op.getResult(i);
+ if (result.name.empty())
+ continue;
+
+ opClass.declare<Field>("static constexpr int",
+ Twine("odsIndex_") + result.name + " = " + Twine(i));
+ }
+
// Build the initializer string for the result segment size attribute.
std::string attrSizeInitCode;
if (attrSizedResults) {
More information about the Mlir-commits
mailing list