Skip to content

Commit 0dce551

Browse files
authored
try to fix 1.7 (#6)
keep 1.6 compat and also adapt to v1.8 changes
1 parent 33bc3e4 commit 0dce551

File tree

9 files changed

+68
-23
lines changed

9 files changed

+68
-23
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13+
- '1.6'
1314
- '1'
1415
- 'nightly'
1516
os:

src/CompilerPluginTools.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ using Core.Compiler:
113113
isconcretetype,
114114
widenconst,
115115
isdispatchtuple,
116-
isinlineable,
117116
is_inlineable_constant,
118117
copy_exprargs,
119118
convert_to_ircode,
@@ -129,14 +128,14 @@ using Core.Compiler:
129128
# Julia passes
130129
compact!,
131130
ssa_inlining_pass!,
132-
getfield_elim_pass!,
133131
adce_pass!,
134132
type_lift_pass!,
135133
verify_linetable,
136134
verify_ir,
137135
retrieve_code_info,
138136
slot2reg
139137

138+
include("compat.jl")
140139
include("utils.jl")
141140
include("patches.jl")
142141
include("codeinfo.jl")

src/compat.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@static if VERSION < v"1.7-"
2+
ismutabletype(::Type{T}) where T = T.mutable
3+
end

src/interp.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ Core.Compiler.add_remark!(::JuliaLikeInterpreter, ::Core.Compiler.InferenceState
3838
end
3939

4040
function Core.Compiler.optimize(interp::JuliaLikeInterpreter, opt::OptimizationState, params::OptimizationParams, @nospecialize(result))
41-
nargs = Int(opt.nargs) - 1
42-
ir = Core.Compiler.run_passes(opt.src, nargs, opt)
41+
@static if VERSION < v"1.8-"
42+
nargs = Int(opt.nargs) - 1
43+
ir = Core.Compiler.run_passes(opt.src, nargs, opt)
44+
else
45+
ir = Core.Compiler.run_passes(opt.src, opt)
46+
end
47+
4348
ir = optimize(interp, opt, ir)
44-
if VERSION < v"1.7-DEV"
49+
@static if VERSION < v"1.7-DEV"
4550
Core.Compiler.finish(opt, params, ir, result)
4651
else
4752
Core.Compiler.finish(interp, opt, params, ir, result)

src/ircode.jl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,39 @@ Return the `IRCode` object along with inferred return type.
4848
function code_ircode_by_mi(f, mi::MethodInstance; world=get_world_counter(), interp=NativeInterpreter(world))
4949
return typeinf_lock() do
5050
result = Core.Compiler.InferenceResult(mi)
51-
frame = Core.Compiler.InferenceState(result, false, interp)
51+
@static if VERSION < v"1.8-"
52+
frame = Core.Compiler.InferenceState(result, false, interp)
53+
else
54+
frame = Core.Compiler.InferenceState(result, :local, interp)
55+
end
5256
frame === nothing && return nothing
5357

5458
if typeinf(interp, frame)
5559
opt_params = OptimizationParams(interp)
56-
opt = OptimizationState(frame, opt_params, interp)
57-
preserve_coverage = coverage_enabled(opt.mod)
58-
ci = opt.src; nargs = opt.nargs - 1;
59-
ir = convert_to_ircode(ci, copy_exprargs(ci.code), preserve_coverage, nargs, opt)
60-
ir = slot2reg(ir, ci, nargs, opt)
60+
sv = OptimizationState(frame, opt_params, interp)
61+
preserve_coverage = coverage_enabled(sv.mod)
62+
@static if VERSION < v"1.8-"
63+
ci = sv.src; nargs = sv.nargs - 1;
64+
ir = convert_to_ircode(ci, copy_exprargs(ci.code), preserve_coverage, nargs, sv)
65+
ir = slot2reg(ir, ci, nargs, sv)
66+
else
67+
ci = sv.src
68+
ir = convert_to_ircode(ci, sv)
69+
ir = slot2reg(ir, ci, sv)
70+
end
6171
# passes
62-
ir = f(ir, opt)
63-
opt.src.inferred = true
72+
ir = f(ir, sv)
73+
sv.src.inferred = true
6474
end
6575

6676
frame.inferred || return nothing
67-
# TODO(yhls): Fix this upstream
68-
resize!(ir.argtypes, opt.nargs)
77+
@static if VERSION < v"1.8-"
78+
resize!(ir.argtypes, sv.nargs)
79+
else
80+
svdef = sv.linfo.def
81+
nargs = isa(svdef, Method) ? Int(svdef.nargs) : 0
82+
resize!(ir.argtypes, nargs)
83+
end
6984
return ir => widenconst(result.result)
7085
end
7186
end

src/passes.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ function default_julia_pass(ir::IRCode, sv::OptimizationState)
1414
ir = compact!(ir)
1515
ir = ssa_inlining_pass!(ir, ir.linetable, sv.inlining, sv.src.propagate_inbounds)
1616
ir = compact!(ir)
17-
ir = getfield_elim_pass!(ir)
17+
18+
@static if VERSION < v"1.8-"
19+
ir = Core.Compiler.getfield_elim_pass!(ir)
20+
else
21+
ir = Core.Compiler.sroa_pass!(ir)
22+
end
23+
1824
ir = adce_pass!(ir)
1925
ir = type_lift_pass!(ir)
2026
ir = compact!(ir)
@@ -77,9 +83,15 @@ function permute_stmts!(ir::IRCode, perm::Vector{Int})
7783
return ir
7884
end
7985

86+
@static if VERSION < v"1.8-"
87+
argtypes(sig::Signature) = sig.atypes
88+
else
89+
argtypes(sig::Signature) = sig.argtypes
90+
end
91+
8092
function is_allconst(sig::Signature)
8193
allconst = true
82-
for atype in sig.atypes
94+
for atype in argtypes(sig)
8395
if !isa(atype, Const)
8496
allconst = false
8597
break
@@ -101,7 +113,7 @@ end
101113

102114
function is_const_call_inlineable(sig::Signature)
103115
is_allconst(sig) || return false
104-
f, ft, atypes = sig.f, sig.ft, sig.atypes
116+
f, ft, atypes = sig.f, sig.ft, argtypes(sig)
105117

106118
if isa(f, IntrinsicFunction) && is_pure_intrinsic_infer(f) && intrinsic_nothrow(f, atypes[2:end])
107119
return true
@@ -143,7 +155,7 @@ function inline_const!(ir::IRCode)
143155
sig = Core.Compiler.call_sig(ir, new_stmt)
144156
sig === nothing && continue
145157
if is_const_call_inlineable(sig)
146-
fargs = anymap(x::Const -> x.val, sig.atypes[2:end])
158+
fargs = anymap(x::Const -> x.val, argtypes(sig)[2:end])
147159
val = sig.f(fargs...)
148160
ir.stmts[i][:inst] = quoted(val)
149161
ir.stmts[i][:type] = Const(val)
@@ -154,7 +166,7 @@ function inline_const!(ir::IRCode)
154166
ir.stmts[i][:inst] = Expr(:invoke, mi, f, map(eval_global, args)...)
155167
@case Expr(:new, t, args...)
156168
allconst = all(x->is_arg_allconst(ir, x), args)
157-
allconst && isconcretetype(t) && !t.mutable || continue
169+
allconst && isconcretetype(t) && !ismutabletype(t) || continue
158170
args = anymap(arg->unwrap_arg(ir, arg), args)
159171
val = ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), t, args, length(args))
160172
ir.stmts[i][:inst] = quoted(val)

src/patches.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ Base.getindex(ref::UseRef) = Core.Compiler.getindex(ref)
9797
Base.iterate(uses::UseRefIterator) = Core.Compiler.iterate(uses)
9898
Base.iterate(uses::UseRefIterator, st) = Core.Compiler.iterate(uses, st)
9999

100+
@static if VERSION < v"1.8-"
100101
Base.iterate(p::Core.Compiler.Pair) = Core.Compiler.iterate(p)
101102
Base.iterate(p::Core.Compiler.Pair, st) = Core.Compiler.iterate(p, st)
103+
end
102104

103105
Base.getindex(m::Core.Compiler.MethodLookupResult, idx::Int) = Core.Compiler.getindex(m, idx)
104106

test/interp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ function CompilerPluginTools.optimize(interp::TestInterpreter, ir::IRCode)
1010
return ir
1111
end
1212

13-
code_ircode(sin, (Float64, ); interp=TestInterpreter(NativeInterpreter()))
13+
code_ircode(sin, (Float64, ); interp=NativeInterpreter())
1414
code_ircode_by_signature(Tuple{typeof(sin), Float64}; interp=TestInterpreter(NativeInterpreter()))

test/passes.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ end
1313
GlobalRef(Main, :GLOBAL_CONST)::Float64
1414
ReturnNode(SSAValue(1))::Float64
1515
end
16-
16+
1717
ir = inline_const!(ir)
1818
@test ir.stmts[1][:inst] == 1.0
1919
@test ir.stmts[1][:type] == Const(1.0)
@@ -25,7 +25,7 @@ end
2525
Expr(:new, Foo, 2)::Foo
2626
ReturnNode(SSAValue(1))::Float64
2727
end
28-
28+
2929
ir = inline_const!(ir)
3030

3131
@test ir.stmts[2][:inst] == QuoteNode(Foo(2))
@@ -49,6 +49,14 @@ end
4949
@test ir.stmts[1][:type] == Const((1, 2, 3))
5050
end
5151

52+
ir = @make_ircode begin
53+
Expr(:call, Core.Intrinsics.abs_float, 1.0)::Float64
54+
Expr(:new, Foo, 2)::Foo
55+
ReturnNode(SSAValue(1))::Float64
56+
end
57+
58+
ir = inline_const!(ir)
59+
5260
@testset "permute_stmts!" begin
5361
ir = @make_ircode begin
5462
QuoteNode(1.0)::Const(1.0)

0 commit comments

Comments
 (0)