llvm / llvm/llvm-project

[Xtensa] Cannot select AtomicLoadAdd -- missing atomic lowering for generic Xtensa target

Open
#190,208 2 comments 0 reactions 0 assignees View on GitHub
backend:Xtensa
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This report was isolated/reduced by Claude, but it's pretty basic. Just looks like a big old TODO?

## Summary

The upstream LLVM Xtensa backend fails with "Cannot select: AtomicLoadAdd" when compiling code that contains `atomicrmw add` instructions targeting the generic Xtensa CPU. Espressif's LLVM fork (esp-clang 20.1.1) handles this by lowering atomics to external `__atomic_*` libcall functions. The upstream backend has no such lowering.

The `--thread-model=single` flag does NOT prevent the crash, because `atomicrmw` is an explicit instruction in the IR that the backend must handle regardless of thread model.

## Reproducer

```llvm
; atomic_crash.ll
target datalayout = "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32"
target triple = "xtensa-unknown-none-elf"

@counter = global i32 0, align 4

define void @increment() #0 {
%1 = atomicrmw add ptr @counter, i32 1 seq_cst, align 4
ret void
}

attributes #0 = { "target-cpu"="generic" "target-features"="+density,+threadptr,+windowed,+mul16,+mul32,+mul32high,+div32,+sext,+nsa,+clamps,+minmax,+bool,+fp,+loop" }
```

```bash
# Upstream LLVM 21:
llc -mtriple=xtensa-none-elf -filetype=obj -o test.o atomic_crash.ll
# LLVM ERROR: Cannot select: i32,ch = AtomicLoadAdd<(load store seq_cst (s32))> ...

# With --thread-model=single (still crashes):
llc -mtriple=xtensa-none-elf --thread-model=single -filetype=obj -o test.o atomic_crash.ll
# Same error

# Espressif LLVM 20.1.1 (works -- lowers to __atomic_fetch_add_4 libcall):
esp-clang/bin/llc -mtriple=xtensa-none-elf -filetype=obj -o test.o atomic_crash.ll
# Success -- generates callx8 to __atomic_fetch_add_4
```

### Verified output

The IR was verified using Espressif's llc (LLVM 20.1.1) which compiles it successfully, and LDC 1.42.0 (LLVM 21.1.8) which crashes with the exact error above.

## Expected Behavior

The backend should lower `atomicrmw` operations to either:
1. Hardware atomic sequences using `s32c1i` (compare-and-swap) when the target has the appropriate Xtensa extension, or
2. External `__atomic_*` libcall functions (as Espressif's fork does), or
3. Plain non-atomic load/store when `--thread-model=single` is specified

Espressif's fork implements option 2, lowering all atomic operations to `__atomic_fetch_add_4`, `__atomic_compare_exchange_4`, etc. from libatomic/libgcc.

## Environment

- LLVM: 21.1.8 (stock upstream, via LDC 1.42.0) -- crashes
- Espressif LLVM 20.1.1 (esp-clang) -- works (libcall lowering)
- Target: xtensa-none-elf, generic CPU

Contributor guide

Open the contributing guide

Research direction

Start by running llc on the supplied atomic_crash.ll reproducer, with and without --thread-model=single, to confirm the Xtensa AtomicLoadAdd selection failure. Read the upstream Xtensa backend's atomic lowering entry points and compare the successful Espressif llc behavior. Done means the generic Xtensa target compiles the reproducer without crashing and uses one of the stated atomic lowering approaches.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.