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

Xiang Li via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 07:04:25 PDT 2024


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

>From 52ba10466cfb68e6677ac9829b9794ede55d920d 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 1/2] [DirectX] fix illegal behavior flag in module flags.

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.
---
 llvm/lib/Target/DirectX/DXILPrepare.cpp       | 27 +++++++++++++++++++
 .../CodeGen/DirectX/clean-module-flags.ll     | 20 ++++++++++++++
 .../CodeGen/DirectX/clean-module-flags2.ll    | 17 ++++++++++++
 llvm/test/tools/dxil-dis/di-compile-unit.ll   |  4 +--
 llvm/test/tools/dxil-dis/di-subprogram.ll     |  4 +--
 llvm/test/tools/dxil-dis/di-subrange.ll       |  4 +--
 6 files changed, 70 insertions(+), 6 deletions(-)
 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..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/clean-module-flags.ll b/llvm/test/CodeGen/DirectX/clean-module-flags.ll
new file mode 100644
index 0000000000000..2570eb9189d87
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/clean-module-flags.ll
@@ -0,0 +1,20 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; Make sure behavior flag > 6 is fixed.
+; CHECK-NOT: !{i32 7, !"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/clean-module-flags2.ll b/llvm/test/CodeGen/DirectX/clean-module-flags2.ll
new file mode 100644
index 0000000000000..37571e0df8a91
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/clean-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-NOT:{i32 7, !"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")

>From 5fd7acded954a8a708bb5fe0cb1384dadfbb2049 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Thu, 11 Jul 2024 10:03:59 -0400
Subject: [PATCH 2/2] Update test.

---
 .../DirectX/{clean-module-flags.ll => legalize-module-flags.ll} | 2 +-
 .../{clean-module-flags2.ll => legalize-module-flags2.ll}       | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
 rename llvm/test/CodeGen/DirectX/{clean-module-flags.ll => legalize-module-flags.ll} (91%)
 rename llvm/test/CodeGen/DirectX/{clean-module-flags2.ll => legalize-module-flags2.ll} (89%)

diff --git a/llvm/test/CodeGen/DirectX/clean-module-flags.ll b/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
similarity index 91%
rename from llvm/test/CodeGen/DirectX/clean-module-flags.ll
rename to llvm/test/CodeGen/DirectX/legalize-module-flags.ll
index 2570eb9189d87..1483a87e0b4bd 100644
--- a/llvm/test/CodeGen/DirectX/clean-module-flags.ll
+++ b/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
@@ -1,7 +1,7 @@
 ; RUN: opt -S -dxil-prepare < %s | FileCheck %s
 
 ; Make sure behavior flag > 6 is fixed.
-; CHECK-NOT: !{i32 7, !"frame-pointer", i32 2}
+; CHECK: !{i32 2, !"frame-pointer", i32 2}
 ; CHECK: !{i32 2, !"Dwarf Version", i32 4}
 ; CHECK: !{i32 2, !"Debug Info Version", i32 3}
 
diff --git a/llvm/test/CodeGen/DirectX/clean-module-flags2.ll b/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
similarity index 89%
rename from llvm/test/CodeGen/DirectX/clean-module-flags2.ll
rename to llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
index 37571e0df8a91..e1803b4672684 100644
--- a/llvm/test/CodeGen/DirectX/clean-module-flags2.ll
+++ b/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
@@ -2,7 +2,7 @@
 
 ; CHECK: define void @main()
 ; Make sure behavior flag > 6 is fixed.
-; CHECK-NOT:{i32 7, !"frame-pointer", i32 2}
+; CHECK:{i32 2, !"frame-pointer", i32 2}
 
 ; Function Attrs: nounwind memory(none)
 define void @main() local_unnamed_addr #0 {



More information about the llvm-commits mailing list