From 40e9b78df97db98d23c9d377da740a94ad7898f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAmr?= Date: Sat, 27 Sep 2025 12:21:52 +0200 Subject: [PATCH 1/2] [CIR] Implement ChooseExpr for Aggre Expr --- clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp | 4 +--- clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 2 ++ clang/test/CIR/CodeGen/struct.cpp | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp index 5596499ee94b5..af42d1d882ae0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp @@ -211,9 +211,7 @@ class AggExprEmitter : public StmtVisitor { cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitAbstractConditionalOperator"); } - void VisitChooseExpr(const ChooseExpr *e) { - cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitChooseExpr"); - } + void VisitChooseExpr(const ChooseExpr *e) { Visit(e->getChosenSubExpr()); } void VisitCXXParenListInitExpr(CXXParenListInitExpr *e) { cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitCXXParenListInitExpr"); diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index 0abb21a670719..9d98361a8b6a2 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -841,6 +841,8 @@ LValue CIRGenFunction::emitLValue(const Expr *e) { return emitCastLValue(cast(e)); case Expr::MaterializeTemporaryExprClass: return emitMaterializeTemporaryExpr(cast(e)); + case Expr::ChooseExprClass: + return emitLValue(cast(e)->getChosenSubExpr()); } } diff --git a/clang/test/CIR/CodeGen/struct.cpp b/clang/test/CIR/CodeGen/struct.cpp index 75374284d09d0..ed571f4adea17 100644 --- a/clang/test/CIR/CodeGen/struct.cpp +++ b/clang/test/CIR/CodeGen/struct.cpp @@ -129,3 +129,27 @@ void paren_expr() { // OGCG: %[[B_ADDR:.*]] = alloca %struct.Point, align 4 // OGCG: call void @llvm.memset.p0.i64(ptr align 4 %[[A_ADDR]], i8 0, i64 8, i1 false) // OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %[[B_ADDR]], ptr align 4 %[[A_ADDR]], i64 8, i1 false) + +void choose_expr() { + CompleteS a; + CompleteS b; + CompleteS c = __builtin_choose_expr(true, a, b); +} + +// CIR: cir.func{{.*}} @_Z11choose_exprv() +// CIR: %[[A_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr, ["a"] +// CIR: %[[B_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr, ["b"] +// CIR: %[[C_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr, ["c", init] +// CIR: cir.call @_ZN9CompleteSC1ERKS_(%[[C_ADDR]], %[[A_ADDR]]) nothrow : (!cir.ptr, !cir.ptr) -> () + +// LLVM: define{{.*}} void @_Z11choose_exprv() +// LLVM: %[[A_ADDR:.*]] = alloca %struct.CompleteS, i64 1, align 4 +// LLVM: %[[B_ADDR:.*]] = alloca %struct.CompleteS, i64 1, align 4 +// LLVM: %[[C_ADDR:.*]] = alloca %struct.CompleteS, i64 1, align 4 +// LLVM: call void @_ZN9CompleteSC1ERKS_(ptr %[[C_ADDR]], ptr %[[A_ADDR]]) + +// OGCG: define{{.*}} void @_Z11choose_exprv() +// OGCG: %[[A_ADDR:.*]] = alloca %struct.CompleteS, align 4 +// OGCG: %[[B_ADDR:.*]] = alloca %struct.CompleteS, align 4 +// OGCG: %[[C_ADDR:.*]] = alloca %struct.CompleteS, align 4 +// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %[[C_ADDR]], ptr align 4 %[[A_ADDR]], i64 8, i1 false) From 863fec75627b514d670368b110444d6256ec9234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAmr?= Date: Tue, 30 Sep 2025 18:54:11 +0200 Subject: [PATCH 2/2] Add TODO(cir) for cir.copy op --- clang/test/CIR/CodeGen/struct.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/test/CIR/CodeGen/struct.cpp b/clang/test/CIR/CodeGen/struct.cpp index ed571f4adea17..1dc16f3b79497 100644 --- a/clang/test/CIR/CodeGen/struct.cpp +++ b/clang/test/CIR/CodeGen/struct.cpp @@ -140,6 +140,7 @@ void choose_expr() { // CIR: %[[A_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr, ["a"] // CIR: %[[B_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr, ["b"] // CIR: %[[C_ADDR:.*]] = cir.alloca !rec_CompleteS, !cir.ptr, ["c", init] +// TODO(cir): Call to default copy constructor should be replaced by `cir.copy` op // CIR: cir.call @_ZN9CompleteSC1ERKS_(%[[C_ADDR]], %[[A_ADDR]]) nothrow : (!cir.ptr, !cir.ptr) -> () // LLVM: define{{.*}} void @_Z11choose_exprv()