[llvm] 76e37b1 - [DirectX] fix illegal behavior flag in module flags. (#96577)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 15:46:26 PDT 2024


Author: Xiang Li
Date: 2024-07-12T18:46:22-04:00
New Revision: 76e37b1a08906620537440ebcd5162697079cba5

URL: https://github.com/llvm/llvm-project/commit/76e37b1a08906620537440ebcd5162697079cba5
DIFF: https://github.com/llvm/llvm-project/commit/76e37b1a08906620537440ebcd5162697079cba5.diff

LOG: [DirectX] fix illegal behavior flag in module flags. (#96577)

For DXIL which is based on llvm 3.7, max supported behavior flag for
module flags is 6.

The commit will check all module flags, for behavior flag > 6, change it
to 2 (Warning).
    
This is to fix the behavior flag part for #96912.

Added: 
    llvm/test/CodeGen/DirectX/legalize-module-flags.ll
    llvm/test/CodeGen/DirectX/legalize-module-flags2.ll

Modified: 
    llvm/lib/Target/DirectX/DXILPrepare.cpp
    llvm/test/tools/dxil-dis/di-compile-unit.ll
    llvm/test/tools/dxil-dis/di-subprogram.ll
    llvm/test/tools/dxil-dis/di-subrange.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index 24be644d9fc0e..889de3a81536d 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -116,6 +116,31 @@ static void removeStringFunctionAttributes(Function &F,
   F.removeRetAttrs(DeadAttrs);
 }
 
+static void cleanModuleFlags(Module &M) {
+  NamedMDNode *MDFlags = M.getModuleFlagsMetadata();
+  if (!MDFlags)
+    return;
+
+  SmallVector<llvm::Module::ModuleFlagEntry> FlagEntries;
+  M.getModuleFlagsMetadata(FlagEntries);
+  bool Updated = false;
+  for (auto &Flag : FlagEntries) {
+    // llvm 3.7 only supports behavior up to AppendUnique.
+    if (Flag.Behavior <= Module::ModFlagBehavior::AppendUnique)
+      continue;
+    Flag.Behavior = Module::ModFlagBehavior::Warning;
+    Updated = true;
+  }
+
+  if (!Updated)
+    return;
+
+  MDFlags->eraseFromParent();
+
+  for (auto &Flag : FlagEntries)
+    M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
+}
+
 class DXILPrepareModule : public ModulePass {
 
   static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
@@ -213,6 +238,8 @@ class DXILPrepareModule : public ModulePass {
         }
       }
     }
+    // Remove flags not for DXIL.
+    cleanModuleFlags(M);
     return true;
   }
 

diff  --git a/llvm/test/CodeGen/DirectX/legalize-module-flags.ll b/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
new file mode 100644
index 0000000000000..1483a87e0b4bd
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
@@ -0,0 +1,20 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; Make sure behavior flag > 6 is fixed.
+; CHECK: !{i32 2, !"frame-pointer", i32 2}
+; CHECK: !{i32 2, !"Dwarf Version", i32 4}
+; CHECK: !{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 4}
+!3 = !{i32 2, !"Debug Info Version", i32 3}

diff  --git a/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll b/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
new file mode 100644
index 0000000000000..e1803b4672684
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
@@ -0,0 +1,17 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; CHECK: define void @main()
+; Make sure behavior flag > 6 is fixed.
+; CHECK:{i32 2, !"frame-pointer", i32 2}
+
+; 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}

diff  --git a/llvm/test/tools/dxil-dis/di-compile-unit.ll b/llvm/test/tools/dxil-dis/di-compile-unit.ll
index 11ad2c43d0d8a..10988fc3defe4 100644
--- a/llvm/test/tools/dxil-dis/di-compile-unit.ll
+++ b/llvm/test/tools/dxil-dis/di-compile-unit.ll
@@ -13,5 +13,5 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 ; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2)
 ; CHECK: !1 = !DIFile(filename: "di-compile-unit.src", directory: "/some-path")
 ; CHECK: !2 = !{}
-; CHECK: !3 = !{i32 7, !"Dwarf Version", i32 2}
-; CHECK: !4 = !{i32 2, !"Debug Info Version", i32 3}
+; CHECK: !{i32 2, !"Dwarf Version", i32 2}
+; CHECK: !{i32 2, !"Debug Info Version", i32 3}

diff  --git a/llvm/test/tools/dxil-dis/di-subprogram.ll b/llvm/test/tools/dxil-dis/di-subprogram.ll
index 1ed561c99f60d..8255d396dd55d 100644
--- a/llvm/test/tools/dxil-dis/di-subprogram.ll
+++ b/llvm/test/tools/dxil-dis/di-subprogram.ll
@@ -12,8 +12,8 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 !1 = !DIFile(filename: "some-source", directory: "some-path")
 !2 = !{}
 
-; CHECK: !3 = !{i32 7, !"Dwarf Version", i32 2}
-!3 = !{i32 7, !"Dwarf Version", i32 2}
+; CHECK: !3 = !{i32 2, !"Dwarf Version", i32 4}
+!3 = !{i32 2, !"Dwarf Version", i32 4}
 ; CHECK: !4 = !{i32 2, !"Debug Info Version", i32 3}
 !4 = !{i32 2, !"Debug Info Version", i32 3}
 

diff  --git a/llvm/test/tools/dxil-dis/di-subrange.ll b/llvm/test/tools/dxil-dis/di-subrange.ll
index f9527d18dab02..5141fe509d647 100644
--- a/llvm/test/tools/dxil-dis/di-subrange.ll
+++ b/llvm/test/tools/dxil-dis/di-subrange.ll
@@ -4,7 +4,7 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 !llvm.module.flags = !{!0, !1}
 !llvm.dbg.cu = !{!2}
 
-!0 = !{i32 7, !"Dwarf Version", i32 2}
+!0 = !{i32 2, !"Dwarf Version", i32 4}
 !1 = !{i32 2, !"Debug Info Version", i32 3}
 !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "xyz", isOptimized: true, runtimeVersion: 0, emissionKind: 1, retainedTypes: !4)
 !3 = !DIFile(filename: "input.hlsl", directory: "/some/path")
@@ -17,7 +17,7 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 ; CHECK: !llvm.module.flags = !{!0, !1}
 ; CHECK: !llvm.dbg.cu = !{!2}
 
-; CHECK: !0 = !{i32 7, !"Dwarf Version", i32 2}
+; CHECK: !0 = !{i32 2, !"Dwarf Version", i32 4}
 ; CHECK: !1 = !{i32 2, !"Debug Info Version", i32 3}
 ; CHECK: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "xyz", isOptimized: true, runtimeVersion: 0, emissionKind: 1, retainedTypes: !4)
 ; CHECK: !3 = !DIFile(filename: "input.hlsl", directory: "/some/path")


        


More information about the llvm-commits mailing list