Skip to content

[DirectX] Convert private global variables to internal linkage during Finalize Linkage pass #146406

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 2 commits into from
Jul 7, 2025
Merged
Changes from 1 commit
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
20 changes: 17 additions & 3 deletions llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
using namespace llvm;

static bool finalizeLinkage(Module &M) {
bool MadeChange = false;

// Convert private global variables to internal linkage.
for (GlobalVariable &GV : M.globals()) {
if (GV.hasPrivateLinkage()) {
GV.setLinkage(GlobalValue::InternalLinkage);
MadeChange = true;
}
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be removing these if GV.use_empty() to match what we do with functions? Alternatively, if we don't expect to ever get here with unused private globals, can we assert(!GV.use_empty()) here? Or is it okay/expected for us to leave unused internal variables around?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats the solution we worked for this ticket #139023. My understanding was though we didn't want to blanket do this because of external linkage. so, I suppose we could do something like

if(GV.use_empty() && !GV.hasExtenalLinkage())
    ToErase.push_back(&G);

}

SmallVector<Function *> Funcs;

// Collect non-entry and non-exported functions to set to internal linkage.
Expand All @@ -32,13 +42,17 @@ static bool finalizeLinkage(Module &M) {
}

for (Function *F : Funcs) {
if (F->getLinkage() == GlobalValue::ExternalLinkage)
if (F->getLinkage() == GlobalValue::ExternalLinkage) {
F->setLinkage(GlobalValue::InternalLinkage);
if (F->isDefTriviallyDead())
MadeChange = true;
}
if (F->isDefTriviallyDead()) {
M.getFunctionList().erase(F);
MadeChange = true;
}
}

return false;
return MadeChange;
}

PreservedAnalyses DXILFinalizeLinkage::run(Module &M,
Expand Down
Loading