Skip to content

[DXIL] Add support for root signature flag element in DXContainer #123147

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 72 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
8adb678
adding rootsignature to obj2yaml
Jan 13, 2025
ba78f21
adding test
Jan 13, 2025
0a54559
removing old test
Jan 13, 2025
557075f
remove useless includes
Jan 13, 2025
e0d3dcd
addressing comments
Jan 14, 2025
80587dd
updating test
Jan 14, 2025
be3764d
removing useless header
Jan 15, 2025
7582ca6
fix formating
Jan 15, 2025
6aaa0a5
renaming test
Jan 15, 2025
916b2f1
addressing pr comments
Jan 16, 2025
d9bce0a
adding str to ROOT_ELEMENT_FLAG
Jan 16, 2025
e7676ed
formating
Jan 17, 2025
a0cee57
refactoring to follow llvm standards
Jan 18, 2025
1e7a1fe
addressing comments
Jan 27, 2025
0ed658a
clean up
Jan 27, 2025
932062e
remove version
Jan 30, 2025
628937c
fix pr
Jan 30, 2025
1378c9f
adding dxil-dis test
Jan 31, 2025
e3206c9
adding compatibility test
Jan 31, 2025
f93d42d
addressing test concerns
Feb 3, 2025
25e3f37
clean up
Feb 3, 2025
751cbdc
addressing comments
Feb 4, 2025
44532d6
adding fail test
Feb 4, 2025
ca21878
adding comment
Feb 4, 2025
987901c
adding few more tests
Feb 4, 2025
0fbe900
format
Feb 4, 2025
b771aea
cleanup
Feb 5, 2025
aabdfe7
adding metadata extraction
Jan 15, 2025
4f6f941
moving root signature to it's own pass
Jan 16, 2025
a7f7784
formating
Jan 16, 2025
bf3b2e0
removing useless imports
Jan 16, 2025
16b4d03
fixing pr changes
Jan 16, 2025
e043370
adding some asserts
Jan 16, 2025
57bf935
format
Jan 16, 2025
1f8c0a5
fixing assert
Jan 18, 2025
0905b83
cleaning
Jan 27, 2025
77e8544
clean up
Jan 29, 2025
1351fb0
addressing comments
Jan 30, 2025
09e645a
removing version
Jan 30, 2025
5a44b62
fix test
Jan 30, 2025
d1a79b3
addressing PR Comments
Jan 31, 2025
9f8e512
fix test
Feb 3, 2025
5c7ed7e
filtering root signatures not associated with entry function
Feb 4, 2025
93f7c4c
separating parsing and validation
Feb 4, 2025
5aac761
improve error handling
Feb 6, 2025
47b01f7
clean up
Feb 6, 2025
486ab88
clean up
Feb 6, 2025
852ac25
formating
Feb 6, 2025
74f7226
addressing comments and fix tests
Feb 7, 2025
7a82fa9
fixing tests
Feb 7, 2025
c67d039
formating
Feb 7, 2025
e80ef33
Merge branch 'main' into users/joaosaffran/123147
Feb 7, 2025
ef1ce8a
fix
Feb 7, 2025
b7f2716
Merge branch 'main' into users/joaosaffran/123147
Feb 10, 2025
83b0979
addressing pr comments
Feb 10, 2025
b175b65
addressing PR comments
Feb 11, 2025
01b49a7
addressing pr comments
Feb 11, 2025
7bba9d3
removing copies from root signature use in dx container globals
Feb 11, 2025
2809c2f
adding more tests
Feb 12, 2025
023dcb8
maybe fix test?
Feb 12, 2025
aedb446
fixing clang format
Feb 12, 2025
39e60c0
try fix format
Feb 12, 2025
3b135c6
removing test
Feb 12, 2025
ae3d03f
adding llvm unreachable and testing test
Feb 12, 2025
3d19f8b
stopping compilation if root signature error were emitted
Feb 12, 2025
5a3be7c
making sure Error tests fail
Feb 12, 2025
f64c608
refactoring root signature analysis to return a map instead
Feb 12, 2025
a5a2093
addressing pr comments
Feb 12, 2025
5b3fedc
clean up
Feb 12, 2025
605f225
addressing pr comments
Feb 13, 2025
e4bca2b
implementing find interface for RootSignatureAnalysisWrapper
Feb 13, 2025
825673f
adding test for null function
Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/Target/DirectX/DXContainerGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@ void DXContainerGlobals::addRootSignature(Module &M,
SmallVector<GlobalValue *> &Globals) {

auto &RSA = getAnalysis<RootSignatureAnalysisWrapper>();

if (!RSA.getResult())
return;

const ModuleRootSignature &MRS = RSA.getResult().value();
SmallString<256> Data;
raw_svector_ostream OS(Data);

RootSignatureHeader RSH;
RSH.Flags = RSA.getResult()->Flags;
RSH.Flags = MRS.Flags;

RSH.write(OS);

Expand Down
44 changes: 26 additions & 18 deletions llvm/lib/Target/DirectX/DXILRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
Expand Down Expand Up @@ -56,16 +57,12 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,

RootSignatureElementKind ElementKind =
StringSwitch<RootSignatureElementKind>(ElementText->getString())
.Case("RootFlags", RootSignatureElementKind::RootFlags)
.Default(RootSignatureElementKind::None);
.Case("RootFlags", RootSignatureElementKind::RootFlags);

switch (ElementKind) {

case RootSignatureElementKind::RootFlags:
return parseRootFlags(Ctx, MRS, Element);
default:
return reportError(Ctx,
"Invalid Root Element: " + ElementText->getString());
}

return true;
Expand Down Expand Up @@ -95,8 +92,14 @@ static bool parse(LLVMContext *Ctx, ModuleRootSignature *MRS, NamedMDNode *Root,
continue;
}

const MDOperand &FunctionPointerMdNode = Node->getOperand(0);
if (FunctionPointerMdNode == nullptr) {
// Function was pruned during compilation.
continue;
}
Copy link
Contributor

@damyanp damyanp Feb 12, 2025

Choose a reason for hiding this comment

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

Can you construct a test case that includes this? Is this something that can actually happen - a function is referenced by some metadata, and the function is removed but not the metadata that references it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found this error when running test: RootSignature-MultipleEntryFunctions.ll. During root signature generation, function @main was null, since entry point was function @anotherMain.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is that normal, or is this an indication of a bug somewhere where we also need to strip the corresponding metadata when a function is pruned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems expected, not used functions will be removed, since we record function pointers in the metadata, if the function is removed the pointer will be null. @bogner please correct me.

I think we should clean it, but we might need to do it in another pass.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be able to get here without hand-written invalid IR. The RootSignature-MultipleEntryFunctions.ll doesn't have multiple entry functions - it has entry function metadata for functions that are not entry functions - since @main doesn't have hlsl.shader metadata and is not exported it's removed by DXILFinalizeLinkage.

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

Suggested change
if (FunctionPointerMdNode == nullptr) {
// Function was pruned during compilation.
continue;
}
// Function was pruned during compilation.
if (FunctionPointerMdNode == nullptr)
continue;


ValueAsMetadata *VAM =
llvm::dyn_cast<ValueAsMetadata>(Node->getOperand(0).get());
llvm::dyn_cast<ValueAsMetadata>(FunctionPointerMdNode.get());
if (VAM == nullptr) {
HasError =
reportError(Ctx, "First element of root signature is not a value");
Expand Down Expand Up @@ -140,24 +143,26 @@ static bool validate(LLVMContext *Ctx, ModuleRootSignature *MRS) {
return false;
}

std::optional<ModuleRootSignature>
ModuleRootSignature::analyzeModule(Module &M, ModuleMetadataInfo MMI) {
if (MMI.ShaderProfile == Triple::Library)
return std::nullopt;
static const Function *getEntryFunction(Module &M, ModuleMetadataInfo MMI) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need to copy or modify the MMI here.

Suggested change
static const Function *getEntryFunction(Module &M, ModuleMetadataInfo MMI) {
static const Function *getEntryFunction(Module &M, const ModuleMetadataInfo &MMI) {


LLVMContext *Ctx = &M.getContext();

if (MMI.EntryPropertyVec.size() != 1) {
reportError(Ctx, "More than one entry function defined.");
return std::nullopt;
return nullptr;
}
return MMI.EntryPropertyVec[0].Entry;
}

std::optional<ModuleRootSignature>
ModuleRootSignature::analyzeModule(Module &M, const Function *F) {

LLVMContext *Ctx = &M.getContext();

ModuleRootSignature MRS;
const Function *EntryFunction = MMI.EntryPropertyVec[0].Entry;

NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
if (RootSignatureNode == nullptr ||
parse(Ctx, &MRS, RootSignatureNode, EntryFunction) || validate(Ctx, &MRS))
if (RootSignatureNode == nullptr || parse(Ctx, &MRS, RootSignatureNode, F) ||
validate(Ctx, &MRS))
return std::nullopt;

return MRS;
Expand All @@ -168,15 +173,18 @@ AnalysisKey RootSignatureAnalysis::Key;
std::optional<ModuleRootSignature>
RootSignatureAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
ModuleMetadataInfo MMI = AM.getResult<DXILMetadataAnalysis>(M);
return ModuleRootSignature::analyzeModule(M, MMI);
if (MMI.ShaderProfile == Triple::Library)
return std::nullopt;
return ModuleRootSignature::analyzeModule(M, getEntryFunction(M, MMI));
}

//===----------------------------------------------------------------------===//
bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {

dxil::ModuleMetadataInfo &MMI =
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
MRS = ModuleRootSignature::analyzeModule(M, MMI);
if (MMI.ShaderProfile == Triple::Library)
return false;
MRS = ModuleRootSignature::analyzeModule(M, getEntryFunction(M, MMI));
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/DirectX/DXILRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
namespace llvm {
namespace dxil {

enum class RootSignatureElementKind { None = 0, RootFlags = 1 };
enum class RootSignatureElementKind { RootFlags = 1 };

struct ModuleRootSignature {
uint32_t Flags = 0;
static std::optional<ModuleRootSignature>
analyzeModule(Module &M, ModuleMetadataInfo MMI);
static std::optional<ModuleRootSignature> analyzeModule(Module &M,
const Function *F);
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't really need to be public API like this - better to just have a static analyzeModule function in DXILRootSignature.cpp rather than a static member of the ModuleRootSignature struct.

};

class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: not --crash llc %s --filetype=obj -o -
; expected-error@-1: More than one entry function defined

target triple = "dxil-unknown-shadermodel6.0-compute"


define void @main() #0 {
entry:
ret void
}

define void @anotherMain() #1 {
entry:
ret void
}

attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
attributes #1 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }

!dx.rootsignatures = !{!2, !5} ; list of function/root signature pairs
!2 = !{ ptr @main, !3 } ; function, root signature
!3 = !{ !4 } ; list of root signature elements
!4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout
!5 = !{ ptr @anotherMain, !6 } ; function, root signature
!6 = !{ !7 } ; list of root signature elements
!7 = !{ !"RootFlags", i32 2 } ; 1 = allow_input_assembler_input_layout
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this test trying to cover? There's only one entry function here since @main does not have "hlsl.shader" metadata, but if you change it to @main #0 then we'll obviously get the error like in the -Error version of this test

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s

target triple = "dxil-unknown-shadermodel6.0-compute"


define void @main() {
entry:
ret void
}

define void @anotherMain() #0 {
entry:
ret void
}

attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }

!dx.rootsignatures = !{!2, !5} ; list of function/root signature pairs
!2 = !{ ptr @main, !3 } ; function, root signature
!3 = !{ !4 } ; list of root signature elements
!4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout
!5 = !{ ptr @anotherMain, !6 } ; function, root signature
!6 = !{ !7 } ; list of root signature elements
!7 = !{ !"RootFlags", i32 2 } ; 1 = allow_input_assembler_input_layout


; CHECK: - Name: RTS0
; CHECK-NEXT: Size: 24
; CHECK-NEXT: RootSignature:
; CHECK-NEXT: Version: 2
; CHECK-NEXT: NumParameters: 0
; CHECK-NEXT: RootParametersOffset: 0
; CHECK-NEXT: NumStaticSamplers: 0
; CHECK-NEXT: StaticSamplersOffset: 0
; CHECK-NEXT: DenyVertexShaderRootAccess: true
Loading