<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/134749>134749</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [SPIRV][HLSL] Crash in SPIRVInstructionSelector::loadHandleBeforePosition
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            HLSL,
            backend:SPIR-V
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            farzonl
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          farzonl
      </td>
    </tr>
</table>

<pre>
    
## HLSL source
https://hlsl.godbolt.org/z/a3KK9cxKa
```HLSL
// FLAGS: -T cs_6_0  -E main -spirv -fspv-target-env=vulkan1.3

[numthreads(1,1,1)]
void main(uint GI : SV_GroupIndex) {
  Out[GI] = In[GI] * In[GI];
}
```

## LLVM IR
https://hlsl.godbolt.org/z/3cx68n68W
```llvm
define void @main() local_unnamed_addr #0 {
  %1 = tail call i32 @llvm.spv.flattened.thread.id.in.group()
  %2 = tail call noundef nonnull align 16 dereferenceable(16) ptr @llvm.spv.resource.getpointer.p0.tspirv.Image_f32_5_2_0_0_2_0t(target("spirv.Image", float, 5, 2, 0, 0, 2, 0) poison, i32 %1), !dbg !58
  %3 = load <4 x float>, ptr %2, align 16, !dbg !60
  %4 = fmul reassoc nnan ninf nsz arcp afn <4 x float> %3, %3, !dbg !66
  %5 = tail call noundef nonnull align 16 dereferenceable(16) ptr @llvm.spv.resource.getpointer.p0.tspirv.Image_f32_5_2_0_0_2_0t(target("spirv.Image", float, 5, 2, 0, 0, 2, 0) poison, i32 %1), !dbg !67
  store <4 x float> %4, ptr %5, align 16, !dbg !69
  ret void
}
```

