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

Undef description of branching in LangRef seems ambiguous. #122532

Open
Chobbes opened this issue Jan 10, 2025 · 1 comment
Open

Undef description of branching in LangRef seems ambiguous. #122532

Chobbes opened this issue Jan 10, 2025 · 1 comment
Labels
documentation llvm Umbrella label for LLVM issues

Comments

@Chobbes
Copy link

Chobbes commented Jan 10, 2025

In the section on undefined values https://llvm.org/docs/LangRef.html#undefined-values

LangRef provides the following example:

  %A = select undef, %X, %Y
  %B = select undef, 42, %Y
  %C = select %X, %Y, undef
Safe:
  %A = %X     (or %Y)
  %B = 42     (or %Y)
  %C = %Y     (if %Y is provably not poison; unsafe otherwise)
Unsafe:
  %A = undef
  %B = undef
  %C = undef

And states:

This set of examples shows that undefined ‘select’ (and conditional branch) conditions can go either way, but they have to come from one of the two operands

Later on in this section, the following is stated:

Branching on an undefined value is undefined behavior. This explains optimizations that depend on branch conditions to construct predicates, such as Correlated Value Propagation and Global Value Numbering. In case of switch instruction, the branch condition should be frozen, otherwise it is undefined behavior.

The first quote seems to suggest that a conditional branch on undef is a non-deterministic jump. The second statement claims that it is in fact UB, unless the undef value has been frozen first.

My interpretation of what is meant is the following:

  1. select instructions don't count as branches, and selecting with an undef condition is allowed.
  2. Branching on an undef value is UB (i.e., if the value represents multiple values, branching on it is UB).

Under this interpretation the first quote isn't necessarily wrong (the program can do anything when UB is encountered, including going down one of the branches), but I assume that isn't the intention, and maybe this should be rephrased to be more clear --- perhaps we should just remove the "(and conditional branch)" part?

Would love to hear what other people think, especially if I'm misunderstanding something!

Thanks!

@nikic
Copy link
Contributor

nikic commented Jan 10, 2025

My interpretation of what is meant is the following:

  1. select instructions don't count as branches, and selecting with an undef condition is allowed.
  2. Branching on an undef value is UB (i.e., if the value represents multiple values, branching on it is UB).

This is correct.

perhaps we should just remove the "(and conditional branch)" part?

Yes, we should do that.

@EugeneZelenko EugeneZelenko added documentation llvm Umbrella label for LLVM issues and removed new issue labels Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation llvm Umbrella label for LLVM issues
Projects
None yet
Development

No branches or pull requests

4 participants