Skip to content

Commit

Permalink
Even more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaraldi committed Mar 17, 2023
1 parent 25df71c commit 2f488ed
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 143 deletions.
4 changes: 2 additions & 2 deletions COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LLVM API coverage
Find functions in `lib` not mentioned in this document:
```
for f in $(grep -ohR "function \w*" lib | cut -d ' ' -f 2)
for f in $(grep -ohR "function \w*" lib | cut -d ' ' -f 2)
do
grep -q $f COVERAGE.md || echo $f
done
Expand Down Expand Up @@ -585,7 +585,7 @@ Function parameters:
- [ ] LLVMRemoveAttribute
- [ ] LLVMGetAttribute
- [ ] LLVMSetParamAlignment


### Metadata

Expand Down
4 changes: 4 additions & 0 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ void LLVMExtraGetNamedMetadataOperands2(LLVMNamedMDNodeRef NMD, LLVMMetadataRef

void LLVMExtraAddNamedMetadataOperand2(LLVMNamedMDNodeRef NMD, LLVMMetadataRef Val);

LLVMTypeRef LLVMGetFunctionType(LLVMValueRef Fn);

LLVMTypeRef LLVMGetType(LLVMValueRef V);

#if LLVM_VERSION_MAJOR >= 12
void LLVMAddCFGSimplificationPass2(LLVMPassManagerRef PM,
int BonusInstThreshold,
Expand Down
10 changes: 10 additions & 0 deletions deps/LLVMExtra/lib/llvm-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,13 @@ LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const void *Data, unsigne
StringRef S((const char *)Data, NumElements * unwrap(ElementTy)->getPrimitiveSizeInBits() / 8);
return wrap(ConstantDataArray::getRaw(S, NumElements, unwrap(ElementTy)));
}

LLVMTypeRef LLVMGetType(LLVMValueRef V) {
return wrap(unwrap(V)->getType());
}

LLVMTypeRef LLVMGetFunctionType(LLVMValueRef Fn) {
auto Ftype = unwrap<Function>(Fn)->getFunctionType();
return wrap(Ftype);
}

8 changes: 4 additions & 4 deletions examples/Kaleidoscope/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
function codegen(cg::CodeGen, expr::VariableExprAST)
V = get(current_scope(cg), expr.name, nothing)
V == nothing && error("did not find variable $(expr.name)")
return LLVM.load!(cg.builder, V, expr.name)
return LLVM.load!(cg.builder, LLVM.DoubleType(cg.ctx), V, expr.name)
end

function codegen(cg::CodeGen, expr::BinaryExprAST)
Expand Down Expand Up @@ -94,8 +94,8 @@ function codegen(cg::CodeGen, expr::CallExprAST)
for v in expr.args
push!(args, codegen(cg, v))
end

return LLVM.call!(cg.builder, func, args, "calltmp")
ft = LLVM.FunctionType(func)
return LLVM.call!(cg.builder, ft, func, args, "calltmp")
end

function codegen(cg::CodeGen, expr::PrototypeAST)
Expand Down Expand Up @@ -190,7 +190,7 @@ function codegen(cg::CodeGen, expr::ForExprAST)
step = codegen(cg, expr.step)
endd = codegen(cg, expr.endd)

curvar = LLVM.load!(cg.builder, alloc, expr.varname)
curvar = LLVM.load!(cg.builder, LLVM.DoubleType(cg.ctx), alloc, expr.varname)
nextvar = LLVM.fadd!(cg.builder, curvar, step, "nextvar")
LLVM.store!(cg.builder, nextvar, alloc)

Expand Down
4 changes: 2 additions & 2 deletions examples/constrained.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ meta(::Type{FPExceptStrict}) = "fpexcept.strict"
mod = LLVM.parent(llvm_f)
intrinsic = Intrinsic("llvm.experimental.constrained.$(func(F))")
intrinsic_fun = LLVM.Function(mod, intrinsic, [typ])

ftype = LLVM.FunctionType(intrinsic,[typ])
# generate IR
@dispose builder=Builder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)
val = call!(builder, intrinsic_fun,
val = call!(builder, ftype, intrinsic_fun,
[parameters(llvm_f)..., Value(mround; ctx), Value(mfpexcept; ctx)])
ret!(builder, val)
end
Expand Down
4 changes: 2 additions & 2 deletions examples/generated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ end

ptr = inttoptr!(builder, parameters(llvmf)[1], T_ptr)

ptr = gep!(builder, ptr, [parameters(llvmf)[2]])
val = load!(builder, ptr)
ptr = gep!(builder, eltyp, ptr, [parameters(llvmf)[2]])
val = load!(builder, eltyp, ptr)
ret!(builder, val)
end

Expand Down
8 changes: 8 additions & 0 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,11 @@ end
function LLVMConstDataArray(ElementTy, Data, NumElements)
ccall((:LLVMConstDataArray, libLLVMExtra), LLVMValueRef, (LLVMTypeRef, Ptr{Cvoid}, Cuint), ElementTy, Data, NumElements)
end

function LLVMGetFunctionType(Fn)
ccall((:LLVMGetFunctionType,libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
end

function LLVMGetType(V)
ccall((:LLVMGetType, libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), V)
end
2 changes: 2 additions & 0 deletions src/core/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ register(Function, API.LLVMFunctionValueKind)
Function(mod::Module, name::String, ft::FunctionType) =
Function(API.LLVMAddFunction(mod, name, ft))

FunctionType(Fn::Function) = FunctionType(API.LLVMGetFunctionType(Fn))

Base.empty!(f::Function) = API.LLVMFunctionDeleteBody(f)

unsafe_delete!(::Module, f::Function) = API.LLVMDeleteFunction(f)
Expand Down
10 changes: 5 additions & 5 deletions src/core/type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ end

if has_opaque_ptr()

function PointerType(ctx::Context, addrspace=0)
return PointerType(API.LLVMPointerTypeInContext(ctx, addrspace))
end
function PointerType(ctx::Context, addrspace=0)
return PointerType(API.LLVMPointerTypeInContext(ctx, addrspace))
end

Base.eltype(typ::PointerType) =
throw(error("Taking the type of an opaque pointer is illegal"))
Base.eltype(typ::PointerType) =
throw(error("Taking the type of an opaque pointer is illegal"))

end
addrspace(ptrtyp::PointerType) =
Expand Down
16 changes: 4 additions & 12 deletions src/core/value/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,27 +472,19 @@ function const_gep(val::Constant, Indices::Vector{<:Constant})
end

function const_gep(val::Constant, Ty::LLVMType, Indices::Vector{<:Constant})
if has_opaque_ptr()
return Value(API.LLVMConstGEP2(val, Ty, Indices, length(Indices)))
else
return Value(API.LLVMConstGEP(val, Indices, length(Indices)))
end
return Value(API.LLVMConstGEP2(val, Ty, Indices, length(Indices)))
end

function const_gep(val::Constant, Indices::Vector{<:Constant})
function const_inbounds_gep(val::Constant, Indices::Vector{<:Constant})
if has_opaque_ptr()
throw(error("Typed Pointers not supported on this version"))
else
return Value(API.LLVMConstInboundsGEP(val, Indices, length(Indices)))
end
end

function const_gep(val::Constant, Ty::LLVMType, Indices::Vector{<:Constant})
if has_opaque_ptr()
return Value(API.LLVMConstInBoundsGEP2(val, Ty, Indices, length(Indices)))
else
return Value(API.LLVMConstInBoundsGEP(val, Indices, length(Indices)))
end
function const_inbounds_gep(val::Constant, Ty::LLVMType, Indices::Vector{<:Constant})
return Value(API.LLVMConstInBoundsGEP2(val, Ty, Indices, length(Indices)))
end

const_trunc(val::Constant, ToType::LLVMType) =
Expand Down
2 changes: 1 addition & 1 deletion src/interop/asmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export @asmcall
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)

val = call!(builder, inline_asm, collect(parameters(llvm_f)))
val = call!(builder, llvm_ft, inline_asm, collect(parameters(llvm_f)))
if rettyp == Nothing
ret!(builder)
else
Expand Down
4 changes: 2 additions & 2 deletions src/interop/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ end
@dispose builder=Builder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)
if !has_opaque_ptr()
if !LLVM.has_opaque_ptr()
typed_ptr = bitcast!(builder, parameters(llvm_f)[1], T_typed_ptr)
ld = load!(builder, typed_ptr)
else
ld = load!(builder, eltype, parameters(llvm_f)[1])
ld = load!(builder, eltyp, parameters(llvm_f)[1])
end
ordering!(ld, llvm_order)

Expand Down
34 changes: 19 additions & 15 deletions src/interop/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ using Core: LLVMPtr

T_int = convert(LLVMType, Int; ctx)
T_ptr = convert(LLVMType, ptr; ctx)

T_typed_ptr = LLVM.PointerType(eltyp, A)

# create a function
param_types = [T_ptr, T_int]
llvm_f, _ = create_function(eltyp, param_types)
Expand All @@ -24,19 +22,21 @@ using Core: LLVMPtr
@dispose builder=Builder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)

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)

if !LLVM.has_opaque_ptr()
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)
else
ptr = inbounds_gep!(builder, eltyp, parameters(llvm_f)[1],[parameters(llvm_f)[2]])
ld = load!(builder, eltyp, ptr)
end
if A != 0
metadata(ld)[LLVM.MD_tbaa] = tbaa_addrspace(A; ctx)
end
alignment!(ld, align)

ret!(builder, ld)
end

call_function(llvm_f, T, Tuple{LLVMPtr{T,A}, Int}, :ptr, :(Int(i-one(i))))
end
end
Expand All @@ -59,12 +59,16 @@ end
@dispose builder=Builder(ctx) begin
entry = BasicBlock(llvm_f, "entry"; ctx)
position!(builder, entry)

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)

if !LLVM.has_opaque_ptr()
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)
else
ptr = inbounds_gep!(builder, eltyp, parameters(llvm_f)[1], [parameters(llvm_f)[3]])
val = parameters(llvm_f)[2]
st = store!(builder, val, ptr)
end
if A != 0
metadata(st)[LLVM.MD_tbaa] = tbaa_addrspace(A; ctx)
end
Expand Down Expand Up @@ -241,7 +245,7 @@ end
intr_ft = LLVM.FunctionType(T_ret_actual, T_actual_args)
intr_f = LLVM.Function(mod, String(intr), intr_ft)

rv = call!(builder, intr_f, actual_args)
rv = call!(builder, intr_ft, intr_f, actual_args)

if T_ret_actual == LLVM.VoidType(ctx)
ret!(builder)
Expand Down
48 changes: 7 additions & 41 deletions src/irbuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ switch!(builder::Builder, V::Value, Else::BasicBlock, NumCases::Integer=10) =
indirectbr!(builder::Builder, Addr::Value, NumDests::Integer=10) =
Instruction(API.LLVMBuildIndirectBr(builder, Addr, NumDests))

invoke!(builder::Builder, Fn::Value, Args::Vector{<:Value}, Then::BasicBlock, Catch::BasicBlock, Name::String="") =
Instruction(API.LLVMBuildInvoke(builder, Fn, Args, length(Args), Then, Catch, Name))

function invoke!(builder::Builder, Fn::Value, Args::Vector{<:Value}, Then::BasicBlock, Catch::BasicBlock, Name::String="")
if has_opaque_ptr()
throw(error("Typed Pointers not supported on this version"))
Expand All @@ -117,11 +114,7 @@ function invoke!(builder::Builder, Fn::Value, Args::Vector{<:Value}, Then::Basic
end

function invoke!(builder::Builder, Ty::LLVMType, Fn::Value, Args::Vector{<:Value}, Then::BasicBlock, Catch::BasicBlock, Name::String="")
if has_opaque_ptr()
return Instruction(API.LLVMBuildInvoke2(builder, Ty, Fn, Args, length(Args), Then, Catch, Name))
else
return Instruction(API.LLVMBuildInvoke(builder, Fn, Args, length(Args), Then, Catch, Name))
end
return Instruction(API.LLVMBuildInvoke2(builder, Ty, Fn, Args, length(Args), Then, Catch, Name))
end

resume!(builder::Builder, Exn::Value) =
Expand Down Expand Up @@ -271,11 +264,7 @@ function load!(builder::Builder, PointerVal::Value, Name::String="")
end

function load!(builder::Builder, Ty::LLVMType, PointerVal::Value, Name::String="")
if !has_opaque_ptr()
return load!(builder, PointerVal, Name)
else
return Instruction(API.LLVMBuildLoad2(builder, Ty, PointerVal, Name))
end
return Instruction(API.LLVMBuildLoad2(builder, Ty, PointerVal, Name))
end

store!(builder::Builder, Val::Value, Ptr::Value) =
Expand All @@ -299,11 +288,7 @@ function gep!(builder::Builder, Pointer::Value, Indices::Vector{<:Value}, Name::
end

function gep!(builder::Builder, Ty::LLVMType, Pointer::Value, Indices::Vector{<:Value}, Name::String="")
if has_opaque_ptr()
return Value(API.LLVMBuildGEP2(builder, Ty, Pointer, Indices, length(Indices), Name))
else
return Value(API.LLVMBuildGEP(builder, Pointer, Indices, length(Indices), Name))
end
return Value(API.LLVMBuildGEP2(builder, Ty, Pointer, Indices, length(Indices), Name))
end

function inbounds_gep!(builder::Builder, Pointer::Value, Indices::Vector{<:Value}, Name::String="")
Expand All @@ -315,11 +300,7 @@ function inbounds_gep!(builder::Builder, Pointer::Value, Indices::Vector{<:Value
end

function inbounds_gep!(builder::Builder, Ty::LLVMType, Pointer::Value, Indices::Vector{<:Value}, Name::String="")
if has_opaque_ptr()
return Value(API.LLVMBuildInBoundsGEP2(builder, Ty, Pointer, Indices, length(Indices), Name))
else
return Value(API.LLVMBuildInBoundsGEP(builder, Pointer, Indices, length(Indices), Name))
end
return Value(API.LLVMBuildInBoundsGEP2(builder, Ty, Pointer, Indices, length(Indices), Name))
end

function struct_gep!(builder::Builder, Pointer::Value, Idx, Name::String="")
Expand All @@ -331,11 +312,7 @@ function struct_gep!(builder::Builder, Pointer::Value, Idx, Name::String="")
end

function struct_gep!(builder::Builder, Ty::LLVMType, Pointer::Value, Idx, Name::String="")
if has_opaque_ptr()
return Value(API.LLVMBuildStructGEP2(builder,Ty, Pointer, Idx, Name))
else
return Value(API.LLVMBuildStructGEP(builder, Pointer, Idx, Name))
end
return Value(API.LLVMBuildStructGEP2(builder,Ty, Pointer, Idx, Name))
end

# conversion operations
Expand Down Expand Up @@ -424,11 +401,7 @@ function call!(builder::Builder, Fn::Value, Args::Vector{<:Value}=Value[], Name:
end

function call!(builder::Builder, Ty::LLVMType, Fn::Value, Args::Vector{<:Value}=Value[], Name::String="")
if has_opaque_ptr()
return Instruction(API.LLVMBuildCall2(builder, Ty, Fn, Args, length(Args), Name))
else
return Instruction(API.LLVMBuildCall(builder, Fn, Args, length(Args), Name))
end
return Instruction(API.LLVMBuildCall2(builder, Ty, Fn, Args, length(Args), Name))
end

call!(builder::Builder, Fn::Value, Args::Vector{<:Value},
Expand Down Expand Up @@ -479,9 +452,6 @@ isnull!(builder::Builder, Val::Value, Name::String="") =
isnotnull!(builder::Builder, Val::Value, Name::String="") =
Value(API.LLVMBuildIsNotNull(builder, Val, Name))

ptrdiff!(builder::Builder, LHS::Value, RHS::Value, Name::String="") =
Value(API.LLVMBuildPtrDiff(builder, LHS, RHS, Name))

function ptrdiff!(builder::Builder, LHS::Value, RHS::Value, Name::String="")
if has_opaque_ptr()
throw(error("Typed Pointers not supported on this version"))
Expand All @@ -491,9 +461,5 @@ function ptrdiff!(builder::Builder, LHS::Value, RHS::Value, Name::String="")
end

function ptrdiff!(builder::Builder, Ty::LLVMType, LHS::Value, RHS::Value, Name::String="")
if has_opaque_ptr()
return Value(API.LLVMBuildPtrDiff2(builder, Ty, LHS, RHS, Name))
else
return Value(API.LLVMBuildPtrDiff(builder, LHS, RHS, Name))
end
return Value(API.LLVMBuildPtrDiff2(builder, Ty, LHS, RHS, Name))
end
Loading

0 comments on commit 2f488ed

Please sign in to comment.