From 8fb120c6d685fcb95f9f8c2b76f0006023f62d65 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Fri, 24 Mar 2023 13:12:15 +0900 Subject: [PATCH 1/3] adjust to JuliaLang/julia#49071 --- test/test_Cthulhu.jl | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/test/test_Cthulhu.jl b/test/test_Cthulhu.jl index e63ad441..b74273bb 100644 --- a/test/test_Cthulhu.jl +++ b/test/test_Cthulhu.jl @@ -19,6 +19,17 @@ function empty_func(::Bool) end anykwargs(a; kwargs...) = println(a, " keyword args: ", kwargs...) hasdefaultargs(a, b=2) = a + b +function first_specialization(m::Method) + specs = m.specializations + if specs isa Core.MethodInstance + return specs + elseif specs isa Core.SimpleVector + return specs[1]::Core.MethodInstance + else + throw((m, specs)) + end +end + @testset "Callsites" begin callsites = find_callsites_by_ftt(testf_simple) @test length(callsites) >= 4 @@ -53,7 +64,7 @@ hasdefaultargs(a, b=2) = a + b # Callsite handling in source-view mode: for kwarg functions, strip the body, and use "typed" callsites for m in (@which(anykwargs("animals")), @which(anykwargs("animals"; cat=1, dog=2))) - mi = m.specializations[1] + mi = first_specialization(m) src, rt = Cthulhu.TypedSyntax.getsrc(mi) tsn, mappings = Cthulhu.get_typed_sourcetext(mi, src, rt; warn=false) str = sprint(printstyled, tsn) @@ -62,7 +73,7 @@ hasdefaultargs(a, b=2) = a + b end # Likewise for methods that fill in default positional arguments m = @which hasdefaultargs(1) - mi = m.specializations[1] + mi = first_specialization(m) src, rt = Cthulhu.TypedSyntax.getsrc(mi) tsn, mappings = Cthulhu.get_typed_sourcetext(mi, src, rt; warn=false) str = sprint(printstyled, tsn) @@ -70,7 +81,7 @@ hasdefaultargs(a, b=2) = a + b @test !occursin("a + b", str) @test isempty(mappings) m = @which hasdefaultargs(1, 5) - mi = m.specializations[1] + mi = first_specialization(m) src, rt = Cthulhu.TypedSyntax.getsrc(mi) tsn, mappings = Cthulhu.get_typed_sourcetext(mi, src, rt; warn=false) str = sprint(printstyled, tsn) @@ -699,8 +710,7 @@ fst5(x) = fst4(x) @testset "backedges and treelist" begin @test fbackedge2(0.2) == 1 @test fbackedge2(-0.2) == -1 - mspec = @which(fbackedge1()).specializations - mi = isa(mspec, Core.SimpleVector) ? mspec[1] : mspec.func + mi = first_specialization(@which(fbackedge1())) root = Cthulhu.treelist(mi) @test Cthulhu.count_open_leaves(root) == 2 @test root.data.callstr == "fbackedge1()" @@ -709,7 +719,7 @@ fst5(x) = fst4(x) # issue #114 unspecva(@nospecialize(i::Int...)) = 1 @test unspecva(1, 2) == 1 - mi = something(first(methods(unspecva)).specializations...) + mi = first_specialization(only(methods(unspecva))) root = Cthulhu.treelist(mi) @test occursin("Vararg", root.data.callstr) From 3245168e202a0e5fa81dcbc7950b13937f486876 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Fri, 24 Mar 2023 13:14:47 +0900 Subject: [PATCH 2/3] adjust to JuliaLang/julia#49113 After JuliaLang/julia#49113, `src.slot[names|types|flags]` may be resized by the optimization pipeline, so we have to make sure to store our own copies of them to keep the pre-optimization state of `CodeInfo`. --- src/codeview.jl | 8 ++++++-- src/interpreter.jl | 8 +++++++- src/reflection.jl | 6 +++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/codeview.jl b/src/codeview.jl index 75744dca..b23272e8 100644 --- a/src/codeview.jl +++ b/src/codeview.jl @@ -332,8 +332,12 @@ function InteractiveUtils.code_typed(b::Bookmark; optimize::Bool=true) (; src, rt, codeinf) = lookup(interp, mi, optimize) src = preprocess_ci!(src, mi, optimize, CONFIG) if src isa IRCode - nargs = Int((mi.def::Method).nargs) - 1 - CC.replace_code_newstyle!(codeinf, src, nargs) + @static if VERSION ≥ v"1.10.0-DEV.870" + CC.replace_code_newstyle!(codeinf, src) + else + nargs = Int((mi.def::Method).nargs) - 1 + CC.replace_code_newstyle!(codeinf, src, nargs) + end end return codeinf => rt end diff --git a/src/interpreter.jl b/src/interpreter.jl index ab9ceb23..ab22ba08 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -138,8 +138,14 @@ end function CC.finish(state::InferenceState, interp::CthulhuInterpreter) res = @invoke CC.finish(state::InferenceState, interp::AbstractInterpreter) key = CC.any(state.result.overridden_by_const) ? state.result : state.linfo + unoptsrc = copy(state.src) + unoptsrc.slotnames = copy(unoptsrc.slotnames) + unoptsrc.slottypes = let slottypes = unoptsrc.slottypes + slottypes === nothing ? nothing : copy(unoptsrc.slottypes) + end + unoptsrc.slotflags = copy(unoptsrc.slotflags) interp.unopt[key] = InferredSource( - copy(state.src), + unoptsrc, copy(state.stmt_info), isdefined(CC, :Effects) ? state.ipo_effects : nothing, state.result.result) diff --git a/src/reflection.jl b/src/reflection.jl index bb815f9f..f2cdc0b6 100644 --- a/src/reflection.jl +++ b/src/reflection.jl @@ -261,7 +261,11 @@ function preprocess_ci!(ci::CodeInfo, mi::MethodInstance, optimize, config::Cthu end ir = CC.inflate_ir(ci, sptypes_from_meth_instance(mi), argtypes) ir = dce!(ir) - ci = CC.replace_code_newstyle!(ci, ir, length(argtypes)-1) + @static if VERSION ≥ v"1.10.0-DEV.870" + ci = CC.replace_code_newstyle!(ci, ir) + else + ci = CC.replace_code_newstyle!(ci, ir, length(argtypes)-1) + end end return ci end From 5f7dac12e2af2b5c0c6f4382d09046b4bc64793a Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Fri, 24 Mar 2023 13:50:32 +0900 Subject: [PATCH 3/3] Update test/test_Cthulhu.jl --- test/test_Cthulhu.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_Cthulhu.jl b/test/test_Cthulhu.jl index b74273bb..583b1ee8 100644 --- a/test/test_Cthulhu.jl +++ b/test/test_Cthulhu.jl @@ -24,7 +24,7 @@ function first_specialization(m::Method) if specs isa Core.MethodInstance return specs elseif specs isa Core.SimpleVector - return specs[1]::Core.MethodInstance + return something(specs...)::Core.MethodInstance else throw((m, specs)) end