llvm / llvm/llvm-project

[HLSL] Assertion failed: ((Size == -1 || Size > 0) && "invalid size") when assigning a resource from a resource array

Open
#205,168 0 comments 0 reactions 0 assignees View on GitHub
crash HLSL
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Given this hlsl:

```hlsl
RWBuffer In : register(u0);
RWStructuredBuffer Out0 : register(u1);
RWStructuredBuffer OutArr[];

cbuffer c {
bool cond;
};

void branched_assignment_with_array(uint idx) {
RWStructuredBuffer Out = Out0;
if (cond) {
Out = OutArr[0];
}
Out[idx] = In[idx];
}

[numthreads(64, 1, 1)]
void main(uint3 tid : SV_DispatchThreadID) {
branched_assignment_with_array(tid.x);
}
```

We get this stack trace when compiling with -T cs_6_0:

```
# .---command stderr------------
# | /Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/tools/OffloadTest/test/clang-mtl/Feature/LocalResources/Output/local_resource_conditional_reassign_from_array.test.tmp/source.hlsl:12:15: warning: assignment of 'OutArr[0]' to local resource 'Out' is not to the same unique global resource [-Whlsl-explicit-binding]
# | 12 | Out = OutArr[0];
# | | ^
# | /Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/tools/OffloadTest/test/clang-mtl/Feature/LocalResources/Output/local_resource_conditional_reassign_from_array.test.tmp/source.hlsl:10:30: note: variable 'Out' is declared here
# | 10 | RWStructuredBuffer Out = Out0;
# | | ^
# | error: Resource access is not guaranteed to map to a unique global resource
# | Assertion failed: ((Size == -1 || Size > 0) && "invalid size"), function findAvailableBinding, file HLSLBinding.cpp, line 37.
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
# | Stack dump:
# | 0. Program arguments: /Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/bin/clang-23 -cc1 -triple dxilv1.0-unknown-shadermodel6.0-compute -O3 -Wconversion -Wvector-conversion -Wmatrix-conversion -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -main-file-name source.hlsl -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -debugger-tuning=gdb -fdebug-compilation-dir=/Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/tools/OffloadTest/test/clang-mtl/Feature/LocalResources -target-linker-version 1230.1 -fcoverage-compilation-dir=/Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/tools/OffloadTest/test/clang-mtl/Feature/LocalResources -resource-dir /Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/lib/clang/23 -ferror-limit 19 -O3 -finclude-default-header -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -o /var/folders/9v/g7k79jss47nfbjfnb9fvj_2w0000gn/T/lit-tmp-smeanvz6/source-94fa2d.obj -x hlsl /Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/tools/OffloadTest/test/clang-mtl/Feature/LocalResources/Output/local_resource_conditional_reassign_from_array.test.tmp/source.hlsl
# | 1. parser at end of file
# | 2. Code generation
# | 3. Running pass 'DXIL Resource Implicit Binding' on module '/Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/tools/OffloadTest/test/clang-mtl/Feature/LocalResources/Output/local_resource_conditional_reassign_from_array.test.tmp/source.hlsl'.
# | Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
# | 0 clang-23 0x0000000101be161c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
# | 1 clang-23 0x0000000101bdf360 llvm::sys::RunSignalHandlers() + 204
# | 2 clang-23 0x0000000101be2120 SignalHandler(int, __siginfo*, void*) + 300
# | 3 clang-23 0x0000000101be1fb8 SignalHandlerTerminate(int, __siginfo*, void*) + 20
# | 4 libsystem_platform.dylib 0x00000001976d3744 _sigtramp + 56
# | 5 libsystem_pthread.dylib 0x00000001976c9888 pthread_kill + 296
# | 6 libsystem_c.dylib 0x00000001975ce850 abort + 124
# | 7 libsystem_c.dylib 0x00000001975cda84 err + 0
# | 8 clang-23 0x0000000104747318 llvm::hlsl::BindingInfoBuilder::calculateBindingInfo(llvm::function_ref) (.cold.7) + 0
# | 9 clang-23 0x000000010432b42c llvm::hlsl::BindingInfo::RegisterSpace::findAvailableBinding(int) + 208
# | 10 clang-23 0x000000010432b200 llvm::hlsl::BindingInfo::findAvailableBinding(llvm::dxil::ResourceClass, unsigned int, int) + 88
# | 11 clang-23 0x000000010077be04 (anonymous namespace)::assignBindings(llvm::Module&, llvm::DXILResourceBindingInfo&, llvm::DXILResourceTypeMap&) + 1068
# | 12 clang-23 0x000000010133c1e0 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1016
# | 13 clang-23 0x00000001020837b0 clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr, std::__1::unique_ptr>, clang::BackendConsumer*) + 3260
# | 14 clang-23 0x00000001023002a0 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 1308
# | 15 clang-23 0x00000001031e60f0 clang::ParseAST(clang::Sema&, bool, bool) + 544
# | 16 clang-23 0x00000001026ceb14 clang::HLSLFrontendAction::ExecuteAction() + 392
# | 17 clang-23 0x00000001026b4d64 clang::FrontendAction::Execute() + 48
# | 18 clang-23 0x000000010264d2c4 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 1104
# | 19 clang-23 0x000000010273651c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 468
# | 20 clang-23 0x000000010063183c cc1_main(llvm::ArrayRef, char const*, void*) + 1420
# | 21 clang-23 0x000000010062fb14 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) + 1000
# | 22 clang-23 0x000000010062f424 clang_main(int, char**, llvm::ToolContext const&) + 5868
# | 23 clang-23 0x000000010063ae24 main + 92
# | 24 dyld 0x0000000197301d54 start + 7184
# | clang-dxc: error: unable to execute command: Abort trap: 6
# | clang-dxc: error: clang frontend command failed with exit code -2 (use -v to see invocation)
# | clang version 23.0.0git (https://github.com/llvm/llvm-project a40055caee3c372675da0190dcce1e7b14831485)
# | Target: dxilv1.0-unknown-shadermodel6.0-compute
# | Thread model: posix
# | InstalledDir: /Users/admin/actions-runner/_work/offload-test-suite/offload-test-suite/llvm-project/build/bin
# | Build config: +assertions
# | clang-dxc: note: diagnostic msg:
# | ********************
# |
# | PLEASE ATTACH THE FOLLOWING CRASH REPRODUCER FILES TO THE BUG REPORT:
# | clang-dxc: note: diagnostic msg: /var/folders/9v/g7k79jss47nfbjfnb9fvj_2w0000gn/T/lit-tmp-smeanvz6/source-dfd38e.hlsl
# | clang-dxc: note: diagnostic msg: /var/folders/9v/g7k79jss47nfbjfnb9fvj_2w0000gn/T/lit-tmp-smeanvz6/source-dfd38e.sh
# | clang-dxc: note: diagnostic msg: Crash backtrace is located in
# | clang-dxc: note: diagnostic msg: /Users/admin/Library/Logs/DiagnosticReports/clang-23__.crash
# | clang-dxc: note: diagnostic msg: (choose the .crash file that corresponds to your crash)
# | clang-dxc: note: diagnostic msg:
# |
# | ********************
# `-----------------------------
```
This assertion seems to get triggered when a resource is not guaranteed to be unique, and is assigned from a resource array.
We should expect the error to be diagnosed, but the compiler should exit gracefully, and not crash on an assertion error.

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with the HLSL example and -T cs_6_0, then inspect HLSLBinding.cpp around line 37 and the BindingInfoBuilder::calculateBindingInfo, findAvailableBinding, and assignBindings paths named in the stack trace. Done means the invalid resource assignment is diagnosed and compilation exits with an error instead of asserting.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.