Skip to content

Commit 6495149

Browse files
tommymcmlanza
authored andcommitted
[CIR][CodeGen] Implement replace global handling of cir::ConstRecordAttr (#1778)
Implemented NYI handling for `cir::ConstRecordAttr` in CIR CodeGen global replacement. Refactored existing support for `cir::ConstArrayAttr` to share duplicate code between the two.
1 parent ab41d8f commit 6495149

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -968,22 +968,34 @@ static mlir::Attribute getNewInitValue(CIRGenModule &cgm, GlobalOp newGlob,
968968
if (auto oldView = mlir::dyn_cast<cir::GlobalViewAttr>(oldInit)) {
969969
return createNewGlobalView(cgm, newGlob, oldView, oldTy);
970970
}
971-
if (auto oldArray = mlir::dyn_cast<ConstArrayAttr>(oldInit)) {
972-
llvm::SmallVector<mlir::Attribute> newArray;
973-
auto eltsAttr = dyn_cast<mlir::ArrayAttr>(oldArray.getElts());
974-
for (auto elt : eltsAttr) {
975-
if (auto view = dyn_cast<GlobalViewAttr>(elt))
976-
newArray.push_back(createNewGlobalView(cgm, newGlob, view, oldTy));
977-
else if (auto view = dyn_cast<ConstArrayAttr>(elt))
978-
newArray.push_back(getNewInitValue(cgm, newGlob, oldTy, user, elt));
971+
972+
auto getNewInitElements =
973+
[&](mlir::ArrayAttr oldElements) -> mlir::ArrayAttr {
974+
llvm::SmallVector<mlir::Attribute> newElements;
975+
for (auto elt : oldElements) {
976+
if (auto view = mlir::dyn_cast<cir::GlobalViewAttr>(elt))
977+
newElements.push_back(createNewGlobalView(cgm, newGlob, view, oldTy));
978+
else if (mlir::isa<cir::ConstArrayAttr, cir::ConstRecordAttr>(elt))
979+
newElements.push_back(getNewInitValue(cgm, newGlob, oldTy, user, elt));
979980
else
980-
newArray.push_back(elt);
981+
newElements.push_back(elt);
981982
}
983+
return mlir::ArrayAttr::get(cgm.getBuilder().getContext(), newElements);
984+
};
982985

983-
auto &builder = cgm.getBuilder();
984-
mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray);
985-
return builder.getConstArray(ar, cast<cir::ArrayType>(oldArray.getType()));
986+
if (auto oldArray = mlir::dyn_cast<cir::ConstArrayAttr>(oldInit)) {
987+
mlir::Attribute newElements =
988+
getNewInitElements(mlir::dyn_cast<mlir::ArrayAttr>(oldArray.getElts()));
989+
return cgm.getBuilder().getConstArray(
990+
newElements, mlir::cast<cir::ArrayType>(oldArray.getType()));
991+
}
992+
if (auto oldRecord = mlir::dyn_cast<cir::ConstRecordAttr>(oldInit)) {
993+
mlir::ArrayAttr newMembers = getNewInitElements(oldRecord.getMembers());
994+
auto recordTy = mlir::cast<cir::RecordType>(oldRecord.getType());
995+
return cgm.getBuilder().getConstRecordOrZeroAttr(
996+
newMembers, recordTy.getPacked(), recordTy.getPadded(), recordTy);
986997
}
998+
987999
llvm_unreachable("NYI");
9881000
}
9891001

clang/test/CIR/CodeGen/globals.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ double *const glob_ptr = &glob.b[1];
9696

9797
// TODO: test tentatives with internal linkage.
9898

99+
// Use a tentative definition in an initializer.
100+
struct A {
101+
signed *x;
102+
} tentativeF[];
103+
struct A useTentative = {tentativeF};
104+
// CHECK: cir.global external @tentativeF = #cir.zero
105+
// CHECK: cir.global external @useTentative = #cir.const_record<{#cir.global_view<@tentativeF> : !cir.ptr<!s32i>}>
106+
99107
// Tentative definition is THE definition. Should be zero-initialized.
100108
int tentativeA;
101109
float tentativeC;

0 commit comments

Comments
 (0)