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 LTO causes Arduino blink example to break #127651

Closed
beakthoven opened this issue Feb 18, 2025 · 31 comments
Closed

[AVR] clang LTO causes Arduino blink example to break #127651

beakthoven opened this issue Feb 18, 2025 · 31 comments
Assignees
Labels
backend:AVR LTO Link time optimization (regular/full LTO or ThinLTO) miscompilation

Comments

@beakthoven
Copy link

beakthoven commented Feb 18, 2025

When compiling the Blink example from the Arduino IDE for AVR using Clang with LTO enabled, the program fails to function correctly. The LED blinks once and then remains stuck in the ON position indefinitely. Disabling LTO for wiring.c from Arduino AVR core resolves the issue.

LLVM version: 20.1.0-rc2

Blink example code:

#include <Arduino.h>

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(1000); 
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000); 
}

Compiler flags used (other than -flto -fno-fat-lto-objects):

--target=avr -c -g -Os -Wall -Wextra --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -mmcu=atmega2560 -DF_CPU=16000000L

Expected Behavior

The LED should continue blinking at a 1-second interval indefinitely.

Attachments

LLVM IR files for wiring.c with LTO turned on and off.
wiring.c-lto.ll.txt
wiring.c-nonlto.ll.txt

wiring.c: https://github.com/ClangBuiltArduino/core_arduino_avr/blob/8b327d7bede1c1245db99daeba4e168c92c11194/cores/arduino/wiring.c

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Feb 18, 2025
@EugeneZelenko EugeneZelenko added LTO Link time optimization (regular/full LTO or ThinLTO) and removed clang Clang issues not falling into any other category labels Feb 18, 2025
@benshi001 benshi001 self-assigned this Feb 19, 2025
@foxtran
Copy link
Member

foxtran commented Feb 19, 2025

Does GCC LTO have the same effect? Just to be sure that is not a problem with C code.

@beakthoven
Copy link
Author

Does GCC LTO have the same effect? Just to be sure that is not a problem with C code.

No, AVR GCC works perfectly fine with the same code with and without LTO.

@benshi001
Copy link
Member

benshi001 commented Mar 16, 2025

I am a little confused, I have tried your attached two .ll.txt files, with

wujie@WUJIE-16:~/Downloads$ llc wiring.c-lto.ll.txt -mcpu=atmega2560 -o wiring.c-lto.s.txt
wujie@WUJIE-16:~/Downloads$ llc wiring.c-nonlto.ll.txt -mcpu=atmega2560 wiring.c-nonlto.s.txt

The generated assembly source files only have difference in debug information, but not functional code.

wiring.c-lto.s.txt

wiring.c-nonlto.s.txt

@benshi001
Copy link
Member

I also tried to compile wiring.c without debug information,

clang --target=avr  -S  -Os -Wall -Wextra  -std=gnu11 -ffunction-sections -fdata-sections -MMD -mmcu=atmega2560 -DF_CPU=16000000L ./core_arduino_avr/cores/arduino/wiring.c  -I core_arduino_avr/variants/mega/ -o wiring.nolto.s

clang --target=avr  -S  -Os -Wall -Wextra  -std=gnu11 -ffunction-sections -fdata-sections -MMD -mmcu=atmega2560 -DF_CPU=16000000L ./core_arduino_avr/cores/arduino/wiring.c -flto -I core_arduino_avr/variants/mega/ -o wiring.lto.ll
clang --target=avr wiring.lto.ll -S -mmcu=atmega2560 -o wiring.lto.s

The generated two .s files are also same in functional code.

so I guess, there is something wrong in the core libraries, other than wire.c.

@benshi001
Copy link
Member

BTW: how did build arduino core library by clang? I used the attached script but failed.

arduino.sh.txt

The failure log is

