[llvm] [DirectX] Strip `dx.rootsignatures` metadata during `dxil-prepare` (PR #145746)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 25 10:40:01 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-directx
Author: Finn Plummer (inbelic)
<details>
<summary>Changes</summary>
The `dx.rootsignatures` metadata is not recognized in DXIL, so failure to remove this will cause validation errors.
This metadata is parsed (within `RootSignatureAnalysisWrapper`) into its binary format. As such, once it has been used to construct the binary form, it can be safely discarded without loss of information.
This pr ensures that the dxil prepare pass will depend and preserve on the root signature analysis so that it runs before the metadata is removed.
- Update `DXILPrepare.cpp` to preserve and depend on `RootSignatureAnalysisWrapper`
- Update test to demonstrate order is correct
- Provide test-case to demonstrate the metadata is removed
Resolves https://github.com/llvm/llvm-project/issues/145437.
---
Full diff: https://github.com/llvm/llvm-project/pull/145746.diff
3 Files Affected:
- (modified) llvm/lib/Target/DirectX/DXILPrepare.cpp (+11)
- (modified) llvm/test/CodeGen/DirectX/llc-pipeline.ll (+1-1)
- (added) llvm/test/CodeGen/DirectX/strip-rootsignatures.ll (+17)
``````````diff
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index cb58f4833631d..c8866bfefdfc5 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -11,6 +11,7 @@
/// Language (DXIL).
//===----------------------------------------------------------------------===//
+#include "DXILRootSignature.h"
#include "DXILShaderFlags.h"
#include "DirectX.h"
#include "DirectXIRPasses/PointerTypeAnalysis.h"
@@ -286,12 +287,21 @@ class DXILPrepareModule : public ModulePass {
}
// Remove flags not for DXIL.
cleanModuleFlags(M);
+
+ // dx.rootsignatures will have been parsed from its metadata form as its
+ // binary form as part of the RootSignatureAnalysisWrapper, so safely
+ // remove it as it is not recognized in DXIL
+ if (NamedMDNode *RootSignature = M.getNamedMetadata("dx.rootsignatures"))
+ RootSignature->eraseFromParent();
+
return true;
}
DXILPrepareModule() : ModulePass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
+ AU.addRequired<RootSignatureAnalysisWrapper>();
+ AU.addPreserved<RootSignatureAnalysisWrapper>();
AU.addPreserved<ShaderFlagsAnalysisWrapper>();
AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
AU.addPreserved<DXILResourceWrapperPass>();
@@ -305,6 +315,7 @@ char DXILPrepareModule::ID = 0;
INITIALIZE_PASS_BEGIN(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module",
false, false)
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
+INITIALIZE_PASS_DEPENDENCY(RootSignatureAnalysisWrapper)
INITIALIZE_PASS_END(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module", false,
false)
diff --git a/llvm/test/CodeGen/DirectX/llc-pipeline.ll b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
index 2b29fd30a7a56..5934de2c9758a 100644
--- a/llvm/test/CodeGen/DirectX/llc-pipeline.ll
+++ b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
@@ -33,13 +33,13 @@
; CHECK-NEXT: DXIL Translate Metadata
; CHECK-NEXT: DXIL Post Optimization Validation
; CHECK-NEXT: DXIL Op Lowering
+; CHECK-NEXT: DXIL Root Signature Analysis
; CHECK-NEXT: DXIL Prepare Module
; CHECK-ASM-NEXT: DXIL Metadata Pretty Printer
; CHECK-ASM-NEXT: Print Module IR
; CHECK-OBJ-NEXT: DXIL Embedder
-; CHECK-OBJ-NEXT: DXIL Root Signature Analysis
; CHECK-OBJ-NEXT: DXContainer Global Emitter
; CHECK-OBJ-NEXT: FunctionPass Manager
; CHECK-OBJ-NEXT: Lazy Machine Block Frequency Analysis
diff --git a/llvm/test/CodeGen/DirectX/strip-rootsignatures.ll b/llvm/test/CodeGen/DirectX/strip-rootsignatures.ll
new file mode 100644
index 0000000000000..3b3ddf4886511
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/strip-rootsignatures.ll
@@ -0,0 +1,17 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; Ensures that dxil-prepare will remove the dx.rootsignatures metadata
+
+target triple = "dxil-unknown-shadermodel6.0-compute"
+
+define void @main() {
+entry:
+ ret void
+}
+
+; CHECK-NOT: !dx.rootsignature
+
+!dx.rootsignatures = !{!2} ; list of function/root signature pairs
+!2 = !{ ptr @main, !3, i32 2 } ; function, root signature
+!3 = !{ !4 } ; list of root signature elements
+!4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout
``````````
</details>
https://github.com/llvm/llvm-project/pull/145746
More information about the llvm-commits
mailing list