[clang] [CIR] Implement CXXRewrittenBinaryOperator for LValue (PR #223888)

Aman Maurya via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 16 01:43:14 PDT 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/223888

>From ef4c8abdf9a1252e11f4efa10a93904f01618e57 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 16 Sep 2026 06:07:24 +0530
Subject: [PATCH 1/3] [CIR] Implement CXXRewrittenBinaryOperator for LValue

Support CXXRewrittenBinaryOperator in emitLValue via getSemanticForm().

Fixes #223743

Formatted with Gemini
---
 clang/lib/CIR/CodeGen/CIRGenFunction.cpp      |  4 +-
 .../CodeGen/cxx-rewritten-binary-operator.cpp | 42 +++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 80525339bd7a6..66b528cf40c54 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -1212,9 +1212,7 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
   case Expr::UserDefinedLiteralClass:
     return emitCallExprLValue(cast<CallExpr>(e));
   case Expr::CXXRewrittenBinaryOperatorClass:
-    getCIRGenModule().errorNYI(e->getSourceRange(),
-                               "emitLValue: CXXRewrittenBinaryOperator");
-    return LValue();
+    return emitLValue(cast<CXXRewrittenBinaryOperator>(e)->getSemanticForm());
   case Expr::VAArgExprClass:
     getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: VAArgExpr");
     return LValue();
diff --git a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
index e4427441cb2c9..8b18873434132 100644
--- a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
+++ b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
@@ -148,3 +148,45 @@ void cxx_rewritten_binary_operator_aggr_expr() {
 // OGCG: %[[RESULT:.*]] = call i32 @_ZNK15SpaceshipResultltEi(ptr noundef nonnull align 1 dereferenceable(1) %[[TMP_ADDR]], i32 noundef 0)
 // OGCG: %[[R_ADDR_PTR:.*]] = getelementptr inbounds nuw %struct.Result, ptr %[[R_ADDR:.*]], i32 0, i32 0
 // OGCG: store i32 %[[RESULT:.*]], ptr %[[R_ADDR_PTR]], align 4
+
+struct SpaceshipLValueResult {
+  int &operator<(int) const;
+};
+
+struct LValueItem {
+  SpaceshipLValueResult operator<=>(const LValueItem &) const;
+};
+
+void cxx_rewritten_binary_operator_lvalue_expr() {
+  LValueItem a;
+  LValueItem b;
+  int &ref = (a < b);
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!rec_LValueItem>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!rec_LValueItem>
+// CIR: %[[REF_ADDR:.*]] = cir.alloca "ref" {{.*}} init : !cir.ptr<!cir.ptr<!s32i>>
+// CIR: %[[TMP_ADDR:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_SpaceshipLValueResult>
+// CIR: cir.call @_ZNK10LValueItemssERKS_(%[[A_ADDR]], %[[B_ADDR]]) : (!cir.ptr<!rec_LValueItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !cir.ptr<!rec_LValueItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}) -> ()
+// CIR: %[[OP_RESULT:.*]] = cir.const #cir.poison : !rec_SpaceshipLValueResult
+// CIR: cir.store {{.*}} %[[OP_RESULT]], %[[TMP_ADDR]] : !rec_SpaceshipLValueResult, !cir.ptr<!rec_SpaceshipLValueResult>
+// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s32i
+// CIR: %[[RESULT:.*]] = cir.call @_ZNK21SpaceshipLValueResultltEi(%[[TMP_ADDR]], %[[CONST_0]]) : (!cir.ptr<!rec_SpaceshipLValueResult> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !s32i {llvm.noundef}) -> !cir.ptr<!s32i>
+// CIR: cir.store %[[RESULT]], %[[REF_ADDR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+
+// LLVM: %[[A_ADDR:.*]] = alloca %struct.LValueItem, align 1
+// LLVM: %[[B_ADDR:.*]] = alloca %struct.LValueItem, align 1
+// LLVM: %[[REF_ADDR:.*]] = alloca ptr, align 8
+// LLVM: %[[TMP_ADDR:.*]] = alloca %struct.SpaceshipLValueResult, align 1
+// LLVM: call void @_ZNK10LValueItemssERKS_(ptr noundef nonnull align 1 dereferenceable(1) %[[A_ADDR]], ptr noundef nonnull align 1 dereferenceable(1) %[[B_ADDR]])
+// LLVM: store %struct.SpaceshipLValueResult poison, ptr %[[TMP_ADDR]], align 1
+// LLVM: %[[RESULT:.*]] = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK21SpaceshipLValueResultltEi(ptr noundef nonnull align 1 dereferenceable(1) %[[TMP_ADDR]], i32 noundef 0)
+// LLVM: store ptr %[[RESULT]], ptr %[[REF_ADDR]], align 8
+
+// OGCG: %[[A_ADDR:.*]] = alloca %struct.LValueItem, align 1
+// OGCG: %[[B_ADDR:.*]] = alloca %struct.LValueItem, align 1
+// OGCG: %[[REF_ADDR:.*]] = alloca ptr, align 8
+// OGCG: %[[TMP_ADDR:.*]] = alloca %struct.SpaceshipLValueResult, align 1
+// OGCG: call void @_ZNK10LValueItemssERKS_(ptr noundef nonnull align 1 dereferenceable(1) %[[A_ADDR]], ptr noundef nonnull align 1 dereferenceable(1) %[[B_ADDR]])
+// OGCG: %[[RESULT:.*]] = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK21SpaceshipLValueResultltEi(ptr noundef nonnull align 1 dereferenceable(1) %[[TMP_ADDR]], i32 noundef 0)
+// OGCG: store ptr %[[RESULT]], ptr %[[REF_ADDR]], align 8

>From dce64ac7081d6d2f3569afd9cf8ea2a2dc7b6b13 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 16 Sep 2026 13:42:33 +0530
Subject: [PATCH 2/3] [CIR][test] Fix reference alloca FileCheck expectation in
 cxx-rewritten-binary-operator.cpp

ClangIR marks reference alloca with 'init const'. Update the FileCheck line accordingly.

Formatted with Gemini
---
 clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
index 8b18873434132..bc76890dfa592 100644
--- a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
+++ b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
@@ -165,7 +165,7 @@ void cxx_rewritten_binary_operator_lvalue_expr() {
 
 // CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!rec_LValueItem>
 // CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!rec_LValueItem>
-// CIR: %[[REF_ADDR:.*]] = cir.alloca "ref" {{.*}} init : !cir.ptr<!cir.ptr<!s32i>>
+// CIR: %[[REF_ADDR:.*]] = cir.alloca "ref" {{.*}} init const : !cir.ptr<!cir.ptr<!s32i>>
 // CIR: %[[TMP_ADDR:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_SpaceshipLValueResult>
 // CIR: cir.call @_ZNK10LValueItemssERKS_(%[[A_ADDR]], %[[B_ADDR]]) : (!cir.ptr<!rec_LValueItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !cir.ptr<!rec_LValueItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}) -> ()
 // CIR: %[[OP_RESULT:.*]] = cir.const #cir.poison : !rec_SpaceshipLValueResult

>From 7d2f292527829b6a3c1ea9bd748fc7c782ddc2c9 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 16 Sep 2026 14:12:49 +0530
Subject: [PATCH 3/3] [CIR][test] Fix call return attributes and store
 alignment in cxx-rewritten-binary-operator.cpp

Match LLVM ABI attributes on the reference return type and add wildcard for store alignment.

Formatted with Gemini
---
 clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
index bc76890dfa592..e07a50d007e8b 100644
--- a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
+++ b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp
@@ -171,8 +171,8 @@ void cxx_rewritten_binary_operator_lvalue_expr() {
 // CIR: %[[OP_RESULT:.*]] = cir.const #cir.poison : !rec_SpaceshipLValueResult
 // CIR: cir.store {{.*}} %[[OP_RESULT]], %[[TMP_ADDR]] : !rec_SpaceshipLValueResult, !cir.ptr<!rec_SpaceshipLValueResult>
 // CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s32i
-// CIR: %[[RESULT:.*]] = cir.call @_ZNK21SpaceshipLValueResultltEi(%[[TMP_ADDR]], %[[CONST_0]]) : (!cir.ptr<!rec_SpaceshipLValueResult> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !s32i {llvm.noundef}) -> !cir.ptr<!s32i>
-// CIR: cir.store %[[RESULT]], %[[REF_ADDR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+// CIR: %[[RESULT:.*]] = cir.call @_ZNK21SpaceshipLValueResultltEi(%[[TMP_ADDR]], %[[CONST_0]]) : (!cir.ptr<!rec_SpaceshipLValueResult> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !s32i {llvm.noundef}) -> (!cir.ptr<!s32i> {llvm.align = 4 : i64, llvm.dereferenceable = 4 : i64, llvm.nonnull, llvm.noundef})
+// CIR: cir.store {{.*}} %[[RESULT]], %[[REF_ADDR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
 
 // LLVM: %[[A_ADDR:.*]] = alloca %struct.LValueItem, align 1
 // LLVM: %[[B_ADDR:.*]] = alloca %struct.LValueItem, align 1



More information about the cfe-commits mailing list