Skip to content

Commit 4d47993

Browse files
gitoleglanza
authored andcommitted
[CIR][Bugfix] fixes global array of pointers (#1350)
This PR fixes a case with global vars replacement when an array element is neither a`GlobalView` nor `ConstArray` but just a null pointer . Previously we just skip such element. Basically, the case in question is `static S* global[] = {0, &another_global}`
1 parent 59493c0 commit 4d47993

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ static mlir::Attribute getNewInitValue(CIRGenModule &CGM, GlobalOp newGlob,
885885
newArray.push_back(createNewGlobalView(CGM, newGlob, view, oldTy));
886886
else if (auto view = dyn_cast<ConstArrayAttr>(elt))
887887
newArray.push_back(getNewInitValue(CGM, newGlob, oldTy, user, elt));
888+
else
889+
newArray.push_back(elt);
888890
}
889891

890892
auto &builder = CGM.getBuilder();

clang/test/CIR/CodeGen/globals-ref-globals.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ typedef struct {
8888
} S3;
8989

9090
static S3 g13 = {-1L,0L,1L};
91-
static S3* g14[2][2] = {{&g13, &g13}, {&g13, &g13}};
91+
static S3* g14[2][2] = {{0, &g13}, {&g13, &g13}};
9292

9393
// CHECK-DAG: g13 = #cir.const_struct<{#cir.int<-1> : !s16i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array<!u8i x 2>, #cir.int<0> : !s32i, #cir.int<1> : !s8i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array<!u8i x 3>}> : !ty_anon_struct3
94-
// CHECK-DAG: g14 = #cir.const_array<[#cir.const_array<[#cir.global_view<@g13> : !cir.ptr<!ty_S3_>, #cir.global_view<@g13> : !cir.ptr<!ty_S3_>]> : !cir.array<!cir.ptr<!ty_S3_> x 2>, #cir.const_array<[#cir.global_view<@g13> : !cir.ptr<!ty_S3_>, #cir.global_view<@g13> : !cir.ptr<!ty_S3_>]> : !cir.array<!cir.ptr<!ty_S3_> x 2>]> : !cir.array<!cir.array<!cir.ptr<!ty_S3_> x 2> x 2>
94+
// CHECK-DAG: g14 = #cir.const_array<[#cir.const_array<[#cir.ptr<null> : !cir.ptr<!ty_S3_>, #cir.global_view<@g13> : !cir.ptr<!ty_S3_>]> : !cir.array<!cir.ptr<!ty_S3_> x 2>, #cir.const_array<[#cir.global_view<@g13> : !cir.ptr<!ty_S3_>, #cir.global_view<@g13> : !cir.ptr<!ty_S3_>]> : !cir.array<!cir.ptr<!ty_S3_> x 2>]> : !cir.array<!cir.array<!cir.ptr<!ty_S3_> x 2> x 2>
9595

9696
typedef struct {
9797
int f0;

0 commit comments

Comments
 (0)