llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[ValueTracking] Missed Constant Fold due to Imprecision in Knownbits::mul

Open 134ARG opened this issue 8 months ago • 5 comments

For such reduced IR pattern:

define i1 @struct5_odd_index_bit2(i32 %idx) {
  %i = or i32 %idx, 1
  %small = and i32 %i, 3          ; i%small is 0000 00?1
  %offset = mul nuw i32 %small, 5 ; %offset is 0000 ?1?1
  %bit2 = and i32 %offset, 4      ; %bit2 is 0000 0100
  %r = icmp ne i32 %bit2, 0 
  ret i1 %r
  ; Expected: ret i1 true, for bit 2 is always non-zero
}

which should be reduced to a constant return but opt fails to do so: https://godbolt.org/z/evfsbTW3h.

This is primarily due to the KnownBits::mul() currently only considers consecutive known bits and ignore disjoint known segments.

While a universal refinement of the mul transformer might introduce much compilation overhead, we could start with simple cases where one of the operand is a constant.

134ARG avatar Mar 31 '26 04:03 134ARG

I could patch for that. Please assign it to me.

134ARG avatar Mar 31 '26 04:03 134ARG

Hi @134ARG, just checking in on this. Are you still actively looking into a fix ? If you've gotten busy and you want to hand it off, I would love to take a swing on this. Let me know.

sai18022001 avatar Apr 23 '26 11:04 sai18022001

What is the real-world motivating context for this?

nikic avatar Apr 23 '26 11:04 nikic

Hi @134ARG, just checking in on this. Are you still actively looking into a fix ? If you've gotten busy and you want to hand it off, I would love to take a swing on this. Let me know.

@sai18022001 Hey, thanks for checking in. I haven’t had enough time to keep pushing on this, so I’d be happy to hand it off.

134ARG avatar Jun 01 '26 02:06 134ARG

What is the real-world motivating context for this?

After carefully evaluating the consequences of refining mul transformer, i decide to close this - indeed we currently have no obvious evidence that this refinement is profitable, and it could introduce quite a analysis overhead.

134ARG avatar Jun 18 '26 06:06 134ARG