[llvm] a0ecb4a - [HLSL] Move DXIL validation version out of ModuleFlags
Xiang Li via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 26 09:21:02 PDT 2022
Author: Xiang Li
Date: 2022-08-26T09:20:45-07:00
New Revision: a0ecb4a2991d6163c16c29bee8cd63720ee7d3d7
URL: https://github.com/llvm/llvm-project/commit/a0ecb4a2991d6163c16c29bee8cd63720ee7d3d7
DIFF: https://github.com/llvm/llvm-project/commit/a0ecb4a2991d6163c16c29bee8cd63720ee7d3d7.diff
LOG: [HLSL] Move DXIL validation version out of ModuleFlags
Put DXIL validation version into separate NamedMetadata to avoid update ModuleFlags.
Currently DXIL validation version is saved in ModuleFlags in clang codeGen.
Then in DirectX backend, the data will be extracted from ModuleFlags and cause rebuild of ModuleFlags.
This patch will build NamedMetadata for DXIL validation version and remove the code to rebuild ModuleFlags.
Reviewed By: beanz
Differential Revision: https://reviews.llvm.org/D130207
Added:
Modified:
clang/lib/CodeGen/CGHLSLRuntime.cpp
clang/test/CodeGenHLSL/validator_version.hlsl
llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
llvm/test/CodeGen/DirectX/dxil_ver.ll
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index fa7bab9121b7a..9ca3b64996edd 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -42,16 +42,18 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
IRBuilder<> B(M.getContext());
MDNode *Val = MDNode::get(Ctx, {ConstantAsMetadata::get(B.getInt32(Major)),
ConstantAsMetadata::get(B.getInt32(Minor))});
- StringRef DxilValKey = "dx.valver";
- M.addModuleFlag(llvm::Module::ModFlagBehavior::AppendUnique, DxilValKey, Val);
+ StringRef DXILValKey = "dx.valver";
+ auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey);
+ DXILValMD->addOperand(Val);
}
} // namespace
void CGHLSLRuntime::finishCodeGen() {
auto &TargetOpts = CGM.getTarget().getTargetOpts();
-
llvm::Module &M = CGM.getModule();
- addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
+ Triple T(M.getTargetTriple());
+ if (T.getArch() == Triple::ArchType::dxil)
+ addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
}
void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
diff --git a/clang/test/CodeGenHLSL/validator_version.hlsl b/clang/test/CodeGenHLSL/validator_version.hlsl
index eee83bd9677be..426918a7221c4 100644
--- a/clang/test/CodeGenHLSL/validator_version.hlsl
+++ b/clang/test/CodeGenHLSL/validator_version.hlsl
@@ -1,8 +1,11 @@
// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -S -emit-llvm -xhlsl -validator-version 1.1 -o - %s | FileCheck %s
+// RUN: %clang -cc1 -S -triple spirv32 -S -emit-llvm -xhlsl -validator-version 1.1 -o - %s | FileCheck %s --check-prefix=NOT_DXIL
-// CHECK:!"dx.valver", ![[valver:[0-9]+]]}
+// CHECK:!dx.valver = !{![[valver:[0-9]+]]}
// CHECK:![[valver]] = !{i32 1, i32 1}
+// NOT_DXIL-NOT:!dx.valver
+
float bar(float a, float b);
float foo(float a, float b) {
diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
index 634ead98a6ae7..85f1b0784cda3 100644
--- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
@@ -58,32 +58,6 @@ static VersionTuple loadDXILValidatorVersion(MDNode *ValVerMD) {
return VersionTuple(Major, Minor);
}
-static void cleanModuleFlags(Module &M) {
- constexpr StringLiteral DeadKeys[] = {ValVerKey};
- // Collect DeadKeys in ModuleFlags.
- StringSet<> DeadKeySet;
- for (auto &Key : DeadKeys) {
- if (M.getModuleFlag(Key))
- DeadKeySet.insert(Key);
- }
- if (DeadKeySet.empty())
- return;
-
- SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
- M.getModuleFlagsMetadata(ModuleFlags);
- NamedMDNode *MDFlags = M.getModuleFlagsMetadata();
- MDFlags->eraseFromParent();
- // Add ModuleFlag which not dead.
- for (auto &Flag : ModuleFlags) {
- StringRef Key = Flag.Key->getString();
- if (DeadKeySet.contains(Key))
- continue;
- M.addModuleFlag(Flag.Behavior, Key, Flag.Val);
- }
-}
-
-static void cleanModule(Module &M) { cleanModuleFlags(M); }
-
namespace {
class DXILTranslateMetadata : public ModulePass {
public:
@@ -101,13 +75,12 @@ class DXILTranslateMetadata : public ModulePass {
} // namespace
bool DXILTranslateMetadata::runOnModule(Module &M) {
- if (MDNode *ValVerMD = cast_or_null<MDNode>(M.getModuleFlag(ValVerKey))) {
- auto ValVer = loadDXILValidatorVersion(ValVerMD);
+ if (NamedMDNode *ValVerMD = M.getNamedMetadata(ValVerKey)) {
+ VersionTuple ValVer = loadDXILValidatorVersion(ValVerMD->getOperand(0));
if (!ValVer.empty())
ValidatorVer = ValVer;
}
emitDXILValidatorVersion(M, ValidatorVer);
- cleanModule(M);
return false;
}
diff --git a/llvm/test/CodeGen/DirectX/dxil_ver.ll b/llvm/test/CodeGen/DirectX/dxil_ver.ll
index dcc9b6a797f6a..e9923a3abce02 100644
--- a/llvm/test/CodeGen/DirectX/dxil_ver.ll
+++ b/llvm/test/CodeGen/DirectX/dxil_ver.ll
@@ -3,18 +3,18 @@ target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32
target triple = "dxil-pc-shadermodel6.3-library"
; Make sure dx.valver metadata is generated.
-; CHECK:!dx.valver = !{![[valver:[0-9]+]]}
+; CHECK-DAG:!dx.valver = !{![[valver:[0-9]+]]}
; Make sure module flags still exist and only have 1 operand left.
-; CHECK:!llvm.module.flags = !{{{![0-9]}}}
+; CHECK-DAG:!llvm.module.flags = !{{{![0-9]}}}
; Make sure validator version is 1.1.
-; CHECK:![[valver]] = !{i32 1, i32 1}
+; CHECK-DAG:![[valver]] = !{i32 1, i32 1}
; Make sure wchar_size still exist.
-; CHECK:!{i32 1, !"wchar_size", i32 4}
+; CHECK-DAG:!{i32 1, !"wchar_size", i32 4}
-!llvm.module.flags = !{!0, !1}
-!llvm.ident = !{!3}
+!llvm.module.flags = !{!0}
+!dx.valver = !{!1}
+!llvm.ident = !{!2}
!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 6, !"dx.valver", !2}
-!2 = !{i32 1, i32 1}
-!3 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project 71de12113a0661649ecb2f533fba4a2818a1ad68)"}
+!1 = !{i32 1, i32 1}
+!2 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project 71de12113a0661649ecb2f533fba4a2818a1ad68)"}
More information about the llvm-commits
mailing list