LangRef says a freeze of poison or undef “returns an arbitrary, but fixed, value.” The middle-end uses this fact for some transformations and, for the most part, honors this rule.
The problem
On the other hand, backends do not generally respect freeze semantics. The SelectionDAG, FastISel, and GlobalISel selectors on AArch64, AMDGPU, RISC-V, and X86 all unconditionally lower freeze/ISD::FREEZE/G_FREEZE to a COPY. If the input to the freeze is undef or poison, what’s left is a COPY of IMPLICIT_DEF, which represents an undef value. ProcessImplicitDefs and the register coalescer then treat the COPY as undefined, and different uses can read different registers. The consequence is a long history of miscompiles including, but not limited to, these open issues:
Contributors have been aware of this issue since at least 20222020, and a few ideas have been suggested over the years. Here are a few:
Pick some value (say, 0) for freeze(poison) in ISel – Safe for this specific case, but still wrong for freeze of a register. Also unacceptable performance regressions in some cases.
Select freeze(poison) as INIT_UNDEF – I’ve been experimenting with this one. INIT_UNDEF is an existing generic pseudo that is a non-rematerializable definition. However, this still doesn’t cover the case of a poison value being produced later on. A freeze of a register could still be selected as a no-op, and we no longer have a way to stop the propagation of a poison value.
Add a generic FREEZE MI opcode – Best for handling undefined lanes created after ISel, but by far the most work to implement without regressions.
It is the opinion of myself and some other folks doing verification work that option 3 is the only real solution here; anything else would be a band-aid. Regardless, I’m happy to volunteer time to work on this. Please let me know your thoughts!
Thanks for working on this, I’ve been worried about this problem for a long time. I think the solutions are straightforward, but just require a lot of brute force work to implement 2 and 3. MIR just needs to have the mirror representation of everything in the IR to maintain all of the same properties, so we do need the full equivalents of undef, poison and freeze.
The de-facto state of things is that IMPLICIT_DEF is equivalent to poison and there is no undef, though both undef and poison select to it.
However, this still doesn’t cover the case of a poison value being produced later on.
What cases are these? I don’t expect many machine passes to introduce new poison values.
I didn’t know about INIT_UNDEF; I can’t say I’m happy about that name. It seems to be IMPLICIT_DEF, except it doesn’t undergo the rewrite to undef flags. I would like to see some renaming to make this system less confusing. Maybe we should rename IMPLICIT_DEF to POISON (relatedly, a PR to add GlobalISel’s G_POISON has been stuck for over a year)
INIT_UNDEF is something of a hack that exists to solve a pretty specific problem related to register constraints (and would probably be better solved directly by the register allocator…) Not sure it’s a good idea to base more things on that, and conflate different issues.
Introducing FREEZE does sound like the right thing to do in principle.
The de-facto state of things is that IMPLICIT_DEF is equivalent to poison and there is no undef, though both undef and poison select to it.
From what I’ve seen, many parts of the backend treat IMPLICIT_DEF as being able to observe multiple values, and it is still documented as equivalent to undef here:
However, this still doesn’t cover the case of a poison value being produced later on.
What cases are these? I don’t expect many machine passes to introduce new poison values.
I have no idea how prevalent these cases are. I’ve seen some where there are COPYs from 16-bit values to 32-bit registers, and some pass introduces a new IMPLICIT_DEF, although it’s arguable whether the undefined lanes already existed “implicitly.” Anyway, if we wanted to go for an approach that doesn’t involve creating FREEZE, we would have to explicitly prohibit backends from introducing new poisons, right?
Here’s a case of an IMPLICIT_DEF being created in AMDGPU: Compiler Explorer
Maybe a seperate conversation.
Right. As a first order of business here, should we rename IMPLICIT_DEF → POISON? What’s the procedure for something like this? I’d imagine a PR with a gigantic diff is unavoidable.
It would probably be a better idea to document how this works and get the full naming scheme down. In particular given the undef flag ↔ IMPLICIT_DEF link, just renaming the instruction on its own doesn’t make the situation that much less confusing
The basic problem with having poison post-isel is that we need to define poison propagation rules for every target-specific instruction. Which is a lot of work; we have a lot of instructions across all targets. It would be much simpler to say poison doesn’t exist, and lower literal “undef”/“poison” constants to zero or a frozen value. Almost all post-isel optimizations can’t take advantage of poison anyway.
On the other hand, if we’re going to have NoSWrap after isel, we need POISON/FREEZE. And if we have them, we should give them the proper names.
I’d much prefer it if poison did not exist at the MI level. Unfortunately, we do support essentially the whole set of poison-generating flags on MI. I think mostly these just get used by GlobalISel gMIR, but unfortunately there are other uses as well.
From a cursory look, the most popular use-case seems to be nsw for various comparison folding?
If the MI-level semantics of that flag is like freeze(poison), that would still be valid.
OTOH that would make any operation with such a flag potentially impure, so it cannot be duplicated as multiple executions may produce different results.
Maybe this would be better handled by an IR-level pass? As long as the pass respects general IR semantics, it’s in principle entirely fine for a pass to be “target-specific” in the sense that it is only enabled for certain targets.
What sort of documentation are you thinking? I would love to see something like GlobalISel’s docs for SelectionDAG. We could even partially generate it from comments in TargetOpcodes.def. Is this asking for too much given that the long-term goal is to deprecate SD in favor of GISel?
As Nikita mentioned, GlobalISel does indeed have things like nuw nsw G_ADD, and I’ve seen several miscompiles that are a result of mishandling these flags. Every GIsel backend (except SPIR-V) directly selects G_FREEZE as a copy, which is particularly sketchy because G_IMPLICIT_DEF is still treated like undef.
There may be a compromise here. Since freeze(poison) refines poison, say we pick freeze(poison) as the output of or disjoint at the MI level. Then if we do some duplication, could we just drop the flags on all copies of the instruction (including the original)?
Freezing the result of every instruction is a possible rule, I guess, but it leads to subtly different semantics from actual poison; that seems very likely to confuse developers. The classic example is trying to prove that a loop like for (int i = 0; i <= n; ++i) is finite.
Maybe we can do something like this: instructions can have a “PoisonPropagating” property. Any instruction with this property must not access memory or have unmodeled side-effects. Any input poison leads to output poison. Instructions which are not “PoisonPropagating” must not have any overflow/fast-math/etc. flags, and implicitly freeze any inputs. PoisonPropagating is opt-in.
So by default, you don’t get any poison. But backends can opt-in to poison propagation in specific situations where they need it.
Yes, it’s not ideal, so maybe we ought to call it something other than “POISON”. Also, it’ll be more optimal to preserve actual poison info as late as possible.
The idea of having an opt-in “PoisonPropagating” or “PropagatesPoison” property seems good to me.