In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/CDC.cpp:20:
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/wdt.h:464:12: error: value '64' out of range for constraint 'I'
  464 |                                 : "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)),
      |                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/sfr_defs.h:183:27: note: expanded from macro '_SFR_IO_ADDR'
  183 | #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/CDC.cpp:20:
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/wdt.h:507:23: error: value '64' out of range for constraint 'I'
  507 |                                 : [WDTREG]  "I"  (_SFR_IO_ADDR(_WD_CONTROL_REG)),
      |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/sfr_defs.h:183:27: note: expanded from macro '_SFR_IO_ADDR'
  183 | #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
clang++: /home/wujie/benshi/llvm-project/clang/include/clang/CodeGen/CGFunctionInfo.h:185: static clang::CodeGen::ABIArgInfo clang::CodeGen::ABIArgInfo::getExtend(clang::QualType, llvm::Type*): Assertion `Ty->isIntegralOrEnumerationType() && "Unexpected QualType"' failed.
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++ --target=avr -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/usr/share/arduino/hardware/arduino/avr/cores/arduino -I/usr/share/arduino/hardware/arduino/avr/variants/eightanaloginputs /usr/share/arduino/hardware/arduino/avr/cores/arduino/new.cpp -o /tmp/arduino_build_960004/core/new.cpp.o
1.	/usr/share/arduino/hardware/arduino/avr/cores/arduino/new.cpp:68:1: current parser token 'void'
2.	/usr/share/arduino/hardware/arduino/avr/cores/arduino/new.cpp:59:8: LLVM IR generation of declaration 'operator new'
3.	/usr/share/arduino/hardware/arduino/avr/cores/arduino/new.cpp:59:8: Generating code for declaration 'operator new'
 #0 0x00005aa720523900 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x27fd900)
 #1 0x00005aa7205201ff llvm::sys::RunSignalHandlers() (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x27fa1ff)
 #2 0x00005aa7203fbb38 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007d6b34e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007d6b34e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007d6b34e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007d6b34e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007d6b34e2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007d6b34e39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00005aa720c5f084 (anonymous namespace)::AVRABIInfo::computeInfo(clang::CodeGen::CGFunctionInfo&) const AVR.cpp:0:0
#10 0x00005aa720ec80fa clang::CodeGen::CodeGenTypes::arrangeLLVMFunctionInfo(clang::CanQual<clang::Type>, clang::CodeGen::FnInfoOpts, llvm::ArrayRef<clang::CanQual<clang::Type>>, clang::FunctionType::ExtInfo, llvm::ArrayRef<clang::FunctionType::ExtParameterInfo>, clang::CodeGen::RequiredArgs) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x31a20fa)
#11 0x00005aa720ecc97b arrangeLLVMFunctionInfo(clang::CodeGen::CodeGenTypes&, bool, llvm::SmallVectorImpl<clang::CanQual<clang::Type>>&, clang::CanQual<clang::FunctionProtoType>) CGCall.cpp:0:0
#12 0x00005aa720ecec25 clang::CodeGen::CodeGenTypes::arrangeFunctionDeclaration(clang::FunctionDecl const*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x31a8c25)
#13 0x00005aa720b87325 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x2e61325)
#14 0x00005aa720b82884 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x2e5c884)
#15 0x00005aa720b82efb clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x2e5cefb)
#16 0x00005aa720b8bbd7 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#17 0x00005aa720fe0cd6 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#18 0x00005aa720fce981 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x32a8981)
#19 0x00005aa723021e94 clang::ParseAST(clang::Sema&, bool, bool) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x52fbe94)
#20 0x00005aa721376bd9 clang::FrontendAction::Execute() (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x3650bd9)
#21 0x00005aa7212d151e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x35ab51e)
#22 0x00005aa721496dc8 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x3770dc8)
#23 0x00005aa71ea333bb cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xd0d3bb)
#24 0x00005aa71ea24e9a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#25 0x00005aa72104211d 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 0x00005aa7203fbfe0 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x26d5fe0)
#27 0x00005aa72104476e 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 0x00005aa720fed8c1 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/home/wujie/benshi/llvm-project/build/bin/clang-21+0x32c78c1)
#29 0x00005aa720fee9e1 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+0x32c89e1)
#30 0x00005aa720ffeeb4 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+0x32d8eb4)
#31 0x00005aa71ea2b13e clang_main(int, char**, llvm::ToolContext const&) (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xd0513e)
#32 0x00005aa71e95bb0b main (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xc35b0b)
#33 0x00007d6b34e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#34 0x00007d6b34e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#35 0x00005aa71ea245c5 _start (/home/wujie/benshi/llvm-project/build/bin/clang-21+0xcfe5c5)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
clang version 21.0.0git ([email protected]:llvm/llvm-project.git 254951749febef42620242fd22dfba62f624f79f)
Target: avr
Thread model: posix
InstalledDir: /home/wujie/benshi/llvm-project/build/bin
Build config: +assertions, +expensive-checks
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/new-fd3c2f.cpp
clang++: note: diagnostic msg: /tmp/new-fd3c2f.sh
clang++: note: diagnostic msg: 

********************
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ar: /tmp/arduino_build_960004/core/CDC.cpp.o: 没有那个文件或目录
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ar: /tmp/arduino_build_960004/core/new.cpp.o: 没有那个文件或目录

@benshi001
Copy link
Member

I think I should fix the above failure first, it indeed is clang's ICE, as the first step to let clang support arduino.

@beakthoven
Copy link
Author

I am a little confused, I have tried your attached two .ll.txt files, with

wujie@WUJIE-16:/Downloads$ llc wiring.c-lto.ll.txt -mcpu=atmega2560 -o wiring.c-lto.s.txt
wujie@WUJIE-16:
/Downloads$ llc wiring.c-nonlto.ll.txt -mcpu=atmega2560 wiring.c-nonlto.s.txt
The generated assembly source files only have difference in debug information, but not functional code.

wiring.c-lto.s.txt

wiring.c-nonlto.s.txt

Then i think LTO optimizations made during link time are then likely the cause of the issue. I'll try to upload LTO intermediate files shortly in order to debug this.

@beakthoven
Copy link
Author

In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/CDC.cpp:20:
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/wdt.h:464:12: error: value '64' out of range for constraint 'I'
464 | : "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/sfr_defs.h:183:27: note: expanded from macro '_SFR_IO_ADDR'
183 | #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/CDC.cpp:20:
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/wdt.h:507:23: error: value '64' out of range for constraint 'I'
507 | : [WDTREG] "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/sfr_defs.h:183:27: note: expanded from macro '_SFR_IO_ADDR'
183 | #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.

The constraint error you have encountered is because of a code in AVR libc thats written in a way that makes use of gcc specific behavior.
avr-libc issue: avrdudes/avr-libc#998
llvm issue: #51513

For time being i have applied the following patch in my fork of avr-libc to mitigate the constraint issue since there is no official fix from avr-libc.

However about to the clang crash, I haven't encountered it during my builds so i dont know what we're looking here.

BTW: how did build arduino core library by clang?

Well i wrote this simple bash script to use during initial stage. Now i have completely moved to arduino's build system, the required changes for this were made to the build files in the core lib repo as of commit ClangBuiltArduino/core_arduino_avr@96cc1db. This should be compatible with arduino-cli as well.

@benshi001
Copy link
Member

I am a little confused, I have tried your attached two .ll.txt files, with
wujie@WUJIE-16:/Downloads$ llc wiring.c-lto.ll.txt -mcpu=atmega2560 -o wiring.c-lto.s.txt
wujie@WUJIE-16:
/Downloads$ llc wiring.c-nonlto.ll.txt -mcpu=atmega2560 wiring.c-nonlto.s.txt
The generated assembly source files only have difference in debug information, but not functional code.
wiring.c-lto.s.txt
wiring.c-nonlto.s.txt

Then i think LTO optimizations made during link time are then likely the cause of the issue. I'll try to upload LTO intermediate files shortly in order to debug this.

It would be better if you can give the final linked file in the form of

  1. LTO linked in IR
  2. LTO linked in assembly
  3. not LTO linked in IR
  4. not LTO linked in assembly

@benshi001
Copy link
Member

benshi001 commented Mar 17, 2025

In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/CDC.cpp:20:
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/wdt.h:464:12: error: value '64' out of range for constraint 'I'
464 | : "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/sfr_defs.h:183:27: note: expanded from macro '_SFR_IO_ADDR'
183 | #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/share/arduino/hardware/arduino/avr/cores/arduino/CDC.cpp:20:
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/wdt.h:507:23: error: value '64' out of range for constraint 'I'
507 | : [WDTREG] "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/avr/5.4.0/../../../avr/include/avr/sfr_defs.h:183:27: note: expanded from macro '_SFR_IO_ADDR'
183 | #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.

The constraint error you have encountered is because of a code in AVR libc thats written in a way that makes use of gcc specific behavior. avr-libc issue: avrdudes/avr-libc#998 llvm issue: #51513

For time being i have applied the following patch in my fork of avr-libc to mitigate the constraint issue since there is no official fix from avr-libc.

However about to the clang crash, I haven't encountered it during my builds so i dont know what we're looking here.

BTW: how did build arduino core library by clang?

Well i wrote this simple bash script to use during initial stage. Now i have completely moved to arduino's build system, the required changes for this were made to the build files in the core lib repo as of commit ClangBuiltArduino/core_arduino_avr@96cc1db. This should be compatible with arduino-cli as well.

Thanks for your clarification, I would try

  1. apply your patch to dismiss the first error
  2. fix the second error
  3. reproduce the bug you encountered

@benshi001
Copy link
Member

benshi001 commented Mar 20, 2025

I have fixed the first crash, as patch #131976,

but I encountered a second crash, as reported in #132203

@benshi001
Copy link
Member

benshi001 commented Mar 20, 2025

@dakkshesh07 , may I ask

  1. Is there a instruction about how to build your ClangBuiltArduino project? Or I simply follow the arduino CLI way ?
  2. which version of clang /llvm are you using? Is yours the newest main branch ?

I still failed build the simplest blink program with the above two crashes.

@beakthoven
Copy link
Author

beakthoven commented Mar 20, 2025

  1. Is there a instruction about how to build your ClangBuiltArduino project? Or I simply follow the arduino CLI way ?

You can directly add the custom board package json and then install the custom board support to use it.

arduino-cli config add board_manager.additional_urls https://raw.githubusercontent.com/ClangBuiltArduino/BoardManagerFiles/refs/heads/main/package_clangbuiltarduino_index.json
arduino-cli core update-index
arduino-cli core install ClangBuiltArduino:avr
arduino-cli compile -b ClangBuiltArduino:avr:mega $dir
  1. which version of clang /llvm are you using? Is yours the newest main branch ?

I haven't updated the json since 20.1.0-rc2 build, I have a 20.1.0 build ready to be used. I will update the json and try once.
I will also update toolchain scripts to add the ability to build off of main branch instead of release tarballs so that we can test bleeding edge builds.

Btw can you take a look at my llvm build configuration, maybe i disabled some crucial feature so I'm not getting those crashes?
https://github.com/ClangBuiltArduino/tc-build/blob/main/llvm/build-llvm-stage2.sh#L63-L223

I still failed build the simplest blink program with the above two crashes.

I will try to use the latest 20.1.0 build or main branch build to see if i can hit those crashes.

@beakthoven
Copy link
Author

I have updated the toolchain to 20.1.0 as of commit:
ClangBuiltArduino/BoardManagerFiles@9584288

$ arduino-cli compile --clean -b ClangBuiltArduino:avr:mega Blink

Sketch uses 1418 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 8183 bytes for local variables. Maximum is 8192 bytes.

Used platform         Version Path
ClangBuiltArduino:avr 1.0.1   /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1

@beakthoven
Copy link
Author

I have fixed the first crash, as patch #131976,

I'll try building off of main branch once this patch is merged.

@benshi001
Copy link
Member

benshi001 commented Mar 22, 2025

I have fixed the 2nd crash as #132398, but a third error (a link failure) rised. I never thought building the simplest blink with clang is such that hard.

/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o): In function `serialEventRun()':
/data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:(.text._Z14serialEventRunv+0xa): undefined reference to `serialEvent()'
/data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:(.text._Z14serialEventRunv+0xc): undefined reference to `serialEvent()'
/data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:67: undefined reference to `serialEvent()'
/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o):(.rodata._ZTI14HardwareSerial+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o):(.rodata._ZTI6Stream[_ZTI6Stream]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(Print.cpp.o):(.rodata._ZTI5Print+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
clang: error: avr-ld command failed with exit code 1 (use -v to see invocation)

@beakthoven
Copy link
Author

beakthoven commented Mar 22, 2025

/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o): In function serialEventRun()': /data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:(.text._Z14serialEventRunv+0xa): undefined reference to serialEvent()'
/data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:(.text._Z14serialEventRunv+0xc): undefined reference to serialEvent()' /data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:67: undefined reference to serialEvent()'

These errors should be fixed using the following commits:
ClangBuiltArduino/core_arduino_avr@698ad5d
ClangBuiltArduino/core_arduino_avr@9602bbb

/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o):(.rodata._ZTI14HardwareSerial+0x0): undefined reference to vtable for __cxxabiv1::__si_class_type_info' /data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o):(.rodata._ZTI6Stream[_ZTI6Stream]+0x0): undefined reference to vtable for __cxxabiv1::__si_class_type_info'
/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(Print.cpp.o):(.rodata._ZTI5Print+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
clang: error: avr-ld command failed with exit code 1 (use -v to see invocation)

This error should be fixed by using the -fno-rtti compiler flag

I have fixed the 2nd crash as #132398, but a third error (a link failure) rised. I never thought building the simplest blink with clang is such that hard.

It indeed is, i suggest you to use my fork of the core lib instead which already contains all required fixes.

@beakthoven
Copy link
Author

beakthoven commented Mar 22, 2025

I just used a llvm built off of main branch at commit c51d396 with #132398 applied. The compiler was able to compile the blink sketch with no issues even with expensive checks enabled but the program breakage still exists, with LTO enabled globally the LED blinks once and then gets stuck in ON state forever. Please let me know if you need any other information.

Build logs
$ arduino-cli compile -v --clean -b ClangBuiltArduino:avr:mega Blink

FQBN: ClangBuiltArduino:avr:mega
Using board 'mega' from platform in folder: /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1
Using core 'arduino' from platform in folder: /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1

Detecting libraries used...
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/sketch/Blink.ino.cpp -o /dev/null
Generating function prototypes...
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/sketch/Blink.ino.cpp -o /tmp/3623586140/sketch_merged.cpp
/home/dakkshesh/.arduino15/packages/builtin/tools/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /tmp/3623586140/sketch_merged.cpp
Compiling sketch...
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/sketch/Blink.ino.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/sketch/Blink.ino.cpp.o
Compiling libraries...
Compiling core...
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/HardwareSerial2.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial2.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/CDC.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/CDC.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/HardwareSerial0.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial0.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/HardwareSerial3.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial3.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/PluggableUSB.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/PluggableUSB.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/HardwareSerial1.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial1.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/HardwareSerial.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/IPAddress.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/IPAddress.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/Print.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/Print.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/Stream.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/Stream.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/Tone.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/Tone.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/USBCore.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/USBCore.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/WMath.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/WMath.cpp.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/WInterrupts.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/WInterrupts.c.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/WString.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/WString.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/abi.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/abi.cpp.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/hooks.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/hooks.c.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/main.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/main.cpp.o
/home/dakkshesh/install/bin/clang++ --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/new.cpp -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/new.cpp.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/wiring.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring.c.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/wiring_analog.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_analog.c.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/wiring_digital.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_digital.c.o
/home/dakkshesh/install/bin/clang --target=avr --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -c -g -x assembler-with-cpp -flto -MMD -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/wiring_pulse.S -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_pulse.S.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/wiring_pulse.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_pulse.c.o
/home/dakkshesh/install/bin/clang --target=avr -c -g -Os -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino -I/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/variants/mega /home/dakkshesh/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1/cores/arduino/wiring_shift.c -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_shift.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/CDC.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial0.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial1.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial2.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/HardwareSerial3.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/IPAddress.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/PluggableUSB.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/Print.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/Stream.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/Tone.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/USBCore.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/WInterrupts.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/WMath.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/WString.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/abi.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/hooks.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/main.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/new.cpp.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_analog.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_digital.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_pulse.S.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_pulse.c.o
/home/dakkshesh/install/bin/llvm-ar rcs /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/wiring_shift.c.o
Archiving built core (caching) in: /home/dakkshesh/.cache/arduino/cores/ClangBuiltArduino_avr_mega_116424f3ccbe01a19dd3deb05bebea6b/core.a
Linking everything together...
/home/dakkshesh/install/bin/clang++ --target=avr -w --sysroot=/home/dakkshesh/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-sysroot/12022025 -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections -mmcu=atmega2560 -o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/Blink.ino.elf /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/sketch/Blink.ino.cpp.o /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/core/core.a -L/home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7 -lm
/home/dakkshesh/install/bin/llvm-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/Blink.ino.elf /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/Blink.ino.eep
/home/dakkshesh/install/bin/llvm-objcopy -O ihex -R .eeprom /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/Blink.ino.elf /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/Blink.ino.hex

/home/dakkshesh/install/bin/llvm-size -A /home/dakkshesh/.cache/arduino/sketches/0A3424900B1B32407700B6A1409F8FE7/Blink.ino.elf
Sketch uses 1328 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 8183 bytes for local variables. Maximum is 8192 bytes.

Looking at the LED behavior i think the issue could be isolated to the interrupts or the delay timer? Im not sure though.

@benshi001
Copy link
Member

/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o): In function serialEventRun()': /data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:(.text._Z14serialEventRunv+0xa): undefined reference to serialEvent()'
/data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:(.text._Z14serialEventRunv+0xc): undefined reference to serialEvent()' /data/home/bennshi/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.cpp:67: undefined reference to serialEvent()'

These errors should be fixed using the following commits: ClangBuiltArduino/core_arduino_avr@698ad5d ClangBuiltArduino/core_arduino_avr@9602bbb

/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o):(.rodata._ZTI14HardwareSerial+0x0): undefined reference to vtable for __cxxabiv1::__si_class_type_info' /data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(HardwareSerial.cpp.o):(.rodata._ZTI6Stream[_ZTI6Stream]+0x0): undefined reference to vtable for __cxxabiv1::__si_class_type_info'
/data/home/bennshi/.cache/arduino/sketches/CEE06FE2D07E31C0BC8B101357BC67E2/core/core.a(Print.cpp.o):(.rodata._ZTI5Print+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
clang: error: avr-ld command failed with exit code 1 (use -v to see invocation)

This error should be fixed by using the -fno-rtti compiler flag

I have fixed the 2nd crash as #132398, but a third error (a link failure) rised. I never thought building the simplest blink with clang is such that hard.

It indeed is, i suggest you to use my fork of the core lib instead which already contains all required fixes.

thanks. I will try your fork.

@benshi001
Copy link
Member

/home/dakkshesh/install/bin/llvm-ar rcs

BTW, I saw /home/dakkshesh/install/bin/llvm-ar is used during your build process, I guess it may be
the reason why LTO failed? because clang -flto emits LLVM byte code instead of relocatable ELF (which avr-gcc emits), so you have to use llvm-link instead of llvm-ar ?

@beakthoven
Copy link
Author

/home/dakkshesh/install/bin/llvm-ar rcs

BTW, I saw /home/dakkshesh/install/bin/llvm-ar is used during your build process, I guess it may be the reason why LTO failed? because clang -flto emits LLVM byte code instead of relocatable ELF (which avr-gcc emits), so you have to use llvm-link instead of llvm-ar ?

I believe llvm-ar can handle LLVM bitcode? According to the docs

The :program:llvm-ar command is similar to the common Unix utility, :program:ar. It archives several files, such as objects and LLVM bitcode files into a single archive library that can be linked into a program.

@benshi001
Copy link
Member

/home/dakkshesh/install/bin/llvm-ar rcs

BTW, I saw /home/dakkshesh/install/bin/llvm-ar is used during your build process, I guess it may be the reason why LTO failed? because clang -flto emits LLVM byte code instead of relocatable ELF (which avr-gcc emits), so you have to use llvm-link instead of llvm-ar ?

I believe llvm-ar can handle LLVM bitcode? According to the docs

The :program:llvm-ar command is similar to the common Unix utility, :program:ar. It archives several files, such as objects and LLVM bitcode files into a single archive library that can be linked into a program.

I see. Your are correct 👍

@beakthoven
Copy link
Author

Here are the bitcode and IR LTO intermediate files for the Blink sketch, these should help us analyze what LTO is doing.

Blink-intermediate.zip

@benshi001
Copy link
Member

  1. Is there a instruction about how to build your ClangBuiltArduino project? Or I simply follow the arduino CLI way ?

You can directly add the custom board package json and then install the custom board support to use it.

arduino-cli config add board_manager.additional_urls https://raw.githubusercontent.com/ClangBuiltArduino/BoardManagerFiles/refs/heads/main/package_clangbuiltarduino_index.json
arduino-cli core update-index
arduino-cli core install ClangBuiltArduino:avr
arduino-cli compile -b ClangBuiltArduino:avr:mega $dir

  1. which version of clang /llvm are you using? Is yours the newest main branch ?

I haven't updated the json since 20.1.0-rc2 build, I have a 20.1.0 build ready to be used. I will update the json and try once. I will also update toolchain scripts to add the ability to build off of main branch instead of release tarballs so that we can test bleeding edge builds.

Btw can you take a look at my llvm build configuration, maybe i disabled some crucial feature so I'm not getting those crashes? https://github.com/ClangBuiltArduino/tc-build/blob/main/llvm/build-llvm-stage2.sh#L63-L223

I still failed build the simplest blink program with the above two crashes.

I will try to use the latest 20.1.0 build or main branch build to see if i can hit those crashes.

Unfortunately I got the following error with your tools.

/data/home/bennshi/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-bfd/2.44-14022025/glibc/bin/avr-ld: /data/home/bennshi/.arduino15/packages/ClangBuiltArduino/tools/cba-llvm/20.1.0-06032025/bin/../lib/LLVMgold.so: error loading plugin: /lib64/libm.so.6: version `GLIBC_2.29' not found (required by /data/home/bennshi/.arduino15/packages/ClangBuiltArduino/tools/cba-llvm/20.1.0-06032025/bin/../lib/LLVMgold.so)
clang++: error: avr-ld command failed with exit code 1 (use -v to see invocation)
Error: exit status 1

