[clang] [llvm] [AMDGPU] Match bitsin(typeof(x)) - ctpop(x) to s_bcnt0_i32 (PR #164847)

Patrick Simmons via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 17 14:50:25 PDT 2026


linuxrocks123 wrote:

@jayfoad I believe I can confirm worse code generation using this command as well: `opt -O2 -mtriple=amdgcn -mcpu=gfx900 -S < s_bcnt0.ll | llc -mtriple=amdgcn -mcpu=gfx900`

As is:
```
bcnt064_ctpop_multiple_uses:            ; @bcnt064_ctpop_multiple_uses
; %bb.0:
	s_bcnt1_i32_b64 s2, s[0:1]
	s_bcnt0_i32_b64 s4, s[0:1]
	s_cmp_lg_u64 s[0:1], -1
	s_mov_b32 s3, 0
	s_cselect_b32 s0, 1, 0
	s_mov_b32 s5, s3
	;;#ASMSTART
	; use s[2:3]
	;;#ASMEND
	;;#ASMSTART
	; use s[4:5]
	;;#ASMEND
	; return to shader part epilog
```

With patch:
```
bcnt064_ctpop_multiple_uses:            ; @bcnt064_ctpop_multiple_uses
; %bb.0:
	s_bcnt1_i32_b64 s2, s[0:1]
	s_sub_u32 s4, 64, s2
	s_subb_u32 s5, 0, 0
	s_cmp_lg_u64 s[0:1], -1
	s_cselect_b32 s0, 1, 0
	s_mov_b32 s3, 0
	;;#ASMSTART
	; use s[2:3]
	;;#ASMEND
	;;#ASMSTART
	; use s[4:5]
	;;#ASMEND
	; return to shader part epilog
```

I know it's the same number of instructions in this particular test, but it's clear the pattern isn't firing when it should.

We can't get rid of the pattern I have, because it's firing in cases where yours doesn't fire.  If you think we need both patterns, though, I'll add both.  Would you like me to?

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


More information about the cfe-commits mailing list