[llvm] [DirectX] remove module flags and function attribute DXIL not allowed (PR #90778)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 14:03:31 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Xiang Li (python3kgae)

<details>
<summary>Changes</summary>

Remove module flags other than "Dwarf Version" and "Debug Info Version". Remove string function attribute other than "waveops-include-helper-lanes" and "fp32-denorm-mode".

Move DXILPrepareModulePass after DXILTranslateMetadataPass since DXILTranslateMetadataPass needs to use attribute like hlsl.numthreads.

Fixes #<!-- -->90773

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


5 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILPrepare.cpp (+64-1) 
- (modified) llvm/lib/Target/DirectX/DirectXTargetMachine.cpp (+1-1) 
- (modified) llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll (+4) 
- (modified) llvm/test/CodeGen/DirectX/dxil_ver.ll (+5) 
- (modified) llvm/test/tools/dxil-dis/attribute-filter.ll (+2-2) 


``````````diff
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index 026911946b47f0..e3a7ddee083b0e 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -11,10 +11,13 @@
 /// Language (DXIL).
 //===----------------------------------------------------------------------===//
 
+#include "DXILResourceAnalysis.h"
+#include "DXILShaderFlags.h"
 #include "DirectX.h"
 #include "DirectXIRPasses/PointerTypeAnalysis.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/IR/AttributeMask.h"
 #include "llvm/IR/IRBuilder.h"
@@ -80,6 +83,60 @@ constexpr bool isValidForDXIL(Attribute::AttrKind Attr) {
                       Attr);
 }
 
+static void collectDeadStringAttrs(AttributeMask &DeadAttrs, AttributeSet &&AS,
+                                   StringSet<> LiveKeys) {
+  for (auto &Attr : AS) {
+    if (!Attr.isStringAttribute())
+      continue;
+    StringRef Key = Attr.getKindAsString();
+    if (LiveKeys.contains(Key))
+      continue;
+    DeadAttrs.addAttribute(Key);
+  }
+}
+
+static void removeStringFunctionAttributes(Function &F) {
+  AttributeList Attrs = F.getAttributes();
+  StringSet<> LiveKeys = {"waveops-include-helper-lanes"
+                          "fp32-denorm-mode"};
+  // Collect DeadKeys in FnAttrs.
+  AttributeMask DeadAttrs;
+  collectDeadStringAttrs(DeadAttrs, Attrs.getFnAttrs(), LiveKeys);
+  collectDeadStringAttrs(DeadAttrs, Attrs.getRetAttrs(), LiveKeys);
+
+  F.removeFnAttrs(DeadAttrs);
+  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);
+
+  bool HasDeadKey = false;
+  for (auto &Flag : FlagEntries) {
+    if (!LiveKeys.count(Flag.Key->getString())) {
+      HasDeadKey = true;
+      break;
+    }
+  }
+  if (!HasDeadKey)
+    return;
+
+  MDFlags->eraseFromParent();
+
+  for (auto &Flag : FlagEntries) {
+    if (!LiveKeys.count(Flag.Key->getString()))
+      continue;
+    M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
+  }
+}
+
 class DXILPrepareModule : public ModulePass {
 
   static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
@@ -113,6 +170,7 @@ class DXILPrepareModule : public ModulePass {
     for (auto &F : M.functions()) {
       F.removeFnAttrs(AttrMask);
       F.removeRetAttrs(AttrMask);
+      removeStringFunctionAttributes(F);
       for (size_t Idx = 0, End = F.arg_size(); Idx < End; ++Idx)
         F.removeParamAttrs(Idx, AttrMask);
 
@@ -168,11 +226,16 @@ class DXILPrepareModule : public ModulePass {
         }
       }
     }
+    // Remove flags not in llvm3.7.
+    cleanModuleFlags(M);
     return true;
   }
 
   DXILPrepareModule() : ModulePass(ID) {}
-
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.addPreserved<ShaderFlagsAnalysisWrapper>();
+    AU.addPreserved<DXILResourceWrapper>();
+  }
   static char ID; // Pass identification.
 };
 char DXILPrepareModule::ID = 0;
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index bebca0675522f3..c853393e4282a4 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -79,8 +79,8 @@ class DirectXPassConfig : public TargetPassConfig {
   void addCodeGenPrepare() override {
     addPass(createDXILIntrinsicExpansionLegacyPass());
     addPass(createDXILOpLoweringLegacyPass());
-    addPass(createDXILPrepareModulePass());
     addPass(createDXILTranslateMetadataPass());
+    addPass(createDXILPrepareModulePass());
   }
 };
 
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
index be4b46f22ef25f..f617ad8d299ef5 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
@@ -1,4 +1,6 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-prepare  %s | FileCheck %s  --check-prefix=REMOVE_EXTRA_ATTRIBUTE
+
 target triple = "dxil-pc-shadermodel6.6-compute"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
@@ -9,4 +11,6 @@ entry:
   ret void
 }
 
+; Make sure extra attribute like hlsl.numthreads are removed.
+; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind } 
 attributes #0 = { noinline nounwind "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
diff --git a/llvm/test/CodeGen/DirectX/dxil_ver.ll b/llvm/test/CodeGen/DirectX/dxil_ver.ll
index e9923a3abce02d..c893a912f92f1f 100644
--- a/llvm/test/CodeGen/DirectX/dxil_ver.ll
+++ b/llvm/test/CodeGen/DirectX/dxil_ver.ll
@@ -1,4 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit < %s | FileCheck %s
+; RUN: opt -S -dxil-prepare  %s | FileCheck %s  --check-prefix=REMOVE_EXTRA_MODULE_FLAG
 target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
 target triple = "dxil-pc-shadermodel6.3-library"
 
@@ -11,6 +12,10 @@ target triple = "dxil-pc-shadermodel6.3-library"
 ; Make sure wchar_size still exist.
 ; CHECK-DAG:!{i32 1, !"wchar_size", i32 4}
 
+; Make sure no !llvm.module.flags left.
+; REMOVE_EXTRA_MODULE_FLAG: target triple = "dxil-pc-shadermodel6.3-library"
+; REMOVE_EXTRA_MODULE_FLAG-NOT: !llvm.module.flags
+
 !llvm.module.flags = !{!0}
 !dx.valver = !{!1}
 !llvm.ident = !{!2}
diff --git a/llvm/test/tools/dxil-dis/attribute-filter.ll b/llvm/test/tools/dxil-dis/attribute-filter.ll
index 432a5a1b71018c..03c7c36c312584 100644
--- a/llvm/test/tools/dxil-dis/attribute-filter.ll
+++ b/llvm/test/tools/dxil-dis/attribute-filter.ll
@@ -19,8 +19,8 @@ define float @fma2(float %0, float %1, float %2) #1 {
   ret float %5
 }
 
-; CHECK: attributes #0 = { nounwind readnone "disable-tail-calls"="false" }
+; CHECK: attributes #0 = { nounwind readnone }
 attributes #0 = { norecurse nounwind readnone willreturn "disable-tail-calls"="false" }
 
-; CHECK: attributes #1 = { readnone "disable-tail-calls"="false" }
+; CHECK: attributes #1 = { readnone }
 attributes #1 = { norecurse memory(none) willreturn "disable-tail-calls"="false" }

``````````

</details>


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


More information about the llvm-commits mailing list