From 706b2f06ad562a5665a86e2bbb9387bdb1a830d7 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Thu, 29 May 2025 13:30:23 +0900 Subject: [PATCH] use the stdlib version of Compiler instead of `Core.Compiler` This allows `Compiler.CFG` created from tools like JET to be passed directly to LCU then. --- Project.toml | 4 +++- src/codeedges.jl | 8 +++----- src/domtree.jl | 1 - src/packagedef.jl | 9 ++++++--- src/utils.jl | 22 +++++++++++----------- test/codeedges.jl | 3 ++- test/signatures.jl | 2 +- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Project.toml b/Project.toml index 0ba85a8..6019869 100644 --- a/Project.toml +++ b/Project.toml @@ -1,12 +1,14 @@ name = "LoweredCodeUtils" uuid = "6f1432cf-f94c-5a45-995e-cdbf5db27b0b" -version = "3.3.1" +version = "3.4.0" authors = ["Tim Holy "] [deps] +Compiler = "807dbc54-b67e-4c79-8afb-eafe4df6f2e1" JuliaInterpreter = "aa1ae85d-cabe-5617-a682-6adf51b2e16a" [compat] +Compiler = "0.1" JuliaInterpreter = "0.10" julia = "1.10" diff --git a/src/codeedges.jl b/src/codeedges.jl index 91854c5..d508b12 100644 --- a/src/codeedges.jl +++ b/src/codeedges.jl @@ -106,13 +106,13 @@ function print_with_code(preprint, postprint, io::IO, src::CodeInfo) used = BitSet() cfg = compute_basic_blocks(src.code) for stmt in src.code - Core.Compiler.scan_ssa_use!(push!, used, stmt) + CC.scan_ssa_use!(push!, used, stmt) end @static if isdefined(Base, :__has_internal_change) && Base.__has_internal_change(v"1.12-alpha", :printcodeinfocalls) sptypes = let parent = src.parent parent isa MethodInstance ? - Core.Compiler.sptypes_from_meth_instance(parent) : - Core.Compiler.EMPTY_SPTYPES + CC.sptypes_from_meth_instance(parent) : + CC.EMPTY_SPTYPES end end line_info_preprinter = Base.IRShow.lineinfo_disabled @@ -763,8 +763,6 @@ end ## Add control-flow -using Core: CodeInfo -using Core.Compiler: CFG, BasicBlock, compute_basic_blocks # The goal of this function is to request concretization of the minimal necessary control # flow to evaluate statements whose concretization have already been requested. diff --git a/src/domtree.jl b/src/domtree.jl index 90513e4..7412c57 100644 --- a/src/domtree.jl +++ b/src/domtree.jl @@ -5,7 +5,6 @@ # A few items needed to be added: -using Core.Compiler: BasicBlock import Base: length, copy, copy! # END additions diff --git a/src/packagedef.jl b/src/packagedef.jl index c99cedd..a690c1e 100644 --- a/src/packagedef.jl +++ b/src/packagedef.jl @@ -5,11 +5,14 @@ using Core.IR: CodeInfo, GotoIfNot, GotoNode, IR, MethodInstance, ReturnNode @static if isdefined(Core.IR, :EnterNode) using Core.IR: EnterNode end -using Core.Compiler: construct_domtree, construct_postdomtree, nearest_common_dominator, - postdominates +using Compiler: Compiler as CC +using .CC: + BasicBlock, CFG, + compute_basic_blocks, construct_domtree, construct_postdomtree, + nearest_common_dominator, postdominates using Base.Meta: isexpr -const SSAValues = Union{Core.Compiler.SSAValue, JuliaInterpreter.SSAValue} +const SSAValues = Union{Core.IR.SSAValue, JuliaInterpreter.SSAValue} const trackedheads = (:method,) # Revise uses this (for now), don't delete; also update test/hastrackedexpr if this list gets expanded const structdecls = (:_structtype, :_abstracttype, :_primitivetype) diff --git a/src/utils.jl b/src/utils.jl index fa5c658..931cc04 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,15 +1,15 @@ -const AnySSAValue = Union{Core.Compiler.SSAValue,JuliaInterpreter.SSAValue} -const AnySlotNumber = Union{Core.Compiler.SlotNumber,JuliaInterpreter.SlotNumber} +const AnySSAValue = Union{Core.IR.SSAValue,JuliaInterpreter.SSAValue} +const AnySlotNumber = Union{Core.IR.SlotNumber,JuliaInterpreter.SlotNumber} # to circumvent https://github.com/JuliaLang/julia/issues/37342, we inline these `isa` # condition checks at surface AST level # https://github.com/JuliaLang/julia/pull/38905 will get rid of the need of these hacks macro isssa(stmt) - :($(GlobalRef(Core, :isa))($(esc(stmt)), $(GlobalRef(Core.Compiler, :SSAValue))) || + :($(GlobalRef(Core, :isa))($(esc(stmt)), $(GlobalRef(Core.IR, :SSAValue))) || $(GlobalRef(Core, :isa))($(esc(stmt)), $(GlobalRef(JuliaInterpreter, :SSAValue)))) end macro issslotnum(stmt) - :($(GlobalRef(Core, :isa))($(esc(stmt)), $(GlobalRef(Core.Compiler, :SlotNumber))) || + :($(GlobalRef(Core, :isa))($(esc(stmt)), $(GlobalRef(Core.IR, :SlotNumber))) || $(GlobalRef(Core, :isa))($(esc(stmt)), $(GlobalRef(JuliaInterpreter, :SlotNumber)))) end @@ -211,8 +211,8 @@ end showempty(list) = isempty(list) ? '∅' : list -# Smooth the transition between Core.Compiler and Base -rng(bb::Core.Compiler.BasicBlock) = (r = bb.stmts; return Core.Compiler.first(r):Core.Compiler.last(r)) +# Smooth the transition between CC and Base (not requried for v1.12 and above) +rng(bb::BasicBlock) = (r = bb.stmts; return CC.first(r):CC.last(r)) function pushall!(dest, src) for item in src @@ -224,7 +224,7 @@ end # computes strongly connected components of a control flow graph `cfg` # NOTE adapted from https://github.com/JuliaGraphs/Graphs.jl/blob/5878e7be4d68b2a1c179d1367aea670db115ebb5/src/connectivity.jl#L265-L357 # since to load an entire Graphs.jl is a bit cost-ineffective in terms of a trade-off of latency vs. maintainability -function strongly_connected_components(g::Core.Compiler.CFG) +function strongly_connected_components(g::CFG) T = Int zero_t = zero(T) one_t = one(T) @@ -322,12 +322,12 @@ function strongly_connected_components(g::Core.Compiler.CFG) end # compatibility with Graphs.jl interfaces -@inline nv(cfg::Core.Compiler.CFG) = length(cfg.blocks) -@inline vertices(cfg::Core.Compiler.CFG) = 1:nv(cfg) -@inline outneighbors(cfg::Core.Compiler.CFG, v) = cfg.blocks[v].succs +@inline nv(cfg::CFG) = length(cfg.blocks) +@inline vertices(cfg::CFG) = 1:nv(cfg) +@inline outneighbors(cfg::CFG, v) = cfg.blocks[v].succs # using Graphs: SimpleDiGraph, add_edge!, strongly_connected_components as oracle_scc -# function cfg_to_sdg(cfg::Core.Compiler.CFG) +# function cfg_to_sdg(cfg::CFG) # g = SimpleDiGraph(length(cfg.blocks)) # for (v, block) in enumerate(cfg.blocks) # for succ in block.succs diff --git a/test/codeedges.jl b/test/codeedges.jl index 412620a..eea1e01 100644 --- a/test/codeedges.jl +++ b/test/codeedges.jl @@ -2,6 +2,7 @@ module codeedges using LoweredCodeUtils using LoweredCodeUtils.JuliaInterpreter +using LoweredCodeUtils: CC using LoweredCodeUtils: callee_matches, istypedef, exclude_named_typedefs using JuliaInterpreter: is_global_ref, is_quotenode using Test @@ -324,7 +325,7 @@ module ModSelective end src = frame.framecode.src edges = CodeEdges(ModEval, src) isrequired = minimal_evaluation(@nospecialize(stmt)->(LoweredCodeUtils.ismethod3(stmt),false), src, edges; norequire=exclude_named_typedefs(src, edges)) # initially mark only the constructor - bbs = Core.Compiler.compute_basic_blocks(src.code) + bbs = CC.compute_basic_blocks(src.code) for (iblock, block) in enumerate(bbs.blocks) r = LoweredCodeUtils.rng(block) if iblock == length(bbs.blocks) diff --git a/test/signatures.jl b/test/signatures.jl index 259c0ae..c79c8d4 100644 --- a/test/signatures.jl +++ b/test/signatures.jl @@ -319,7 +319,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5 methoddefs!(signatures, frame; define=false) @test !isempty(signatures) - # Inner methods in structs. Comes up in, e.g., Core.Compiler.Params. + # Inner methods in structs. Comes up in, e.g., CC.Params. # The body of CustomMS is an SSAValue. ex = quote struct MyStructWithMeth