Skip to content

Commit 9f7512a

Browse files
authored
Merge pull request #694 from JuliaLLVM/tb/host-perf-4
Inline the remaining C API wrappers of non-specialized builders
2 parents 6965c82 + 2cef137 commit 9f7512a

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎src/LLVM.jl‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,19 @@ end
4141
# for every combination of, e.g., value types). that makes calls with abstractly-typed
4242
# arguments resolve statically, and inlining them makes the argument conversions do so too.
4343
function inline_wrapper(ex)
44-
if Meta.isexpr(ex, :function) && Meta.isexpr(ex.args[2], :block)
45-
pushfirst!(ex.args[2].args, Expr(:meta, :inline))
44+
def = undocumented(ex)
45+
if Meta.isexpr(def, :function) && Meta.isexpr(def.args[2], :block)
46+
pushfirst!(def.args[2].args, Expr(:meta, :inline))
4647
end
4748
return ex
4849
end
4950

51+
# the definition in a top-level expression of the wrappers, which is wrapped in a call to
52+
# `@doc` if it is documented
53+
undocumented(ex) =
54+
Meta.isexpr(ex, :macrocall) && ex.args[1] == GlobalRef(Core, Symbol("@doc")) ?
55+
ex.args[end] : ex
56+
5057
# enum members that the C API lacks, at least on older versions of LLVM, numbered as in newer
5158
# C APIs or as their next values, so that they can be named and enumerated on every LLVM
5259
# version. they are added to the definitions of the enums in the generated wrappers, so that
@@ -64,9 +71,8 @@ const backfilled_enum_members = (
6471
)
6572
const backfilled_enum_definitions = Dict{Symbol,Int}()
6673
function backfill_enum_members(ex)
67-
# the definition is `@cenum Name::UInt32 begin ... end`, documented
68-
def = Meta.isexpr(ex, :macrocall) && ex.args[1] == GlobalRef(Core, Symbol("@doc")) ?
69-
ex.args[end] : ex
74+
# the definition is `@cenum Name::UInt32 begin ... end`
75+
def = undocumented(ex)
7076
Meta.isexpr(def, :macrocall) && def.args[1] === Symbol("@cenum") &&
7177
Meta.isexpr(def.args[3], :(::)) || return ex
7278
enum = def.args[3].args[1]

‎src/core/value/constant.jl‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,8 +1543,7 @@ Create a global variable in the given module with the given type, name, and opti
15431543
address space.
15441544
"""
15451545
GlobalVariable(mod::Module, typ::LLVMType, name::AbstractString, addrspace::Integer=0) =
1546-
GlobalVariable(API.LLVMAddGlobalInAddressSpace(mod, typ,
1547-
name, addrspace))
1546+
GlobalVariable(API.LLVMAddGlobalInAddressSpace(mod, typ, name, Cuint(addrspace)))
15481547

15491548
"""
15501549
erase!(gv::GlobalVariable)

‎src/irbuilder.jl‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function extract_value!(builder::IRBuilder, AggVal::Value,
407407
Indices::AbstractVector{<:Integer}, Name::AbstractString="")
408408
check_aggregate_indices(value_type(AggVal), Indices)
409409
isempty(Indices) && return AggVal
410-
idxs = Vector{Cuint}(Indices)
410+
idxs = convert(Vector{Cuint}, Indices)
411411
Value(API.LLVMExtraBuildExtractValue(builder, AggVal, idxs, length(idxs), Name))
412412
end
413413

@@ -432,18 +432,18 @@ function insert_value!(builder::IRBuilder, AggVal::Value, EltVal::Value,
432432
Indices::AbstractVector{<:Integer}, Name::AbstractString="")
433433
check_inserted_value(AggVal, EltVal, Indices)
434434
isempty(Indices) && return EltVal
435-
idxs = Vector{Cuint}(Indices)
435+
idxs = convert(Vector{Cuint}, Indices)
436436
Value(API.LLVMExtraBuildInsertValue(builder, AggVal, EltVal, idxs, length(idxs), Name))
437437
end
438438

439439

440440
# memory access and addressing operations
441441

442442
# address spaces are 24-bit numbers
443-
function check_addrspace(addrspace)
443+
function check_addrspace(addrspace)::Cuint
444444
addrspace isa Integer && 0 <= addrspace < 2^24 ||
445445
throw(ArgumentError("Address spaces must be integers between 0 and 2^24-1, got $addrspace"))
446-
return Cuint(addrspace)
446+
return addrspace
447447
end
448448

449449
"""

‎test/instructions.jl‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,30 @@ let calls_wrapper(f, types) = any(only(code_typed(f, types))[1].code) do stmt
743743
@test !calls_wrapper(memmove!,
744744
Tuple{IRBuilder, Value, Integer, Value, Integer, Value})
745745
end
746+
indices = AbstractVector{<:Integer}
747+
@test !calls_wrapper(extract_value!, Tuple{IRBuilder, Value, indices, AbstractString})
748+
@test !calls_wrapper(insert_value!,
749+
Tuple{IRBuilder, Value, Value, indices, AbstractString})
750+
751+
# methods with keyword arguments are compiled for the keyword arguments' declared types
752+
kwbody(f, types) = Base.bodyfunction(which(f, types))
753+
@test !calls_wrapper(kwbody(alloca!, Tuple{IRBuilder, LLVMType, AbstractString}),
754+
Tuple{Any, Any, typeof(alloca!), IRBuilder, LLVMType,
755+
AbstractString})
756+
@test !calls_wrapper(kwbody(array_alloca!,
757+
Tuple{IRBuilder, LLVMType, Value, AbstractString}),
758+
Tuple{Any, Any, typeof(array_alloca!), IRBuilder, LLVMType,
759+
Value, AbstractString})
760+
@test !calls_wrapper(kwbody(globalstring!,
761+
Tuple{LLVM.Module, AbstractString, AbstractString}),
762+
Tuple{Union{Nothing, Integer}, Bool, typeof(globalstring!),
763+
LLVM.Module, AbstractString, AbstractString})
764+
765+
# documented wrappers are inlined too
766+
@test !calls_wrapper(intcast!, Tuple{IRBuilder, Value, LLVMType, AbstractString})
767+
@test !calls_wrapper(LLVM.Interop._generate_llvmcall,
768+
Tuple{Any, Any, Any, Vector{Any}, String,
769+
Union{Nothing, Vector{Any}}})
746770
end
747771

748772
end

0 commit comments

Comments
 (0)