[Mlir-commits] [mlir] [mlir][EmitC] Make pre-increment and pre-decrement return left value (PR #212391)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jul 27 19:20:24 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-emitc

Author: Jianjian Guan (jacquesguan)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/212391.diff


2 Files Affected:

- (modified) mlir/include/mlir/Dialect/EmitC/IR/EmitC.td (+25-12) 
- (modified) mlir/test/Dialect/EmitC/invalid_ops.mlir (+18-2) 


``````````diff
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 49412d1dfb01c..e62f843216e2e 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -66,19 +66,15 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
 }
 
 // Base class for increment and decrement operations.
-class EmitC_IncDecOp<string mnemonic, string summaryStr>
-    : EmitC_Op<mnemonic, [
-        CExpressionInterface,
-        TypesMatchWith<"input and result reference the same type",
-                       "operand", "result",
-                       "::llvm::cast<emitc::LValueType>($_self).getValueType()">
-      ]> {
+class EmitC_IncDecOp<string mnemonic, string summaryStr, Type resultType,
+                     list<Trait> traits>
+    : EmitC_Op<mnemonic, !listconcat(traits, [CExpressionInterface])> {
   let summary = summaryStr;
   let arguments = (ins
     Res<EmitC_LValueType, "",
         [MemRead<DefaultResource, 0, FullEffect>,
          MemWrite<DefaultResource, 0, FullEffect>]>:$operand);
-  let results = (outs EmitCType:$result);
+  let results = (outs resultType:$result);
   let assemblyFormat = "$operand attr-dict `:` type($operand)";
 
   let extraClassDeclaration = [{
@@ -88,6 +84,19 @@ class EmitC_IncDecOp<string mnemonic, string summaryStr>
   }];
 }
 
+// Postfix increment and decrement yield the operand's value as an rvalue.
+class EmitC_PostIncDecOp<string mnemonic, string summaryStr>
+    : EmitC_IncDecOp<
+          mnemonic, summaryStr, EmitCType,
+          [TypesMatchWith<"input and result reference the same type",
+                          "operand", "result",
+                          "::llvm::cast<emitc::LValueType>($_self).getValueType()">]>;
+
+// Prefix increment and decrement yield an lvalue referring to their operand.
+class EmitC_PreIncDecOp<string mnemonic, string summaryStr>
+    : EmitC_IncDecOp<mnemonic, summaryStr, EmitC_LValueType,
+                      [AllTypesMatch<["operand", "result"]>]>;
+
 // Base class for compound assignment operations.
 class EmitC_CompoundAssignOp<string mnemonic, string summaryStr>
     : EmitC_Op<mnemonic, []> {
@@ -1297,9 +1306,10 @@ def EmitC_ConditionalOp : EmitC_Op<"conditional",
 }
 
 def EmitC_PreIncrementOp
-    : EmitC_IncDecOp<"pre_increment", "Pre-increment operation"> {
+    : EmitC_PreIncDecOp<"pre_increment", "Pre-increment operation"> {
   let description = [{
     This operation models the C/C++ pre-increment operator on an lvalue.
+    Its result is an lvalue referring to the incremented operand.
 
     Example:
 
@@ -1310,9 +1320,10 @@ def EmitC_PreIncrementOp
 }
 
 def EmitC_PostIncrementOp
-    : EmitC_IncDecOp<"post_increment", "Post-increment operation"> {
+    : EmitC_PostIncDecOp<"post_increment", "Post-increment operation"> {
   let description = [{
     This operation models the C/C++ post-increment operator on an lvalue.
+    Its result is an rvalue of the operand's underlying type.
 
     Example:
 
@@ -1323,9 +1334,10 @@ def EmitC_PostIncrementOp
 }
 
 def EmitC_PreDecrementOp
-    : EmitC_IncDecOp<"pre_decrement", "Pre-decrement operation"> {
+    : EmitC_PreIncDecOp<"pre_decrement", "Pre-decrement operation"> {
   let description = [{
     This operation models the C/C++ pre-decrement operator on an lvalue.
+    Its result is an lvalue referring to the decremented operand.
 
     Example:
 
@@ -1336,9 +1348,10 @@ def EmitC_PreDecrementOp
 }
 
 def EmitC_PostDecrementOp
-    : EmitC_IncDecOp<"post_decrement", "Post-decrement operation"> {
+    : EmitC_PostIncDecOp<"post_decrement", "Post-decrement operation"> {
   let description = [{
     This operation models the C/C++ post-decrement operator on an lvalue.
+    Its result is an rvalue of the operand's underlying type.
 
     Example:
 
diff --git a/mlir/test/Dialect/EmitC/invalid_ops.mlir b/mlir/test/Dialect/EmitC/invalid_ops.mlir
index 889138a22ede3..d0f6628e62294 100644
--- a/mlir/test/Dialect/EmitC/invalid_ops.mlir
+++ b/mlir/test/Dialect/EmitC/invalid_ops.mlir
@@ -985,8 +985,16 @@ func.func @dereference(%arg0: !emitc.ptr<i32>) {
 // -----
 
 func.func @pre_increment_unmatch_type(%arg0: !emitc.lvalue<i32>) {
-  // expected-error @+1 {{failed to verify that input and result reference the same type}}
-  %1 = "emitc.pre_increment"(%arg0) : (!emitc.lvalue<i32>) -> i8
+  // expected-error @+1 {{failed to verify that all of {operand, result} have same type}}
+  %1 = "emitc.pre_increment"(%arg0) : (!emitc.lvalue<i32>) -> !emitc.lvalue<i8>
+  return
+}
+
+// -----
+
+func.func @pre_increment_result_is_lvalue(%arg0: !emitc.lvalue<i32>) {
+  // expected-error @+1 {{result #0 must be EmitC lvalue type, but got 'i32'}}
+  %1 = "emitc.pre_increment"(%arg0) : (!emitc.lvalue<i32>) -> i32
   return
 }
 
@@ -1000,6 +1008,14 @@ func.func @post_decrement_unmatch_type(%arg0: !emitc.lvalue<i32>) {
 
 // -----
 
+func.func @post_decrement_result_is_rvalue(%arg0: !emitc.lvalue<i32>) {
+  // expected-error @+1 {{result #0 must be type supported by EmitC, but got '!emitc.lvalue<i32>'}}
+  %1 = "emitc.post_decrement"(%arg0) : (!emitc.lvalue<i32>) -> !emitc.lvalue<i32>
+  return
+}
+
+// -----
+
 func.func @add_assign_to_block_argument(%arg0: i32, %arg1: !emitc.lvalue<i32>) {
   // expected-error @+1 {{'emitc.add_assign' op cannot assign to block argument}}
   emitc.add_assign %arg0 : i32 to %arg1 : !emitc.lvalue<i32>

``````````

</details>


https://github.com/llvm/llvm-project/pull/212391


More information about the Mlir-commits mailing list