Skip to content

[HLSL] Adding support for Root Constants in LLVM Metadata #135085

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 143 commits into from
Apr 24, 2025

Conversation

joaosaffran
Copy link
Contributor

@joaosaffran joaosaffran changed the base branch from users/joaosaffran/127840 to main April 17, 2025 17:03
@joaosaffran joaosaffran marked this pull request as draft April 17, 2025 18:10
@joaosaffran joaosaffran force-pushed the metadata/root-constants branch from 02c2d6f to 7aed7d1 Compare April 17, 2025 18:36
@joaosaffran joaosaffran marked this pull request as ready for review April 17, 2025 19:30
@joaosaffran joaosaffran requested a review from inbelic April 17, 2025 19:30
@@ -25,7 +25,7 @@ struct RootSignatureDesc {

uint32_t Version = 2U;
uint32_t Flags = 0U;
uint32_t RootParameterOffset = 0U;
uint32_t RootParameterOffset = 24U;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a comment as to why it should be 24

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clang emits the root signature data in dxcontainer following a specific sequence, first the header, then the root parameters, the header is always 24 bytes long, so this is why we have 24 here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good thanks. Can we add this to the code as well so it doesn't get lost

Copy link
Contributor

Choose a reason for hiding this comment

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

Given that the root parameter offset doesn't actually have to be 24 it feels awkward to set it like it's a constant here. I think it's more logical to leave this as zero and fill it in when we create these from the metadata, much like we'll have to do for StaticSamplersOffset

@joaosaffran joaosaffran requested a review from bogner April 23, 2025 17:33
@@ -25,7 +25,7 @@ struct RootSignatureDesc {

uint32_t Version = 2U;
uint32_t Flags = 0U;
uint32_t RootParameterOffset = 0U;
uint32_t RootParameterOffset = 24U;
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that the root parameter offset doesn't actually have to be 24 it feels awkward to set it like it's a constant here. I think it's more logical to leave this as zero and fill it in when we create these from the metadata, much like we'll have to do for StaticSamplersOffset

Comment on lines 43 to 44
static bool reportValueError(LLVMContext *Ctx, Twine ParamName, uint32_t Value,
DiagnosticSeverity Severity = DS_Error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that the function is called reportValueError it seems weird for there to be a Severity parameter. We should either drop that parameter or rename this.

Comment on lines 50 to 58
static bool extractMdIntValue(uint32_t &Value, MDNode *Node,
unsigned int OpId) {
auto *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(OpId).get());
if (CI == nullptr)
return true;

Value = CI->getZExtValue();
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It does end up being slightly more verbose, but I think it's slightly clearer to use std::optional here rather than a boolean return value and an out parameter, like

static std::optional<uint32_t> extractMdIntValue(MDNode *Node,
                                                 unsigned int OpId) {
  if (auto *CI =
          mdconst::dyn_extract<ConstantInt>(Node->getOperand(OpId).get()))
    return CI->getZExtValue();
  return std::nullopt;
}

then:

  if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 1))
    NewParameter.Header.ShaderVisibility = *Val;
  else
    return reportError(Ctx, "Invalid value for ShaderVisibility");

Comment on lines 151 to 166
static bool verifyShaderVisibility(uint32_t Flags) {
switch (Flags) {

case llvm::to_underlying(dxbc::ShaderVisibility::All):
case llvm::to_underlying(dxbc::ShaderVisibility::Vertex):
case llvm::to_underlying(dxbc::ShaderVisibility::Hull):
case llvm::to_underlying(dxbc::ShaderVisibility::Domain):
case llvm::to_underlying(dxbc::ShaderVisibility::Geometry):
case llvm::to_underlying(dxbc::ShaderVisibility::Pixel):
case llvm::to_underlying(dxbc::ShaderVisibility::Amplification):
case llvm::to_underlying(dxbc::ShaderVisibility::Mesh):
return true;
}

return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this just dxbc::isValidShaderVisibility()?

Comment on lines 168 to 175
static bool verifyParameterType(uint32_t Type) {
switch (Type) {
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
return true;
}

return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

for (const auto &P : RSD.Parameters) {
if (!verifyShaderVisibility(P.Header.ShaderVisibility))
return reportValueError(Ctx, "ShaderVisibility",
(uint32_t)P.Header.ShaderVisibility);
Copy link
Contributor

Choose a reason for hiding this comment

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

P.Header.ShaderVisibility is already uint32_t. What's this cast for?

Comment on lines 318 to 321
OS << indent(Space)
<< "Parameter Type: " << (uint32_t)P.Header.ParameterType << "\n";
OS << indent(Space)
<< "Shader Visibility: " << (uint32_t)P.Header.ShaderVisibility
Copy link
Contributor

Choose a reason for hiding this comment

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

A couple more superfluous casts.

OS << indent(Space)
<< "StaticSamplersOffset: " << RSHSize + RS.Parameters.size_in_bytes()
<< ":\n";
<< "\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be some kind of header to indicate that we're printing the root parameters here, or at least an empty line? The way this reads currently it looks almost like these are associated with the static samplers since that's what printed right before.

We could also consider printing these jinline near the parts about root parameters. Something like

Definition for 'main':
  Flags: 0x000001
  Version: 2
  RootParametersOffset: 24
  NumParameters: 2
    - Parameter Type: 1
      Shader Visibility: 0
      Register Space: 2
      Shader Register: 1
      Num 32 Bit Values: 3
    - Parameter Type: ...
      ...
  NumStaticSamplers: 0
  StaticSamplersOffset: 48

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you took pieces of both of my suggestions. I think we should either:

  • Put the list of parameters inline after the num parameters / offset fields
  • Print out a header that says "Root Parameters:" before the list of parameters

As it is now, we print the parameters immediately after the "static samplers offset", and it isn't clear that it's a list of parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry, I missed the first point indeed, will fix it.

OS << indent(Space) << "Flags: " << format_hex(RS.Flags, 8) << "\n";
OS << indent(Space) << "Version: " << RS.Version << "\n";
OS << indent(Space) << "NumParameters: " << RS.Parameters.size() << "\n";
OS << indent(Space) << "RootParametersOffset: " << RSHSize << "\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically a pre-existing bug, but this needs to print RS.RootParametersOffset, not RSHSize

; DXC-NEXT: RootSignature:
; DXC-NEXT: Version: 2
; DXC-NEXT: NumStaticSamplers: 0
; DXC-NEXT: StaticSamplersOffset: 0
Copy link
Contributor

Choose a reason for hiding this comment

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

RootSignatureAnalysis also doesn't have support for static samplers yet, and the 48 comes from the fact that it prints RSHSize + RS.Parameters.size_in_bytes() rather than StaticSamplersOffset, which is arguably a bug. That will all need to be fixed as part of the implementation for static samplers though.

@joaosaffran joaosaffran requested review from bogner and inbelic April 24, 2025 19:33
// Clang emits the root signature data in dxcontainer following a specific
// sequence. First the header, then the root parameters. The header is
// always 24 bytes long, this is why we have 24 here.
RSD.RootParameterOffset = 24U;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is sizeof(dxbc::RootSignatureHeader) clearer?

OS << indent(Space)
<< "StaticSamplersOffset: " << RSHSize + RS.Parameters.size_in_bytes()
<< ":\n";
<< "\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you took pieces of both of my suggestions. I think we should either:

  • Put the list of parameters inline after the num parameters / offset fields
  • Print out a header that says "Root Parameters:" before the list of parameters

As it is now, we print the parameters immediately after the "static samplers offset", and it isn't clear that it's a list of parameters.

}

for (const auto &P : RSD.Parameters) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (const auto &P : RSD.Parameters) {
for (const RootParameter &P : RSD.Parameters) {

@joaosaffran joaosaffran requested a review from bogner April 24, 2025 20:05
@joaosaffran joaosaffran merged commit b649b35 into llvm:main Apr 24, 2025
10 of 11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 24, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/16602

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
UNSUPPORTED: lldb-api :: tools/lldb-server/TestGdbRemoteForkNonStop.py (1207 of 2127)
UNSUPPORTED: lldb-api :: tools/lldb-server/TestGdbRemoteForkResume.py (1208 of 2127)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteExitCode.py (1209 of 2127)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteHostInfo.py (1210 of 2127)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteModuleInfo.py (1211 of 2127)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteAuxvSupport.py (1212 of 2127)
PASS: lldb-api :: terminal/TestEditlineCompletions.py (1213 of 2127)
UNSUPPORTED: lldb-api :: tools/lldb-server/TestGdbRemoteSaveCore.py (1214 of 2127)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteKill.py (1215 of 2127)
UNRESOLVED: lldb-api :: tools/lldb-dap/variables/TestDAP_variables.py (1216 of 2127)
******************** TEST 'lldb-api :: tools/lldb-dap/variables/TestDAP_variables.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/tools/lldb-dap/variables -p TestDAP_variables.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision b649b3557e7b0a95612be64c1fe2334f6ecb2132)
  clang revision b649b3557e7b0a95612be64c1fe2334f6ecb2132
  llvm revision b649b3557e7b0a95612be64c1fe2334f6ecb2132
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_darwin_dwarf_missing_obj (TestDAP_variables.TestDAP_variables) (requires one of macosx, darwin, ios, tvos, watchos, bridgeos, iphonesimulator, watchsimulator, appletvsimulator) 
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_darwin_dwarf_missing_obj_with_symbol_ondemand_enabled (TestDAP_variables.TestDAP_variables) (requires one of macosx, darwin, ios, tvos, watchos, bridgeos, iphonesimulator, watchsimulator, appletvsimulator) 
========= DEBUG ADAPTER PROTOCOL LOGS =========
1745529004.260873318 --> (stdin/stdout) {"command":"initialize","type":"request","arguments":{"adapterID":"lldb-native","clientID":"vscode","columnsStartAt1":true,"linesStartAt1":true,"locale":"en-us","pathFormat":"path","supportsRunInTerminalRequest":true,"supportsVariablePaging":true,"supportsVariableType":true,"supportsStartDebuggingRequest":true,"supportsProgressReporting":true,"$__lldb_sourceInitFile":false},"seq":1}
1745529004.262982845 <-- (stdin/stdout) {"body":{"$__lldb_version":"lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision b649b3557e7b0a95612be64c1fe2334f6ecb2132)\n  clang revision b649b3557e7b0a95612be64c1fe2334f6ecb2132\n  llvm revision b649b3557e7b0a95612be64c1fe2334f6ecb2132","completionTriggerCharacters":["."," ","\t"],"exceptionBreakpointFilters":[{"default":false,"filter":"cpp_catch","label":"C++ Catch"},{"default":false,"filter":"cpp_throw","label":"C++ Throw"},{"default":false,"filter":"objc_catch","label":"Objective-C Catch"},{"default":false,"filter":"objc_throw","label":"Objective-C Throw"}],"supportTerminateDebuggee":true,"supportsBreakpointLocationsRequest":true,"supportsCancelRequest":true,"supportsCompletionsRequest":true,"supportsConditionalBreakpoints":true,"supportsConfigurationDoneRequest":true,"supportsDataBreakpoints":true,"supportsDelayedStackTraceLoading":true,"supportsDisassembleRequest":true,"supportsEvaluateForHovers":true,"supportsExceptionInfoRequest":true,"supportsExceptionOptions":true,"supportsFunctionBreakpoints":true,"supportsHitConditionalBreakpoints":true,"supportsInstructionBreakpoints":true,"supportsLogPoints":true,"supportsModulesRequest":true,"supportsReadMemoryRequest":true,"supportsRestartRequest":true,"supportsSetVariable":true,"supportsStepInTargetsRequest":true,"supportsSteppingGranularity":true,"supportsValueFormattingOptions":true},"command":"initialize","request_seq":1,"seq":0,"success":true,"type":"response"}
1745529004.263228655 --> (stdin/stdout) {"command":"launch","type":"request","arguments":{"program":"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/tools/lldb-dap/variables/TestDAP_variables.test_indexedVariables/a.out","initCommands":["settings clear -all","settings set symbols.enable-external-lookup false","settings set target.inherit-tcc true","settings set target.disable-aslr false","settings set target.detach-on-error false","settings set target.auto-apply-fixits false","settings set plugin.process.gdb-remote.packet-timeout 60","settings set symbols.clang-modules-cache-path \"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api\"","settings set use-color false","settings set show-statusline false"],"disableASLR":false,"enableAutoVariableSummaries":false,"enableSyntheticChildDebugging":false,"displayExtendedBacktrace":false,"commandEscapePrefix":null},"seq":2}
1745529004.263423443 <-- (stdin/stdout) {"body":{"category":"console","output":"Running initCommands:\n"},"event":"output","seq":0,"type":"event"}
1745529004.263444901 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings clear -all\n"},"event":"output","seq":0,"type":"event"}
1745529004.263456106 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set symbols.enable-external-lookup false\n"},"event":"output","seq":0,"type":"event"}
1745529004.263465405 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.inherit-tcc true\n"},"event":"output","seq":0,"type":"event"}
1745529004.263474226 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.disable-aslr false\n"},"event":"output","seq":0,"type":"event"}
1745529004.263482094 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.detach-on-error false\n"},"event":"output","seq":0,"type":"event"}
1745529004.263489962 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.auto-apply-fixits false\n"},"event":"output","seq":0,"type":"event"}
1745529004.263497829 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set plugin.process.gdb-remote.packet-timeout 60\n"},"event":"output","seq":0,"type":"event"}
1745529004.263516426 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set symbols.clang-modules-cache-path \"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api\"\n"},"event":"output","seq":0,"type":"event"}
1745529004.263524532 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set use-color false\n"},"event":"output","seq":0,"type":"event"}
1745529004.263533115 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set show-statusline false\n"},"event":"output","seq":0,"type":"event"}
1745529004.341282368 <-- (stdin/stdout) {"command":"launch","request_seq":2,"seq":0,"success":true,"type":"response"}
1745529004.341329575 <-- (stdin/stdout) {"body":{"isLocalProcess":true,"name":"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/tools/lldb-dap/variables/TestDAP_variables.test_indexedVariables/a.out","startMethod":"launch","systemProcessId":3774840},"event":"process","seq":0,"type":"event"}
1745529004.341339588 <-- (stdin/stdout) {"event":"initialized","seq":0,"type":"event"}
1745529004.341617823 --> (stdin/stdout) {"command":"setBreakpoints","type":"request","arguments":{"source":{"name":"main.cpp","path":"main.cpp"},"sourceModified":false,"lines":[40],"breakpoints":[{"line":40}]},"seq":3}
1745529004.343051195 <-- (stdin/stdout) {"body":{"breakpoints":[{"column":1,"id":1,"instructionReference":"0xAAAAB7470C54","line":41,"source":{"name":"main.cpp","path":"main.cpp"},"verified":true}]},"command":"setBreakpoints","request_seq":3,"seq":0,"success":true,"type":"response"}

joaosaffran added a commit to joaosaffran/llvm-project that referenced this pull request Apr 24, 2025
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Apr 24, 2025
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Apr 24, 2025
@joaosaffran joaosaffran deleted the metadata/root-constants branch April 29, 2025 18:51
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
@damyanp damyanp removed this from HLSL Support Jun 25, 2025
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.

[HLSL] Add support to Root Signature Constant element to dxcontainer generation
5 participants