Used platform         Version Path
ClangBuiltArduino:avr 1.0.1   /data/home/bennshi/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1
Error during build: exit status 1

My CentOS-7 is too old.

@beakthoven
Copy link
Author

beakthoven commented Mar 29, 2025

  1. Is there a instruction about how to build your ClangBuiltArduino project? Or I simply follow the arduino CLI way ?

You can directly add the custom board package json and then install the custom board support to use it.
arduino-cli config add board_manager.additional_urls https://raw.githubusercontent.com/ClangBuiltArduino/BoardManagerFiles/refs/heads/main/package_clangbuiltarduino_index.json
arduino-cli core update-index
arduino-cli core install ClangBuiltArduino:avr
arduino-cli compile -b ClangBuiltArduino:avr:mega $dir

  1. which version of clang /llvm are you using? Is yours the newest main branch ?

I haven't updated the json since 20.1.0-rc2 build, I have a 20.1.0 build ready to be used. I will update the json and try once. I will also update toolchain scripts to add the ability to build off of main branch instead of release tarballs so that we can test bleeding edge builds.
Btw can you take a look at my llvm build configuration, maybe i disabled some crucial feature so I'm not getting those crashes? https://github.com/ClangBuiltArduino/tc-build/blob/main/llvm/build-llvm-stage2.sh#L63-L223

I still failed build the simplest blink program with the above two crashes.

I will try to use the latest 20.1.0 build or main branch build to see if i can hit those crashes.

Unfortunately I got the following error with your tools.

/data/home/bennshi/.arduino15/packages/ClangBuiltArduino/tools/cba-avr-bfd/2.44-14022025/glibc/bin/avr-ld: /data/home/bennshi/.arduino15/packages/ClangBuiltArduino/tools/cba-llvm/20.1.0-06032025/bin/../lib/LLVMgold.so: error loading plugin: /lib64/libm.so.6: version `GLIBC_2.29' not found (required by /data/home/bennshi/.arduino15/packages/ClangBuiltArduino/tools/cba-llvm/20.1.0-06032025/bin/../lib/LLVMgold.so)
clang++: error: avr-ld command failed with exit code 1 (use -v to see invocation)
Error: exit status 1

Used platform         Version Path
ClangBuiltArduino:avr 1.0.1   /data/home/bennshi/.arduino15/packages/ClangBuiltArduino/hardware/avr/1.0.1
Error during build: exit status 1

My CentOS-7 is too old.

Ah thats a bummer, i compiled them against debian bookworm libs just to be sure i get good compatibility but i think thats not enough.
The main toolchain is statically linked against musl libc so I dont think you would face any issues with it, however the bfd linker avr-ld and the LLVMgold.so are dynamically linked to support loading plugins so these must be compatible with your old libc.

I could try building the dynamically linked programs against older libs to have more compatibility. Or you could edit the platform.txt in the core lib dir to point to your tools instead so that you end up compiling my fork of the core lib with fixes but use your tools to compile it.

@beakthoven
Copy link
Author

This mess could be properly mitigated later once LLD supports AVR linkerscript syntax.

@benshi001
Copy link
Member

benshi001 commented Mar 29, 2025

could you please try to add option -mno-relax to clang in your config file? I find another bug triggered by this option, to avoid this bug -mno-relax is needed when calling avr-ld.

I am not sure if this option affect LTO result, maybe you can have a try, and let me known your result.

@benshi001
Copy link
Member

I have created another issue #133579 for that.

@benshi001
Copy link
Member

@beakthoven I really appreciate you, since I never thought llvm-avr can be such that fragile !!!

@beakthoven
Copy link
Author

beakthoven commented Mar 29, 2025

could you please try to add option -mno-relax to clang in your config file? I find another bug triggered by this option, to avoid this bug -mno-relax is needed when calling avr-ld.

I am not sure if this option affect LTO result, maybe you can have a try, and let me known your result.

Well it actually did fix the issue with LTO enabled! I tried it on both 20.1.1 release build as well as on a bleeding edge build with recent bug fixes, It seems to work fine on both builds when -mno-relax is passed during the final linking phase.

I also tried adding back the serial functions to see if #128536 got resolved as well, but that issue still persists. Regardless, this is a significant step forward. I'll keep an eye on your follow-up issue and see if I can help debug anything.

@beakthoven I really appreciate you, since I never thought llvm-avr can be such that fragile !!!

Yeah! This project has definitely been full of surprises, but at least we’re making progress. Appreciate your effort in digging into these issues!

beakthoven added a commit to ClangBuiltArduino/core_arduino_avr that referenced this issue Mar 29, 2025
As reported in [1] and verified in [2] adding -mno-relax seems to fix the LED blinking issues without needing to disable LTO for the affected files. Use this as a temporary workaround until the issue is fixed.

Remove the usage of clang-wrapper for c and cpp cmds since its not required anymore.

[1]: llvm/llvm-project#133579
[2]: llvm/llvm-project#127651 (comment)

Signed-off-by: Dakkshesh <[email protected]>
@benshi001
Copy link
Member

I closed this issue, and let us continue out discuss in #128536 and #133579. You are welcome to create any further issues if you encounter more clang/llvm bugs in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AVR LTO Link time optimization (regular/full LTO or ThinLTO) miscompilation
Projects
None yet
Development

No branches or pull requests

6 participants