This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Canonicalize gep of addrspacecast
Changes PlannedPublic

Authored by nikic on Jan 2 2023, 7:34 AM.

Details

Reviewers
arsenm
spatel
Summary

With typed pointers, InstCombine will often convert gep of addrspacecast into addrspacecast of gep, see e.g. https://llvm.godbolt.org/z/fYa3oKvdh. This isn't done consistently, because it happens as a side-effect of other folds. The most common one is trivial index descaling (where no descaling actually happens, and only the addrspacecast/gep end up being swapped). These folds are disabled for opaque pointers, so we don't canonicalize in either direction there.

This adds canonicalization of gep(ac) to ac(gep). I've limited this to the case where index types match: While this is always legal, changing index types might introduced truncs/sexts, which might be non-profitable (test diffs were a wash).

After typed pointer support is dropped, we might want to consider inverting this canonicalization and going from ac(gep) to gep(ac) instead, which seems better to me. But for now I'm matching what typed pointers were (mostly) doing.

Diff Detail

Event Timeline

nikic created this revision.Jan 2 2023, 7:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 2 2023, 7:34 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
nikic requested review of this revision.Jan 2 2023, 7:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 2 2023, 7:34 AM
arsenm added inline comments.Jan 2 2023, 10:38 AM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
2473

We probably shouldn't be reassociating if either address space is non-integral. I think that was one of the fixes that never happened for the existing code

2473

But yes, moving the addrspacecast up makes more sense

nikic added inline comments.Jan 2 2023, 11:31 AM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
2473

We probably shouldn't be reassociating if either address space is non-integral.

Hm, why? I don't think we really consider GEP to operate on the integer representation?

But yes, moving the addrspacecast up makes more sense

I think in that case I'll probably just ignore this discrepancy for now (and update tests with current opaque pointer behavior) and then return to this once typed pointers are removed and we don't have to worry about opposing transforms.

arsenm added inline comments.Jan 2 2023, 11:40 AM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
2473

The non-integral GEP could have some special clamping or wrapping behavior in one address space and not the other

nikic added inline comments.Jan 2 2023, 11:55 AM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
2473

The non-integral GEP could have some special clamping or wrapping behavior in one address space and not the other

Hm, is that legal? That doesn't really sound compatible with how we deal with GEP offsets in BasicAA (and stripAndAccumulateConstantOffsets for that matter). We treat offset operations in different address spaces as interchangeable, apart from trunc/sext.

arsenm added inline comments.Jan 2 2023, 12:14 PM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
2473

Key word is nonintegral address spaces, which were only half implemented

nikic planned changes to this revision.Jan 4 2023, 1:49 AM