Skip to content

[DirectX] Strip dx.rootsignatures metadata during dxil-prepare #145746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2025

Conversation

inbelic
Copy link
Contributor

@inbelic inbelic commented Jun 25, 2025

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 #145437.


Co-authored-by: Justin Bogner mail@justinbogner.com

@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-backend-directx

Author: Finn Plummer (inbelic)

Changes

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 #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 --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

ret void
}

; CHECK-NOT: !dx.rootsignature
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here (though it would still catch the problem). Should we also do a CHECK-NOT: {{^!}} or so to make sure the elements are cleaned up too?

Suggested change
; CHECK-NOT: !dx.rootsignature
; CHECK-NOT: !dx.rootsignatures
; CHECK-NOT: {{^!}}

@inbelic inbelic merged commit a8ef75e into llvm:main Jun 25, 2025
7 of 8 checks passed
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
…lvm#145746)

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 llvm#145437.

----------

Co-authored-by: Justin Bogner <mail@justinbogner.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DirectX] dx.rootsignatures metadata is left around, causing validation failure
4 participants