As of the version bump to LLVM 17, typed pointers within LLVM are not supported anymore [0] and will be removed as soon as possible.
To mirror these changes I’d like to propose changing the LLVM Dialect in MLIR to only support opaque pointers, which will allow a major cleanup of the LLVM Dialect and having it follow suite of LLVM IR.
The difficulty in doing this switch is in migration paths for downstream projects and the fact that MLIR essentially has many frontends (all the dialect lowerings!) which have to be switched.
Since there are some very important details in rewriting these lowerings I decided to write this RFC.
Steps required
The gist of changes required boil down to basically:
- add opaque pointer support to upstream conversion passes
- wait for downstream to switch their lowerings to opaque pointers and to make use of upstream conversion passes
- remove typed pointers from the LLVM dialect entirely
Modulo bugs, the LLVM Dialect already supports opaque pointers.
Conversion passes
The current issue, and why not many downstream projects have probably switched to opaque pointers yet is that as of writing this, upstream conversion passes (e.g. -finalize-memref-to-llvm
) do not yet support lowering to opaque pointers.
Using opaque pointers as opposed to typed pointers is currently an all or nothing setting. Either the whole IR has to use opaque pointers or typed pointers.
I therefore propose the following:
All conversion passes making use of pointer types get a new pass option called use-opaque-pointers
, defaulting to 0, which when enabled, will make the conversion pass emit LLVM Dialect IR using opaque pointers. LowerToLLVMOptions
also gets a new option useOpaquePointers
, which acts as a common interface for conversion patterns (and the LLVMTypeConverter
), to know whether it should emit opaque pointers.
In my current implementation LLVMTypeConveter
also offers a getter checking whether opaque pointers should be use, as well as the convenience method getPointer
, which will return a correct pointer type that is either opaque or not given the current setting.
Rationale:
I believe there has to be some amount of time that the conversion passes support both typed and opaque pointers. Downstream projects currently cannot do the switch, since the upstream conversions need to adopt opaque-pointers but at the same time, switching conversion passes to only allow opaque pointers would immediately break downstream projects.
Tests of conversion passes
This is the point I am most conflicted about and where I’d like to hear more opinions. The gist of the issue is how to both support opaque pointers and typed pointers.
I have currently thought of following ways we could handle the testsuite:
-
Radical approach: Switch the testsuite of a pass to opaque pointers, don’t bother testing typed pointers
Pros: Follows LLVM IR; Makes divergence of typed and opaque pointer tests impossible; Less changes
Cons: Loss of test coverage for typed pointers and generally unclear what to do about bugs found in typed pointer specific code; Prone to regress typed pointer support while it may still be needed. -
Duplicate: Copy any tests with pointers, rename the file to add the suffix
-typed-pointers
, and rewrite the main file to use opaque pointers.
Pros: Coverage of typed pointer support remains as is.
Cons: Possibility of the test files for typed pointers and opaque pointers to diverge and the need to test both for new lowerings being added, risking regression in the support of either otherwise.
One could also possibly adopt some more nuanced strategy than either of the above, but we will need some kind of policy.
Implementation Status
As a first step I have already implemented the changes proposed above with the “Duplicate” testsuite for the -finalize-memref-to-llvm
conversion pass, allowing MemRefs to be lowered to LLVM using opaque pointers. It also adds the initial infrastructure in LLVMCommon
for further conversion passes to be adapted.
I have written this RFC following the experience from writing that change.
You can find the review here to get a feeling for the changes required:
https://reviews.llvm.org/D143268
I believe that this is the conversion passes requiring the most amount of changes than any other dialects, so it should not get any worse than this.