Skip to content
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

[AVR] clang/llvm crashed with a simple test program #132203

Open
benshi001 opened this issue Mar 20, 2025 · 2 comments
Open

[AVR] clang/llvm crashed with a simple test program #132203

benshi001 opened this issue Mar 20, 2025 · 2 comments
Assignees
Labels
backend:AVR crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@benshi001
Copy link
Member

a.ii.txt

with the attached a.ii, run clang++ a.ii -c -Os --target=avr -mmcu=atmega328, clang crash with info

In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp:30:
/usr/share/arduino/hardware/arduino/avr/cores/arduino/Arduino.h:164:38: warning: unknown attribute '__progmem__' ignored [-Wunknown-attributes]
  164 | extern const uint16_t __attribute__((__progmem__)) port_to_mode_PGM[];
      |                                      ^~~~~~~~~~~
/usr/share/arduino/hardware/arduino/avr/cores/arduino/Arduino.h:165:38: warning: unknown attribute '__progmem__' ignored [-Wunknown-attributes]
  165 | extern const uint16_t __attribute__((__progmem__)) port_to_input_PGM[];
      |                                      ^~~~~~~~~~~
/usr/share/arduino/hardware/arduino/avr/cores/arduino/Arduino.h:166:38: warning: unknown attribute '__progmem__' ignored [-Wunknown-attributes]
  166 | extern const uint16_t __attribute__((__progmem__)) port_to_output_PGM[];
      |                                      ^~~~~~~~~~~
/usr/share/arduino/hardware/arduino/avr/cores/arduino/Arduino.h:168:37: warning: unknown attribute '__progmem__' ignored [-Wunknown-attributes]
  168 | extern const uint8_t __attribute__((__progmem__)) digital_pin_to_port_PGM[];
      |                                     ^~~~~~~~~~~
/usr/share/arduino/hardware/arduino/avr/cores/arduino/Arduino.h:170:37: warning: unknown attribute '__progmem__' ignored [-Wunknown-attributes]
  170 | extern const uint8_t __attribute__((__progmem__)) digital_pin_to_bit_mask_PGM[];
      |                                     ^~~~~~~~~~~
/usr/share/arduino/hardware/arduino/avr/cores/arduino/Arduino.h:171:37: warning: unknown attribute '__progmem__' ignored [-Wunknown-attributes]
  171 | extern const uint8_t __attribute__((__progmem__)) digital_pin_to_timer_PGM[];
      |                                     ^~~~~~~~~~~

# After AVR pseudo instruction expansion pass
# Machine code for function _ZN14HardwareSerial9availableEv: NoPHIs, TracksLiveness, NoVRegs, TiedOpsRewritten, TracksDebugUserValues
Function Live Ins: $r25r24

bb.0.entry:
  liveins: $r25r24
  $r31r30 = MOVWRdRr killed $r25r24
  $r24 = LDDRdPtrQ $r31r30, 25 :: (volatile dereferenceable load (s8) from %ir._rx_buffer_head, !tbaa !3)
  $r18 = LDDRdPtrQ killed $r31r30, 26 :: (volatile dereferenceable load (s8) from %ir._rx_buffer_tail, !tbaa !14)
  $r24 = SUBRdRr $r24(tied-def 0), $r18, implicit-def $sreg
  $r25 = SBCRdRr $r25(tied-def 0), $r19, implicit-def dead $sreg, implicit killed $sreg
  $r24 = ANDIRdK killed $r24(tied-def 0), 63, implicit-def dead $sreg
  $r25 = ANDIRdK killed undef $r25(tied-def 0), 0, implicit-def dead $sreg
  RET implicit $r25r24, implicit $r1

# End machine code for function _ZN14HardwareSerial9availableEv.

*** Bad machine code: Using an undefined physical register ***
- function:    _ZN14HardwareSerial9availableEv
- basic block: %bb.0 entry (0x64af49c64710)
- instruction: $r25 = SBCRdRr $r25(tied-def 0), $r19, implicit-def dead $sreg, implicit killed $sreg
- operand 1:   $r25(tied-def 0)

