Skip to content

Commit fdfd65d

Browse files
bcardosolopessmeenai
authored andcommitted
[CIR][CIRGen] Fix static init for unions
1 parent 0e97c77 commit fdfd65d

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@ void CIRGenFunction::buildStaticVarDecl(const VarDecl &D,
631631
if (D.getType()->isVariablyModifiedType())
632632
llvm_unreachable("VLAs are NYI");
633633

634+
// Save the type in case adding the initializer forces a type change.
635+
auto expectedType = addr.getType();
636+
634637
auto var = globalOp;
635638

636639
// CUDA's local and local static __shared__ variables should not
@@ -671,12 +674,8 @@ void CIRGenFunction::buildStaticVarDecl(const VarDecl &D,
671674
//
672675
// FIXME: It is really dangerous to store this in the map; if anyone
673676
// RAUW's the GV uses of this constant will be invalid.
674-
//
675-
// Since in CIR the address materialization is done over cir.get_global
676-
// and that's already updated, update the map directly instead of using
677-
// casts.
678-
LocalDeclMap.find(&D)->second =
679-
Address(getAddrOp.getAddr(), elemTy, alignment);
677+
auto castedAddr = builder.createBitcast(getAddrOp.getAddr(), expectedType);
678+
LocalDeclMap.find(&D)->second = Address(castedAddr, elemTy, alignment);
680679
CGM.setStaticLocalDeclAddress(&D, var);
681680

682681
assert(!MissingFeatures::reportGlobalToASan());

clang/test/CIR/CodeGen/stmtexpr-init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ void T2(void) {
4141
// LLVM-DAG: internal constant { i32, [3 x i32] } { i32 3, [3 x i32] [i32 10, i32 20, i32 30] }
4242
struct outer X = {ARRAY_PTR(10, 20, 30)};
4343

44-
// CIR-DAG: cir.cast(bitcast, %1 : !cir.ptr<!cir.ptr<![[sized_array]]>>), !cir.ptr<!cir.ptr<![[annon_struct]]>>
44+
// CIR-DAG: %[[T2A:.*]] = cir.get_global @T2._a : !cir.ptr<![[annon_struct]]>
45+
// CIR-DAG: cir.cast(bitcast, %[[T2A]] : !cir.ptr<![[annon_struct]]>), !cir.ptr<![[sized_array]]>
4546
escape(A);
4647
escape(&X);
4748
}

clang/test/CIR/CodeGen/union-init.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ void foo(int x) {
1212
A a = {.x = x};
1313
}
1414

15-
// CHECK: ![[anon:.*]] = !cir.struct<struct {!s32i}>
16-
// CHECK: #[[bfi_x:.*]] = #cir.bitfield_info<name = "x", storage_type = !u32i, size = 16, offset = 0, is_signed = true>
17-
// CHECK: #[[bfi_y:.*]] = #cir.bitfield_info<name = "y", storage_type = !u32i, size = 16, offset = 16, is_signed = true>
15+
// CHECK-DAG: ![[anon0:.*]] = !cir.struct<struct {!u32i}>
16+
// CHECK-DAG: ![[anon:.*]] = !cir.struct<struct {!s32i}>
17+
// CHECK-DAG: #[[bfi_x:.*]] = #cir.bitfield_info<name = "x", storage_type = !u32i, size = 16, offset = 0, is_signed = true>
18+
// CHECK-DAG: #[[bfi_y:.*]] = #cir.bitfield_info<name = "y", storage_type = !u32i, size = 16, offset = 16, is_signed = true>
19+
// CHECK-DAG: ![[anon1:.*]] = !cir.struct<union "{{.*}}" {!u32i, !cir.array<!u8i x 4>}
1820

1921
// CHECK-LABEL: cir.func @foo(
2022
// CHECK: %[[VAL_1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init] {alignment = 4 : i64}
@@ -30,4 +32,17 @@ void foo(int x) {
3032
// CHECK: cir.return
3133

3234
union { int i; float f; } u = { };
33-
// CHECK: cir.global external @u = #cir.zero : ![[anon]]
35+
// CHECK: cir.global external @u = #cir.zero : ![[anon]]
36+
37+
unsigned is_little(void) {
38+
const union {
39+
unsigned int u;
40+
unsigned char c[4];
41+
} one = {1};
42+
return one.c[0];
43+
}
44+
45+
// CHECK: cir.func @is_little
46+
// CHECK: %[[VAL_1:.*]] = cir.get_global @is_little.one : !cir.ptr<![[anon0]]>
47+
// CHECK: %[[VAL_2:.*]] = cir.cast(bitcast, %[[VAL_1]] : !cir.ptr<![[anon0]]>), !cir.ptr<![[anon1]]>
48+
// CHECK: %[[VAL_3:.*]] = cir.get_member %[[VAL_2]][1] {name = "c"} : !cir.ptr<![[anon1]]> -> !cir.ptr<!cir.array<!u8i x 4>>

0 commit comments

Comments
 (0)