Skip to content

[DirectX] fix illegal behavior flag in module flags. #96577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2024

Conversation

python3kgae
Copy link
Contributor

@python3kgae python3kgae commented Jun 25, 2024

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.

@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2024

@llvm/pr-subscribers-backend-directx

Author: Xiang Li (python3kgae)

Changes

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

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 llvm#96912.
@python3kgae python3kgae changed the title [DirectX] remove module flags not for DXIL. [DirectX] fix illegal behavior flag in module flags. Jul 3, 2024
@@ -0,0 +1,17 @@
; RUN: opt -S -dxil-prepare < %s | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like clean-module-flags.ll and clean-module-flags2.ll are testing the same thing? Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean-module-flags.ll has Dwarf Version and Debug Info Version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems overkill to me - and by this precedent we'd need to add new combinations of flags is new ones ever show up. But probably doesn't hurt.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we give this new test a more descriptive name? This is actually testing updating the flag behavior field, so how about "update-module-flag-behavior.ll"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to legalize module flag.

; RUN: opt -S -dxil-prepare < %s | FileCheck %s

; Make sure behavior flag > 6 is fixed.
; CHECK-NOT: !{i32 7, !"frame-pointer", i32 2}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interested in why:

  • no CHECK line for wchar_size?
  • CHECK-NOT is used for frame-pointer, but CHECK is used for Dwarf Version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For DXIL, it is legal to not have wchar_size and frame-pointer.
But remove Dwarf Version will lost information for DXIL.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check doesn't seem correct. The code change here doesn't remove the flag it changes the behavior value. We should check for the updated behavior not check that the old behavior doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@bob80905
Copy link
Contributor

Could you explain why the flag behavior is set to warning? If I understand, you have 6 options to choose from, so why pick that one?

@bob80905
Copy link
Contributor

Could you explain why the flag behavior is set to warning? If I understand, you have 6 options to choose from, so why pick that one?

I read the issue, I think I get it now.

@python3kgae python3kgae merged commit 76e37b1 into llvm:main Jul 12, 2024
7 checks passed
@python3kgae python3kgae deleted the cleanModuleFlag branch July 12, 2024 22:46
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
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 llvm#96912.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants