llvmlite icon indicating copy to clipboard operation
llvmlite copied to clipboard

Add support for opaque pointers

Open rj-jesus opened this issue 1 year ago • 6 comments

This patch supports the transition of llvmlite from typed to opaque pointers, making the latter the default.

It also includes the following significant changes:

  • Adds the option to fallback to typed pointers; in particular, if LLVMLITE_DISABLE_OPAQUE_POINTERS=1, typed pointers are used.
  • Decouples pointee data from llvmlite pointers, moving it to a new class _TypedPointerType; objects of this class are created automatically from PointerType's when pointee types are known in a way that's transparent to users.
  • Adds explicit types to certain instructions (e.g., loads, gep, etc.) to enable phasing out pointee types on a case-by-case basis.

rj-jesus avatar Jun 25 '24 14:06 rj-jesus

I note that this works with LLVM 15 but not 14. Given the imminent move to LLVM 15, I think it's not worth fixing for 14.

gmarkall avatar Jun 26 '24 09:06 gmarkall

Hi @gmarkall thanks, sounds good! I'll push a fix for the flake8 tests failing soon as well.

rj-jesus avatar Jun 26 '24 10:06 rj-jesus

@sklam I believe this affects #1051. Do you have any thoughts on the changes in llvmlite/ir/types.py to PointerType to accommodatefrom_llvm on opaque pointers?

rj-jesus avatar Jun 26 '24 10:06 rj-jesus

Note from triage discussion - for function types, round tripping the type of arguments should still be able to work (correct me if this is wrong, @sklam)

gmarkall avatar Jul 02 '24 14:07 gmarkall

@sklam I believe this affects #1051. Do you have any thoughts on the changes in llvmlite/ir/types.py to PointerType to accommodatefrom_llvm on opaque pointers?

Looks good. The usecase is solely for getting function type back into a ir.Type and all of that is still working as expected.

I happen to notice that on LLVM14 parse_assembly do not like the opaque pointer despite it is enabled. Is that a LLVM bug?

% python -m unittest  llvmlite.tests.test_binding.TestTypeRef.test_typeref_as_ir_for_wrappers_of_cpp_vector_struct
<string>:5:55: warning: ptr type is only supported in -opaque-pointers mode
declare void @"_Z3foo8Vector2DPS_"(<2 x float> %".1", ptr %".2")
                                                      ^
E
======================================================================
ERROR: test_typeref_as_ir_for_wrappers_of_cpp_vector_struct (llvmlite.tests.test_binding.TestTypeRef)
Exercise extracting C++ struct types that are passed as vectors.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/llvmlite/tests/test_binding.py", line 2091, in test_typeref_as_ir_for_wrappers_of_cpp_vector_struct
    self._check_typeref_as_ir_for_wrappers(
  File "/path/to/llvmlite/tests/test_binding.py", line 2062, in _check_typeref_as_ir_for_wrappers
    new_mod = llvm.parse_assembly(str(wrapper_mod))
  File "/path/to/llvmlite/binding/module.py", line 25, in parse_assembly
    raise RuntimeError("LLVM IR parsing error\n{0}".format(errmsg))
RuntimeError: LLVM IR parsing error
<string>:5:55: error: expected type
declare void @"_Z3foo8Vector2DPS_"(<2 x float> %".1", ptr %".2")

sklam avatar Jul 02 '24 21:07 sklam

Looks good. The usecase is solely for getting function type back into a ir.Type and all of that is still working as expected.

Perfect, thanks for having a look!

I happen to notice that on LLVM14 parse_assembly do not like the opaque pointer despite it is enabled. Is that a LLVM bug?

I'm not sure, most of CI seems to be failing for the same reason though. When I spoke with @gmarkall we concluded it was probably not worth fixing given llvmlite will be moving to LLVM 15 soon. I can look into it though if you think that'd be useful.

rj-jesus avatar Jul 03 '24 09:07 rj-jesus

Let's wait for LLVM15 becoming default

sklam avatar Jul 08 '24 15:07 sklam

That sounds great, do you have an estimate of when that will be?

rj-jesus avatar Jul 09 '24 09:07 rj-jesus

Hi @gmarkall, thanks for having had a look! I'll work on the proposed changes and update the review soon. In the meantime, I've also left a few comments in reply to yours. If you can, please let me know what you think and I'll include any further changes in the next update.

rj-jesus avatar Jul 19 '24 09:07 rj-jesus

Hi @gmarkall, I believe I've addressed most of your comments in the last commit, in particular:

  • Change logic _disable_opaque_pointersopaque_pointers_enabled
  • Remove leading underscore from opaque_pointers_enabled
  • Move LLVMPY_GlobalGetValueType back to value.cpp
  • Reinstate LLVMPY_ABISizeOfElementType and LLVMPY_ABIAlignmentOfElementType with error if used in opaque pointer mode

Please let me know if you have any other comments!

rj-jesus avatar Jul 22 '24 13:07 rj-jesus

Hi @gmarkall, thanks very much for your comments, I believe I've addressed them in the latest revision, in particular:

  • We now default to opaque pointers disabled
  • Moved a few imports to the top-level
  • Refactored if logic in a few constructors
  • Fixed double-init issue you pointed out

Please let me know if you have any other comments.

rj-jesus avatar Aug 01 '24 13:08 rj-jesus

@rj-jesus Thanks for this, I think it looks good - I'll update the CI configuration and docs.

gmarkall avatar Aug 06 '24 11:08 gmarkall

@gmarkall Thank you very much for the feedback! Please let me know if you need help with anything.

rj-jesus avatar Aug 06 '24 11:08 rj-jesus

thanks for the patch

esc avatar Sep 18 '24 19:09 esc