*** Bad machine code: Using an undefined physical register ***
- function:    _ZN14HardwareSerial9availableEv
- basic block: %bb.0 entry (0x64af49c64710)
- instruction: $r25 = SBCRdRr $r25(tied-def 0), $r19, implicit-def dead $sreg, implicit killed $sreg
- operand 2:   $r19
fatal error: error in backend: Found 2 machine code errors.
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: clang++ a.ii --target=avr -mmcu=atmega328 -c -Os
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module '/usr/share/arduino/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp'.
4.	Running pass 'Verify generated machine code' on function '@_ZN14HardwareSerial9availableEv'
 #0 0x000064af1e026060 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x2800060)
 #1 0x000064af1e02295f llvm::sys::RunSignalHandlers() (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x27fc95f)
 #2 0x000064af1defe80a llvm::CrashRecoveryContext::HandleExit(int) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x26d880a)
 #3 0x000064af1e01cc12 llvm::sys::Process::Exit(int, bool) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x27f6c12)
 #4 0x000064af1c52e321 LLVMErrorHandler(void*, char const*, bool) cc1_main.cpp:0:0
 #5 0x000064af1df180c0 llvm::report_fatal_error(llvm::Twine const&, bool) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x26f20c0)
 #6 0x000064af1d15286b (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x192c86b)
 #7 0x000064af1d15290a (anonymous namespace)::MachineVerifier::~MachineVerifier() MachineVerifier.cpp:0:0
 #8 0x000064af1d16e95d (anonymous namespace)::MachineVerifierLegacyPass::runOnMachineFunction(llvm::MachineFunction&) (.part.0) MachineVerifier.cpp:0:0
 #9 0x000064af1d16e9a9 (anonymous namespace)::MachineVerifierLegacyPass::runOnMachineFunction(llvm::MachineFunction&) MachineVerifier.cpp:0:0
#10 0x000064af1d049ab8 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#11 0x000064af1d8ec1bd llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x20c61bd)
#12 0x000064af1d8ec719 llvm::FPPassManager::runOnModule(llvm::Module&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x20c6719)
#13 0x000064af1d8ed6d1 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x20c76d1)
#14 0x000064af1e35330f 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*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x2b2d30f)
#15 0x000064af1eadf191 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x32b9191)
#16 0x000064af20b4b65d clang::ParseAST(clang::Sema&, bool, bool) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x532565d)
#17 0x000064af1ee79649 clang::FrontendAction::Execute() (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x3653649)
#18 0x000064af1edd3afe clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x35adafe)
#19 0x000064af1ef99828 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x3773828)
#20 0x000064af1c53510b cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xd0f10b)
#21 0x000064af1c526bea ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#22 0x000064af1eb446ad 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
#23 0x000064af1defe740 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x26d8740)
#24 0x000064af1eb46cfe 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
#25 0x000064af1eaefe51 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x32c9e51)
#26 0x000064af1eaf0f71 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x32caf71)
#27 0x000064af1eb01444 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x32db444)
#28 0x000064af1c52ce8e clang_main(int, char**, llvm::ToolContext const&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xd06e8e)
#29 0x000064af1c45cc7b main (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xc36c7b)
#30 0x0000724a69c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#31 0x0000724a69c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#32 0x000064af1c526315 _start (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xd00315)
clang++: error: clang frontend command failed with exit code 70 (use -v to see invocation)
clang version 21.0.0git (git@github.com:benshi001/llvm-project.git db000cf7c012ebe8d7fb30e47089b61b7ad9c045)
Target: avr
Thread model: posix
InstalledDir: /home/wujie/benshi/llvm-project/build/bin
Build config: +assertions, +expensive-checks
clang++: note: diagnostic msg: Error generating preprocessed source(s) - no preprocessable inputs.
@benshi001 benshi001 self-assigned this Mar 20, 2025
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 20, 2025
@benshi001 benshi001 added backend:AVR crash Prefer [crash-on-valid] or [crash-on-invalid] labels Mar 20, 2025
@benshi001
Copy link
Member Author

benshi001 commented Mar 21, 2025

simplify the case to

define i16 @food(ptr %this) {
entry:
  %_rx_buffer_head = getelementptr inbounds nuw i8, ptr %this, i16 25
  %0 = load volatile i8, ptr %_rx_buffer_head
  %conv = zext i8 %0 to i16
  %_rx_buffer_tail = getelementptr inbounds nuw i8, ptr %this, i16 26
  %1 = load volatile i8, ptr %_rx_buffer_tail
  %conv2 = zext i8 %1 to i16
  %sub = sub nsw i16 %conv, %conv2
  %rem = and i16 %sub, 63
  ret i16 %rem
}

run llc a.ll -mtriple=avr -mcpu=atmega328, then the crash info is

# After AVR pseudo instruction expansion pass
# Machine code for function food: NoPHIs, TracksLiveness, NoVRegs, TiedOpsRewritten, TracksDebugUserValues
Function Live Ins: $r25r24

