Skip to content
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
20 changes: 9 additions & 11 deletions src/irgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,16 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
ft = function_type(f)
@tracepoint "lower byval" begin

# classify the arguments
args = classify_arguments(job, ft)
filter!(args) do arg
arg.cc != GHOST
end

# find the byval parameters
byval = BitVector(undef, length(parameters(ft)))
types = Vector{LLVMType}(undef, length(parameters(ft)))
for i in 1:length(byval)
attrs = collect(parameter_attributes(f, i))
byval[i] = any(attrs) do attr
kind(attr) == kind(TypeAttribute("byval", LLVM.VoidType()))
byval[i] = false
for attr in collect(parameter_attributes(f, i))
if kind(attr) == kind(TypeAttribute("byval", LLVM.VoidType()))
byval[i] = true
types[i] = value(attr)
end
end
end

Expand Down Expand Up @@ -421,7 +419,7 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
new_types = LLVM.LLVMType[]
for (i, param) in enumerate(parameters(ft))
if byval[i]
llvm_typ = convert(LLVMType, args[i].typ)
llvm_typ = convert(LLVMType, types[i])
push!(new_types, llvm_typ)
else
push!(new_types, param)
Expand All @@ -444,7 +442,7 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
for (i, param) in enumerate(parameters(ft))
if byval[i]
# copy the argument value to a stack slot, and reference it.
llvm_typ = convert(LLVMType, args[i].typ)
llvm_typ = convert(LLVMType, types[i])
ptr = alloca!(builder, llvm_typ)
if LLVM.addrspace(param) != 0
ptr = addrspacecast!(builder, ptr, param)
Expand Down
28 changes: 10 additions & 18 deletions src/spirv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,32 +269,24 @@ end
function wrap_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.Function)
ft = function_type(f)::LLVM.FunctionType

args = classify_arguments(job, ft)
filter!(args) do arg
arg.cc != GHOST
end

# find the byval parameters
byval = BitVector(undef, length(parameters(ft)))
if LLVM.version() >= v"12"
for i in 1:length(byval)
attrs = collect(parameter_attributes(f, i))
byval[i] = any(attrs) do attr
kind(attr) == kind(TypeAttribute("byval", LLVM.VoidType()))
types = Vector{LLVMType}(undef, length(parameters(ft)))
for i in 1:length(byval)
byval[i] = false
for attr in collect(parameter_attributes(f, i))
if kind(attr) == kind(TypeAttribute("byval", LLVM.VoidType()))
byval[i] = true
types[i] = value(attr)
end
end
else
# XXX: byval is not round-trippable on LLVM < 12 (see maleadt/LLVM.jl#186)
for arg in args
byval[arg.idx] = (arg.cc == BITS_REF)
end
end

# generate the wrapper function type & definition
new_types = LLVM.LLVMType[]
for (i, param) in enumerate(parameters(ft))
typ = if byval[i]
llvm_typ = convert(LLVMType, args[i].typ)
llvm_typ = convert(LLVMType, types[i])
st = LLVM.StructType([llvm_typ])
LLVM.PointerType(st, addrspace(param))
else
Expand All @@ -318,7 +310,7 @@ function wrap_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.F
# perform argument conversions
for (i, param) in enumerate(parameters(new_f))
if byval[i]
llvm_typ = convert(LLVMType, args[i].typ)
llvm_typ = convert(LLVMType, types[i])
ptr = struct_gep!(builder, LLVM.StructType([llvm_typ]), param, 0)
push!(new_args, ptr)
else
Expand All @@ -339,7 +331,7 @@ function wrap_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.F
for i in 1:length(byval)
attrs = parameter_attributes(new_f, i)
if byval[i]
llvm_typ = convert(LLVMType, args[i].typ)
llvm_typ = convert(LLVMType, types[i])
push!(attrs, TypeAttribute("byval", LLVM.StructType([llvm_typ])))
end
end
Expand Down
Loading