Skip to content

[DirectX] Remove unused global variables #149184

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ static bool finalizeLinkage(Module &M) {
}
}

// Remove unused global variables.
for (GlobalVariable &GV : make_early_inc_range(M.globals())) {
if (GV.use_empty()) {
GV.eraseFromParent();
MadeChange = true;
}
}
Comment on lines +32 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit: unecessary curly braces

Suggested change
for (GlobalVariable &GV : make_early_inc_range(M.globals())) {
if (GV.use_empty()) {
GV.eraseFromParent();
MadeChange = true;
}
}
for (GlobalVariable &GV : make_early_inc_range(M.globals()))
if (GV.use_empty()) {
GV.eraseFromParent();
MadeChange = true;
}


SmallVector<Function *> Funcs;

// Collect non-entry and non-exported functions to set to internal linkage.
Expand Down
22 changes: 20 additions & 2 deletions llvm/test/CodeGen/DirectX/finalize_linkage.ll
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
; RUN: opt -S -dxil-finalize-linkage -mtriple=dxil-unknown-shadermodel6.5-compute %s | FileCheck %s
; RUN: llc %s --filetype=asm -o - | FileCheck %s --check-prefixes=CHECK-LLC
; TODO: Add back the llc test once #149179 and #149180 are fixed
Copy link
Contributor

Choose a reason for hiding this comment

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

These are fixed now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, thanks!


target triple = "dxilv1.5-pc-shadermodel6.5-compute"

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

; CHECK-NOT: @aTile
@aTile = hidden addrspace(3) global [4 x [1 x i32]] zeroinitializer, align 4

; CHECK-NOT: @bTile
@bTile = hidden addrspace(3) global [1 x <1 x i32>] zeroinitializer, align 4

; 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
Expand All @@ -27,6 +34,17 @@ target triple = "dxilv1.5-pc-shadermodel6.5-compute"
; CHECK: @hidden_var = hidden global i32
@hidden_var = hidden global i32 1, align 4

define void @anchor_function() #0 {
entry:
%0 = load i32, ptr @switch.table, align 4
%1 = load [3 x float], ptr @private_array, align 4
%2 = load i32, ptr @private_var, align 4
%3 = load i32, ptr @internal_var, align 4
%4 = load i32, ptr @external_var, align 4
%5 = load i32, ptr @hidden_var, align 4
ret void
}

; CHECK-NOT: define internal void @"?f1@@YAXXZ"()
define void @"?f1@@YAXXZ"() #0 {
entry:
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/DirectX/scalar-data.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc %s -mtriple=dxil-pc-shadermodel6.3-library --filetype=asm -o - | FileCheck %s
; RUN: opt -S -passes='dxil-data-scalarization,dxil-flatten-arrays' -mtriple=dxil-unknown-shadermodel6.5-compute %s | FileCheck %s

; Make sure we don't touch arrays without vectors and that can recurse and flatten multiple-dimension arrays of vectors

Expand Down
Loading