[llvm] [DirectX] Add GlobalDCE pass after finalize linkage pass in DirectX backend (PR #151071)

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 29 06:45:11 PDT 2025


================
@@ -20,13 +20,13 @@ 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()) {
+  // Convert private globals and external globals with no usage to internal
+  // linkage.
+  for (GlobalVariable &GV : M.globals())
+    if (GV.hasPrivateLinkage() || (GV.hasExternalLinkage() && GV.use_empty())) {
----------------
farzonl wrote:

For anyone else looking this is the crux of the change right here. GlobalDCE is conservative so it won't remove  globals marked external. This is because Linker would handle these globals and usually something like the internalizer pass would run and do a similar conversion to what we are doing here.

In HLSL's case after finalize linkage we should be able to assume that it is safe to mark all external globals with no uses as internal. If we then run GlobalDCE after this these globals will get cleaned up.

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


More information about the llvm-commits mailing list