Skip to content

Refactor in anticipation of breaking release #340

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

Merged
merged 5 commits into from
Mar 24, 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
4 changes: 4 additions & 0 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
LLVMValueRef *Args, unsigned NumArgs,
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
const char *Name);
LLVMValueRef LLVMBuildCallWithOpBundle2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
LLVMValueRef *Args, unsigned NumArgs,
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
const char *Name);
LLVMValueRef LLVMMetadataAsValue2(LLVMContextRef C, LLVMMetadataRef Metadata);
void LLVMReplaceAllMetadataUsesWith(LLVMValueRef Old, LLVMValueRef New);
void LLVMReplaceMDNodeOperandWith(LLVMMetadataRef MD, unsigned I, LLVMMetadataRef New);
Expand Down
19 changes: 18 additions & 1 deletion deps/LLVMExtra/lib/llvm-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,28 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
llvm::IRBuilder<> *Builder = unwrap(B);
llvm::ArrayRef<llvm::Value*> args = makeArrayRef(unwrap(Args), NumArgs);

FunctionType *FnT = unwrap<Function>(Fn)->getFunctionType();
Value *V = unwrap(Fn);
FunctionType *FnT = cast<FunctionType>(V->getType()->getPointerElementType());
llvm::CallInst *CI = Builder->CreateCall(FnT, unwrap(Fn), args ,BundleArray, Name);
return wrap(CI);
}

LLVMValueRef LLVMBuildCallWithOpBundle2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
LLVMValueRef *Args, unsigned NumArgs,
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
const char *Name) {
SmallVector<OperandBundleDef, 1> BundleArray;
for (auto *Bundle : makeArrayRef(Bundles, NumBundles))
BundleArray.push_back(*unwrap<OperandBundleDef>(Bundle));

llvm::IRBuilder<> *Builder = unwrap(B);
llvm::ArrayRef<llvm::Value*> args = makeArrayRef(unwrap(Args), NumArgs);

FunctionType *FTy = unwrap<FunctionType>(Ty);
llvm::CallInst *CI = Builder->CreateCall(FTy, unwrap(Fn), args ,BundleArray, Name);
return wrap(CI);
}

