[llvm] [DirectX] Remove unused global variables (PR #149184)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 16 13:53:32 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Kaitlin Peng (kmpeng)

<details>
<summary>Changes</summary>

Fixes #<!-- -->139023.

This PR:
- Removes unused global variables in the finalize linkage pass
- Changes the `scalar-data.ll` run command to avoid removing its global variables
- Temporarily removes the llc run command from `finalize_linkage.ll`
  - This needs to be added back once [ #<!-- -->149179](https://github.com/llvm/llvm-project/issues/149179) and [#<!-- -->149180](https://github.com/llvm/llvm-project/issues/149180) are fixed

---
Full diff: https://github.com/llvm/llvm-project/pull/149184.diff


3 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp (+12) 
- (modified) llvm/test/CodeGen/DirectX/finalize_linkage.ll (+20-2) 
- (modified) llvm/test/CodeGen/DirectX/scalar-data.ll (+1-1) 


``````````diff
diff --git a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp b/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
index 5f331dbd2d826..cf497d8e4bba5 100644
--- a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
+++ b/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
@@ -28,6 +28,18 @@ static bool finalizeLinkage(Module &M) {
     }
   }
 
+  // Remove unused global variables.
+  SmallVector<GlobalVariable *> ToErase;
+  for (GlobalVariable &GV : M.globals()) {
+    if (GV.use_empty()) {
+      ToErase.push_back(&GV);
+    }
+  }
+  for (GlobalVariable *GV : ToErase) {
+    GV->eraseFromParent();
+    MadeChange = true;
+  }
+
   SmallVector<Function *> Funcs;
 
   // Collect non-entry and non-exported functions to set to internal linkage.
diff --git a/llvm/test/CodeGen/DirectX/finalize_linkage.ll b/llvm/test/CodeGen/DirectX/finalize_linkage.ll
index dc1140f1c9160..b0e9182c813f7 100644
--- a/llvm/test/CodeGen/DirectX/finalize_linkage.ll
+++ b/llvm/test/CodeGen/DirectX/finalize_linkage.ll
@@ -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
 
 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
+ at aTile = hidden addrspace(3) global [4 x [1 x i32]] zeroinitializer, align 4
+
+; CHECK-NOT: @bTile
+ at 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
@@ -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:
diff --git a/llvm/test/CodeGen/DirectX/scalar-data.ll b/llvm/test/CodeGen/DirectX/scalar-data.ll
index 4861a0890f136..d9c8df9bc169c 100644
--- a/llvm/test/CodeGen/DirectX/scalar-data.ll
+++ b/llvm/test/CodeGen/DirectX/scalar-data.ll
@@ -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
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/149184


More information about the llvm-commits mailing list