Skip to content

[CIR][CodeGen][NFC] Simplify replacing initializer of static decls #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ struct MissingFeatures {

// Address space related
static bool addressSpace() { return false; }
static bool addressSpaceInGlobalVar() { return false; }

// Clang codegen options
static bool strictVTablePointers() { return false; }
Expand Down
17 changes: 2 additions & 15 deletions clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,28 +575,15 @@ cir::GlobalOp CIRGenFunction::addInitializerToStaticVarDecl(
// because some types, like unions, can't be completely represented
// in the LLVM type system.)
if (GV.getSymType() != Init.getType()) {
cir::GlobalOp OldGV = GV;
GV = builder.createGlobal(CGM.getModule(), getLoc(D.getSourceRange()),
OldGV.getName(), Init.getType(),
OldGV.getConstant(), GV.getLinkage());
// FIXME(cir): OG codegen inserts new GV before old one, we probably don't
// need that?
GV.setVisibility(OldGV.getVisibility());
GV.setGlobalVisibilityAttr(OldGV.getGlobalVisibilityAttr());
GV.setInitialValueAttr(Init);
GV.setTlsModelAttr(OldGV.getTlsModelAttr());
assert(!cir::MissingFeatures::setDSOLocal());
assert(!cir::MissingFeatures::setComdat());
assert(!cir::MissingFeatures::addressSpaceInGlobalVar());
GV.setSymType(Init.getType());

// Normally this should be done with a call to CGM.replaceGlobal(OldGV, GV),
// but since at this point the current block hasn't been really attached,
// there's no visibility into the GetGlobalOp corresponding to this Global.
// Given those constraints, thread in the GetGlobalOp and update it
// directly.
GVAddr.getAddr().setType(
cir::PointerType::get(&getMLIRContext(), Init.getType()));
OldGV->erase();
getBuilder().getPointerTo(Init.getType(), GV.getAddrSpaceAttr()));
}

bool NeedsDtor =
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CIR/CodeGen/stmtexpr-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ struct outer {
};

void T2(void) {
// CIR-DAG: cir.global "private" constant internal @T2._a = #cir.const_struct<{#cir.int<2> : !s32i, #cir.const_array<[#cir.int<50> : !s32i, #cir.int<60> : !s32i]> : !cir.array<!s32i x 2>}>
// CIR-DAG: cir.global "private" constant internal dsolocal @T2._a = #cir.const_struct<{#cir.int<2> : !s32i, #cir.const_array<[#cir.int<50> : !s32i, #cir.int<60> : !s32i]> : !cir.array<!s32i x 2>}>
// LLVM-DAG: internal constant { i32, [2 x i32] } { i32 2, [2 x i32] [i32 50, i32 60] }
const struct sized_array *A = ARRAY_PTR(50, 60);

// CIR-DAG: cir.global "private" constant internal @T2._a.1 = #cir.const_struct<{#cir.int<3> : !s32i, #cir.const_array<[#cir.int<10> : !s32i, #cir.int<20> : !s32i, #cir.int<30> : !s32i]> : !cir.array<!s32i x 3>}>
// CIR-DAG: cir.global "private" constant internal dsolocal @T2._a.1 = #cir.const_struct<{#cir.int<3> : !s32i, #cir.const_array<[#cir.int<10> : !s32i, #cir.int<20> : !s32i, #cir.int<30> : !s32i]> : !cir.array<!s32i x 3>}>
// LLVM-DAG: internal constant { i32, [3 x i32] } { i32 3, [3 x i32] [i32 10, i32 20, i32 30] }
struct outer X = {ARRAY_PTR(10, 20, 30)};

Expand Down