[RFC] : LLVM IR should allow bitcast between address spaces with the same size

[AMD Official Use Only]

TL;DR

There are targets, where address spaces are separate, that is the same value addresses different entities in different spaces. Size of the pointers in these spaces can be the same. Cast between such pointers is invalid operation.

[AMD Official Use Only]

This bitcast is intended to be used by transform passes when its been decided that the resultant operation is no-op( Like in GVN case of inserting ptrtoint/inttoptr ).
When reinterpretation of bits is not supported by target, the resultant value is poison.

Regards,
Chaitanya.

This will depend on the target’s own interpretation of what each address space is meant to be.

Each optimisation that uses this will have to ask the target info classes for the similarity (for some definition of) between two address spaces and then checking if the cast is actually a no-op or not.

As Serge said, some targets use address spaces for things that are completely segregated even if they have the same data layout (ex. [1]). Bit-casting on those cases is an invalid operation.

As a result, the infrastructure that allows this must be extremely conservative (ie. return false on the base class) and only return true after enough checks are done by the target library, and then it’s up to that target to allow/deny bit-casts between address spaces (which at that point, don’t even need to have the same data layout).

In a nutshell, I don’t think only looking at the data layout will be safe. You need to dig deeper and make that a target decision, and allow passes to completely ignore this specific pattern (ie. avoid searching the whole IR for it) if there’s no chance it will ever be useful. This makes sure targets that don’t support that don’t pay the penalty of traversing the IR for no benefit, because the response will always be false.

Makes sense?

cheers,
–renato

[1] https://www.kernel.org/doc/html/latest/x86/x86_64/fsgs.html

Strong -1 here. This seems like an abuse of bitcast to avoid nailing down interfaces and semantics for restricted address space casts. IMO, we should not accept the proposed IR change and should instead work on standardizing APIs/semantics for restricted addrspacecasts.

Philip

This bitcast is intended to be used by transform passes when its been decided that the resultant operation is no-op( Like in GVN case of inserting ptrtoint/inttoptr ).

When reinterpretation of bits is not supported by target, the resultant value is poison.

This will depend on the target’s own interpretation of what each address space is meant to be.

No, this change doesn’t really have to do with the address spaces at all. It’s fixing the representation of a no-op cast so the optimizer does not not have to introduce ptrtoint. It isn’t intended as a semantic change for the interpretation of address spaces or address space casts.

Each optimisation that uses this will have to ask the target info classes for the similarity (for some definition of) between two address spaces and then checking if the cast is actually a no-op or not.

The point here is this optimization does not care about whether a proper address space cast is a no-op or not. The original code was not performing an address space cast, it was doing type punning and reinterpreting some bits as a different address space.

As Serge said, some targets use address spaces for things that are completely segregated even if they have the same data layout (ex. [1]). Bit-casting on those cases is an invalid operation.

This is true, but this isn’t directly related to this change. If the target does not have no-op conversions between a given pair of address spaces, the original code was broken to begin with. This change does not allow introducing new illegal address space conversions that were not already present in the original program. It’s garbage in, garbage out. If the target can’t really reinterpret these pointers, it would be UB on access.

As a result, the infrastructure that allows this must be extremely conservative (ie. return false on the base class) and only return true after enough checks are done by the target library, and then it’s up to that target to allow/deny bit-casts between address spaces (which at that point, don’t even need to have the same data layout).

This would be introducing target dependence on core IR instruction semantics, which would be bad. The transformation this intends to fix would not be improved by target information. This pointer-reinterpret-to-cast transformation is and should be done unconditionally. If we were to introduce target information here, it would look something like if (isNoopCast()) { doNoOpAddrSpaceCast } else { doPtrToIntIntToPtr}. This doesn’t avoid the need to introduce ptrtoint, it’s just given some targets better IR some of the time.

In a nutshell, I don’t think only looking at the data layout will be safe. You need to dig deeper and make that a target decision, and allow passes to completely ignore this specific pattern (ie. avoid searching the whole IR for it) if there’s no chance it will ever be useful. This makes sure targets that don’t support that don’t pay the penalty of traversing the IR for no benefit, because the response will always be false.

To reiterate, there is no target specific pattern to search for here. There is a general type punning pattern and we need to introduce new IR that represents the same type punning, only using IR values instead of memory. Currently the only way to represent this is with an inttptr/ptrtoint pair, which everyone agrees the optimizers should never introduce.

