[wasm] Suboptimal wasm32 compilation. Also demonstrate regression compare to LLVM 19.1.0
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
LLVM IR source:
godbolt: https://godbolt.org/z/xq4eG655b
```llvm
define noundef i32 @utf8_seq_len(i8 noundef zeroext %0) {
%2 = xor i8 %0, -1
%3 = call i8 @llvm.ctlz.i8(i8 %2, i1 false)
%4 = icmp eq i8 %3, 0
%5 = zext i1 %4 to i32
%6 = sub i8 %3, 2
%7 = icmp ule i8 %6, 2
%8 = zext nneg i8 %3 to i32
%9 = select i1 %7, i32 %8, i32 %5
ret i32 %9
}
```
What I expected:
```wasm
local.get 0
i32.const 255
i32.xor
i32.const 24
i32.shl
i32.clz
local.tee 1
local.get 1
local.get 1
i32.eqz
local.get 1
i32.const 2
i32.sub
i32.const 3
i32.lt_u
i32.select
```
and what I actually get:
llc (trunc) wasm32 target:
```wasm
local.get 0
i32.const 24
i32.shl
i32.const -1
i32.xor
i32.clz
local.tee 1
local.get 1
i32.eqz
local.get 0
i32.const 255
i32.xor
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
i32.clz ;; extra i32.clz ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
i32.const -26
i32.add
i32.const 255
i32.and
i32.const 3
i32.lt_u
i32.select
```
llc (19.1.0) wasm32 target (no extra i32.clz, but still suboptimal)
```wasm
local.get 0
i32.const 255
i32.xor
i32.clz
local.tee 0
i32.const -24
i32.add
local.tee 1
i32.const 255
i32.and
local.get 1
i32.eqz
local.get 0
i32.const -26
i32.add
i32.const 255
i32.and
i32.const 3
i32.lt_u
i32.select
```
Contributor guide
Research direction
Start with the LLVM IR example and compile it through llc for the wasm32 target, using the Godbolt link and the shown LLVM 19.1.0 output as comparisons. Inspect the wasm32 code-generation path responsible for ctlz and the extra i32.clz, then verify that current output no longer contains the reported regression and is closer to the expected sequence.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- wasm
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100