[Mlir-commits] [mlir] [mlir][emitc] Set operator precedence for `GetFieldOp` (PR #203416)

Bhavesh M llvmlistbot at llvm.org
Mon Jun 15 20:02:42 PDT 2026


https://github.com/beamandala updated https://github.com/llvm/llvm-project/pull/203416

>From 3693abb3298bcc7d04598563a84242611ebfd48b Mon Sep 17 00:00:00 2001
From: Bhavesh Mandalapu <bmandalapu at google.com>
Date: Thu, 11 Jun 2026 15:18:59 -0700
Subject: [PATCH 1/3] [mlir][emitc] Set operator precedence for GetFieldOp

This sets the precedence for the GetFieldOp, preventing an unsupported
operation error.
---
 mlir/lib/Target/Cpp/TranslateToCpp.cpp | 1 +
 1 file changed, 1 insertion(+)

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; })

>From 4d77243f68456e0d2e70abee0e1dc9df8d5ce4dd Mon Sep 17 00:00:00 2001
From: Bhavesh Mandalapu <bmandalapu at google.com>
Date: Fri, 12 Jun 2026 11:20:27 -0700
Subject: [PATCH 2/3] Add unit test

---
 mlir/test/Target/Cpp/class.mlir | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/mlir/test/Target/Cpp/class.mlir b/mlir/test/Target/Cpp/class.mlir
index afc31c63b9ac7..fb1b48285974e 100644
--- a/mlir/test/Target/Cpp/class.mlir
+++ b/mlir/test/Target/Cpp/class.mlir
@@ -98,3 +98,23 @@ emitc.class union @unionClass {
 // CHECK-NEXT:    float asFloat;
 // CHECK-NEXT:  };
 
+// Test that get_field is supported inside an expression
+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:  };

>From faff393ffceeb6be9a639ef181736a31caa802cf Mon Sep 17 00:00:00 2001
From: Bhavesh Mandalapu <bmandalapu at google.com>
Date: Mon, 15 Jun 2026 20:02:12 -0700
Subject: [PATCH 3/3] Clarify test comment

---
 mlir/test/Target/Cpp/class.mlir | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/mlir/test/Target/Cpp/class.mlir b/mlir/test/Target/Cpp/class.mlir
index fb1b48285974e..8ff9602c9a480 100644
--- a/mlir/test/Target/Cpp/class.mlir
+++ b/mlir/test/Target/Cpp/class.mlir
@@ -98,7 +98,10 @@ emitc.class union @unionClass {
 // CHECK-NEXT:    float asFloat;
 // CHECK-NEXT:  };
 
-// Test that get_field is supported inside an expression
+// 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 {



More information about the Mlir-commits mailing list