[all-commits] [llvm/llvm-project] 1e0899: [libc][baremetal][BigInt] fix codegen issue for UI...

Schrodinger ZHU Yifan via All-commits all-commits at lists.llvm.org
Mon Sep 14 13:27:50 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1e089918fca00a683921f0f45b32cce8d1f9026b
      https://github.com/llvm/llvm-project/commit/1e089918fca00a683921f0f45b32cce8d1f9026b
  Author: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
  Date:   2026-09-14 (Mon, 14 Sep 2026)

  Changed paths:
    M libc/src/__support/big_int.h
    M libc/src/__support/math_extras.h

  Log Message:
  -----------
  [libc][baremetal][BigInt] fix codegen issue for UInt128 (#220389)

`BigInt::operator*` always computes the full 2N-word product (`ful_mul`,
i.e. `multiword::multiply_with_carry`) and then truncates. In order to
scalarize the process to generate efficient code, the loop needs to be
fully unrolled, which fails on 32-bit targets for int128 due to LLVM's
default threshold. This patch adjust the hint to allow int128 to be
unrolled while gating larger integers away.


### 64-bit targets: roughly okay

aarch64 is byte-identical before/after; x86-64 gets one `mov` shorter.
After the patch, 128×128→128 (lo/hi in memory):

```asm
; aarch64                          ; x86-64
ldp   x8, x12, [x0]                movq   (%rdi), %r9
ldp   x9, x10, [x1]                movq   (%rsi), %r8
mul   x10, x10, x8                 movq   %r8, %rax
mul   x11, x9, x8                  mulq   %r9
umulh x8, x9, x8                   imulq  8(%rsi), %r9
madd  x9, x9, x12, x10             imulq  8(%rdi), %r8
add   x8, x9, x8                   addq   %r9, %r8
stp   x11, x8, [x2]                addq   %rdx, %r8
ret                                ...
```

### 32-bit targets (e.g. cortex-m85): badly bloated

A 64-byte stack frame, carry selection through IT blocks, and a call
into an outlined `ful_mul`. Kernel is the folded multiply (`lo ^ hi` of
the `UInt<128>` product):

```asm
; before                           ; after
sub    sp, #64                     umull  r12, lr, r2, r0
mov.w  r12, #0                     umull  r2, r4, r2, r1
strd   r12, r12, [sp, #24]         umull  r0, r5, r3, r0
orrs.w lr, r0, r1                  umaal  r4, r5, r3, r1
it     ne                          adds.w r1, lr, r2
strdne r0, r1, [sp, #16]           adcs   r2, r4, #0
... (5 more stores)                adc    r3, r5, #0
bl     BigInt<128>::ful_mul        adds   r0, r0, r1
ldm    r3, {r0, r1, r2, r3}        adcs   r2, r2, #0
eors   r1, r3                      adc    r1, r3, #0
eors   r0, r2                      eors   r1, r0
add    sp, #64                     eor.w  r0, r2, r12
```

llvm-mca (cortex-m85): **114 → 11 cycles** per folded multiply.

### Benchmarks

| environment | before | after | speedup |
|---|---|---|---|
| EK-RA8M2 (Cortex-M85 r1p1, DWT cycle counter, I-cache on) | 250
cyc/mul | 20 cyc/mul | 12.5× |
| wasm32 under wasmer 7.3, `-U__SIZEOF_INT128__` (see notes) | 0.70 s /
100M muls | 0.28 s | 2.5× |

Bench kernel (dependent chain, identical results before/after):

```cpp
using B = LIBC_NAMESPACE::BigInt<128, false, uint64_t>;
B x = (B(a) << 64) | B(~a);
B y = (B(b) << 64) | B(~b);
for (int i = 0; i < n; i++) {
  x = x * y;
  y.val[0] ^= x.val[1];
}
return x.val[0] ^ x.val[1];
```

### Carry builtin dispatch fix

Additionally, fix a typo in the `add_with_carry` / `sub_with_borrow`
builtin dispatch: the `#if`/`#elif` chain over `__has_builtin`.

### Notes

1. Another workaround for such targets is to force-enable native 128-bit
integers (clang's `-fforce-enable-int128`)
2. The wasm number above is benched with 128-bit integers forcibly
disabled (`-U__SIZEOF_INT128__`), just to check its effect.

---

Guided and reviewed by human

Co-Authored-By: Claude Fable 5 <noreply at anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply at anthropic.com>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list