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

Improve API for setting pointer mode #371

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ void LLVMReplaceMDNodeOperandWith(LLVMMetadataRef MD, unsigned I, LLVMMetadataRe
#if LLVM_VERSION_MAJOR >= 13
LLVMBool LLVMContextSupportsTypedPointers(LLVMContextRef C);
#endif
#if LLVM_VERSION_MAJOR >= 15
LLVMBool LLVMContextHasSetOpaquePointersValue(LLVMContextRef C);
#endif

// constant data
LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const void *Data, unsigned NumElements);
Expand Down
5 changes: 5 additions & 0 deletions deps/LLVMExtra/lib/llvm-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ LLVMBool LLVMContextSupportsTypedPointers(LLVMContextRef C) {
return unwrap(C)->supportsTypedPointers();
}
#endif
#if LLVM_VERSION_MAJOR >= 15
LLVMBool LLVMContextHasSetOpaquePointersValue(LLVMContextRef C) {
return unwrap(C)->hasSetOpaquePointersValue();
}
#endif

LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const void *Data, unsigned NumElements) {
StringRef S((const char *)Data, NumElements * unwrap(ElementTy)->getPrimitiveSizeInBits() / 8);
Expand Down
7 changes: 6 additions & 1 deletion lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,16 @@ function LLVMPostDominatorTreeInstructionDominates(Tree, InstA, InstB)
ccall((:LLVMPostDominatorTreeInstructionDominates, libLLVMExtra), LLVMBool, (LLVMPostDominatorTreeRef, LLVMValueRef, LLVMValueRef), Tree, InstA, InstB)
end

if version() > v"12"
if version() >= v"13"
function LLVMContextSupportsTypedPointers(Ctx)
ccall((:LLVMContextSupportsTypedPointers, libLLVMExtra), LLVMBool, (LLVMContextRef,), Ctx)
end
end
if version() >= v"15"
function LLVMContextHasSetOpaquePointersValue(Ctx)
ccall((:LLVMContextHasSetOpaquePointersValue, libLLVMExtra), LLVMBool, (LLVMContextRef,), Ctx)
end
end

function LLVMConstDataArray(ElementTy, Data, NumElements)
ccall((:LLVMConstDataArray, libLLVMExtra), LLVMValueRef, (LLVMTypeRef, Ptr{Cvoid}, Cuint), ElementTy, Data, NumElements)
Expand Down
68 changes: 51 additions & 17 deletions src/core/context.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Contexts are execution states for the core LLVM IR system.

export Context, dispose, GlobalContext, typed_pointers, opaque_pointers!
export Context, dispose, GlobalContext

@checked struct Context
ref::API.LLVMContextRef
end

Base.unsafe_convert(::Type{API.LLVMContextRef}, ctx::Context) = ctx.ref

function Context()
function Context(; opaque_pointers=nothing)
ctx = Context(API.LLVMContextCreate())
if opaque_pointers !== nothing
opaque_pointers!(ctx, opaque_pointers)
end
_install_handlers(ctx)
activate(ctx)
ctx
Expand All @@ -20,8 +23,8 @@ function dispose(ctx::Context)
API.LLVMContextDispose(ctx)
end

function Context(f::Core.Function)
ctx = Context()
function Context(f::Core.Function; kwargs...)
ctx = Context(; kwargs...)
try
f(ctx)
finally
Expand All @@ -31,19 +34,6 @@ end

GlobalContext() = Context(API.LLVMGetGlobalContext())

if version() >= v"13"
typed_pointers(ctx::Context) =
convert(Core.Bool, API.LLVMContextSupportsTypedPointers(ctx))

opaque_pointers!(ctx::Context, enable::Core.Bool) =
API.LLVMContextSetOpaquePointers(ctx, enable)
else
typed_pointers(ctx::Context) = true

opaque_pointers!(ctx::Context, enable::Bool) =
error("Opaque pointers not supported")
end

function Base.show(io::IO, ctx::Context)
@printf(io, "LLVM.Context(%p", ctx.ref)
if ctx == GlobalContext()
Expand All @@ -64,6 +54,50 @@ end
argument, or use an environment that sypports typed pointers.""")


## opaque pointer handling

export typed_pointers

if version() >= v"13"
typed_pointers(ctx::Context) =
convert(Core.Bool, API.LLVMContextSupportsTypedPointers(ctx))

if version() >= v"15"
has_set_opaque_pointers_value(ctx::Context) =
convert(Core.Bool, API.LLVMContextHasSetOpaquePointersValue(ctx))
end

function unsafe_opaque_pointers!(ctx::Context, enable::Core.Bool)
@static if version() >= v"15"
if has_set_opaque_pointers_value(ctx) && typed_pointers(ctx) != !enable
error("Cannot $(enable ? "enable" : "disable") opaque pointers, as the context has already been configured to use $(typed_pointers(ctx) ? "typed" : "opaque") pointers")
end
end
API.LLVMContextSetOpaquePointers(ctx, enable)
end
else
typed_pointers(ctx::Context) = true
end

function opaque_pointers!(ctx::Context, opaque_pointers::Core.Bool)
@static if version() < v"13"
if opaque_pointers
error("LLVM <13 does not support opaque pointers")
end
end

@static if version() >= v"17"
if !opaque_pointers
error("LLVM >=17 does not support typed pointers")
end
end

@static if v"13" <= version() < v"17"
unsafe_opaque_pointers!(ctx, opaque_pointers)
end
end


## wrapper exception type

export LLVMException
Expand Down
9 changes: 6 additions & 3 deletions src/executionengine/ts_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
end
Base.unsafe_convert(::Type{API.LLVMOrcThreadSafeContextRef}, ctx::ThreadSafeContext) = ctx.ref

function ThreadSafeContext()
function ThreadSafeContext(; opaque_pointers=nothing)
ts_ctx = ThreadSafeContext(API.LLVMOrcCreateNewThreadSafeContext())
if opaque_pointers !== nothing
opaque_pointers!(context(ts_ctx), opaque_pointers)
end
activate(ts_ctx)
ts_ctx
end

function ThreadSafeContext(f::Core.Function)
ctx = ThreadSafeContext()
function ThreadSafeContext(f::Core.Function; kwargs...)
ctx = ThreadSafeContext(; kwargs...)
try
f(ctx)
finally
Expand Down