This is an archive of the discontinued LLVM Phabricator instance.

[VectorCombine] Insert addrspacecast when crossing address space boundaries
ClosedPublic

Authored by frasercrmck on Mar 16 2022, 3:30 AM.

Details

Summary

We can not bitcast pointers across different address spaces. This was
previously fixed in D89577 but then in D93229 an enhancement was added
which peeks further through the ponter operand, opening up the
possibility that address-space violations could be introduced.

Instead of bailing as the previous fix did, simply insert an
addrspacecast cast instruction.

Diff Detail

Event Timeline

frasercrmck created this revision.Mar 16 2022, 3:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2022, 3:30 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
frasercrmck requested review of this revision.Mar 16 2022, 3:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2022, 3:30 AM
tra added a comment.Mar 16 2022, 1:04 PM

It's unclear to me whether we need to bail in this situation as I've
done, or whether we can issue an addrspace cast, at least under certain
circumstances.

You could use TTI.isNoopAddrSpaceCast() to allow some other combinations.
E.g. if both AS can be cast to generic AS, then we may be allowed to carry on, after adding appropriate ASC to generic AS.

Does adding an offset of i32 %x bytes do the same thing to the pointer regardless of the address space?
Can't we just emit said missing ASC?

Does adding an offset of i32 %x bytes do the same thing to the pointer regardless of the address space?
Can't we just emit said missing ASC?

That was my initial thought. I don't see why we're forced into a bitcast when we have addrspacecast. But given the original fix kept us down the bitcast lines, I assumed there must be something I'm missing.

AFAICT, address spaces have no effect on GEPs. But then again, are non-integral pointer types a concern?

There may well be a question of performance, as @tra seems to imply, and that's why we don't issue addrspacecasts. Obviously if we find ourselves in this situation there's already some kind of cast going on. But it's possible that a load from one address space is much worse than a load from the original address space. isNoopAddrSpaceCast is an easy win in any case, as @tra points out.

Does adding an offset of i32 %x bytes do the same thing to the pointer regardless of the address space?
Can't we just emit said missing ASC?

That was my initial thought. I don't see why we're forced into a bitcast when we have addrspacecast. But given the original fix kept us down the bitcast lines, I assumed there must be something I'm missing.

AFAICT, address spaces have no effect on GEPs.

Then let's just emit said ASC :)

But then again, are non-integral pointer types a concern?

We don't do any integers<->pointers casts here, so i don't see why it would be?

There may well be a question of performance, as @tra seems to imply,

I don't follow. Performance in what sense?

and that's why we don't issue addrspacecasts. Obviously if we find ourselves in this situation there's already some kind of cast going on. But it's possible that a load from one address space is much worse than a load from the original address space. isNoopAddrSpaceCast is an easy win in any case, as @tra points out.

use addrspace cast, move test to common file

Then let's just emit said ASC :)

I've updated the patch to do this now. No impact on any tests other than the one I added.

We don't do any integers<->pointers casts here, so i don't see why it would be?

Yes I now see I misunderstood the semantics of non-integral pointer types. I thought they could be a different bitwidth than what they claimed to be which would have impacted GEP offsetting. But they're just about the bitwise representation.

I don't follow. Performance in what sense?

I'm not sure exactly what, just picking up on @tra's comment and previous fix (why we didn't just use addrspacecast before). But like I said, if we find ourselves with a different AS to the original, musn't we have an addrspace cast in the original IR, so we're not introducing a new one? Therefore I don't think the cost of addrspacecast is important, and we already take the cost of the scalar-vs-vector load into account.

lebedev.ri accepted this revision.Mar 17 2022, 3:22 AM

LG, thanks.

This revision is now accepted and ready to land.Mar 17 2022, 3:22 AM
frasercrmck retitled this revision from [VectorCombine] Avoid crossing address space boundaries to [VectorCombine] Insert addrspacecast when crossing address space boundarie.Mar 18 2022, 1:13 AM
frasercrmck edited the summary of this revision. (Show Details)
frasercrmck retitled this revision from [VectorCombine] Insert addrspacecast when crossing address space boundarie to [VectorCombine] Insert addrspacecast when crossing address space boundaries.
frasercrmck added a subscriber: arsenm.

update AMDGPU tests. @arsenm do these test changes look okay to you?

tra added a comment.Mar 18 2022, 11:52 AM

I don't follow. Performance in what sense?

I'm not sure exactly what, just picking up on @tra's comment and previous fix (why we didn't just use addrspacecast before). But like I said, if we find ourselves with a different AS to the original, musn't we have an addrspace cast in the original IR, so we're not introducing a new one? Therefore I don't think the cost of addrspacecast is important, and we already take the cost of the scalar-vs-vector load into account.

My comment was about relaxing the AS mismatch check and allowing different AS with no-cost ASC. With that check gone, my suggestion is now irrelevant.

fix AMDGPU test checks, which were updated with old opt binary