[llvm] [DirectX backend] emits metadata for DXIL version. (PR #88350)
Xiang Li via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 10 21:14:31 PDT 2024
https://github.com/python3kgae created https://github.com/llvm/llvm-project/pull/88350
Emit named metadata "dx.version" for DXIL version.
Default to DXIL 1.0
>From c1f0280b29d8cb13c48b1ba3a9c57d3659aed56a Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Thu, 11 Apr 2024 00:09:46 -0400
Subject: [PATCH] [DirectX backend] emit metadata for DXIL version.
Emit named metadata "dx.version".
Default to DXIL 1.0
---
llvm/lib/Target/DirectX/DXILMetadata.cpp | 12 ++++++++++++
llvm/lib/Target/DirectX/DXILMetadata.h | 1 +
llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp | 1 +
llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll | 12 ++++++++++++
llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll | 12 ++++++++++++
5 files changed, 38 insertions(+)
create mode 100644 llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
create mode 100644 llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
diff --git a/llvm/lib/Target/DirectX/DXILMetadata.cpp b/llvm/lib/Target/DirectX/DXILMetadata.cpp
index 2d94490a7f24c3..61882f85095664 100644
--- a/llvm/lib/Target/DirectX/DXILMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILMetadata.cpp
@@ -81,6 +81,18 @@ void dxil::createShaderModelMD(Module &M) {
Entry->addOperand(MDNode::get(Ctx, Vals));
}
+void dxil::createDXILVersionMD(Module &M) {
+ Triple TT(M.getTargetTriple());
+ VersionTuple Ver = TT.getOSVersion();
+ LLVMContext &Ctx = M.getContext();
+ IRBuilder<> B(Ctx);
+ NamedMDNode *Entry = M.getOrInsertNamedMetadata("dx.version");
+ Metadata *Vals[2];
+ Vals[0] = ConstantAsMetadata::get(B.getInt32(1));
+ Vals[1] = ConstantAsMetadata::get(B.getInt32(Ver.getMinor().value_or(0)));
+ Entry->addOperand(MDNode::get(Ctx, Vals));
+}
+
static uint32_t getShaderStage(Triple::EnvironmentType Env) {
return (uint32_t)Env - (uint32_t)llvm::Triple::Pixel;
}
diff --git a/llvm/lib/Target/DirectX/DXILMetadata.h b/llvm/lib/Target/DirectX/DXILMetadata.h
index 2f5d7d9fe7683d..c105c15f3d47af 100644
--- a/llvm/lib/Target/DirectX/DXILMetadata.h
+++ b/llvm/lib/Target/DirectX/DXILMetadata.h
@@ -33,6 +33,7 @@ class ValidatorVersionMD {
};
void createShaderModelMD(Module &M);
+void createDXILVersionMD(Module &M);
void createEntryMD(Module &M, const uint64_t ShaderFlags);
} // namespace dxil
diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
index 80d94bf0c9d488..ae6d6f96904c86 100644
--- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
@@ -48,6 +48,7 @@ bool DXILTranslateMetadata::runOnModule(Module &M) {
if (ValVerMD.isEmpty())
ValVerMD.update(VersionTuple(1, 0));
dxil::createShaderModelMD(M);
+ dxil::createDXILVersionMD(M);
const dxil::Resources &Res =
getAnalysis<DXILResourceWrapper>().getDXILResource();
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
new file mode 100644
index 00000000000000..254479e5f94cbd
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
@@ -0,0 +1,12 @@
+; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+target triple = "dxil-pc-shadermodel6.0-vertex"
+
+; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
+; CHECK: ![[DXVER]] = !{i32 1, i32 0}
+
+define void @entry() #0 {
+entry:
+ ret void
+}
+
+attributes #0 = { noinline nounwind "hlsl.shader"="vertex" }
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
new file mode 100644
index 00000000000000..efeb5a1b24862e
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
@@ -0,0 +1,12 @@
+; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+target triple = "dxil-pc-shadermodel6.8-compute"
+
+; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
+; CHECK: ![[DXVER]] = !{i32 1, i32 8}
+
+define void @entry() #0 {
+entry:
+ ret void
+}
+
+attributes #0 = { noinline nounwind "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
More information about the llvm-commits
mailing list