Skip to content

Commit

Permalink
[CIR][CIRGen][Bugfix] Fix bool zero initialization (llvm#411)
Browse files Browse the repository at this point in the history
Support missing zero initialization of Bools
  • Loading branch information
YazZz1k authored and lanza committed Oct 12, 2024
1 parent b6df0a6 commit e4acd04
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
return getConstPtrAttr(ptrTy, 0);
if (auto structTy = ty.dyn_cast<mlir::cir::StructType>())
return getZeroAttr(structTy);
if (ty.isa<mlir::cir::BoolType>()) {
return getCIRBoolAttr(false);
}
llvm_unreachable("Zero initializer for given type is NYI");
}

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,8 @@ class CIRGlobalOpLowering
// Initializer is a constant integer: convert to MLIR builtin constant.
else if (auto intAttr = init.value().dyn_cast<mlir::cir::IntAttr>()) {
init = rewriter.getIntegerAttr(llvmType, intAttr.getValue());
} else if (auto boolAttr = init.value().dyn_cast<mlir::cir::BoolAttr>()) {
init = rewriter.getBoolAttr(boolAttr.getValue());
} else if (isa<mlir::cir::ZeroAttr, mlir::cir::ConstPtrAttr>(
init.value())) {
// TODO(cir): once LLVM's dialect has a proper zeroinitializer attribute
Expand Down
4 changes: 3 additions & 1 deletion clang/test/CIR/CodeGen/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -41,7 +42,8 @@ int use_func() { return func<int>(); }
// CHECK-NEXT: [[TMP2:%.*]] = cir.load [[TMP1]] : cir.ptr <!s32i>, !s32i
// CHECK-NEXT: cir.store [[TMP2]], [[TMP0]] : !s32i, cir.ptr <!s32i>

// 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<!u8i x 3>
Expand Down
7 changes: 5 additions & 2 deletions clang/test/CIR/Lowering/bool.cir
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#true = #cir.bool<true> : !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 <!cir.bool>, ["a", init] {alignment = 1 : i64}
cir.store %1, %0 : !cir.bool, cir.ptr <!cir.bool>
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
Expand All @@ -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
}

0 comments on commit e4acd04

Please sign in to comment.