<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/154407>154407</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[HLSL] Assert & crash in OpLowerer when resolving `llvm.dx.resource.casthandle` on a phi node
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hekota
</td>
</tr>
</table>
<pre>
```
RWStructuredBuffer<int> Out[4] : register(u0);
[numthreads(4,1,1)]
void main(uint GI : SV_GroupIndex) {
for (int i = 0; i < GI; i++)
Out[GI].IncrementCounter();
Out[GI][0] = Out[GI].IncrementCounter();
}
```
https://godbolt.org/z/Ezjdfadj1
`OpLowerer::cleanupHandleCasts` is expecting the operand of `@llvm.dx.resource.casthandle` to be a `CallInst` and it is a phi node:
```
%.pre-phi = phi target("dx.RawBuffer", i32, 1, 0) [ %1, %entry._Z4mainj.exit_crit_edge ], [ %3, %for.body.i ]
%6 = call %dx.types.Handle @llvm.dx.resource.casthandle.s_dx.types.Handles.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) %.pre-phi)
```
```
#0 0x0000000003cff348 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3cff348)
#1 0x0000000003cfcd1c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3cfcd1c)
#2 0x0000000003c4cfd8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007a5b5fe42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00000000036cca1c llvm::Value::doRAUW(llvm::Value*, llvm::Value::ReplaceMetadataUses) (/opt/compiler-explorer/clang-trunk/bin/clang+0x36cca1c)
#5 0x0000000002b3e445 (anonymous namespace)::OpLowerer::lowerIntrinsics() (.constprop.0) DXILOpLowering.cpp:0:0
#6 0x0000000002b40de5 (anonymous namespace)::DXILOpLoweringLegacy::runOnModule(llvm::Module&) DXILOpLowering.cpp:0:0
#7 0x0000000003644709 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3644709)
#8 0x0000000003fa339d clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3fa339d)
#9 0x000000000464753c clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x464753c)
#10 0x00000000062ce46c clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+0x62ce46c)
#11 0x0000000004647d2d clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+0x4647d2d)
#12 0x0000000004953a3a clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+0x4953a3a)
#13 0x00000000048ccd7b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x48ccd7b)
#14 0x0000000004a44df3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+0x4a44df3)
#15 0x0000000000dc0775 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+0xdc0775)
#16 0x0000000000db871d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#17 0x00000000046ba419 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#18 0x0000000003c4d3f3 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3c4d3f3)
#19 0x00000000046ba639 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#20 0x000000000467d5bd clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+0x467d5bd)
#21 0x000000000467e571 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+0x467e571)
#22 0x000000000468860c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x468860c)
#23 0x0000000000dbcf81 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+0xdbcf81)
#24 0x0000000000c6bb44 main (/opt/compiler-explorer/clang-trunk/bin/clang+0xc6bb44)
#25 0x00007a5b5fe29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#26 0x00007a5b5fe29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#27 0x0000000000db81b5 _start (/opt/compiler-explorer/clang-trunk/bin/clang+0xdb81b5)
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkWV9z26zS_zTkholGQn9sX-TCceI-6aSTTtz2eee90SBY27QYdAAlzvPpz4DkSHKdtJ3knJvTcSoZ2OW3v112AVNrxUYBXKD8EuVXZ7RxW20utvBDO3pWaf50gYq4-8Tz-79XzjTMNQb4ZbN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