This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Ensure isGuaranteedNotToBeUndefOrPoison scans CtxI's parent basic block if CtxI is given
Needs ReviewPublic

Authored by StephenFan on May 15 2023, 12:30 AM.

Details

Summary

As far as I known, if CtxI is given in isGuaranteedNotToBeUndefOrPoison,
it means that V is guaranteedNotToBeUndefOrPoison in CtxI's parent basic
block. However, in guaranteedNotToBeUndefOrPoison, we call
programUndefinedifUndefOrPoison that only scan's V's parent basic block
if V is instruction or entry block if V is argument. That will make
isGuaranteedNotToBePoisonOrUndef return incorrect result for the
following IR, and we call isGuaranteedNotToBePoisonOrUndef('%zext',
nullptr, '%x').

declare void @maythrow()
declare void @use.i32(i32)
define void @zext_sdiv_not_ok3_maybe_poison_denum(i32 noundef %nn, i16 %xx) {
entry:
  %n = and i32 %nn, 123
  %x = or i16 %xx, 1
  br label %loop
loop:
  call void @maythrow()
  %zext = zext i16 %x to i32
  %div = sdiv i32 %n, %zext
  call void @use.i32(i32 %div)
  br label %loop
}

Actually, %zext in block entry is not guaranteed not to be undef or
poison. But current isGuarnateedNotToBePoisonOrUndef calls
programUndefIfUndefOrPoison that scans loop block and it returns true
since there is a sdiv instruction that uses %zext as divisor.

And current isSafeToSpeculativeExecute has not called
isGuaranteedNotToBePoison, but it will happen if D149423 is landed.
And then if we call isSafeToSpeculativeExecute(%div`, %x)`, it will
return true, which is wrong.

Diff Detail

Event Timeline

StephenFan created this revision.May 15 2023, 12:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2023, 12:30 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
StephenFan requested review of this revision.May 15 2023, 12:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2023, 12:30 AM

I agree this looks to be a bug, but I'm no expert in this area.

nikic added a comment.Jun 13 2023, 6:23 AM

I've commented in more detail in https://reviews.llvm.org/D149423#4416984 on why this is not a complete solution.