Skip to content

Commit dfee088

Browse files
authored
Updates for LLVM 5. (#416)
1 parent df484de commit dfee088

File tree

12 files changed

+88
-82
lines changed

12 files changed

+88
-82
lines changed

Manifest.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ version = "1.4.1"
3939

4040
[[LLVM]]
4141
deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"]
42-
git-tree-sha1 = "ee3374fcbdcd90fb10b27f1d847af7ade3a3213b"
43-
repo-rev = "master"
44-
repo-url = "https://github.com/maleadt/LLVM.jl.git"
42+
git-tree-sha1 = "a8960cae30b42b66dd41808beb76490519f6f9e2"
4543
uuid = "929cbde3-209d-540e-8aea-75f648917ca0"
4644
version = "5.0.0"
4745

4846
[[LLVMExtra_jll]]
4947
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "TOML"]
50-
git-tree-sha1 = "e46e3a40daddcbe851f86db0ec4a4a3d4badf800"
48+
git-tree-sha1 = "09b7505cc0b1cee87e5d4a26eea61d2e1b0dcd35"
5149
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
52-
version = "0.0.19+0"
50+
version = "0.0.21+0"
5351

5452
[[LazyArtifacts]]
5553
deps = ["Artifacts", "Pkg"]

src/driver.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ const __llvm_initialized = Ref(false)
302302
# insert a pointer to the function everywhere the entry is used
303303
T_ptr = convert(LLVMType, Ptr{Cvoid}; ctx=unwrap_context(ctx))
304304
for call in worklist[dyn_job]
305-
@dispose builder=Builder(unwrap_context(ctx)) begin
305+
@dispose builder=IRBuilder(unwrap_context(ctx)) begin
306306
position!(builder, call)
307307
fptr = ptrtoint!(builder, dyn_entry, T_ptr)
308308
replace_uses!(call, fptr)

src/gcn.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function lower_throw_extra!(mod::LLVM.Module)
124124
call = user(use)::LLVM.CallInst
125125

126126
# replace the throw with a trap
127-
@dispose builder=Builder(ctx) begin
127+
@dispose builder=IRBuilder(ctx) begin
128128
position!(builder, call)
129129
emit_exception!(builder, f_name, call)
130130
end
@@ -168,12 +168,12 @@ function fix_alloca_addrspace!(fn::LLVM.Function)
168168
for bb in blocks(fn)
169169
for insn in instructions(bb)
170170
if isa(insn, LLVM.AllocaInst)
171-
ty = llvmtype(insn)
171+
ty = value_type(insn)
172172
ety = eltype(ty)
173173
addrspace(ty) == alloca_as && continue
174174

175175
new_insn = nothing
176-
@dispose builder=Builder(ctx) begin
176+
@dispose builder=IRBuilder(ctx) begin
177177
position!(builder, insn)
178178
_alloca = alloca!(builder, ety, name(insn))
179179
new_insn = addrspacecast!(builder, _alloca, ty)
@@ -189,10 +189,11 @@ end
189189

190190
function emit_trap!(job::CompilerJob{GCNCompilerTarget}, builder, mod, inst)
191191
ctx = context(mod)
192+
trap_ft = LLVM.FunctionType(LLVM.VoidType(ctx))
192193
trap = if haskey(functions(mod), "llvm.trap")
193194
functions(mod)["llvm.trap"]
194195
else
195-
LLVM.Function(mod, "llvm.trap", LLVM.FunctionType(LLVM.VoidType(ctx)))
196+
LLVM.Function(mod, "llvm.trap", trap_ft)
196197
end
197198
if Base.libllvm_version < v"9"
198199
rl_ft = LLVM.FunctionType(LLVM.Int32Type(ctx),
@@ -209,9 +210,9 @@ function emit_trap!(job::CompilerJob{GCNCompilerTarget}, builder, mod, inst)
209210
# this, the target will only attempt to do a "masked branch", which
210211
# only works on vector instructions (trap is a scalar instruction, and
211212
# therefore it is executed even when EXEC==0).
212-
rl_val = call!(builder, rl, [ConstantInt(Int32(32); ctx)])
213+
rl_val = call!(builder, rl_ft, rl, [ConstantInt(Int32(32); ctx)])
213214
rl_bc = inttoptr!(builder, rl_val, LLVM.PointerType(LLVM.Int32Type(ctx)))
214215
store!(builder, rl_val, rl_bc)
215216
end
216-
call!(builder, trap)
217+
call!(builder, trap_ft, trap)
217218
end

src/interface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function process_entry!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
214214
if job.config.kernel && needs_byval(job)
215215
# pass all bitstypes by value; by default Julia passes aggregates by reference
216216
# (this improves performance, and is mandated by certain back-ends like SPIR-V).
217-
args = classify_arguments(job, eltype(llvmtype(entry)))
217+
args = classify_arguments(job, function_type(entry))
218218
for arg in args
219219
if arg.cc == BITS_REF
220220
attr = if LLVM.version() >= v"12"

src/irgen.jl

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function lower_throw!(mod::LLVM.Module)
250250
call = user(use)::LLVM.CallInst
251251

252252
# replace the throw with a PTX-compatible exception
253-
@dispose builder=Builder(ctx) begin
253+
@dispose builder=IRBuilder(ctx) begin
254254
position!(builder, call)
255255
emit_exception!(builder, name, call)
256256
end
@@ -330,12 +330,13 @@ end
330330

331331
function emit_trap!(@nospecialize(job::CompilerJob), builder, mod, inst)
332332
ctx = context(mod)
333+
trap_ft = LLVM.FunctionType(LLVM.VoidType(ctx))
333334
trap = if haskey(functions(mod), "llvm.trap")
334335
functions(mod)["llvm.trap"]
335336
else
336-
LLVM.Function(mod, "llvm.trap", LLVM.FunctionType(LLVM.VoidType(ctx)))
337+
LLVM.Function(mod, "llvm.trap", trap_ft)
337338
end
338-
call!(builder, trap)
339+
call!(builder, trap_ft, trap)
339340
end
340341

341342

@@ -435,8 +436,8 @@ end
435436
# https://reviews.llvm.org/D79744
436437
function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.Function)
437438
ctx = context(mod)
438-
ft = eltype(llvmtype(f))
439-
@compiler_assert LLVM.return_type(ft) == LLVM.VoidType(ctx) job
439+
ft = function_type(f)
440+
@compiler_assert return_type(ft) == LLVM.VoidType(ctx) job
440441
@timeit_debug to "lower byval" begin
441442

442443
# find the byval parameters
@@ -500,7 +501,7 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
500501
push!(new_types, param)
501502
end
502503
end
503-
new_ft = LLVM.FunctionType(LLVM.return_type(ft), new_types)
504+
new_ft = LLVM.FunctionType(return_type(ft), new_types)
504505
new_f = LLVM.Function(mod, "", new_ft)
505506
linkage!(new_f, linkage(f))
506507
for (arg, new_arg) in zip(parameters(f), parameters(new_f))
@@ -509,7 +510,7 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
509510

510511
# emit IR performing the "conversions"
511512
new_args = LLVM.Value[]
512-
@dispose builder=Builder(ctx) begin
513+
@dispose builder=IRBuilder(ctx) begin
513514
entry = BasicBlock(new_f, "conversion"; ctx)
514515
position!(builder, entry)
515516

@@ -591,6 +592,7 @@ function add_kernel_state!(mod::LLVM.Module)
591592
# intrinsic returning an opaque pointer to the kernel state.
592593
# this is both for extern uses, and to make this transformation a two-step process.
593594
state_intr = kernel_state_intr(mod, T_state)
595+
state_intr_ft = LLVM.FunctionType(T_state)
594596

595597
kernels = []
596598
kernels_md = metadata(mod)["julia.kernel"]
@@ -636,12 +638,12 @@ function add_kernel_state!(mod::LLVM.Module)
636638
workmap = Dict{LLVM.Function, LLVM.Function}()
637639
for f in worklist
638640
fn = LLVM.name(f)
639-
ft = eltype(llvmtype(f))
641+
ft = function_type(f)
640642
LLVM.name!(f, fn * ".stateless")
641643