LLVMValueRef LLVMMetadataAsValue2(LLVMContextRef C, LLVMMetadataRef Metadata) {
auto *MD = unwrap(Metadata);
if (auto *VAM = dyn_cast<ValueAsMetadata>(MD))
Expand Down
6 changes: 3 additions & 3 deletions examples/Kaleidoscope/codegen.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mutable struct CodeGen
ctx::LLVM.Context
builder::LLVM.Builder
builder::LLVM.IRBuilder
current_scope::CurrentScope
mod::LLVM.Module

CodeGen(ctx::LLVM.Context) =
new(
ctx,
LLVM.Builder(ctx),
LLVM.IRBuilder(ctx),
CurrentScope(),
LLVM.Module("KaleidoscopeModule"; ctx),
)
Expand All @@ -23,7 +23,7 @@ Base.show(io::IO, cg::CodeGen) = print(io, "CodeGen")

function create_entry_block_allocation(cg::CodeGen, fn::LLVM.Function, varname::String)
local alloc
LLVM.@dispose builder=LLVM.Builder(cg.ctx) begin
LLVM.@dispose builder=LLVM.IRBuilder(cg.ctx) begin
# Set the builder at the start of the function
entry_block = LLVM.entry(fn)
if isempty(LLVM.instructions(entry_block))
Expand Down
2 changes: 1 addition & 1 deletion examples/constrained.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ meta(::Type{FPExceptStrict}) = "fpexcept.strict"
intrinsic_fun = LLVM.Function(mod, intrinsic, [typ])
ftype = LLVM.FunctionType(intrinsic,[typ])
# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)
val = call!(builder, ftype, intrinsic_fun,
Expand Down
2 changes: 1 addition & 1 deletion examples/generated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end
llvmf, _ = create_function(eltyp, paramtyps)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvmf, "entry"; ctx)
position!(builder, entry)

Expand Down
2 changes: 1 addition & 1 deletion examples/sum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end
sum = LLVM.Function(mod, "sum", fun_type)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(sum, "entry"; ctx)
position!(builder, entry)

Expand Down
2 changes: 1 addition & 1 deletion examples/sum_integrated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end
sum, _ = create_function(ret_type, param_types)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(sum, "entry"; ctx)
position!(builder, entry)

Expand Down
2 changes: 1 addition & 1 deletion examples/sum_orc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function codegen!(mod::LLVM.Module, name, tm)
sum = LLVM.Function(mod, name, ft)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(sum, "entry"; ctx)
position!(builder, entry)

Expand Down
4 changes: 4 additions & 0 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ function LLVMBuildCallWithOpBundle(B, Fn, Args, NumArgs, Bundles, NumBundles, Na
ccall((:LLVMBuildCallWithOpBundle, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMValueRef, Ptr{LLVMValueRef}, Cuint, Ptr{LLVMOperandBundleDefRef}, Cuint, Cstring), B, Fn, Args, NumArgs, Bundles, NumBundles, Name)
end

function LLVMBuildCallWithOpBundle2(B, Ty, Fn, Args, NumArgs, Bundles, NumBundles, Name)
ccall((:LLVMBuildCallWithOpBundle2, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMTypeRef, LLVMValueRef, Ptr{LLVMValueRef}, Cuint, Ptr{LLVMOperandBundleDefRef}, Cuint, Cstring), B, Ty, Fn, Args, NumArgs, Bundles, NumBundles, Name)
end

function LLVMMetadataAsValue2(C, MD)
ccall((:LLVMMetadataAsValue2, libLLVMExtra), LLVMValueRef, (LLVMContextRef, LLVMMetadataRef), C, MD)
end
Expand Down
4 changes: 0 additions & 4 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ end
# calling `refcheck` on the ref field argument
macro checked(typedef)
# decode structure definition
if Meta.isexpr(typedef, :macrocall)
# handle `@compat` prefixing 0.6-style type declarations
typedef = macroexpand(typedef)
end
if Meta.isexpr(typedef, :struct)
structure = typedef.args[2]
body = typedef.args[3]
Expand Down
2 changes: 2 additions & 0 deletions src/core/value/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ const_ashr(lhs::Constant, rhs::Constant) =

function const_gep(val::Constant, Indices::Vector{<:Constant})
supports_typed_pointers(context(val)) || throw_typedpointererror()
Base.depwarn("const_gep without specifying the destination type is deprecated", :const_gep)
Value(API.LLVMConstGEP(val, Indices, length(Indices)))
end

Expand All @@ -474,6 +475,7 @@ end

function const_inbounds_gep(val::Constant, Indices::Vector{<:Constant})
supports_typed_pointers(context(val)) || throw_typedpointererror()
Base.depwarn("const_inbounds_gep without specifying the destination type is deprecated", :const_inbounds_gep)
Value(API.LLVMConstInboundsGEP(val, Indices, length(Indices)))
end

Expand Down
7 changes: 7 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# deprecated methods

Base.@deprecate llvmtype(x) value_type(x)
Base.@deprecate llvmeltype(x) eltype(value_type(x))

Base.@deprecate_binding Builder IRBuilder

# NOTE: there's more deprecated methods in irbuilder.jl
3 changes: 0 additions & 3 deletions src/interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ module Interop
using ..LLVM
import ..LLVM: API


const jlctx = Ref{LLVM.Context}()

include("interop/base.jl")
include("interop/asmcall.jl")
include("interop/passes.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/interop/asmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export @asmcall
inline_asm = InlineAsm(llvm_ft, String(asm), String(constraints), side_effects)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)

Expand Down
29 changes: 13 additions & 16 deletions src/interop/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ using Core: LLVMPtr
llvm_f, _ = create_function(eltyp, param_types)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)
if supports_typed_pointers(ctx)
ptr = if supports_typed_pointers(ctx)
typed_ptr = bitcast!(builder, parameters(llvm_f)[1], T_typed_ptr)
typed_ptr = inbounds_gep!(builder, typed_ptr, [parameters(llvm_f)[2]])
ld = load!(builder, typed_ptr)
inbounds_gep!(builder, eltyp, typed_ptr, [parameters(llvm_f)[2]])
else
ptr = inbounds_gep!(builder, eltyp, parameters(llvm_f)[1],[parameters(llvm_f)[2]])
ld = load!(builder, eltyp, ptr)
inbounds_gep!(builder, eltyp, parameters(llvm_f)[1], [parameters(llvm_f)[2]])
end
ld = load!(builder, eltyp, ptr)
if A != 0
metadata(ld)[LLVM.MD_tbaa] = tbaa_addrspace(A; ctx)
end
Expand Down Expand Up @@ -59,19 +58,17 @@ end
llvm_f, _ = create_function(LLVM.VoidType(ctx), param_types)

# generate IR
@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)
if supports_typed_pointers(ctx)
ptr = if supports_typed_pointers(ctx)
typed_ptr = bitcast!(builder, parameters(llvm_f)[1], T_typed_ptr)
typed_ptr = inbounds_gep!(builder, typed_ptr, [parameters(llvm_f)[3]])
val = parameters(llvm_f)[2]
st = store!(builder, val, typed_ptr)
inbounds_gep!(builder, eltyp, typed_ptr, [parameters(llvm_f)[3]])
else
ptr = inbounds_gep!(builder, eltyp, parameters(llvm_f)[1], [parameters(llvm_f)[3]])
val = parameters(llvm_f)[2]
st = store!(builder, val, ptr)
inbounds_gep!(builder, eltyp, parameters(llvm_f)[1], [parameters(llvm_f)[3]])
end
val = parameters(llvm_f)[2]
st = store!(builder, val, ptr)
if A != 0
metadata(st)[LLVM.MD_tbaa] = tbaa_addrspace(A; ctx)
end
Expand Down Expand Up @@ -129,7 +126,7 @@ Base.signed(x::LLVMPtr) = Int(x)
llvm_f, _ = create_function(T_dest, [T_src])
mod = LLVM.parent(llvm_f)

@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)

Expand Down Expand Up @@ -164,7 +161,7 @@ end
llvm_f, _ = create_function(T_ret, T_args)
mod = LLVM.parent(llvm_f)

@dispose builder=Builder(ctx) begin
@dispose builder=IRBuilder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)

Expand Down
Loading