RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

WIP, POC: Add LLVM support for AVR

Open maribu opened this issue 4 years ago • 8 comments

Contribution description

This PR is working towards adding LLVM support for the AVR platform.

WARNING: This is work in progress and not in a shape in which it makes sense to start a code review.

I needed to add some workarounds for features of the avrlibc that do not compile with clang. One big pain point is that clang verifies constraints of inline assembly before eliminating dead branches, while GCC does it the other way round. E.g. something like

static inline __attribute__((always_inline)) uint8_t get_foo(void)
{
    uint8_t dest;
    if (FOO < 64) {
         __asm__(
            "in r0,%[foo] \r\n"
            : [dest] "=r"(dest)
            : [foo] "I"(foo)
        );
    }
    else {
        dest = *_SFR_MEM_ADDR(FOO);
    }
    return dest;
}

will fail to compile for FOO >= 64 with LLVM, despite the inline assembly never being executed in that case. The header in <avr/wdt.h> uses this a lot, so I provided versions of wdt_enable() etc. inline - I'm not sure if those will work on al AT(X)mega MCUs supported by RIOT, though.

Another issue with LLVM is that commonly used helpers like __tmp_reg__, __SREG__, __SP_L__, __SP_H__ are not available. For now, I replaced them with magic numbers / register names.

Testing procedure

$ make BOARD=atmega328p TOOLCHAIN=llvm -C examples/hello-world compiles and links, but cannot be flashed.

Issues/PRs references

Depends on:

  • [ ] https://github.com/RIOT-OS/RIOT/pull/16919
  • [ ] https://github.com/RIOT-OS/RIOT/pull/16920
  • [ ] https://github.com/RIOT-OS/RIOT/pull/16790

maribu avatar Sep 30 '21 15:09 maribu

@chrysn: Since you seemed to be interested in using LLVM with AVR: I dumped my current state of work here. It does at least compile and link finally, but it cannot be flashed for now...

maribu avatar Sep 30 '21 15:09 maribu

Thanks, and I appreciate any WIP PRs as they make sure that it's findable -- but to clarify: I have no interest in AVR itself (last project had on it was >10a ago, not looking back), it was just that #16919 means that I can avoid the mess the C2Rust transpiler makes of C11 atomics by telling it to go through the atomic utils instead.

(Staying subscribed as in general I like having LLVM usable more widely -- it opens up cross-language-LTO opportunities.)

chrysn avatar Sep 30 '21 15:09 chrysn

This one needs a rebase

benpicco avatar Feb 04 '22 15:02 benpicco

Indeed :) But more importantly, it also depends on some infrastructure work. First, https://github.com/llvm/llvm-project/issues/51557 needs to be resolved. Afterwards, the avrlibc headers need to be fixed to be compatible with clang.

Since the avrlibc was recently resurrected, this seems to be quite feasible actually :)

maribu avatar Feb 04 '22 16:02 maribu

LLVM14 will contain support for the register aliases and the AVR backend has received quite some attention. Also avr-libc became active maintenance again, so I guess I can just aim for fixing things upstream first.

maribu avatar Mar 30 '22 11:03 maribu

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

stale[bot] avatar Nov 02 '22 02:11 stale[bot]

@maribu Still working on this?

Teufelchen1 avatar Jan 30 '24 17:01 Teufelchen1

I think some work on LLVMs AVR support has been merged since the last time I looked. Maybe now is a good time to pick this up again.

There are some bugs in avrlibc that GCC doesn't trigger, that can cause a headache. Upstream is pretty much dead for avrlibc.

maribu avatar Jan 30 '24 17:01 maribu

Reminder: Soft-freeze is on the 25th.

Teufelchen1 avatar Mar 18 '24 11:03 Teufelchen1

This does actually compile now on both LLVM and GCC. There are still some rough corners, but this may be on track to get in prior to the soft feature freeze.

maribu avatar Mar 19 '24 12:03 maribu

WARNING: This is work in progress and not in a shape in which it makes sense to start a code review.

Can we do reviewing now?

Teufelchen1 avatar Mar 19 '24 13:03 Teufelchen1

Murdock results

:x: FAILED

470564eba39b643b2adc49c2fceb13346bc6299a cpu/avr8_common: Add support for LLVM

Success Failures Total Runtime
1 0 9019 01m:22s

Artifacts

riot-ci avatar Mar 19 '24 13:03 riot-ci

Looks like there are still compilation issues with GCC (at least the old version) that need fixing and static tests. I'm also not happy with the quality yet.

But I split out the commit that I think is ready, though: https://github.com/RIOT-OS/RIOT/pull/20483

maribu avatar Mar 19 '24 13:03 maribu