This is an archive of the discontinued LLVM Phabricator instance.

[mlir][MemRef] Add required address space cast when lowering alloc to LLVM

Authored by zero9178 on Feb 5 2023, 6:49 AM.



alloc uses either malloc or a plugable allocation function for allocating the required memory. Both of these functions always return a llvm.ptr<i8>, aka a pointer in the default address space. When allocating for a memref in a different memory space however, no address space cast is created, leading to invalid LLVM IR being generated.

This is currently not caught by the verifier since the pointer to the memory is always bitcast which currently lacks a verifier disallowing address space casts. Translating to actual LLVM IR would cause the verifier to go off, since bitcast cannot translate from one address space to another:

This patch fixes that issue by generating an address space cast if the address space of the allocation function does not match the address space of the resulting memref.

Not sure whether this is actually a real life problem. I found this issue while converting the pass to using opaque pointers which gets rid of all the bitcasts and hence caused type errors without the address space cast.

Diff Detail

Event Timeline

zero9178 created this revision.Feb 5 2023, 6:49 AM
zero9178 requested review of this revision.Feb 5 2023, 6:49 AM
Herald added a project: Restricted Project. · View Herald Transcript
qcolombet accepted this revision.Feb 6 2023, 2:43 AM

Nice catch!


This revision is now accepted and ready to land.Feb 6 2023, 2:43 AM