642644
# create a new function
643645
new_param_types = [T_state, parameters(ft)...]
644-
new_ft = LLVM.FunctionType(LLVM.return_type(ft), new_param_types)
646+
new_ft = LLVM.FunctionType(return_type(ft), new_param_types)
645647
new_f = LLVM.Function(mod, fn, new_ft)
646648
LLVM.name!(parameters(new_f)[1], "state")
647649
linkage!(new_f, linkage(f))
@@ -671,15 +673,15 @@ function add_kernel_state!(mod::LLVM.Module)
671673
#
672674
# XXX: ptrtoint/inttoptr pairs can also lose the state argument...
673675
# is all this even sound?
674-
typ = llvmtype(val)::LLVM.PointerType
676+
typ = value_type(val)::LLVM.PointerType
675677
ft = eltype(typ)::LLVM.FunctionType
676-
new_ft = LLVM.FunctionType(LLVM.return_type(ft), [T_state, parameters(ft)...])
678+
new_ft = LLVM.FunctionType(return_type(ft), [T_state, parameters(ft)...])
677679
return const_bitcast(workmap[target], LLVM.PointerType(new_ft, addrspace(typ)))
678680
end
679681
elseif opcode(val) == LLVM.API.LLVMPtrToInt
680682
target = operands(val)[1]
681683
if target isa LLVM.Function && haskey(workmap, target)
682-
return const_ptrtoint(workmap[target], llvmtype(val))
684+
return const_ptrtoint(workmap[target], value_type(val))
683685
end
684686
end
685687
end
@@ -720,9 +722,9 @@ function add_kernel_state!(mod::LLVM.Module)
720722
end
721723

722724
# update uses of the new function, modifying call sites to include the kernel state
723-
function rewrite_uses!(f)
725+
function rewrite_uses!(f, ft)
724726
# update uses
725-
@dispose builder=Builder(ctx) begin
727+
@dispose builder=IRBuilder(ctx) begin
726728
for use in uses(f)
727729
val = user(use)
728730
if val isa LLVM.CallBase && called_value(val) == f
@@ -739,9 +741,9 @@ function add_kernel_state!(mod::LLVM.Module)
739741

740742
# forward the state argument
741743
position!(builder, val)
742-
state = call!(builder, state_intr, Value[], "state")
744+
state = call!(builder, state_intr_ft, state_intr, Value[], "state")
743745
new_val = if val isa LLVM.CallInst
744-
call!(builder, f, [state, arguments(val)...], operand_bundles(val))
746+
call!(builder, ft, f, [state, arguments(val)...], operand_bundles(val))
745747
else
746748
# TODO: invoke and callbr
747749
error("Rewrite of $(typeof(val))-based calls is not implemented: $val")
@@ -757,15 +759,16 @@ function add_kernel_state!(mod::LLVM.Module)
757759
elseif val isa LLVM.StoreInst
758760
# the function is being stored, which again we'll permit like before.
759761
elseif val isa ConstantExpr
760-
rewrite_uses!(val)
762+
rewrite_uses!(val, ft)
761763
else
762764
error("Cannot rewrite $(typeof(val)) use of function: $val")
763765
end
764766
end
765767
end
766768
end
767769
for f in values(workmap)
768-
rewrite_uses!(f)
770+
ft = function_type(f)
771+
rewrite_uses!(f, ft)
769772
end
770773

771774
return true
@@ -791,7 +794,7 @@ function lower_kernel_state!(fun::LLVM.Function)
791794
state_intr = functions(mod)["julia.gpu.state_getter"]
792795
state_arg = nothing # only look-up when needed
793796

794-
@dispose builder=Builder(ctx) begin
797+
@dispose builder=IRBuilder(ctx) begin
795798
for use in uses(state_intr)
796799
inst = user(use)
797800
@assert inst isa LLVM.CallInst
@@ -807,7 +810,7 @@ function lower_kernel_state!(fun::LLVM.Function)
807810
# the function, but only when this function needs the state!
808811
state_arg = parameters(fun)[1]
809812
T_state = convert(LLVMType, state; ctx)
810-
@assert llvmtype(state_arg) == T_state
813+
@assert value_type(state_arg) == T_state
811814
end
812815

813816
replace_uses!(inst, state_arg)
@@ -865,13 +868,14 @@ function kernel_state_value(state)
865868

866869
# get intrinsic
867870
state_intr = kernel_state_intr(mod, T_state)
871+
state_intr_ft = function_type(state_intr)
868872

869873
# generate IR
870-
@dispose builder=Builder(ctx) begin
874+
@dispose builder=IRBuilder(ctx) begin
871875
entry = BasicBlock(llvm_f, "entry"; ctx)
872876
position!(builder, entry)
873877

874-
val = call!(builder, state_intr, Value[], "state")
878+
val = call!(builder, state_intr_ft, state_intr, Value[], "state")
875879

876880
ret!(builder, val)
877881
end

0 commit comments

Comments
 (0)