bb.0.entry:
  liveins: $r25r24
  $r31r30 = MOVWRdRr killed $r25r24
  $r24 = LDDRdPtrQ $r31r30, 25 :: (volatile load (s8) from %ir._rx_buffer_head)
  $r18 = LDDRdPtrQ killed $r31r30, 26 :: (volatile load (s8) from %ir._rx_buffer_tail)
  $r24 = SUBRdRr $r24(tied-def 0), $r18, implicit-def $sreg
  $r25 = SBCRdRr $r25(tied-def 0), $r19, implicit-def dead $sreg, implicit killed $sreg
  $r24 = ANDIRdK killed $r24(tied-def 0), 63, implicit-def dead $sreg
  $r25 = ANDIRdK killed undef $r25(tied-def 0), 0, implicit-def dead $sreg
  RET implicit $r25r24, implicit $r1

# End machine code for function food.

*** Bad machine code: Using an undefined physical register ***
- function:    food
- basic block: %bb.0 entry (0x6784e00)
- instruction: $r25 = SBCRdRr $r25(tied-def 0), $r19, implicit-def dead $sreg, implicit killed $sreg
- operand 1:   $r25(tied-def 0)

*** Bad machine code: Using an undefined physical register ***
- function:    food
- basic block: %bb.0 entry (0x6784e00)
- instruction: $r25 = SBCRdRr $r25(tied-def 0), $r19, implicit-def dead $sreg, implicit killed $sreg
- operand 2:   $r19
LLVM ERROR: Found 2 machine code errors.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: ./bin/llc e.ll -mtriple=avr -mattr=+movw
1.	Running pass 'Function Pass Manager' on module 'e.ll'.
2.	Running pass 'Verify generated machine code' on function '@food'
 #0 0x000000000218f268 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./bin/llc+0x218f268)
 #1 0x000000000218c93c SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007f3bb088ccf0 __restore_rt (/lib64/libpthread.so.0+0x12cf0)
 #3 0x00007f3baf30cacf raise (/lib64/libc.so.6+0x4eacf)
 #4 0x00007f3baf2dfea5 abort (/lib64/libc.so.6+0x21ea5)
 #5 0x000000000075fbe6 llvm::json::operator==(llvm::json::Value const&, llvm::json::Value const&) (.cold.319) JSON.cpp:0:0
 #6 0x00000000012beafd (anonymous namespace)::MachineVerifier::~MachineVerifier() MachineVerifier.cpp:0:0
 #7 0x00000000012d48a3 (anonymous namespace)::MachineVerifierLegacyPass::runOnMachineFunction(llvm::MachineFunction&) (.part.682) MachineVerifier.cpp:0:0
 #8 0x00000000012d48d9 (anonymous namespace)::MachineVerifierLegacyPass::runOnMachineFunction(llvm::MachineFunction&) MachineVerifier.cpp:0:0
 #9 0x00000000011b2905 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.99) MachineFunctionPass.cpp:0:0
#10 0x0000000001785363 llvm::FPPassManager::runOnFunction(llvm::Function&) (./bin/llc+0x1785363)
#11 0x00000000017855f9 llvm::FPPassManager::runOnModule(llvm::Module&) (./bin/llc+0x17855f9)
#12 0x000000000178621c llvm::legacy::PassManagerImpl::run(llvm::Module&) (./bin/llc+0x178621c)
#13 0x000000000081c19a compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#14 0x0000000000764aa7 main (./bin/llc+0x764aa7)
#15 0x00007f3baf2f8d85 __libc_start_main (/lib64/libc.so.6+0x3ad85)
#16 0x0000000000812dce _start (./bin/llc+0x812dce)
已放弃 (core dumped)

benshi001 added a commit to benshi001/llvm-project that referenced this issue Mar 21, 2025
This is a walk around solution of llvm#132203
benshi001 added a commit to benshi001/llvm-project that referenced this issue Mar 21, 2025
This is a walk around solution of llvm#132203
@benshi001
Copy link
Member Author

That is a walk around solution to avoid the crash: #132398 , however it does not really fix this issue.

benshi001 added a commit to benshi001/llvm-project that referenced this issue Mar 22, 2025
This is a walk around solution of llvm#132203
@EugeneZelenko EugeneZelenko removed the clang Clang issues not falling into any other category label Mar 22, 2025
benshi001 added a commit to benshi001/llvm-project that referenced this issue Mar 23, 2025
Patryk27 pushed a commit that referenced this issue Mar 26, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this issue Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AVR crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

No branches or pull requests

3 participants