[InferAddressSpaces] Do not rewrite index operands of a constant GEP (#229491)
InferAddressSpaces rewrites flat (generic) pointers into a specific
address space once it can infer one. For constant expressions it does
this in cloneConstantExprWithNewAddressSpace(), which clones the
expression with the pointer operand moved into the inferred address
space.
For a constant getelementptr, that function used to recurse into every
operand, including the indices. Only operand 0, the base pointer, has to
live in the inferred address space. An index is an integer, and it can
be built from a pointer in a different address space, for example
ptrtoint (ptr addrspacecast (ptr addrspace(1) @g to ptr) to i64).
Rewriting it for the base's address space is wrong. In an assertions
build it hits the AddrSpaceCast assert, which expects the cast's source
to already be in the inferred address space:
assert(CE->getOperand(0)->getType()->getPointerAddressSpace() ==
NewAddrSpace)
The function is now a switch over the constant expression's opcode, with
cases for AddrSpaceCast, BitCast, IntToPtr and GetElementPtr. The old
loop only ever served GEPs. The GEP case clones only the pointer operand
and keeps the indices unchanged. The base is not always cloned by the
time its GEP is, because the worklist can finish a user before its
operand. The case therefore clones the base on demand if it isn't
already in ValueWithNewAddrSpace.
---------
Co-authored-by: mmalavika <mmalavika@nvidia.com>