## Crash Dump
https://hlsl.godbolt.org/z/T1s7fK83q
```BASH
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang --driver-mode=dxc -Zi -Qembed_debug -Fc /app/output.s -fcolor-diagnostics -fno-crash-diagnostics -T cs_6_0 -E main -spirv -fspv-target-env=vulkan1.3 <source>
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module '<source>'.
4.      Running pass 'InstructionSelect' on function '@main'
 #0 0x0000000003b9cae8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3b9cae8)
 #1 0x0000000003b9ac2c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3b9ac2c)
 #2 0x0000000003aeb558 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007c72b7a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00000000022e820f llvm::foldImm(llvm::MachineOperand const&, llvm::MachineRegisterInfo const*) (/opt/compiler-explorer/clang-trunk/bin/clang+0x22e820f)
 #5 0x000000000232d23b (anonymous namespace)::SPIRVInstructionSelector::loadHandleBeforePosition(llvm::Register&, llvm::MachineInstr const*, llvm::GIntrinsic&, llvm::MachineInstr&) const (.isra.0) SPIRVInstructionSelector.cpp:0:0
 #6 0x000000000232f007 (anonymous namespace)::SPIRVInstructionSelector::selectStore(llvm::MachineInstr&) const SPIRVInstructionSelector.cpp:0:0
 #7 0x000000000233e363 (anonymous namespace)::SPIRVInstructionSelector::selectIntrinsic(llvm::Register, llvm::MachineInstr const*, llvm::MachineInstr&) const SPIRVInstructionSelector.cpp:0:0
 #8 0x0000000002340f69 (anonymous namespace)::SPIRVInstructionSelector::spvSelect(llvm::Register, llvm::MachineInstr const*, llvm::MachineInstr&) const SPIRVInstructionSelector.cpp:0:0
 #9 0x00000000023444e9 (anonymous namespace)::SPIRVInstructionSelector::select(llvm::MachineInstr&) SPIRVInstructionSelector.cpp:0:0
#10 0x0000000004c051eb llvm::InstructionSelect::selectMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x4c051eb)
#11 0x0000000004c06875 llvm::InstructionSelect::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x4c06875)
#12 0x0000000002f98778 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#13 0x000000000350a432 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x350a432)
#14 0x000000000350a6c1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x350a6c1)
#15 0x000000000350c029 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x350c029)
#16 0x0000000003e464ad 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+0x3e464ad)
#17 0x00000000044d26c5 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x44d26c5)
#18 0x00000000060e8dec clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+0x60e8dec)
#19 0x00000000044d2d9d clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+0x44d2d9d)
#20 0x00000000047ddc21 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+0x47ddc21)
#21 0x000000000475c38b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x475c38b)
#22 0x00000000048cdfd3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+0x48cdfd3)
#23 0x0000000000d75af1 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+0xd75af1)
#24 0x0000000000d6e2ed ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#25 0x0000000004544a49 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
#26 0x0000000003aeb973 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+0x3aeb973)
#27 0x0000000004544c69 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
#28 0x000000000450834d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+0x450834d)
#29 0x0000000004509311 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+0x4509311)
#30 0x000000000451365c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+0x451365c)
#31 0x0000000000d728c1 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+0xd728c1)
#32 0x0000000000c325d4 main (/opt/compiler-explorer/clang-trunk/bin/clang+0xc325d4)
#33 0x00007c72b7a29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#34 0x00007c72b7a29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#35 0x0000000000d6dd85 _start (/opt/compiler-explorer/clang-trunk/bin/clang+0xd6dd85)
clang: error: clang frontend command failed with exit code 139 (use -v to see invocation)
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkWt1y2ziyfhr4BiUVBf5f-IKWo4wnScUnysmpOjcqEABlbECAA4AaeZ5-CyBl_kjOOGvP7lZNKrZMEOj--utGdxMUNobvJWPXIL4BCFVY_6GkAAiB-PYKt_ZB6et-8KpU9PEaBAVAIUAh_OXj9iM0qtWEgaB4sLYxICwA2gC0eRBGLPeKlkrYpdJ7gDZ_ALTB4YcPOTl-wE5KEnT_nRwv1S2Em4_F-y0IC7j4ConZJbsAwsU7WGMu4cI0XB_gojLNYWGx3jO7YPIAwttDK75juVqGTlBQgPhGtrV90AxTA1C2Amjd_-QgvgVBcVCceqEAZS2XFr6_g07r9tvuvVZtcycpOwKUQ5DegKCA8HNrQXzz_g7EtxCEt_BODpeoGF2C0C0A6e3Yxh5WR9zHj98-wbsvLyUtJMckk0n2f2OBQhxqEBSUVVwy6K0BUdAb5HALRbDYtVLimtEdplRDgMLgyR6A4pU3xGIuIMFCQB4iJ8SJXprmsKwEtpZJRpcdk0tOl1wu946fTstJEppJkqqVlFVQKilbISAWfC_hKoGUaVYxzSRhuBTMuSZxaBurJ6o16wJruWe2UVxappdNsLQ-AJZ3Nd6zXRWiXbxDu2AXuN8WoKyLCY8Njaa6cEZrWAmFrfsjdr_8UPD063SZw0Zxo6S79ISg2EcNWkOAVrTcu484O1keesuFwhSCcB3BY68lfOdWeLNQ7GWfOJhK8qHhJUVeUlW3AmqGjVEESokllFxWUJo_INakgbiSc0UeRif19PkkPTlJj_9mHkpSb7mxSrNLhEUj98Q_cE8X4ppZv8f-ZGOvNTYP8Latm5fu7a8rk1YfsvC3scybYvsLCIqtxeQ7pE5a6JQESxDk91rtNa4h1vu2ZtI6DRCgjWosQBui6oYLphfs2AilmXZjAsv9wupWfgdoU7oE0Y3BxYJqfmB6USvKQHhLjwQu_p_Dxf-wumR0R1nZ7uFiQ5wC3DROTWub1i4NXFRECaUXlOO9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