llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
26.99k stars 11.06k forks source link

OpenCL (CLC++2021) compilation to SPIR-V fails #68305

Open davidrohr opened 9 months ago

davidrohr commented 9 months ago

Compilation with clang 17.0.2 (on Gentoo Linux) fails with the below error message. I was using clang 15 before, which didn't fail. I also tried clang 16 now, which failed with the same error, I didn't try other versions.

I am attaching a tarball with my .cl file, and with the 2 files in /tmp that clang asked me to attach to the bug report: bugreport.tar.gz

Command to reproduce:

clang-17 -O0 --target=spirv64 -ferror-limit=1000 -Dcl_clang_storage_class_specifiers -Wno-invalid-constexpr -Wno-unused-command-line-argument -cl-std=CLC++2021 -Xclang -fdenormal-fp-math-f32=ieee -cl-mad-enable -cl-no-signed-zeros -c foo.cl -o foo.spirv

Error message:

llvm-spirv: /usr/lib/llvm/17/include/llvm/ADT/SmallVector.h:294: T& llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::operator[](size_type) [with T = llvm::Type*; <template-parameter-1-2> = void; reference = llvm::Type*&; size_type = long unsigned int]: Assertion `idx < size()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /usr/lib/llvm/17/bin/llvm-spirv /tmp/foo-21cdb2.bc -o /home/qon/foo.spirv
 #0 0x00007f161a086fae llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm/17/lib64/libLLVM-17.so+0xc86fae)
 #1 0x00007f161a084c44 llvm::sys::RunSignalHandlers() (/usr/lib/llvm/17/lib64/libLLVM-17.so+0xc84c44)
 #2 0x00007f161a084db6 (/usr/lib/llvm/17/lib64/libLLVM-17.so+0xc84db6)
 #3 0x00007f1618e641b0 (/lib64/libc.so.6+0x391b0)
 #4 0x00007f1618eb208c (/lib64/libc.so.6+0x8708c)
 #5 0x00007f1618e64112 gsignal (/lib64/libc.so.6+0x39112)
 #6 0x00007f1618e4d4f2 abort (/lib64/libc.so.6+0x224f2)
 #7 0x00007f1618e4d415 (/lib64/libc.so.6+0x22415)
 #8 0x00007f1618e5cd32 (/lib64/libc.so.6+0x31d32)
 #9 0x00007f16213da2bf SPIRV::BuiltinCallMutator::doConversion() (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1da2bf)
#10 0x00007f16213a03ef (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1a03ef)
#11 0x00007f16213a90fb SPIRV::OCLToSPIRVBase::transBuiltin(llvm::CallInst*, OCLUtil::OCLBuiltinTransInfo&) (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1a90fb)
#12 0x00007f16213aa280 SPIRV::OCLToSPIRVBase::visitCallBuiltinSimple(llvm::CallInst*, llvm::StringRef, llvm::StringRef) (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1aa280)
#13 0x00007f16213b189c SPIRV::OCLToSPIRVBase::visitCallInst(llvm::CallInst&) (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1b189c)
#14 0x00007f16213a0841 SPIRV::OCLToSPIRVBase::runOCLToSPIRV(llvm::Module&) (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1a0841)
#15 0x00007f16213a0b6e SPIRV::OCLToSPIRVPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x1a0b6e)
#16 0x00007f16214b021d (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x2b021d)
#17 0x00007f16214f4f27 (/usr/lib/llvm/17/lib64/libLLVMSPIRVLib.so.17+0x2f4f27)
#18 0x000055b379753efd (/usr/lib/llvm/17/bin/llvm-spirv+0x13efd)
#19 0x000055b37974d0c2 (/usr/lib/llvm/17/bin/llvm-spirv+0xd0c2)
#20 0x00007f1618e4eb8a (/lib64/libc.so.6+0x23b8a)
#21 0x00007f1618e4ec45 __libc_start_main (/lib64/libc.so.6+0x23c45)
#22 0x000055b37974d471 (/usr/lib/llvm/17/bin/llvm-spirv+0xd471)
clang-17: error: unable to execute command: Aborted
clang-17: error: llvm-spirv command failed due to signal (use -v to see invocation)
clang version 17.0.2
Target: spirv64
Thread model: posix
InstalledDir: /usr/lib/llvm/17/bin
clang-17: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-17: note: diagnostic msg: /tmp/foo-f70c1d.cl
clang-17: note: diagnostic msg: /tmp/foo-f70c1d.sh
clang-17: note: diagnostic msg: 

********************
michalpaszkowski commented 9 months ago

Hi @davidrohr! The error does not appear in the SPIR-V backend, but rather the Khronos SPIR-V LLVM Translator which is installed in your system and depends on LLVM.

davidrohr commented 9 months ago

ok, clang told me to file a bug report, which I did here since it seemed natural. If you want me to file the bug report again at https://github.com/KhronosGroup/SPIRV-LLVM-Translator, I can do so and close this one.

davidrohr commented 8 months ago

I just tried again the same with -fintegrated-objemitter, which should force using the clang-internal SPIR-V backend if I understand correctly.

Also that fails with an error. The message is below, and the reproducer files are attached.

testcase.tar.gz

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm/17/bin/clang -O0 --target=spirv64 -fintegrated-objemitter -ferror-limit=1000 -Dcl_clang_storage_class_specifiers -Wno-invalid-constexpr -Wno-unused-command-line-argument -cl-std=CLC++2021 -Xclang -fdenormal-fp-math-f32=ieee -cl-mad-enable -cl-no-signed-zeros -DGPUCA_BUILD_QA -DWITH_OPENMP -DGPUCA_STANDALONE -DNDEBUG -DGPUCA_TPC_GEOMETRY_O2 -DGPUCA_HAVE_O2HEADERS -DGPUCA_NO_ITS_TRAITS -DFMT_SHARED -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../Common -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../Utils -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../TPCFastTransformation -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Debug -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Definitions -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/DataTypes -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Base -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Base/opencl-common -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/dEdx -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/TPCConvert -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Global -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/HLTHeaders -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Merger -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Refit -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/qa -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/SliceTracker -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/DataCompression -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/TRDTracking -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/TPCClusterFinder -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/ITS -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../GPUTracking/Interface -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Common/Field/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Common/Constants/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Common/MathUtils/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Common/Utils/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/common/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Detectors/Common/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Detectors/ITSMFT/common/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Detectors/ITSMFT/ITS/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Detectors/TOF/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Detectors/TPC/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Detectors/TRD/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Headers/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/MemoryResources/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Reconstruction/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/Reconstruction/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../DataFormats/simulation/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/Base/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/Base/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/common/base/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/ITS/base/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/ITS/tracking/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/ITS/tracking/cuda/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/ITS/tracking/cuda/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/ITS/tracking/hip/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/ITSMFT/ITS/tracking/hip/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/Raw/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/TOF/base/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/TPC/base/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/TRD/base/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Detectors/TRD/base/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../../../Framework/Foundation/3rdparty/include -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/../..//GPUTracking/display/3rdparty -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/Detectors/TRD/base/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/Detectors/Base/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/DataFormats/Reconstruction/src -I/home/qon/alice/O2/GPU/GPUTracking/Standalone/Detectors/ITSMFT/ITS/tracking/cuda/include -DGPUCA_GPULIBRARY=OCL2 -D__OPENCLCPP__ -o /home/qon/standalone/build/GPU/GPUTracking/Base/opencl2/GPUReconstructionOCL2Code.spirv -c /home/qon/alice/O2/GPU/GPUTracking/Standalone/..//Base/opencl-common/GPUReconstructionOCL.cl
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module '/home/qon/alice/O2/GPU/GPUTracking/Standalone/..//Base/opencl-common/GPUReconstructionOCL.cl'.
4.      Running pass 'InstructionSelect' on function '@"_ZNU3AS42o23gpu18GPUCommonAlgorithm18UnguardedPartitionIPPU3AS4NS0_18GPUTPCGMSliceTrackES5_ZNU3AS4S0_14GPUTPCGMMerger19CollectMergedTracksEiiiiE3$_0EET_SA_SA_T0_T1_"'
 #0 0x00007f959a686fae llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xc86fae)
 #1 0x00007f959a684c44 llvm::sys::RunSignalHandlers() (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xc84c44)
 #2 0x00007f959a5b74a8 (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xbb74a8)
 #3 0x00007f95994641b0 (/lib64/libc.so.6+0x391b0)
 #4 0x00007f959d009463 (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0x3609463)
 #5 0x00007f959d00ad54 (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0x360ad54)
 #6 0x00007f959b17ec7e llvm::InstructionSelect::runOnMachineFunction(llvm::MachineFunction&) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0x177ec7e)
 #7 0x00007f959aa9fd11 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0x109fd11)
 #8 0x00007f959a7db6ed llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xddb6ed)
 #9 0x00007f959a7db983 llvm::FPPassManager::runOnModule(llvm::Module&) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xddb983)
#10 0x00007f959a7dc024 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xddc024)
#11 0x00007f95a3762548 (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x1f62548)
#12 0x00007f95a3762902 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, 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>>) (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x1f62902)
#13 0x00007f95a3bfe04f (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x23fe04f)
#14 0x00007f95a24167d6 clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0xc167d6)
#15 0x00007f95a45b9cd1 clang::FrontendAction::Execute() (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2db9cd1)
#16 0x00007f95a453bfb3 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2d3bfb3)
#17 0x00007f95a464ab27 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2e4ab27)
#18 0x0000557d1e158483 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm/17/bin/clang+0x17483)
#19 0x0000557d1e153482 (/usr/lib/llvm/17/bin/clang+0x12482)
#20 0x00007f95a4160499 (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2960499)
#21 0x00007f959a5b7593 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm/17/bin/../lib64/libLLVM-17.so+0xbb7593)
#22 0x00007f95a416316d clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x296316d)
#23 0x00007f95a4124f6c clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2924f6c)
#24 0x00007f95a4125aad clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2925aad)
#25 0x00007f95a4136e0c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/usr/lib/llvm/17/bin/../lib64/libclang-cpp.so.17+0x2936e0c)
#26 0x0000557d1e154df9 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm/17/bin/clang+0x13df9)
#27 0x0000557d1e14f8c1 main (/usr/lib/llvm/17/bin/clang+0xe8c1)
#28 0x00007f959944eb8a (/lib64/libc.so.6+0x23b8a)
#29 0x00007f959944ec45 __libc_start_main (/lib64/libc.so.6+0x23c45)
#30 0x0000557d1e14f8f1 _start (/usr/lib/llvm/17/bin/clang+0xe8f1)
clang: error: clang frontend command failed with exit code 139 (use -v to see invocation)
clang version 17.0.2
Target: spirv64
Thread model: posix
InstalledDir: /usr/lib/llvm/17/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/GPUReconstructionOCL-746d6e.cl
clang: note: diagnostic msg: /tmp/GPUReconstructionOCL-746d6e.sh
clang: note: diagnostic msg: 

********************
davidrohr commented 3 months ago

For reference, I just tried the same with clang 18.1, and it fails in the same way

davidrohr commented 2 months ago

For reference, the related problem I had with the spirv-llvm-translater (https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2193) is now worked around / fixed.

I can compile the code to SPIR-V using clang 18.1.4 with https://github.com/llvm/llvm-project/pull/90048 and using the translator. While the same code still fails using the internal spirv backend with -fintegrated-objemitter. I am attaching my latest testcase. testcase.tar.gz