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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 17:20:03 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Xiang Li (python3kgae)

<details>
<summary>Changes</summary>

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

Fixes #<!-- -->90773

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


3 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILPrepare.cpp (+26) 
- (added) llvm/test/CodeGen/DirectX/clean-module-flags.ll (+23) 
- (added) llvm/test/CodeGen/DirectX/clean-module-flags2.ll (+18) 


``````````diff
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}

``````````

</details>


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


More information about the llvm-commits mailing list