[llvm] [DirectX] remove string function attribute DXIL not allowed (PR #90778)
Xiang Li via llvm-commits
llvm-commits at lists.llvm.org
Fri May 3 15:18:57 PDT 2024
https://github.com/python3kgae updated https://github.com/llvm/llvm-project/pull/90778
>From af945856d226ac6cfcf695d3836c029db551134f Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Wed, 1 May 2024 16:58:44 -0400
Subject: [PATCH 1/3] [DirectX] remove module flags and function attribute DXIL
not allowed
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
---
llvm/lib/Target/DirectX/DXILPrepare.cpp | 65 ++++++++++++++++++-
.../Target/DirectX/DirectXTargetMachine.cpp | 2 +-
.../DirectX/Metadata/shaderModel-cs.ll | 4 ++
llvm/test/CodeGen/DirectX/dxil_ver.ll | 5 ++
llvm/test/tools/dxil-dis/attribute-filter.ll | 4 +-
5 files changed, 76 insertions(+), 4 deletions(-)
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" }
>From 463b280c75198cab5c127bc47f45df5f41049a44 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Fri, 3 May 2024 16:36:47 -0400
Subject: [PATCH 2/3] Remove the module flag part.
---
llvm/lib/Target/DirectX/DXILPrepare.cpp | 31 -------------------------
llvm/test/CodeGen/DirectX/dxil_ver.ll | 5 ----
2 files changed, 36 deletions(-)
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index e3a7ddee083b0e..a6680a4a400892 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -108,35 +108,6 @@ 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);
-
- 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,
@@ -226,8 +197,6 @@ class DXILPrepareModule : public ModulePass {
}
}
}
- // Remove flags not in llvm3.7.
- cleanModuleFlags(M);
return true;
}
diff --git a/llvm/test/CodeGen/DirectX/dxil_ver.ll b/llvm/test/CodeGen/DirectX/dxil_ver.ll
index c893a912f92f1f..e9923a3abce02d 100644
--- a/llvm/test/CodeGen/DirectX/dxil_ver.ll
+++ b/llvm/test/CodeGen/DirectX/dxil_ver.ll
@@ -1,5 +1,4 @@
; 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"
@@ -12,10 +11,6 @@ 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}
>From fb3e1e7a1b6d7bb57d626556c95fa6e745c37b94 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Fri, 3 May 2024 18:18:38 -0400
Subject: [PATCH 3/3] Test supprted atttribute not be removed.
---
llvm/lib/Target/DirectX/DXILPrepare.cpp | 6 +++---
llvm/test/tools/dxil-dis/attribute-filter.ll | 8 ++++----
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index a6680a4a400892..9b5e9ebad09147 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -84,7 +84,7 @@ constexpr bool isValidForDXIL(Attribute::AttrKind Attr) {
}
static void collectDeadStringAttrs(AttributeMask &DeadAttrs, AttributeSet &&AS,
- StringSet<> LiveKeys) {
+ const StringSet<> &LiveKeys) {
for (auto &Attr : AS) {
if (!Attr.isStringAttribute())
continue;
@@ -97,8 +97,8 @@ static void collectDeadStringAttrs(AttributeMask &DeadAttrs, AttributeSet &&AS,
static void removeStringFunctionAttributes(Function &F) {
AttributeList Attrs = F.getAttributes();
- StringSet<> LiveKeys = {"waveops-include-helper-lanes"
- "fp32-denorm-mode"};
+ const StringSet<> LiveKeys = {"waveops-include-helper-lanes",
+ "fp32-denorm-mode"};
// Collect DeadKeys in FnAttrs.
AttributeMask DeadAttrs;
collectDeadStringAttrs(DeadAttrs, Attrs.getFnAttrs(), LiveKeys);
diff --git a/llvm/test/tools/dxil-dis/attribute-filter.ll b/llvm/test/tools/dxil-dis/attribute-filter.ll
index 03c7c36c312584..27590e10d79b54 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 }
-attributes #0 = { norecurse nounwind readnone willreturn "disable-tail-calls"="false" }
+; CHECK: attributes #0 = { nounwind readnone "fp32-denorm-mode"="any" "waveops-include-helper-lanes" }
+attributes #0 = { norecurse nounwind readnone willreturn "disable-tail-calls"="false" "waveops-include-helper-lanes" "fp32-denorm-mode"="any" }
-; CHECK: attributes #1 = { readnone }
-attributes #1 = { norecurse memory(none) willreturn "disable-tail-calls"="false" }
+; CHECK: attributes #1 = { readnone "fp32-denorm-mode"="ftz" "waveops-include-helper-lanes" }
+attributes #1 = { norecurse memory(none) willreturn "disable-tail-calls"="false" "waveops-include-helper-lanes" "fp32-denorm-mode"="ftz" }
More information about the llvm-commits
mailing list