-Matt

I think you are misunderstanding the point of the change. The motivating example is specifically not an address space cast. The reason to do this is not to improve the optimization of no-op address space casts, although that is a potential side effect. The point is to give the IR a way to represent a cast we know is a no-op cast to represent type punning in the original program without resorting to ptrtoint/inttoptr. This is to avoid the pessimization of ptrtoint, not to make no-op addrspacecast optimizations better.

-Matt

My counter proposal would be: use an addrspacecast for the cast. Have GVN insert an addrspacecast instead of ptrtoint/inttoptr.

From GVNs perspective, type punning *implies* that the resulting addrspacecast must be mustalias. It doesn't have to explicitly query anything to prove that. One thing to highlight: Two pointers in different addrspaces can be mustalias *without* sharing the same bit pattern. As a result, the GVN case implies mustalias, but *not* pointer equality.

(I forget where we stand on whether addrspacecast is allowed to fault. If we allow faults, we need a semantic restriction on introducing the addrspacecast, but that's simply a target property check. I vaguely think we don't allow addrspacecast to fault, and thus this aside is irrelevant.)

For the nop case (e.g. where the bits are the same), add a TTI hook, and teach ComputeKnownBits and other analyzes to selectively look back through them.

Philip

Ah, I see.

So IIUC, adding the casts in the first place would have to know about target address spaces, but this specific change only allows eliding the inttoptr cast in the specific case where the address spaces have the same data layout?

Assuming the initial cast was legal to begin with, this looks sensible to me.

I was worried this would allow (encourage?) front-ends and other passes to add an address space cast where none (could have) existed in the first place.

Sorry for the noise.

–renato

This still forces adding more target information into places where it does not belong, Additionally, this is still no help if the given pair of address spaces is not a no-op. We would still have to fallback to the ptrtoint/inttoptr pair, so there’s still a semantic hole in the IR that requires introducing them

-Matt

This is true, but this isn’t directly related to this change. If the target does not have no-op conversions between a given pair of address spaces, the original code was broken to begin with. This change does not allow introducing new illegal address space conversions that were not already present in the original program. It’s garbage in, garbage out. If the target can’t really reinterpret these pointers, it would be UB on access.

Ah, I see.

So IIUC, adding the casts in the first place would have to know about target address spaces, but this specific change only allows eliding the inttoptr cast in the specific case where the address spaces have the same data layout?

Assuming the initial cast was legal to begin with, this looks sensible to me.

The target’s notion of address space casts doesn’t apply here. The user decided to reinterpret the pointer in a different address space, which may or may not be valid to use on the target.

I was worried this would allow (encourage?) front-ends and other passes to add an address space cast where none (could have) existed in the first place.

No, we’re converting from an in-memory representation of reinterpreting bits to one in values. Frontends still need to have some contextual knowledge of what the language and target expects out of address spaces.

-Matt

[AMD Official Use Only]

Re-iterating the proposed change and clarifying the concerns raised :

Proposed change:

  • Allow no-op bitcast between pointers of same size but from different addrspaces.

Clarifying the concerns:

  • No-op reinterprent of bits between addrspaces is already done in the IR like introducing ptrtoint/inttoptr(ex. GVN).
    Main motivation for this change is to give the IR a way to represent a cast we know is a no-op without resorting to ptrtoint/inttoptr.

  • With this change we do not want to introduce new illegal address space conversions that were not already present in the original program.
    The original code was not performing an address space cast, it was doing type punning and reinterpreting some bits as a different address space.
    It was broken to begin with if the target didn’t allow no-op conversions between given pair of address spaces. We are not intending to change that.
    Since the IR transform like in GVN case, did a no-op reinterpretation of bits between addrspaces, We are just trying to optimise it by using a no-op bitcast.

  • Since the original IR already did this reinterpret of bits without querying the target, the change we propose will also not query the target.
    This pointer-reinterpret-to-cast transformation is and should be done unconditionally. Querying TTI to check for no-op would introduce target dependence on Core IR instruction semantics.
    We used DataLayout to match the pointer sizes. This check is sufficient to allow the no-op bitcast.

  • Also, there is no target specific pattern to search for here. Irrespective of the target, we need to introduce new IR that represents the same no-op reinterpret of bits between addrspaces.
    All the targets are intended to benefit from this change.

Regards,
Chaitanya