Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isGuaranteedNotToBeUndefOrPoison is taking too long to execute #130110

Closed
dianqk opened this issue Mar 6, 2025 · 2 comments
Closed

isGuaranteedNotToBeUndefOrPoison is taking too long to execute #130110

dianqk opened this issue Mar 6, 2025 · 2 comments

Comments

@dianqk
Copy link
Member

dianqk commented Mar 6, 2025

Using the script can reproduce this:

#!/usr/bin/env bash

num_switches="$1"

cat <<EOF
define i64 @foo(i64 noundef %i) {
start:
  br label %loop

loop:
EOF
echo -n "  %nonpoison = phi i64 [ 0, %start ]"

for i in $(seq 0 $((num_switches - 1))); do
  echo -n ", [ %nonpoison, %bb$i ]"
done

echo ", [ %nonpoison, %bb ], [ %i, %back_to_loop ]"

cat <<EOF
  switch i64 %i, label %exit0 [
    i64 -1, label %exit1
    i64 2, label %back_to_loop
    i64 0, label %bb
  ]

exit0:
  br label %exit1

exit1:
  %r = phi i64 [ %nonpoison, %loop ], [ undef, %exit0 ]
  ret i64 %r

back_to_loop:
  br label %loop

bb:
  switch i64 %nonpoison, label %loop [
EOF

for i in $(seq 0 $((num_switches - 1))); do
  echo "    i64 $i, label %bb$i"
done
cat <<EOF
  ]

EOF

for i in $(seq 0 $((num_switches - 1))); do
  echo "bb$i:"
  echo "  br label %loop"
done

echo "}"
./gen.sh 50 | opt -passes=instsimplify --disable-output

This is primarily due to a recursive check we're performing on a phi node that contains itself:
%nonpoison = phi i64 [ 0, %start ], [ %nonpoison, %bb0 ], [ %nonpoison, %bb1 ], [ %nonpoison, %bb2 ], [ %nonpoison, %bb3 ], ..., [ %nonpoison, %bbn ], [ %nonpoison, %bb ], [ %i, %back_to_loop ].

The following code explains why returning true after exceeding the recursion depth:

// If V is used as a branch condition before reaching CtxI, V cannot be
// undef or poison.
// br V, BB1, BB2
// BB1:
// CtxI ; V cannot be undef or poison here

@dianqk dianqk self-assigned this Mar 6, 2025
dianqk added a commit that referenced this issue Mar 6, 2025
…`isGuaranteedNotToBeUndefOrPoison` (#130111)

Fixes (keep it open) #130110.

If the incoming value is PHI itself, we can skip this. If we can
guarantee that the other incoming values are neither undef nor poison,
then we can also guarantee that the value isn't either. If we cannot
guarantee that, it makes no sense in calculating it.
@nikic nikic added this to the LLVM 20.X Release milestone Mar 7, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Mar 7, 2025
@nikic
Copy link
Contributor

nikic commented Mar 9, 2025

/cherry-pick 462eb7e

@llvmbot llvmbot closed this as completed Mar 9, 2025
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in LLVM Release Status Mar 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 9, 2025

/pull-request #130474

swift-ci pushed a commit to swiftlang/llvm-project that referenced this issue Mar 12, 2025
…`isGuaranteedNotToBeUndefOrPoison` (llvm#130111)

Fixes (keep it open) llvm#130110.

If the incoming value is PHI itself, we can skip this. If we can
guarantee that the other incoming values are neither undef nor poison,
then we can also guarantee that the value isn't either. If we cannot
guarantee that, it makes no sense in calculating it.

(cherry picked from commit 462eb7e)
jph-13 pushed a commit to jph-13/llvm-project that referenced this issue Mar 21, 2025
…`isGuaranteedNotToBeUndefOrPoison` (llvm#130111)

Fixes (keep it open) llvm#130110.

If the incoming value is PHI itself, we can skip this. If we can
guarantee that the other incoming values are neither undef nor poison,
then we can also guarantee that the value isn't either. If we cannot
guarantee that, it makes no sense in calculating it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants