[HLSL] Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!")
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Consider this hlsl:
```hlsl
RWBuffer In : register(u0);
RWStructuredBuffer Out0 : register(u1);
RWStructuredBuffer Out1 : register(u2);
cbuffer c {
bool cond;
};
void branched_assignment(uint idx) {
RWStructuredBuffer Out = Out0;
if (cond) {
Out = Out1;
}
Out[idx] = In[idx];
}
[numthreads(64, 1, 1)]
void main(uint3 tid : SV_DispatchThreadID) {
branched_assignment(tid.x);
}
```
When compiled with -T cs_6_0, we get an assertion and a crash, shown below:
```
# .---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_different_global.test.tmp/source.hlsl:12:15: warning: assignment of 'Out1' to local resource 'Out' is not to the same unique global resource [-Whlsl-explicit-binding]
# | 12 | Out = Out1;
# | | ^
# | /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_different_global.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: (isa(Val) && "cast() argument of incompatible type!"), function cast, file Casting.h, line 572.
# | 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-da0fd4.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_different_global.test.tmp/source.hlsl
# | 1. parser at end of file
# | 2. Code generation
# | 3. Running pass 'DXIL Op Lowering' 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_different_global.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 0x000000010197961c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
# | 1 clang-23 0x0000000101977360 llvm::sys::RunSignalHandlers() + 204
# | 2 clang-23 0x000000010197a120 SignalHandler(int, __siginfo*, void*) + 300
# | 3 clang-23 0x0000000101979fb8 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 0x00000001043ac2dc addOpsFromMDNode(llvm::MDNode*, llvm::MCInst&, llvm::SPIRV::ModuleAnalysisInfo*) (.cold.4) + 0
# | 9 clang-23 0x0000000100501c80 (anonymous namespace)::OpLowerer::lowerIntrinsics() + 4416
# | 10 clang-23 0x0000000100507844 (anonymous namespace)::DXILOpLoweringLegacy::runOnModule(llvm::Module&) + 388
# | 11 clang-23 0x00000001010d41e0 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1016
# | 12 clang-23 0x0000000101e1b7b0 clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr, std::__1::unique_ptr>, clang::BackendConsumer*) + 3260
# | 13 clang-23 0x00000001020982a0 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 1308
# | 14 clang-23 0x0000000102f7e0f0 clang::ParseAST(clang::Sema&, bool, bool) + 544
# | 15 clang-23 0x0000000102466b14 clang::HLSLFrontendAction::ExecuteAction() + 392
# | 16 clang-23 0x000000010244cd64 clang::FrontendAction::Execute() + 48
# | 17 clang-23 0x00000001023e52c4 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 1104
# | 18 clang-23 0x00000001024ce51c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 468
# | 19 clang-23 0x00000001003c983c cc1_main(llvm::ArrayRef, char const*, void*) + 1420
# | 20 clang-23 0x00000001003c7b14 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) + 1000
# | 21 clang-23 0x00000001003c7424 clang_main(int, char**, llvm::ToolContext const&) + 5868
# | 22 clang-23 0x00000001003d2e24 main + 92
# | 23 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-bdd902.hlsl
# | clang-dxc: note: diagnostic msg: /var/folders/9v/g7k79jss47nfbjfnb9fvj_2w0000gn/T/lit-tmp-smeanvz6/source-bdd902.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:
# |
# | ********************
# `-----------------------------
# error: command failed with exit status: -6
```
Note specifically `# | Assertion failed: (isa(Val) && "cast() argument of incompatible type!"), function cast, file Casting.h, line 572.`
We should not be encountering this assertion, and instead the error diagnostic that was emitted should be the last thing the compiler emits before exiting gracefully.
The compiler should recognize that the branch is not statically knowable, and emit a diagnostic error without crashing.
This occurs for other shaders too, not just this one.
The general issue is that the compiler crashes when it recognizes resource assignment isn't guaranteed to be unique, and it shouldn't.
Contributor guide
Research direction
Reproduce the failure with the supplied HLSL using the cs_6_0 path, then inspect the DXIL Op Lowering pass named in the stack dump and its handling of the resource-uniqueness diagnostic. Done means the compiler reports that diagnostic and exits gracefully instead of reaching the incompatible-type assertion or crashing.
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
- 48/100