[llvm-branch-commits] [llvm] [BPF] expand cttz, ctlz for i32, i64 (PR #73668)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jan 29 15:01:34 PST 2024


eddyz87 wrote:

> @eddyz87 Could you please take a look? This has been stalled for a while :)

Hello, I tried this with simple C test:

```c
unsigned int test(unsigned int v) {
  return __builtin_ctz(v);
  //return __builtin_clz(v);
}
```

<details>
<summary>
The <code>clz</code> part compiles fine, but when <code>ctz</code> is used I still get an assertion, however a different one:
</summary>

```
$ clang  --target=bpf -S -O2 test-clz.c -o -
...
LLVM ERROR: Cannot select: t15: i64 = ConstantPool<[32 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09"> 0
In function: test
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: llc -debug-only=isel --asm-show-inst -mtriple=bpf -mcpu=v3 -filetype=obj -o - test-clz.ll
1.	Running pass 'Function Pass Manager' on module 'test-clz.ll'.
2.	Running pass 'BPF DAG->DAG Pattern Instruction Selection' on function '@test'
 #0 0x000055d4977b1db8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/eddy/work/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x000055d4977afeb0 llvm::sys::RunSignalHandlers() /home/eddy/work/llvm-project/llvm/lib/Support/Signals.cpp:106:18
 #2 0x000055d4977b2588 SignalHandler(int) /home/eddy/work/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00007fdbaa03f190 __restore_rt (/lib64/libc.so.6+0x3f190)
 #4 0x00007fdbaa091dec __pthread_kill_implementation (/lib64/libc.so.6+0x91dec)
 #5 0x00007fdbaa03f0c6 gsignal (/lib64/libc.so.6+0x3f0c6)
 #6 0x00007fdbaa0268d7 abort (/lib64/libc.so.6+0x268d7)
 #7 0x000055d497735145 llvm::report_fatal_error(llvm::Twine const&, bool) /home/eddy/work/llvm-project/llvm/lib/Support/ErrorHandling.cpp:125:5
 #8 0x000055d4975f4b6d llvm::SDNode::getValueType(unsigned int) const /home/eddy/work/llvm-project/llvm/include/llvm/CodeGen/SelectionDAGNodes.h:1007:5
 #9 0x000055d4975f4b6d llvm::SDValue::getValueType() const /home/eddy/work/llvm-project/llvm/include/llvm/CodeGen/SelectionDAGNodes.h:1162:16
#10 0x000055d4975f4b6d llvm::SelectionDAGISel::CannotYetSelect(llvm::SDNode*) /home/eddy/work/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:4232:43
```
</details>

<details>
<summary>
Looking at debug info from llc it looks like <code>cttz_zero_undef</code> is expanded using some kind of a lookup table:
</summary>

```
$ llc -debug-only=isel --asm-show-inst -mtriple=bpf -mcpu=v3 -filetype=asm -o - test-clz.ll
...
Type-legalized selection DAG: %bb.0 'test:entry'
SelectionDAG has 7 nodes:
  t0: ch,glue = EntryToken
      t2: i32,ch = CopyFromReg t0, Register:i32 %0
    t3: i32 = cttz_zero_undef t2
  t5: ch,glue = CopyToReg t0, Register:i32 $w0, t3
  t6: ch = BPFISD::RET_GLUE t5, Register:i32 $w0, t5:1



Legalized selection DAG: %bb.0 'test:entry'
SelectionDAG has 18 nodes:
  t0: ch,glue = EntryToken
  t2: i32,ch = CopyFromReg t0, Register:i32 %0
                t8: i32 = sub Constant:i32<0>, t2
              t9: i32 = and t2, t8
            t11: i32 = mul t9, Constant:i32<125613361>
          t13: i32 = srl t11, Constant:i32<27>
        t14: i64 = sign_extend t13
      t16: i64 = add ConstantPool:i64<[32 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09"> 0, t14
    t18: i32,ch = load<(load (s8) from constant-pool), zext from i8> t0, t16, undef:i64
  t5: ch,glue = CopyToReg t0, Register:i32 $w0, t18
  t6: ch = BPFISD::RET_GLUE t5, Register:i32 $w0, t5:1
```
</details>

If there is no way to convince lowering to use some other strategy, and you don't want to spend time on implementing translation for `ConstantPool`, I think it would be fine to leave `ctz` as-is, or just adjust error reporting, so that it clearly says that `ctz` is not supported w/o showing a stack-trace. Wdyt?

https://github.com/llvm/llvm-project/pull/73668


More information about the llvm-branch-commits mailing list