From e4acd045b12028b907fa469faefef6ec461b65b4 Mon Sep 17 00:00:00 2001 From: Kirill Yansitov <36601354+YazZz1k@users.noreply.github.com> Date: Fri, 2 Feb 2024 04:48:05 +0300 Subject: [PATCH] [CIR][CIRGen][Bugfix] Fix bool zero initialization (#411) Support missing zero initialization of Bools --- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 3 +++ clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 2 ++ clang/test/CIR/CodeGen/globals.cpp | 4 +++- clang/test/CIR/Lowering/bool.cir | 7 +++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 4433e0027fc6..af080050b068 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -232,6 +232,9 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy { return getConstPtrAttr(ptrTy, 0); if (auto structTy = ty.dyn_cast()) return getZeroAttr(structTy); + if (ty.isa()) { + return getCIRBoolAttr(false); + } llvm_unreachable("Zero initializer for given type is NYI"); } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 12548f8b5af9..0dfe1bcf29cb 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1550,6 +1550,8 @@ class CIRGlobalOpLowering // Initializer is a constant integer: convert to MLIR builtin constant. else if (auto intAttr = init.value().dyn_cast()) { init = rewriter.getIntegerAttr(llvmType, intAttr.getValue()); + } else if (auto boolAttr = init.value().dyn_cast()) { + init = rewriter.getBoolAttr(boolAttr.getValue()); } else if (isa( init.value())) { // TODO(cir): once LLVM's dialect has a proper zeroinitializer attribute diff --git a/clang/test/CIR/CodeGen/globals.cpp b/clang/test/CIR/CodeGen/globals.cpp index 4792cb341400..52c146cce421 100644 --- a/clang/test/CIR/CodeGen/globals.cpp +++ b/clang/test/CIR/CodeGen/globals.cpp @@ -6,6 +6,7 @@ const int b = 4; // unless used wont be generated unsigned long int c = 2; int d = a; +bool e; float y = 3.4; double w = 4.3; char x = '3'; @@ -41,7 +42,8 @@ int use_func() { return func(); } // CHECK-NEXT: [[TMP2:%.*]] = cir.load [[TMP1]] : cir.ptr , !s32i // CHECK-NEXT: cir.store [[TMP2]], [[TMP0]] : !s32i, cir.ptr -// CHECK: cir.global external @y = 3.400000e+00 : f32 +// CHECK: cir.global external @e = #false +// CHECK-NEXT: cir.global external @y = 3.400000e+00 : f32 // CHECK-NEXT: cir.global external @w = 4.300000e+00 : f64 // CHECK-NEXT: cir.global external @x = #cir.int<51> : !s8i // CHECK-NEXT: cir.global external @rgb = #cir.const_array<[#cir.int<0> : !u8i, #cir.int<233> : !u8i, #cir.int<33> : !u8i]> : !cir.array diff --git a/clang/test/CIR/Lowering/bool.cir b/clang/test/CIR/Lowering/bool.cir index 79b406cc1634..34175667ec39 100644 --- a/clang/test/CIR/Lowering/bool.cir +++ b/clang/test/CIR/Lowering/bool.cir @@ -5,14 +5,16 @@ #true = #cir.bool : !cir.bool module { + cir.global external @g_bl = #false +// MLIR: llvm.mlir.global external @g_bl(false) {addr_space = 0 : i32} : i8 +// LLVM: @g_bl = global i8 0 + cir.func @foo() { %1 = cir.const(#true) : !cir.bool %0 = cir.alloca !cir.bool, cir.ptr , ["a", init] {alignment = 1 : i64} cir.store %1, %0 : !cir.bool, cir.ptr cir.return } -} - // MLIR: llvm.func @foo() // MLIR-DAG: = llvm.mlir.constant(1 : i8) : i8 // MLIR-DAG: [[Value:%[a-z0-9]+]] = llvm.mlir.constant(1 : index) : i64 @@ -24,3 +26,4 @@ module { // LLVM-NEXT: %1 = alloca i8, i64 1, align 1 // LLVM-NEXT: store i8 1, ptr %1, align 1 // LLVM-NEXT: ret void +}