Author: Morris Hafner Date: 2025-08-29T19:22:56+02:00 New Revision: 8cc1d6fb2197bc02d778969fd81a712a25e9b9f5
URL: https://github.com/llvm/llvm-project/commit/8cc1d6fb2197bc02d778969fd81a712a25e9b9f5 DIFF: https://github.com/llvm/llvm-project/commit/8cc1d6fb2197bc02d778969fd81a712a25e9b9f5.diff LOG: [CIR] Add value initialization for scalar types (#156036) Added: Modified: clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp clang/test/CIR/CodeGen/local-vars.cpp Removed: ################################################################################ diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 46934e7155adf..9fe37b7cbbab6 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -92,6 +92,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> { mlir::Value value, CastKind kind, QualType destTy); + mlir::Value emitNullValue(QualType ty, mlir::Location loc) { + return cgf.cgm.emitNullConstant(ty, loc); + } + mlir::Value emitPromotedValue(mlir::Value result, QualType promotionType) { return builder.createFloatingCast(result, cgf.convertType(promotionType)); } @@ -182,6 +186,13 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> { return builder.getBool(e->getValue(), cgf.getLoc(e->getExprLoc())); } + mlir::Value VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *e) { + if (e->getType()->isVoidType()) + return {}; + + return emitNullValue(e->getType(), cgf.getLoc(e->getSourceRange())); + } + mlir::Value VisitCastExpr(CastExpr *e); mlir::Value VisitCallExpr(const CallExpr *e); @@ -1966,11 +1977,9 @@ mlir::Value ScalarExprEmitter::VisitInitListExpr(InitListExpr *e) { cgf.getLoc(e->getSourceRange()), vectorType, elements); } - if (numInitElements == 0) { - cgf.cgm.errorNYI(e->getSourceRange(), - "InitListExpr Non VectorType with 0 init elements"); - return {}; - } + // C++11 value-initialization for the scalar. + if (numInitElements == 0) + return emitNullValue(e->getType(), cgf.getLoc(e->getExprLoc())); return Visit(e->getInit(0)); } diff --git a/clang/test/CIR/CodeGen/local-vars.cpp b/clang/test/CIR/CodeGen/local-vars.cpp index 9385fdfa65601..0c5c972469758 100644 --- a/clang/test/CIR/CodeGen/local-vars.cpp +++ b/clang/test/CIR/CodeGen/local-vars.cpp @@ -1,5 +1,9 @@ -// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t-cir.ll -// RUN: FileCheck -input-file=%t-cir.ll %s +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG void test() { int i = 1; @@ -21,46 +25,182 @@ void test() { bool uib; } -// CHECK: module -// CHECK: cir.func{{.*}} @_Z4testv() -// CHECK: %[[I_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init] {alignment = 4 : i64} -// CHECK: %[[L_PTR:.*]] = cir.alloca !s64i, !cir.ptr<!s64i>, ["l", init] {alignment = 8 : i64} -// CHECK: %[[F_PTR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f", init] {alignment = 4 : i64} -// CHECK: %[[D_PTR:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["d", init] {alignment = 8 : i64} -// CHECK: %[[B1_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b1", init] {alignment = 1 : i64} -// CHECK: %[[B2_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b2", init] {alignment = 1 : i64} -// CHECK: %[[CI_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["ci", init, const] {alignment = 4 : i64} -// CHECK: %[[CL_PTR:.*]] = cir.alloca !s64i, !cir.ptr<!s64i>, ["cl", init, const] {alignment = 8 : i64} -// CHECK: %[[CF_PTR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["cf", init, const] {alignment = 4 : i64} -// CHECK: %[[CD_PTR:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["cd", init, const] {alignment = 8 : i64} -// CHECK: %[[CB1_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["cb1", init, const] {alignment = 1 : i64} -// CHECK: %[[CB2_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["cb2", init, const] {alignment = 1 : i64} -// CHECK: %[[UII_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["uii"] {alignment = 4 : i64} -// CHECK: %[[UIL_PTR:.*]] = cir.alloca !s64i, !cir.ptr<!s64i>, ["uil"] {alignment = 8 : i64} -// CHECK: %[[UIF_PTR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["uif"] {alignment = 4 : i64} -// CHECK: %[[UID_PTR:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["uid"] {alignment = 8 : i64} -// CHECK: %[[UIB_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["uib"] {alignment = 1 : i64} -// CHECK: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i -// CHECK: cir.store align(4) %[[ONE]], %[[I_PTR]] : !s32i, !cir.ptr<!s32i> -// CHECK: %[[TWO:.*]] = cir.const #cir.int<2> : !s64i -// CHECK: cir.store align(8) %[[TWO]], %[[L_PTR]] : !s64i, !cir.ptr<!s64i> -// CHECK: %[[THREE:.*]] = cir.const #cir.fp<3.0{{.*}}> : !cir.float -// CHECK: cir.store align(4) %[[THREE]], %[[F_PTR]] : !cir.float, !cir.ptr<!cir.float> -// CHECK: %[[FOUR:.*]] = cir.const #cir.fp<4.0{{.*}}> : !cir.double -// CHECK: cir.store align(8) %[[FOUR]], %[[D_PTR]] : !cir.double, !cir.ptr<!cir.double> -// CHECK: %[[TRUE:.*]] = cir.const #true -// CHECK: cir.store align(1) %[[TRUE]], %[[B1_PTR]] : !cir.bool, !cir.ptr<!cir.bool> -// CHECK: %[[FALSE:.*]] = cir.const #false -// CHECK: cir.store align(1) %[[FALSE]], %[[B2_PTR]] : !cir.bool, !cir.ptr<!cir.bool> -// CHECK: %[[ONEC:.*]] = cir.const #cir.int<1> : !s32i -// CHECK: cir.store align(4) %[[ONEC]], %[[CI_PTR]] : !s32i, !cir.ptr<!s32i> -// CHECK: %[[TWOC:.*]] = cir.const #cir.int<2> : !s64i -// CHECK: cir.store align(8) %[[TWOC]], %[[CL_PTR]] : !s64i, !cir.ptr<!s64i> -// CHECK: %[[THREEC:.*]] = cir.const #cir.fp<3.0{{.*}}> : !cir.float -// CHECK: cir.store align(4) %[[THREEC]], %[[CF_PTR]] : !cir.float, !cir.ptr<!cir.float> -// CHECK: %[[FOURC:.*]] = cir.const #cir.fp<4.0{{.*}}> : !cir.double -// CHECK: cir.store align(8) %[[FOURC]], %[[CD_PTR]] : !cir.double, !cir.ptr<!cir.double> -// CHECK: %[[TRUEC:.*]] = cir.const #true -// CHECK: cir.store align(1) %[[TRUEC]], %[[CB1_PTR]] : !cir.bool, !cir.ptr<!cir.bool> -// CHECK: %[[FALSEC:.*]] = cir.const #false -// CHECK: cir.store align(1) %[[FALSEC]], %[[CB2_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: module +// CIR: cir.func{{.*}} @_Z4testv() +// CIR: %[[I_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init] {alignment = 4 : i64} +// CIR: %[[L_PTR:.*]] = cir.alloca !s64i, !cir.ptr<!s64i>, ["l", init] {alignment = 8 : i64} +// CIR: %[[F_PTR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f", init] {alignment = 4 : i64} +// CIR: %[[D_PTR:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["d", init] {alignment = 8 : i64} +// CIR: %[[B1_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b1", init] {alignment = 1 : i64} +// CIR: %[[B2_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b2", init] {alignment = 1 : i64} +// CIR: %[[CI_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["ci", init, const] {alignment = 4 : i64} +// CIR: %[[CL_PTR:.*]] = cir.alloca !s64i, !cir.ptr<!s64i>, ["cl", init, const] {alignment = 8 : i64} +// CIR: %[[CF_PTR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["cf", init, const] {alignment = 4 : i64} +// CIR: %[[CD_PTR:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["cd", init, const] {alignment = 8 : i64} +// CIR: %[[CB1_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["cb1", init, const] {alignment = 1 : i64} +// CIR: %[[CB2_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["cb2", init, const] {alignment = 1 : i64} +// CIR: %[[UII_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["uii"] {alignment = 4 : i64} +// CIR: %[[UIL_PTR:.*]] = cir.alloca !s64i, !cir.ptr<!s64i>, ["uil"] {alignment = 8 : i64} +// CIR: %[[UIF_PTR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["uif"] {alignment = 4 : i64} +// CIR: %[[UID_PTR:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["uid"] {alignment = 8 : i64} +// CIR: %[[UIB_PTR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["uib"] {alignment = 1 : i64} +// CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i +// CIR: cir.store align(4) %[[ONE]], %[[I_PTR]] : !s32i, !cir.ptr<!s32i> +// CIR: %[[TWO:.*]] = cir.const #cir.int<2> : !s64i +// CIR: cir.store align(8) %[[TWO]], %[[L_PTR]] : !s64i, !cir.ptr<!s64i> +// CIR: %[[THREE:.*]] = cir.const #cir.fp<3.0{{.*}}> : !cir.float +// CIR: cir.store align(4) %[[THREE]], %[[F_PTR]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[FOUR:.*]] = cir.const #cir.fp<4.0{{.*}}> : !cir.double +// CIR: cir.store align(8) %[[FOUR]], %[[D_PTR]] : !cir.double, !cir.ptr<!cir.double> +// CIR: %[[TRUE:.*]] = cir.const #true +// CIR: cir.store align(1) %[[TRUE]], %[[B1_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: %[[FALSE:.*]] = cir.const #false +// CIR: cir.store align(1) %[[FALSE]], %[[B2_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: %[[ONEC:.*]] = cir.const #cir.int<1> : !s32i +// CIR: cir.store align(4) %[[ONEC]], %[[CI_PTR]] : !s32i, !cir.ptr<!s32i> +// CIR: %[[TWOC:.*]] = cir.const #cir.int<2> : !s64i +// CIR: cir.store align(8) %[[TWOC]], %[[CL_PTR]] : !s64i, !cir.ptr<!s64i> +// CIR: %[[THREEC:.*]] = cir.const #cir.fp<3.0{{.*}}> : !cir.float +// CIR: cir.store align(4) %[[THREEC]], %[[CF_PTR]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[FOURC:.*]] = cir.const #cir.fp<4.0{{.*}}> : !cir.double +// CIR: cir.store align(8) %[[FOURC]], %[[CD_PTR]] : !cir.double, !cir.ptr<!cir.double> +// CIR: %[[TRUEC:.*]] = cir.const #true +// CIR: cir.store align(1) %[[TRUEC]], %[[CB1_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: %[[FALSEC:.*]] = cir.const #false +// CIR: cir.store align(1) %[[FALSEC]], %[[CB2_PTR]] : !cir.bool, !cir.ptr<!cir.bool> + +// LLVM: define dso_local void @_Z4testv() +// LLVM: %[[I_PTR:.+]] = alloca i32 +// LLVM: %[[L_PTR:.+]] = alloca i64 +// LLVM: %[[F_PTR:.+]] = alloca float +// LLVM: %[[D_PTR:.+]] = alloca double +// LLVM: %[[B1_PTR:.+]] = alloca i8 +// LLVM: %[[B2_PTR:.+]] = alloca i8 +// LLVM: %[[CI_PTR:.+]] = alloca i32 +// LLVM: %[[CL_PTR:.+]] = alloca i64 +// LLVM: %[[CF_PTR:.+]] = alloca float +// LLVM: %[[CD_PTR:.+]] = alloca double +// LLVM: %[[CB1_PTR:.+]] = alloca i8 +// LLVM: %[[CB2_PTR:.+]] = alloca i8 +// LLVM: %[[UII_PTR:.+]] = alloca i32 +// LLVM: %[[UIL_PTR:.+]] = alloca i64 +// LLVM: %[[UIF_PTR:.+]] = alloca float +// LLVM: %[[UID_PTR:.+]] = alloca double +// LLVM: %[[UIB_PTR:.+]] = alloca i8 +// LLVM: store i32 1, ptr %[[I_PTR]] +// LLVM: store i64 2, ptr %[[L_PTR]] +// LLVM: store float 3.000000e+00, ptr %[[F_PTR]] +// LLVM: store double 4.000000e+00, ptr %[[D_PTR]] +// LLVM: store i8 1, ptr %[[B1_PTR]] +// LLVM: store i8 0, ptr %[[B2_PTR]] +// LLVM: store i32 1, ptr %[[CI_PTR]] +// LLVM: store i64 2, ptr %[[CL_PTR]] +// LLVM: store float 3.000000e+00, ptr %[[CF_PTR]] +// LLVM: store double 4.000000e+00, ptr %[[CD_PTR]] +// LLVM: store i8 1, ptr %[[CB1_PTR]] +// LLVM: store i8 0, ptr %[[CB2_PTR]] +// LLVM: ret void + +// OGCG: define dso_local void @_Z4testv() +// OGCG: %[[I_PTR:.+]] = alloca i32 +// OGCG: %[[L_PTR:.+]] = alloca i64 +// OGCG: %[[F_PTR:.+]] = alloca float +// OGCG: %[[D_PTR:.+]] = alloca double +// OGCG: %[[B1_PTR:.+]] = alloca i8 +// OGCG: %[[B2_PTR:.+]] = alloca i8 +// OGCG: %[[CI_PTR:.+]] = alloca i32 +// OGCG: %[[CL_PTR:.+]] = alloca i64 +// OGCG: %[[CF_PTR:.+]] = alloca float +// OGCG: %[[CD_PTR:.+]] = alloca double +// OGCG: %[[CB1_PTR:.+]] = alloca i8 +// OGCG: %[[CB2_PTR:.+]] = alloca i8 +// OGCG: %[[UII_PTR:.+]] = alloca i32 +// OGCG: %[[UIL_PTR:.+]] = alloca i64 +// OGCG: %[[UIF_PTR:.+]] = alloca float +// OGCG: %[[UID_PTR:.+]] = alloca double +// OGCG: %[[UIB_PTR:.+]] = alloca i8 +// OGCG: store i32 1, ptr %[[I_PTR]] +// OGCG: store i64 2, ptr %[[L_PTR]] +// OGCG: store float 3.000000e+00, ptr %[[F_PTR]] +// OGCG: store double 4.000000e+00, ptr %[[D_PTR]] +// OGCG: store i8 1, ptr %[[B1_PTR]] +// OGCG: store i8 0, ptr %[[B2_PTR]] +// OGCG: store i32 1, ptr %[[CI_PTR]] +// OGCG: store i64 2, ptr %[[CL_PTR]] +// OGCG: store float 3.000000e+00, ptr %[[CF_PTR]] +// OGCG: store double 4.000000e+00, ptr %[[CD_PTR]] +// OGCG: store i8 1, ptr %[[CB1_PTR]] +// OGCG: store i8 0, ptr %[[CB2_PTR]] +// OGCG: ret void + +void value_init() { + float f{}; + bool b{}; + int i{}; + + float f2 = {}; + bool b2 = {}; + int i2 = {}; + + bool scalar_value_init_expr = int() == 0; +} + +// CIR: cir.func{{.*}} @_Z10value_initv() +// CIR: %[[F_PTR:.+]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f", init] +// CIR: %[[B_PTR:.+]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b", init] +// CIR: %[[I_PTR:.+]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init] +// CIR: %[[F2_PTR:.+]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f2", init] +// CIR: %[[B2_PTR:.+]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b2", init] +// CIR: %[[I2_PTR:.+]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i2", init] +// CIR: %[[S_PTR:.+]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["scalar_value_init_expr", init] +// CIR: %[[ZEROF1:.+]] = cir.const #cir.fp<0.000000e+00> : !cir.float +// CIR: cir.store{{.*}} %[[ZEROF1]], %[[F_PTR]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[FALSE1:.+]] = cir.const #false +// CIR: cir.store{{.*}} %[[FALSE1]], %[[B_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: %[[ZEROI1:.+]] = cir.const #cir.int<0> : !s32i +// CIR: cir.store{{.*}} %[[ZEROI1]], %[[I_PTR]] : !s32i, !cir.ptr<!s32i> +// CIR: %[[ZEROF2:.+]] = cir.const #cir.fp<0.000000e+00> : !cir.float +// CIR: cir.store{{.*}} %[[ZEROF2]], %[[F2_PTR]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[FALSE2:.+]] = cir.const #false +// CIR: cir.store{{.*}} %[[FALSE2]], %[[B2_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: %[[ZEROI2:.+]] = cir.const #cir.int<0> : !s32i +// CIR: cir.store{{.*}} %[[ZEROI2]], %[[I2_PTR]] : !s32i, !cir.ptr<!s32i> +// CIR: %[[ZEROI_LHS:.+]] = cir.const #cir.int<0> : !s32i +// CIR: %[[ZEROI_RHS:.+]] = cir.const #cir.int<0> : !s32i +// CIR: %[[CMP:.+]] = cir.cmp(eq, %[[ZEROI_LHS]], %[[ZEROI_RHS]]) : !s32i, !cir.bool +// CIR: cir.store{{.*}} %[[CMP]], %[[S_PTR]] : !cir.bool, !cir.ptr<!cir.bool> +// CIR: cir.return + +// LLVM: define{{.*}} void @_Z10value_initv() +// LLVM: %[[F_PTR:.+]] = alloca float +// LLVM: %[[B_PTR:.+]] = alloca i8 +// LLVM: %[[I_PTR:.+]] = alloca i32 +// LLVM: %[[F2_PTR:.+]] = alloca float +// LLVM: %[[B2_PTR:.+]] = alloca i8 +// LLVM: %[[I2_PTR:.+]] = alloca i32 +// LLVM: %[[S_PTR:.+]] = alloca i8 +// LLVM: store float 0.000000e+00, ptr %[[F_PTR]] +// LLVM: store i8 0, ptr %[[B_PTR]] +// LLVM: store i32 0, ptr %[[I_PTR]] +// LLVM: store float 0.000000e+00, ptr %[[F2_PTR]] +// LLVM: store i8 0, ptr %[[B2_PTR]] +// LLVM: store i32 0, ptr %[[I2_PTR]] +// LLVM: store i8 1, ptr %[[S_PTR]] +// LLVM: ret void + +// OGCG: define{{.*}} void @_Z10value_initv() +// OGCG: %[[F_PTR:.+]] = alloca float +// OGCG: %[[B_PTR:.+]] = alloca i8 +// OGCG: %[[I_PTR:.+]] = alloca i32 +// OGCG: %[[F2_PTR:.+]] = alloca float +// OGCG: %[[B2_PTR:.+]] = alloca i8 +// OGCG: %[[I2_PTR:.+]] = alloca i32 +// OGCG: %[[S_PTR:.+]] = alloca i8 +// OGCG: store float 0.000000e+00, ptr %[[F_PTR]] +// OGCG: store i8 0, ptr %[[B_PTR]] +// OGCG: store i32 0, ptr %[[I_PTR]] +// OGCG: store float 0.000000e+00, ptr %[[F2_PTR]] +// OGCG: store i8 0, ptr %[[B2_PTR]] +// OGCG: store i32 0, ptr %[[I2_PTR]] +// OGCG: store i8 1, ptr %[[S_PTR]] +// OGCG: ret void _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits