[llvm] [DirectX] remove module flags not for DXIL. (PR #96577)

Xiang Li via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 17:19:26 PDT 2024


https://github.com/python3kgae created https://github.com/llvm/llvm-project/pull/96577

Remove all module flags other than "Dwarf Version" and "Debug Info Version".

Fixes #90773

>From cbfbc4c20e4b71e8c994d5235e4434593ce90d3f Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Mon, 24 Jun 2024 20:14:15 -0400
Subject: [PATCH] [DirectX] remove module flags not for DXIL.

Remove all module flags other than "Dwarf Version" and "Debug Info Version".

Fixes #90773
---
 llvm/lib/Target/DirectX/DXILPrepare.cpp       | 26 +++++++++++++++++++
 .../CodeGen/DirectX/clean-module-flags.ll     | 23 ++++++++++++++++
 .../CodeGen/DirectX/clean-module-flags2.ll    | 18 +++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 llvm/test/CodeGen/DirectX/clean-module-flags.ll
 create mode 100644 llvm/test/CodeGen/DirectX/clean-module-flags2.ll

diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index 24be644d9fc0e..500989a4c2016 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -116,6 +116,30 @@ static void removeStringFunctionAttributes(Function &F,
   F.removeRetAttrs(DeadAttrs);
 }
 
+static void cleanModuleFlags(Module &M) {
+  NamedMDNode *MDFlags = M.getModuleFlagsMetadata();
+  if (!MDFlags)
+    return;
+
+  StringSet<> LiveKeys = {"Dwarf Version", "Debug Info Version"};
+
+  SmallVector<llvm::Module::ModuleFlagEntry> FlagEntries;
+  M.getModuleFlagsMetadata(FlagEntries);
+  SmallVector<llvm::Module::ModuleFlagEntry> LiveFlagEntries;
+  for (auto &Flag : FlagEntries) {
+    if (LiveKeys.count(Flag.Key->getString()))
+      LiveFlagEntries.push_back(Flag);
+  }
+
+  if (LiveFlagEntries.size() == FlagEntries.size())
+    return;
+
+  MDFlags->eraseFromParent();
+
+  for (auto &Flag : LiveFlagEntries)
+    M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
+}
+
 class DXILPrepareModule : public ModulePass {
 
   static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
@@ -213,6 +237,8 @@ class DXILPrepareModule : public ModulePass {
         }
       }
     }
+    // Remove flags not for DXIL.
+    cleanModuleFlags(M);
     return true;
   }
 
diff --git a/llvm/test/CodeGen/DirectX/clean-module-flags.ll b/llvm/test/CodeGen/DirectX/clean-module-flags.ll
new file mode 100644
index 0000000000000..0104cc5fa1b4d
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/clean-module-flags.ll
@@ -0,0 +1,23 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; Make sure non-dxil module flags are removed.
+; CHECK-NOT:"wchar_size"
+; CHECK-NOT:"frame-pointer"
+
+; CHECK:!llvm.module.flags = !{!0, !1}
+; CHECK:!0 = !{i32 7, !"Dwarf Version", i32 2}
+; CHECK:!1 = !{i32 2, !"Debug Info Version", i32 3}
+
+; Function Attrs: nounwind memory(none)
+define void @main() local_unnamed_addr #0 {
+entry:
+  ret void
+}
+
+attributes #0 = { nounwind memory(none) }
+!llvm.module.flags = !{!0, !1, !2, !3}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{i32 7, !"Dwarf Version", i32 2}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
diff --git a/llvm/test/CodeGen/DirectX/clean-module-flags2.ll b/llvm/test/CodeGen/DirectX/clean-module-flags2.ll
new file mode 100644
index 0000000000000..068a908b2b406
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/clean-module-flags2.ll
@@ -0,0 +1,18 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; CHECK: define void @main()
+; Make sure non-dxil module flags are removed.
+; CHECK-NOT:"wchar_size"
+; CHECK-NOT:"frame-pointer"
+
+; Function Attrs: nounwind memory(none)
+define void @main() local_unnamed_addr #0 {
+entry:
+  ret void
+}
+
+attributes #0 = { nounwind memory(none) }
+!llvm.module.flags = !{!0, !1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}



More information about the llvm-commits mailing list