Skip to content

[clang][llc][AArch64] Instruction selection failure when targeting aarch64 architecture with 16bit vector types #135776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JustinKim98 opened this issue Apr 15, 2025 · 3 comments · Fixed by #135838
Assignees
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:SelectionDAG SelectionDAGISel as well

Comments

@JustinKim98
Copy link

JustinKim98 commented Apr 15, 2025

While I was trying to cross-compile following llvm IR bit cast using llc with following command in debug mode towards aarch64 architecture.

llc example.ll --mtriple=aarch64-linux-gnu -o example.s

(LLVM IR I'm trying to compile)
https://godbolt.org/z/Ejzcj6GY9

; example.ll
target triple = "aarch64-unknown-linux-gnu"

define i32 @bitcast_failure(ptr %0, <1 x i16> %1) {
  %3 = bitcast <1 x i16> %1 to <1 x half>
  %4 = extractelement <1 x half> %3, i64 0
  store half %4, ptr %0, align 2
  ret i32 0
}

I'm encountering some kind of weird error. Is this a bug in LLVM? It seems like there's a problem when trying to scalarize 16 bit data types.
I'm using the newest llvm version that I just cloned from main branch.

# llc --version
LLVM (http://llvm.org/):
  LLVM version 21.0.0git
  DEBUG build with assertions.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: znver4

Is this a bug in LLVM? or am I doing something wrong?

Here's the error log

llc: /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h:161: const llvm::SDValue& llvm::DAGTypeLegalizer::getSDValue(llvm::DAGTypeLegalizer::TableId&): Assertion `Id && "TableId should be non-zero"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: ./llvm-build-debug/bin/llc example2.ll --mtriple=aarch64-linux-gnu
1.	Running pass 'Function Pass Manager' on module 'example2.ll'.
2.	Running pass 'AArch64 Instruction Selection' on function '@bitcast_failure'
 #0 0x000064116e051396 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Unix/Signals.inc:804:22
 #1 0x000064116e0517b7 PrintStackTraceSignalHandler(void*) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Unix/Signals.inc:880:1
 #2 0x000064116e04ebe5 llvm::sys::RunSignalHandlers() /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #3 0x000064116e050c05 SignalHandler(int, siginfo_t*, void*) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Unix/Signals.inc:418:13
 #4 0x0000782303a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #5 0x0000782303a969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x0000782303a969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #7 0x0000782303a969fc pthread_kill ./nptl/pthread_kill.c:89:10
 #8 0x0000782303a42476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #9 0x0000782303a287f3 abort ./stdlib/abort.c:81:7
#10 0x0000782303a2871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#11 0x0000782303a39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#12 0x000064116de34302 llvm::DAGTypeLegalizer::getSDValue(unsigned int&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h:162:31
#13 0x000064116de92ad3 llvm::DAGTypeLegalizer::GetScalarizedVector(llvm::SDValue) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h:851:51
#14 0x000064116de53763 llvm::DAGTypeLegalizer::ScalarizeVecRes_BITCAST(llvm::SDNode*) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:406:29
#15 0x000064116de51b47 llvm::DAGTypeLegalizer::ScalarizeVectorResult(llvm::SDNode*, unsigned int) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:57:59
#16 0x000064116de2eeaa llvm::DAGTypeLegalizer::run() /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:281:17
#17 0x000064116de33624 llvm::SelectionDAG::LegalizeTypes() /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:1060:37
#18 0x000064116dd8250e llvm::SelectionDAGISel::CodeGenAndEmitDAG() /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:979:36
#19 0x000064116dd81a23 llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator_w_bits<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void, true, llvm::BasicBlock>, false, true>, llvm::ilist_iterator_w_bits<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void, true, llvm::BasicBlock>, false, true>, bool&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:877:1
#20 0x000064116dd878ed llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:1906:33
#21 0x000064116dd8010b llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:615:7
#22 0x0000641169bb141a (anonymous namespace)::AArch64DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:61:0
#23 0x000064116dd7ee62 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:374:40
#24 0x000064116c98b2dc llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:108:30
#25 0x000064116d248162 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1406:20
#26 0x000064116d248438 llvm::FPPassManager::runOnModule(llvm::Module&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1452:13
#27 0x000064116d248899 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1521:20
#28 0x000064116d2438e1 llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:539:13
#29 0x000064116d24918f llvm::legacy::PassManager::run(llvm::Module&) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1649:1
#30 0x000064116955118a compileModule(char**, llvm::LLVMContext&) /home/jwkim98/llvm_test/llvm-project/llvm/tools/llc/llc.cpp:755:34
#31 0x000064116954e810 main /home/jwkim98/llvm_test/llvm-project/llvm/tools/llc/llc.cpp:400:35
#32 0x0000782303a29d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#33 0x0000782303a29e40 call_init ./csu/../csu/libc-start.c:128:20
#34 0x0000782303a29e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#35 0x000064116954d3e5 _start (./llvm-build-debug/bin/llc+0xf613e5)
Aborted
@JustinKim98 JustinKim98 changed the title Instruction selection failure when targetting aarch64 architecture with 16bit vector types [clang] Instruction selection failure when targetting aarch64 architecture with 16bit vector types Apr 15, 2025
@JustinKim98 JustinKim98 changed the title [clang] Instruction selection failure when targetting aarch64 architecture with 16bit vector types Instruction selection failure when targetting aarch64 architecture with 16bit vector types Apr 15, 2025
@JustinKim98 JustinKim98 changed the title Instruction selection failure when targetting aarch64 architecture with 16bit vector types [clang] Instruction selection failure when targetting aarch64 architecture with 16bit vector types Apr 15, 2025
@JustinKim98 JustinKim98 changed the title [clang] Instruction selection failure when targetting aarch64 architecture with 16bit vector types [clang][llc] Instruction selection failure when targetting aarch64 architecture with 16bit vector types Apr 15, 2025
@JustinKim98 JustinKim98 changed the title [clang][llc] Instruction selection failure when targetting aarch64 architecture with 16bit vector types [clang][llc] Instruction selection failure when targeting aarch64 architecture with 16bit vector types Apr 15, 2025
@JustinKim98 JustinKim98 changed the title [clang][llc] Instruction selection failure when targeting aarch64 architecture with 16bit vector types [clang][llc][AArch64] Instruction selection failure when targeting aarch64 architecture with 16bit vector types Apr 15, 2025
@EugeneZelenko EugeneZelenko added backend:AArch64 crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Apr 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/issue-subscribers-backend-aarch64

Author: Justin Kim (JustinKim98)

While I was trying to cross-compile following llvm IR bit cast using llc with following command in debug mode towards aarch64 architecture.
llc example.ll --mtriple=aarch64-linux-gnu -o example.s

(LLVM IR I'm trying to compile)

; example.ll
target triple = "aarch64-unknown-linux-gnu"

define i32 @<!-- -->bitcast_failure(ptr %0, &lt;1 x i16&gt; %1) {
  %3 = bitcast &lt;1 x i16&gt; %1 to &lt;1 x half&gt;
  %4 = extractelement &lt;1 x half&gt; %3, i64 0
  store half %4, ptr %0, align 2
  ret i32 0
}

I'm encountering some kind of weird error. Is this a bug in LLVM? It seems like there's a problem when trying to scalarize 16 bit data types.
I'm using the newest llvm version that I just cloned from main branch.

# llc --version
LLVM (http://llvm.org/):
  LLVM version 21.0.0git
  DEBUG build with assertions.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: znver4

Is this a bug in LLVM? or am I doing something wrong?

Here's the error log

llc: /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h:161: const llvm::SDValue&amp; llvm::DAGTypeLegalizer::getSDValue(llvm::DAGTypeLegalizer::TableId&amp;): Assertion `Id &amp;&amp; "TableId should be non-zero"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: ./llvm-build-debug/bin/llc example2.ll --mtriple=aarch64-linux-gnu
1.	Running pass 'Function Pass Manager' on module 'example2.ll'.
2.	Running pass 'AArch64 Instruction Selection' on function '@<!-- -->bitcast_failure'
 #<!-- -->0 0x000064116e051396 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Unix/Signals.inc:804:22
 #<!-- -->1 0x000064116e0517b7 PrintStackTraceSignalHandler(void*) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Unix/Signals.inc:880:1
 #<!-- -->2 0x000064116e04ebe5 llvm::sys::RunSignalHandlers() /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #<!-- -->3 0x000064116e050c05 SignalHandler(int, siginfo_t*, void*) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Support/Unix/Signals.inc:418:13
 #<!-- -->4 0x0000782303a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->5 0x0000782303a969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #<!-- -->6 0x0000782303a969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #<!-- -->7 0x0000782303a969fc pthread_kill ./nptl/pthread_kill.c:89:10
 #<!-- -->8 0x0000782303a42476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #<!-- -->9 0x0000782303a287f3 abort ./stdlib/abort.c:81:7
#<!-- -->10 0x0000782303a2871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#<!-- -->11 0x0000782303a39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#<!-- -->12 0x000064116de34302 llvm::DAGTypeLegalizer::getSDValue(unsigned int&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h:162:31
#<!-- -->13 0x000064116de92ad3 llvm::DAGTypeLegalizer::GetScalarizedVector(llvm::SDValue) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h:851:51
#<!-- -->14 0x000064116de53763 llvm::DAGTypeLegalizer::ScalarizeVecRes_BITCAST(llvm::SDNode*) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:406:29
#<!-- -->15 0x000064116de51b47 llvm::DAGTypeLegalizer::ScalarizeVectorResult(llvm::SDNode*, unsigned int) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:57:59
#<!-- -->16 0x000064116de2eeaa llvm::DAGTypeLegalizer::run() /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:281:17
#<!-- -->17 0x000064116de33624 llvm::SelectionDAG::LegalizeTypes() /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:1060:37
#<!-- -->18 0x000064116dd8250e llvm::SelectionDAGISel::CodeGenAndEmitDAG() /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:979:36
#<!-- -->19 0x000064116dd81a23 llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator_w_bits&lt;llvm::ilist_detail::node_options&lt;llvm::Instruction, true, false, void, true, llvm::BasicBlock&gt;, false, true&gt;, llvm::ilist_iterator_w_bits&lt;llvm::ilist_detail::node_options&lt;llvm::Instruction, true, false, void, true, llvm::BasicBlock&gt;, false, true&gt;, bool&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:877:1
#<!-- -->20 0x000064116dd878ed llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:1906:33
#<!-- -->21 0x000064116dd8010b llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:615:7
#<!-- -->22 0x0000641169bb141a (anonymous namespace)::AArch64DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:61:0
#<!-- -->23 0x000064116dd7ee62 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:374:40
#<!-- -->24 0x000064116c98b2dc llvm::MachineFunctionPass::runOnFunction(llvm::Function&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:108:30
#<!-- -->25 0x000064116d248162 llvm::FPPassManager::runOnFunction(llvm::Function&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1406:20
#<!-- -->26 0x000064116d248438 llvm::FPPassManager::runOnModule(llvm::Module&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1452:13
#<!-- -->27 0x000064116d248899 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1521:20
#<!-- -->28 0x000064116d2438e1 llvm::legacy::PassManagerImpl::run(llvm::Module&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:539:13
#<!-- -->29 0x000064116d24918f llvm::legacy::PassManager::run(llvm::Module&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1649:1
#<!-- -->30 0x000064116955118a compileModule(char**, llvm::LLVMContext&amp;) /home/jwkim98/llvm_test/llvm-project/llvm/tools/llc/llc.cpp:755:34
#<!-- -->31 0x000064116954e810 main /home/jwkim98/llvm_test/llvm-project/llvm/tools/llc/llc.cpp:400:35
#<!-- -->32 0x0000782303a29d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#<!-- -->33 0x0000782303a29e40 call_init ./csu/../csu/libc-start.c:128:20
#<!-- -->34 0x0000782303a29e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#<!-- -->35 0x000064116954d3e5 _start (./llvm-build-debug/bin/llc+0xf613e5)
Aborted

@topperc topperc self-assigned this Apr 15, 2025
@topperc
Copy link
Collaborator

topperc commented Apr 15, 2025

I see the problem and I'm working on a fix.

@topperc
Copy link
Collaborator

topperc commented Apr 15, 2025

This should fix it

diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index f934d8b37561..a01e1cff7456 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -400,9 +400,7 @@ SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
 
 SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
   SDValue Op = N->getOperand(0);
-  if (Op.getValueType().isVector()
-      && Op.getValueType().getVectorNumElements() == 1
-      && !isSimpleLegalType(Op.getValueType()))
+  if (getTypeAction(Op.getValueType()) == TargetLowering::TypeScalarizeVector)
     Op = GetScalarizedVector(Op);
   EVT NewVT = N->getValueType(0).getVectorElementType();
   return DAG.getNode(ISD::BITCAST, SDLoc(N),

topperc added a commit to topperc/llvm-project that referenced this issue Apr 15, 2025
Use getTypeAction instead of trying to guess how a type will be
legalized. On AArch64, v1f16 is scalarized but v1f16 is widened.

Fixes llvm#135776
@EugeneZelenko EugeneZelenko added llvm:SelectionDAG SelectionDAGISel as well and removed backend:AArch64 labels Apr 15, 2025
var-const pushed a commit to ldionne/llvm-project that referenced this issue Apr 17, 2025

Unverified

The signing certificate or its chain could not be verified.
…r. (llvm#135838)

Use getTypeAction instead of trying to guess how a type will be
legalized. On AArch64, v1f16 is scalarized but v1f16 is widened.

Fixes llvm#135776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants