[Mlir-commits] [mlir] bf5f523 - [mlir][emitc] Set operator precedence for `GetFieldOp` (#203416)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Jun 17 15:51:00 PDT 2026
Author: Bhavesh M
Date: 2026-06-17T15:50:55-07:00
New Revision: bf5f523583d33d9d59c4199264ab33e6f8a9d387
URL: https://github.com/llvm/llvm-project/commit/bf5f523583d33d9d59c4199264ab33e6f8a9d387
DIFF: https://github.com/llvm/llvm-project/commit/bf5f523583d33d9d59c4199264ab33e6f8a9d387.diff
LOG: [mlir][emitc] Set operator precedence for `GetFieldOp` (#203416)
This sets the precedence for the `GetFieldOp`, preventing an unsupported
operation error. The precedence is set to 18, the same as `GetGlobalOp`,
because `GetFieldOp` behaves the same way.
Added:
Modified:
mlir/lib/Target/Cpp/TranslateToCpp.cpp
mlir/test/Target/Cpp/class.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 7b2ad67df11fe..1bf87e4994d67 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -103,6 +103,7 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
.Case([&](emitc::DereferenceOp op) { return 15; })
.Case([&](emitc::DivOp op) { return 13; })
.Case([&](emitc::GetGlobalOp op) { return 18; })
+ .Case([&](emitc::GetFieldOp op) { return 18; })
.Case([&](emitc::LiteralOp op) { return 18; })
.Case([&](emitc::LoadOp op) { return 16; })
.Case([&](emitc::LogicalAndOp op) { return 4; })
diff --git a/mlir/test/Target/Cpp/class.mlir b/mlir/test/Target/Cpp/class.mlir
index afc31c63b9ac7..8ff9602c9a480 100644
--- a/mlir/test/Target/Cpp/class.mlir
+++ b/mlir/test/Target/Cpp/class.mlir
@@ -98,3 +98,26 @@ emitc.class union @unionClass {
// CHECK-NEXT: float asFloat;
// CHECK-NEXT: };
+// Test that get_field is supported inside an expression.
+// When translating operations inlined inside emitc.expression,
+// the C++ emitter queries getOperatorPrecedence() and
+// operations without a defined precedence fail translation.
+emitc.class @expressionClass {
+ emitc.field @x : i32
+ emitc.func @test_precedence() -> i32 {
+ %0 = emitc.get_field @x : i32
+ %1 = emitc.expression %0 : (i32) -> i32 {
+ %2 = emitc.add %0, %0 : (i32, i32) -> i32
+ emitc.yield %2 : i32
+ }
+ return %1 : i32
+ }
+}
+
+// CHECK-LABEL: class expressionClass {
+// CHECK-NEXT: public:
+// CHECK-NEXT: int32_t x;
+// CHECK-NEXT: int32_t test_precedence() {
+// CHECK-NEXT: return x + x;
+// CHECK-NEXT: }
+// CHECK-NEXT: };
More information about the Mlir-commits
mailing list