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
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
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
23 changes: 22 additions & 1 deletion llvm/test/CodeGen/DirectX/finalize_linkage.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,28 @@
target triple = "dxilv1.5-pc-shadermodel6.5-compute"

; DXILFinalizeLinkage changes linkage of all functions that are hidden to
; internal.
; internal, and converts private global variables to internal linkage.

; CHECK: @switch.table = internal unnamed_addr constant [4 x i32]
@switch.table = private unnamed_addr constant [4 x i32] [i32 1, i32 257, i32 65793, i32 16843009], align 4

; CHECK: @private_array = internal constant [3 x float]
@private_array = private constant [3 x float] [float 1.0, float 2.0, float 3.0], align 4

; CHECK: @private_var = internal global i32
@private_var = private global i32 1, align 4

; Internal global should remain internal
; CHECK: @internal_var = internal global i32
@internal_var = internal global i32 1, align 4

; External global should remain external
; CHECK: @external_var = external global i32
@external_var = external global i32, align 4

; Hidden global should remain hidden
; CHECK: @hidden_var = hidden global i32
@hidden_var = hidden global i32 1, align 4

; CHECK-NOT: define internal void @"?f1@@YAXXZ"()
define void @"?f1@@YAXXZ"() #0 {
Expand Down
Loading