Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLVM: Activate typed pointers through the API instead of a global option. #51840

Merged
merged 3 commits into from
Oct 25, 2023

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented Oct 24, 2023

This allows other users of LLVM to use opaque pointers on their contexts. I'd like to get this in 1.10 (or a minor version) so that we can start updating packages to be compatible with opaque pointers. Currently, that requires overriding the global option, which is unwanted (may break Julia, definitely breaks certain GPUCompiler back-ends, etc).

Picked from #49846.

…ion.

This allows other users of LLVM to use opaque pointers on their contexts.
@maleadt maleadt added compiler:codegen Generation of LLVM IR and native code backport 1.10 Change should be backported to the 1.10 release labels Oct 24, 2023
@maleadt
Copy link
Member Author

maleadt commented Oct 24, 2023

@vchuravy I did just realize that this change results in LLVM's default Context() using opaque pointers by default now. Which means that using that in generated functions results in incompatible IR:

julia> using LLVM, LLVM.Interop

julia> @generated function memset(ptr::Ptr{T}, val::T) where T
           @dispose ctx=Context() begin
               T_ptr = convert(LLVMType, Ptr{T})
               T_val = convert(LLVMType, T)
               T_actual_ptr = LLVM.PointerType(T_val, 0)
               f, ft = create_function(LLVM.VoidType(), [T_ptr, T_val])

               @dispose builder=IRBuilder() begin
                   entry = BasicBlock(f, "entry")
                   position!(builder, entry)
                   actual_ptr = inttoptr!(builder, parameters(f)[1], T_actual_ptr)
                   store!(builder, parameters(f)[2], actual_ptr)
                   ret!(builder)
               end

               call_function(f, Nothing, Tuple{Ptr{T}, T}, :ptr, :val)
           end
       end
memset (generic function with 1 method)

julia> a = [0]
1-element Vector{Int64}:
 0

julia> memset(pointer(a), 42)
<string>:7:27: warning: ptr type is only supported in -opaque-pointers mode
  %2 = inttoptr i64 %0 to ptr
                          ^
ERROR: Failed to parse LLVM assembly:
<string>:7:27: error: expected type
  %2 = inttoptr i64 %0 to ptr
                          ^

Stacktrace:
 [1] macro expansion
   @ Main ~/Julia/pkg/LLVM/src/interop/base.jl:38 [inlined]
 [2] macro expansion
   @ Main ./REPL[2]:1 [inlined]
 [3] memset(ptr::Ptr{Int64}, val::Int64)
   @ Main ./REPL[2]:1
 [4] top-level scope
   @ REPL[4]:1

We also can't have LLVM.jl default to Context() disabling opaque pointers, because you can only set the option once...


EDIT: as noted by @vtjnash, the reverse (using typed IR in an opaque context) is allowed, so we should just default to using typed contexts until we upgrade to LLVM 17.

@maleadt maleadt marked this pull request as draft October 24, 2023 15:01
Co-authored-by: Jameson Nash <[email protected]>
@maleadt maleadt marked this pull request as ready for review October 25, 2023 08:34
@maleadt maleadt merged commit 3493473 into master Oct 25, 2023
4 of 7 checks passed
@maleadt maleadt deleted the tb/llvm_opaque_api branch October 25, 2023 11:57
maleadt added a commit that referenced this pull request Oct 25, 2023
…ion. (#51840)

This allows other users of LLVM to use opaque pointers with their
contexts.

Co-authored-by: Jameson Nash <[email protected]>
@maleadt maleadt removed the backport 1.10 Change should be backported to the 1.10 release label Oct 25, 2023
@maleadt maleadt mentioned this pull request Oct 25, 2023
31 tasks
KristofferC added a commit that referenced this pull request Nov 2, 2023
Backported PRs:
- [x] #50932 <!-- types: fix hash values of Vararg -->
- [x] #50975 <!-- Use rr-safe `nopl; rdtsc` sequence -->
- [x] #50989 <!-- fix incorrect results in `expm1(::Union{Float16,
Float32})` -->
- [x] #51284 <!-- Avoid infinite loop when doing SIGTRAP in arm64-apple
-->
- [x] #51332 <!-- Add s4 field to Xoshiro -->
- [x] #51397 <!-- call Pkg precompile hook in latest world -->
- [x] #51405 <!-- Remove fallback that assigns a module to inlined
frames. -->
- [x] #51491 <!-- Throw clearer ArgumentError for strip with two string
args -->
- [x] #51531 <!-- fix `_tryonce_download_from_cache` (busybox.exe
download error) -->
- [x] #51541 <!-- Fix string index error in tab completion code -->
- [x] #51530 <!-- Don't mark nonlocal symbols as hidden -->
- [x] #51557 <!-- Fix last startup & shutdown precompiles -->
- [x] #51512 <!-- avoid limiting Type{Any} to Type -->
- [x] #51595 <!-- reset `maxprobe` on `empty!` -->
- [x] #51582 <!-- Aggressive constprop in LinearAlgebra.wrap -->
- [x] #51592 <!-- correctly track element pointer in heap snapshot -->
- [x] #51326 <!-- complete false & true more generally as vals -->
- [x] #51376 <!-- make `hash(::Xoshiro)` compatible with `==` -->
- [x] #51557 <!-- Fix last startup & shutdown precompiles -->
- [x] #51845 
- [x] #51840 
- [x] #50663 <!-- Fix Expr(:loopinfo) codegen -->
- [x] #51863 <!-- LLVM 15.0.7-9 -->

Contains multiple commits, manual intervention needed:

- [ ] #51035 <!-- refactor GC scanning code to reflect jl_binding_t are
now first class -->
- [ ] #51092 <!-- inference: fix bad effects for recursion -->

Non-merged PRs with backport label:
- [ ] #51479 <!-- prevent code loading from lookin in the versioned
environment when building Julia -->
- [ ] #51414 <!-- improvements on GC scheduler shutdown -->
- [ ] #51366 <!-- Handle infix operators in REPL completion -->
- [ ] #50919 <!-- Code loading: do the "skipping mtime check for stdlib"
check regardless of the value of `ispath(f)` -->
- [ ] #50824 <!-- Add some aliasing warnings to docstrings for mutating
functions in Base -->
- [ ] #49805 <!-- Limit TimeType subtraction to AbstractDateTime -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:codegen Generation of LLVM IR and native code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants