Skip to content

Commit 78fd186

Browse files
KenoaviateskKristofferC
authored
Make Compiler tests runnable as package (#56632)
Makes `test Compiler` work properly (as in use the Compiler package, not Base.Compiler) and pass tests, but still needs to be made parallel in a follow-on. --------- Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com> Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com>
1 parent 712b2e5 commit 78fd186

22 files changed

+1174
-1108
lines changed

Compiler/Project.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ version = "0.0.2"
44

55
[compat]
66
julia = "1.10"
7+
8+
[extras]
9+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
10+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
11+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
12+
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
13+
14+
[targets]
15+
test = ["Test", "InteractiveUtils", "Random", "Libdl"]

Compiler/test/AbstractInterpreter.jl

Lines changed: 86 additions & 79 deletions
Large diffs are not rendered by default.

Compiler/test/EAUtils.jl

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@ module EAUtils
22

33
export code_escapes, @code_escapes, __clear_cache!
44

5-
const CC = Core.Compiler
5+
if !@isdefined(Compiler)
6+
if Base.identify_package("Compiler") === nothing
7+
import Base.Compiler: Compiler
8+
else
9+
import Compiler
10+
end
11+
end
12+
613
using ..EscapeAnalysis
714
const EA = EscapeAnalysis
815

916
# AbstractInterpreter
1017
# -------------------
1118

1219
# imports
13-
import .CC:
20+
import .Compiler:
1421
AbstractInterpreter, NativeInterpreter, WorldView, WorldRange, InferenceParams,
1522
OptimizationParams, get_world_counter, get_inference_cache, ipo_dataflow_analysis!
1623
# usings
1724
using Core:
1825
CodeInstance, MethodInstance, CodeInfo
19-
using .CC:
26+
using .Compiler:
2027
InferenceResult, InferenceState, OptimizationState, IRCode
2128
using .EA: analyze_escapes, ArgEscapeCache, ArgEscapeInfo, EscapeInfo, EscapeState
2229

2330
struct EAToken end
2431

25-
# when working outside of Core.Compiler,
32+
# when working outside of CC,
2633
# cache entire escape state for later inspection and debugging
2734
struct EscapeCacheInfo
2835
argescapes::ArgEscapeCache
@@ -59,18 +66,18 @@ mutable struct EscapeAnalyzer <: AbstractInterpreter
5966
end
6067
end
6168

62-
CC.InferenceParams(interp::EscapeAnalyzer) = interp.inf_params
63-
CC.OptimizationParams(interp::EscapeAnalyzer) = interp.opt_params
64-
CC.get_inference_world(interp::EscapeAnalyzer) = interp.world
65-
CC.get_inference_cache(interp::EscapeAnalyzer) = interp.inf_cache
66-
CC.cache_owner(::EscapeAnalyzer) = EAToken()
67-
CC.get_escape_cache(interp::EscapeAnalyzer) = GetEscapeCache(interp)
69+
Compiler.InferenceParams(interp::EscapeAnalyzer) = interp.inf_params
70+
Compiler.OptimizationParams(interp::EscapeAnalyzer) = interp.opt_params
71+
Compiler.get_inference_world(interp::EscapeAnalyzer) = interp.world
72+
Compiler.get_inference_cache(interp::EscapeAnalyzer) = interp.inf_cache
73+
Compiler.cache_owner(::EscapeAnalyzer) = EAToken()
74+
Compiler.get_escape_cache(interp::EscapeAnalyzer) = GetEscapeCache(interp)
6875

69-
function CC.ipo_dataflow_analysis!(interp::EscapeAnalyzer, opt::OptimizationState,
76+
function Compiler.ipo_dataflow_analysis!(interp::EscapeAnalyzer, opt::OptimizationState,
7077
ir::IRCode, caller::InferenceResult)
7178
# run EA on all frames that have been optimized
7279
nargs = Int(opt.src.nargs)
73-
𝕃ₒ = CC.optimizer_lattice(interp)
80+
𝕃ₒ = Compiler.optimizer_lattice(interp)
7481
get_escape_cache = GetEscapeCache(interp)
7582
estate = try
7683
analyze_escapes(ir, nargs, 𝕃ₒ, get_escape_cache)
@@ -82,19 +89,19 @@ function CC.ipo_dataflow_analysis!(interp::EscapeAnalyzer, opt::OptimizationStat
8289
end
8390
if caller.linfo === interp.entry_mi
8491
# return back the result
85-
interp.result = EscapeResultForEntry(CC.copy(ir), estate, caller.linfo)
92+
interp.result = EscapeResultForEntry(Compiler.copy(ir), estate, caller.linfo)
8693
end
8794
record_escapes!(interp, caller, estate, ir)
8895

89-
@invoke CC.ipo_dataflow_analysis!(interp::AbstractInterpreter, opt::OptimizationState,
96+
@invoke Compiler.ipo_dataflow_analysis!(interp::AbstractInterpreter, opt::OptimizationState,
9097
ir::IRCode, caller::InferenceResult)
9198
end
9299

93100
function record_escapes!(interp::EscapeAnalyzer,
94101
caller::InferenceResult, estate::EscapeState, ir::IRCode)
95102
argescapes = ArgEscapeCache(estate)
96103
ecacheinfo = EscapeCacheInfo(argescapes, estate, ir)
97-
return CC.stack_analysis_result!(caller, ecacheinfo)
104+
return Compiler.stack_analysis_result!(caller, ecacheinfo)
98105
end
99106

100107
struct GetEscapeCache
@@ -113,19 +120,19 @@ struct FailedAnalysis
113120
get_escape_cache::GetEscapeCache
114121
end
115122

116-
function CC.finish!(interp::EscapeAnalyzer, state::InferenceState; can_discard_trees::Bool=CC.may_discard_trees(interp))
117-
ecacheinfo = CC.traverse_analysis_results(state.result) do @nospecialize result
123+
function Compiler.finish!(interp::EscapeAnalyzer, state::InferenceState; can_discard_trees::Bool=Compiler.may_discard_trees(interp))
124+
ecacheinfo = Compiler.traverse_analysis_results(state.result) do @nospecialize result
118125
return result isa EscapeCacheInfo ? result : nothing
119126
end
120127
ecacheinfo isa EscapeCacheInfo && (interp.escape_cache.cache[state.linfo] = ecacheinfo)
121-
return @invoke CC.finish!(interp::AbstractInterpreter, state::InferenceState; can_discard_trees)
128+
return @invoke Compiler.finish!(interp::AbstractInterpreter, state::InferenceState; can_discard_trees)
122129
end
123130

124131
# printing
125132
# --------
126133

127134
using Core: Argument, SSAValue
128-
using .CC: widenconst, singleton_type
135+
using .Compiler: widenconst, singleton_type
129136

130137
function get_name_color(x::EscapeInfo, symbol::Bool = false)
131138
getname(x) = string(nameof(x))
@@ -323,15 +330,15 @@ function code_escapes(@nospecialize(f), @nospecialize(types=Base.default_tt(f));
323330
debuginfo::Symbol = :none)
324331
tt = Base.signature_type(f, types)
325332
match = Base._which(tt; world, raise=true)
326-
mi = Core.Compiler.specialize_method(match)
333+
mi = Compiler.specialize_method(match)
327334
return code_escapes(mi; world, debuginfo)
328335
end
329336

330337
function code_escapes(mi::MethodInstance;
331338
world::UInt = get_world_counter(),
332339
interp::EscapeAnalyzer=EscapeAnalyzer(world, GLOBAL_ESCAPE_CACHE; entry_mi=mi),
333340
debuginfo::Symbol = :none)
334-
frame = Core.Compiler.typeinf_frame(interp, mi, #=run_optimizer=#true)
341+
frame = Compiler.typeinf_frame(interp, mi, #=run_optimizer=#true)
335342
isdefined(interp, :result) || error("optimization didn't happen: maybe everything has been constant folded?")
336343
slotnames = let src = frame.src
337344
src isa CodeInfo ? src.slotnames : nothing
@@ -357,7 +364,7 @@ Note that this version does not cache the analysis results.
357364
function code_escapes(ir::IRCode, nargs::Int;
358365
world::UInt = get_world_counter(),
359366
interp::AbstractInterpreter=EscapeAnalyzer(world, EscapeCache()))
360-
estate = analyze_escapes(ir, nargs, CC.optimizer_lattice(interp), CC.get_escape_cache(interp))
367+
estate = analyze_escapes(ir, nargs, Compiler.optimizer_lattice(interp), Compiler.get_escape_cache(interp))
361368
return EscapeResult(ir, estate) # return back the result
362369
end
363370

Compiler/test/EscapeAnalysis.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
module test_EA
22

3-
global use_core_compiler::Bool = true
3+
include("irutils.jl")
44

5-
if use_core_compiler
6-
const EscapeAnalysis = Core.Compiler.EscapeAnalysis
7-
else
8-
include(normpath(Sys.BINDIR, "..", "..", "Compiler", "src", "ssair", "EscapeAnalysis.jl"))
9-
end
5+
const EscapeAnalysis = Compiler.EscapeAnalysis
106

117
include("EAUtils.jl")
12-
include("irutils.jl")
138

149
using Test, .EscapeAnalysis, .EAUtils
1510
using .EscapeAnalysis: ignore_argescape

Compiler/test/codegen.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ using Random
66
using InteractiveUtils
77
using Libdl
88

9+
if !@isdefined(Compiler)
10+
if Base.identify_package("Compiler") === nothing
11+
import Base.Compiler: Compiler
12+
else
13+
import Compiler
14+
end
15+
end
16+
917
const opt_level = Base.JLOptions().opt_level
1018
const coverage = (Base.JLOptions().code_coverage > 0) || (Base.JLOptions().malloc_log > 0)
1119
const Iptr = sizeof(Int) == 8 ? "i64" : "i32"
@@ -181,15 +189,15 @@ end
181189
breakpoint_mutable(a::MutableStruct) = ccall(:jl_breakpoint, Cvoid, (Ref{MutableStruct},), a)
182190

183191
# Allocation with uninitialized field as gcroot
184-
mutable struct BadRef
192+
mutable struct BadRefMutableStruct
185193
x::MutableStruct
186194
y::MutableStruct
187-
BadRef(x) = new(x)
195+
BadRefMutableStruct(x) = new(x)
188196
end
189-
Base.cconvert(::Type{Ptr{BadRef}}, a::MutableStruct) = BadRef(a)
190-
Base.unsafe_convert(::Type{Ptr{BadRef}}, ar::BadRef) = Ptr{BadRef}(pointer_from_objref(ar.x))
197+
Base.cconvert(::Type{Ptr{BadRefMutableStruct}}, a::MutableStruct) = BadRefMutableStruct(a)
198+
Base.unsafe_convert(::Type{Ptr{BadRefMutableStruct}}, ar::BadRefMutableStruct) = Ptr{BadRefMutableStruct}(pointer_from_objref(ar.x))
191199

192-
breakpoint_badref(a::MutableStruct) = ccall(:jl_breakpoint, Cvoid, (Ptr{BadRef},), a)
200+
breakpoint_badref(a::MutableStruct) = ccall(:jl_breakpoint, Cvoid, (Ptr{BadRefMutableStruct},), a)
193201

194202
struct PtrStruct
195203
a::Ptr{Cvoid}
@@ -372,10 +380,9 @@ mktemp() do f_22330, _
372380
end
373381

374382
# Alias scope
375-
using Base.Experimental: @aliasscope, Const
376383
function foo31018!(a, b)
377-
@aliasscope for i in eachindex(a, b)
378-
a[i] = Const(b)[i]
384+
@Base.Experimental.aliasscope for i in eachindex(a, b)
385+
a[i] = Base.Experimental.Const(b)[i]
379386
end
380387
end
381388
io = IOBuffer()
@@ -788,8 +795,8 @@ f47247(a::Ref{Int}, b::Nothing) = setfield!(a, :x, b)
788795
@test_throws TypeError f47247(Ref(5), nothing)
789796

790797
f48085(@nospecialize x...) = length(x)
791-
@test Core.Compiler.get_compileable_sig(which(f48085, (Vararg{Any},)), Tuple{typeof(f48085), Vararg{Int}}, Core.svec()) === nothing
792-
@test Core.Compiler.get_compileable_sig(which(f48085, (Vararg{Any},)), Tuple{typeof(f48085), Int, Vararg{Int}}, Core.svec()) === Tuple{typeof(f48085), Any, Vararg{Any}}
798+
@test Compiler.get_compileable_sig(which(f48085, (Vararg{Any},)), Tuple{typeof(f48085), Vararg{Int}}, Core.svec()) === nothing
799+
@test Compiler.get_compileable_sig(which(f48085, (Vararg{Any},)), Tuple{typeof(f48085), Int, Vararg{Int}}, Core.svec()) === Tuple{typeof(f48085), Any, Vararg{Any}}
793800

794801
# Make sure that the bounds check is elided in tuple iteration
795802
@test !occursin("call void @", strip_debug_calls(get_llvm(iterate, Tuple{NTuple{4, Float64}, Int})))

Compiler/test/compact.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
using Core.Compiler: IncrementalCompact, insert_node_here!, finish,
1+
if !@isdefined(Compiler)
2+
if Base.identify_package("Compiler") === nothing
3+
import Base.Compiler: Compiler
4+
else
5+
import Compiler
6+
end
7+
end
8+
9+
using .Compiler: IncrementalCompact, insert_node_here!, finish,
210
NewInstruction, verify_ir, ReturnNode, SSAValue
311

412
foo_test_function(i) = i == 1 ? 1 : 2
@@ -8,19 +16,19 @@ foo_test_function(i) = i == 1 ? 1 : 2
816
compact = IncrementalCompact(ir)
917

1018
# set up first iterator
11-
x = Core.Compiler.iterate(compact)
12-
x = Core.Compiler.iterate(compact, x[2])
19+
x = Compiler.iterate(compact)
20+
x = Compiler.iterate(compact, x[2])
1321

1422
# set up second iterator
15-
x = Core.Compiler.iterate(compact)
23+
x = Compiler.iterate(compact)
1624

1725
# consume remainder
1826
while x !== nothing
19-
x = Core.Compiler.iterate(compact, x[2])
27+
x = Compiler.iterate(compact, x[2])
2028
end
2129

2230
ir = finish(compact)
23-
@test Core.Compiler.verify_ir(ir) === nothing
31+
@test Compiler.verify_ir(ir) === nothing
2432
end
2533

2634
# Test early finish of IncrementalCompact
@@ -40,7 +48,7 @@ end
4048
@testset "IncrementalCompact reverse affinity insert" begin
4149
ir = only(Base.code_ircode(foo_test_function, (Int,)))[1]
4250
compact = IncrementalCompact(ir)
43-
@test !Core.Compiler.did_just_finish_bb(compact)
51+
@test !Compiler.did_just_finish_bb(compact)
4452

4553
insert_node_here!(compact, NewInstruction(ReturnNode(1), Union{}, ir[SSAValue(1)][:line]), true)
4654
new_ir = finish(compact)

Compiler/test/contextual.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
# N.B.: This file is also run from interpreter.jl, so needs to be standalone-executable
44
using Test
55

6+
if !@isdefined(Compiler)
7+
if Base.identify_package("Compiler") === nothing
8+
import Base.Compiler: Compiler
9+
else
10+
import Compiler
11+
end
12+
end
13+
614
# Cassette
715
# ========
816

@@ -11,7 +19,8 @@ module MiniCassette
1119
# fancy features, but sufficient to exercise this code path in the compiler.
1220

1321
using Core.IR
14-
using Core.Compiler: retrieve_code_info, quoted, anymap
22+
using ..Compiler
23+
using ..Compiler: retrieve_code_info, quoted, anymap
1524
using Base.Meta: isexpr
1625

1726
export Ctx, overdub
@@ -45,7 +54,7 @@ module MiniCassette
4554

4655
function transform!(mi::MethodInstance, ci::CodeInfo, nargs::Int, sparams::Core.SimpleVector)
4756
code = ci.code
48-
di = Core.Compiler.DebugInfoStream(mi, ci.debuginfo, length(code))
57+
di = Compiler.DebugInfoStream(mi, ci.debuginfo, length(code))
4958
ci.slotnames = Symbol[Symbol("#self#"), :ctx, :f, :args, ci.slotnames[nargs+1:end]...]
5059
ci.slotflags = UInt8[(0x00 for i = 1:4)..., ci.slotflags[nargs+1:end]...]
5160
# Insert one SSAValue for every argument statement
@@ -82,7 +91,7 @@ module MiniCassette
8291

8392
tt = Tuple{f, args...}
8493
match = Base._which(tt; world)
85-
mi = Core.Compiler.specialize_method(match)
94+
mi = Base.specialize_method(match)
8695
# Unsupported in this mini-cassette
8796
@assert !mi.def.isva
8897
src = retrieve_code_info(mi, world)

0 commit comments

Comments
 (0)