From 51c70d0b9d087cb8e9fd02cf7a657724f35253a4 Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Thu, 11 Dec 2025 19:55:33 +0100 Subject: [PATCH 1/2] [CIR] Add support the ChooseExpr for scalar --- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 3 +-- clang/test/CIR/CodeGen/choose-expr.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 clang/test/CIR/CodeGen/choose-expr.cpp diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 3887433e5e181..8e99b308de36f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -1354,8 +1354,7 @@ class ScalarExprEmitter : public StmtVisitor { } mlir::Value VisitChooseExpr(ChooseExpr *e) { - cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: choose"); - return {}; + return Visit(e->getChosenSubExpr()); } mlir::Value VisitObjCStringLiteral(const ObjCStringLiteral *e) { diff --git a/clang/test/CIR/CodeGen/choose-expr.cpp b/clang/test/CIR/CodeGen/choose-expr.cpp new file mode 100644 index 0000000000000..f1eb21adc52e3 --- /dev/null +++ b/clang/test/CIR/CodeGen/choose-expr.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +void choose_expr() { + int a = __builtin_choose_expr(1, 2, 3); +} + +// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr, ["a", init] +// CIR: %[[CONST_2:.*]] = cir.const #cir.int<2> : !s32i +// CIR: cir.store {{.*}} %[[CONST_2]], %[[A_ADDR]] : !s32i, !cir.ptr + +// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4 +// LLVM: store i32 2, ptr %[[A_ADDR]], align 4 + +// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4 +// OGCG: store i32 2, ptr %[[A_ADDR]], align 4 From c0eda44fe8b343f8165d910a0cc6402b98d546a8 Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Thu, 11 Dec 2025 23:55:58 +0100 Subject: [PATCH 2/2] Add test case for non-constants --- clang/test/CIR/CodeGen/choose-expr.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/clang/test/CIR/CodeGen/choose-expr.cpp b/clang/test/CIR/CodeGen/choose-expr.cpp index f1eb21adc52e3..7a980e2ea8b9b 100644 --- a/clang/test/CIR/CodeGen/choose-expr.cpp +++ b/clang/test/CIR/CodeGen/choose-expr.cpp @@ -18,3 +18,27 @@ void choose_expr() { // OGCG: %[[A_ADDR:.*]] = alloca i32, align 4 // OGCG: store i32 2, ptr %[[A_ADDR]], align 4 + +void choose_expr_non_constant() { + int a; + int b; + int c = __builtin_choose_expr(1, a, b); +} + +// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr, ["a"] +// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr, ["b"] +// CIR: %[[C_ADDR:.*]] = cir.alloca !s32i, !cir.ptr, ["c", init] +// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr, !s32i +// CIR: cir.store {{.*}} %[[TMP_A]], %[[C_ADDR]] : !s32i, !cir.ptr + +// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4 +// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4 +// LLVM: %[[C_ADDR:.*]] = alloca i32, i64 1, align 4 +// LLVM: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4 +// LLVM: store i32 %[[TMP_A]], ptr %[[C_ADDR]], align 4 + +// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4 +// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4 +// OGCG: %[[C_ADDR:.*]] = alloca i32, align 4 +// OGCG: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4 +// OGCG: store i32 %[[TMP_A]], ptr %[[C_ADDR]], align 4