From 5e2513e54afd9c4b763c86f68f5e4ac7f0fe6ac2 Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Sun, 1 Sep 2024 12:10:10 +0200 Subject: [PATCH 1/4] adapt to JuliaLang/julia#54816 I'm not sure whether this is the correct way to do it, but this fixes #586 locally for me. --- src/Cthulhu.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Cthulhu.jl b/src/Cthulhu.jl index 814ff2ac..6a4f9ebe 100644 --- a/src/Cthulhu.jl +++ b/src/Cthulhu.jl @@ -828,10 +828,14 @@ end function do_typeinf!(interp::AbstractInterpreter, mi::MethodInstance) result = InferenceResult(mi) + @static if isdefined(CC, :engine_reserve) + ci = CC.engine_reserve(interp, mi) + result.ci = ci + end # we may want to handle the case when `InferenceState(...)` returns `nothing`, # which indicates code generation of a `@generated` has been failed, # and show it in the UI in some way? - frame = InferenceState(result, #=cache=#:global, interp)::InferenceState + frame = InferenceState(result, #=cache_mode=#:global, interp)::InferenceState CC.typeinf(interp, frame) return nothing end From bd6d8cca2d2c45dace0f83fa0a7e5e1d38a99476 Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Sun, 1 Sep 2024 13:07:52 +0200 Subject: [PATCH 2/4] fix effect free test case --- test/test_Cthulhu.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/test_Cthulhu.jl b/test/test_Cthulhu.jl index f0b52db6..64dc7148 100644 --- a/test/test_Cthulhu.jl +++ b/test/test_Cthulhu.jl @@ -216,11 +216,7 @@ uncached_call3(a) = uncached_call1(Type{a}) @test isa(ci, Cthulhu.UncachedCallInfo) effects = Cthulhu.get_effects(ci) @test !Core.Compiler.is_consistent(effects) - @static if VERSION ≥ v"1.11.0-DEV.392" - @test !Core.Compiler.is_effect_free(effects) - else - @test Core.Compiler.is_effect_free(effects) - end + @test Core.Compiler.is_effect_free(effects) @test !Core.Compiler.is_nothrow(effects) @test !Core.Compiler.is_terminates(effects) @test Cthulhu.is_callsite(ci, ci.wrapped.mi) From 4591fd5bf8dd0522d6bbf60f4036001cc237259e Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Sun, 1 Sep 2024 13:12:11 +0200 Subject: [PATCH 3/4] adapt to JuliaLang/julia#54655 --- src/Cthulhu.jl | 7 +- src/codeview.jl | 206 +++++++++++++++++++++++++++++------------- test/test_codeview.jl | 7 +- 3 files changed, 157 insertions(+), 63 deletions(-) diff --git a/src/Cthulhu.jl b/src/Cthulhu.jl index 6a4f9ebe..9c388be6 100644 --- a/src/Cthulhu.jl +++ b/src/Cthulhu.jl @@ -800,7 +800,12 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs view_cmd = CODEVIEWS[toggle] world = get_inference_world(interp) println(iostream) - view_cmd(iostream, mi, optimize, debuginfo, world, CONFIG) + @static if VERSION < v"1.12.0-DEV.669" + view_cmd(iostream, mi, optimize, debuginfo, world, CONFIG) + else + src = Core.Compiler.typeinf_code(interp, mi, true) + view_cmd(iostream, mi, src, optimize, debuginfo, world, CONFIG) + end display_CI = false else local i = findfirst(ct->ct.toggle === toggle, custom_toggles) diff --git a/src/codeview.jl b/src/codeview.jl index bc55aa42..740a4fbc 100644 --- a/src/codeview.jl +++ b/src/codeview.jl @@ -20,33 +20,68 @@ function highlight(io, x, lexer, config::CthulhuConfig) end end -function cthulhu_llvm(io::IO, mi, optimize::Bool, debuginfo, world::UInt, - config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) - dump = InteractiveUtils._dump_function_llvm( - mi, world, - #=wrapper=# false, !raw, - dump_module, optimize, debuginfo != DInfo.none ? :source : :none, - Base.CodegenParams()) - highlight(io, dump, "llvm", config) -end - -function cthulhu_native(io::IO, mi, ::Bool, debuginfo, world::UInt, - config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) - if dump_module - dump = InteractiveUtils._dump_function_native_assembly( +@static if VERSION < v"1.12.0-DEV.669" + function cthulhu_llvm(io::IO, mi, optimize::Bool, debuginfo, world::UInt, + config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) + dump = InteractiveUtils._dump_function_llvm( mi, world, - #=wrapper=# false, #=syntax=# config.asm_syntax, - debuginfo != DInfo.none ? :source : :none, - #=binary=# false, raw, + #=wrapper=# false, !raw, + dump_module, optimize, debuginfo != DInfo.none ? :source : :none, Base.CodegenParams()) - else - dump = InteractiveUtils._dump_function_native_disassembly( - mi, world, - #=wrapper=# false, #=syntax=# config.asm_syntax, - debuginfo != DInfo.none ? :source : :none, - #=binary=# false) + highlight(io, dump, "llvm", config) + end + + function cthulhu_native(io::IO, mi, ::Bool, debuginfo, world::UInt, + config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) + if dump_module + dump = InteractiveUtils._dump_function_native_assembly( + mi, world, + #=wrapper=# false, #=syntax=# config.asm_syntax, + debuginfo != DInfo.none ? :source : :none, + #=binary=# false, raw, + Base.CodegenParams()) + else + dump = InteractiveUtils._dump_function_native_disassembly( + mi, world, + #=wrapper=# false, #=syntax=# config.asm_syntax, + debuginfo != DInfo.none ? :source : :none, + #=binary=# false) + end + highlight(io, dump, "asm", config) + end +else + function cthulhu_llvm(io::IO, mi, src::CodeInfo, optimize::Bool, debuginfo, world::UInt, + config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) + dump = InteractiveUtils._dump_function_llvm( + mi, src, + #=wrapper=# false, !raw, + dump_module, optimize, debuginfo != DInfo.none ? :source : :none, + Base.CodegenParams()) + highlight(io, dump, "llvm", config) + end + + function cthulhu_native(io::IO, mi, src::CodeInfo, ::Bool, debuginfo, world::UInt, + config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) + if dump_module + dump = InteractiveUtils._dump_function_native_assembly( + mi, src, + #=wrapper=# false, #=syntax=# config.asm_syntax, + debuginfo != DInfo.none ? :source : :none, + #=binary=# false, raw, + Base.CodegenParams()) + else + dump = InteractiveUtils._dump_function_native_disassembly( + mi, world, + #=wrapper=# false, #=syntax=# config.asm_syntax, + debuginfo != DInfo.none ? :source : :none, + #=binary=# false) + end + highlight(io, dump, "asm", config) + end + + function cthulhu_ast(io::IO, mi, ::CodeInfo, optimize::Bool, debuginfo, world::UInt, config::CthulhuConfig) + return cthulhu_ast(io, mi, optimize, debuginfo, world, config) end - highlight(io, dump, "asm", config) end function cthulhu_ast(io::IO, mi, ::Bool, debuginfo, ::UInt, config::CthulhuConfig) @@ -461,42 +496,91 @@ function InteractiveUtils.code_warntype( end InteractiveUtils.code_llvm(b::Bookmark; kw...) = InteractiveUtils.code_llvm(stdout::IO, b; kw...) -InteractiveUtils.code_llvm( - io::IO, - b::Bookmark; - optimize = true, - debuginfo = :source, - dump_module = false, - raw = false, - config = CONFIG, -) = cthulhu_llvm( - io, - b.mi, - optimize, - debuginfo === :source, - get_inference_world(b.interp), - config, - dump_module, - raw, -) - InteractiveUtils.code_native(b::Bookmark; kw...) = InteractiveUtils.code_native(stdout::IO, b; kw...) -InteractiveUtils.code_native( - io::IO, - b::Bookmark; - optimize = true, - debuginfo = :source, - dump_module = false, - raw = false, - config = CONFIG, -) = cthulhu_native( - io, - b.mi, - optimize, - debuginfo === :source, - get_inference_world(b.interp), - config, - dump_module, - raw, -) + +@static if VERSION < v"1.12.0-DEV.669" + InteractiveUtils.code_llvm( + io::IO, + b::Bookmark; + optimize = true, + debuginfo = :source, + dump_module = false, + raw = false, + config = CONFIG, + ) = cthulhu_llvm( + io, + b.mi, + optimize, + debuginfo === :source, + get_inference_world(b.interp), + config, + dump_module, + raw, + ) + + InteractiveUtils.code_native( + io::IO, + b::Bookmark; + optimize = true, + debuginfo = :source, + dump_module = false, + raw = false, + config = CONFIG, + ) = cthulhu_native( + io, + b.mi, + optimize, + debuginfo === :source, + get_inference_world(b.interp), + config, + dump_module, + raw, + ) +else + function InteractiveUtils.code_llvm( + io::IO, + b::Bookmark; + optimize = true, + debuginfo = :source, + dump_module = false, + raw = false, + config = CONFIG, + ) + src = Core.Compiler.typeinf_code(b.interp, b.mi, true) + return cthulhu_llvm( + io, + b.mi, + src, + optimize, + debuginfo === :source, + get_inference_world(b.interp), + config, + dump_module, + raw, + ) + end + + function InteractiveUtils.code_native( + io::IO, + b::Bookmark; + optimize = true, + debuginfo = :source, + dump_module = false, + raw = false, + config = CONFIG, + ) + src = Core.Compiler.typeinf_code(b.interp, b.mi, true) + return cthulhu_native( + io, + b.mi, + src, + optimize, + debuginfo === :source, + get_inference_world(b.interp), + config, + dump_module, + raw, + ) + end +end diff --git a/test/test_codeview.jl b/test/test_codeview.jl index 1c95b63d..13f6ff7c 100644 --- a/test/test_codeview.jl +++ b/test/test_codeview.jl @@ -22,7 +22,12 @@ Revise.track(TestCodeViewSandbox, normpath(@__DIR__, "TestCodeViewSandbox.jl")) config = Cthulhu.CONFIG io = IOBuffer() - codeview(io, mi, optimize, debuginfo, Cthulhu.get_inference_world(interp), config) + @static if VERSION < v"1.12.0-DEV.669" + codeview(io, mi, optimize, debuginfo, Cthulhu.get_inference_world(interp), config) + else + src = Core.Compiler.typeinf_code(interp, mi, true) + codeview(io, mi, src, optimize, debuginfo, Cthulhu.get_inference_world(interp), config) + end @test !isempty(String(take!(io))) # just check it works end end From f083157402ee0ed5c909358d61814b545e2761f9 Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Sun, 1 Sep 2024 13:19:15 +0200 Subject: [PATCH 4/4] fix TypedSyntax --- TypedSyntax/src/node.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TypedSyntax/src/node.jl b/TypedSyntax/src/node.jl index c83f07e0..d464d34c 100644 --- a/TypedSyntax/src/node.jl +++ b/TypedSyntax/src/node.jl @@ -357,7 +357,12 @@ function code_typed1_by_method_instance(mi::MethodInstance; (ccall(:jl_is_in_pure_context, Bool, ()) || world == typemax(UInt)) && error("code reflection should not be used from generated functions") debuginfo = Base.IRShow.debuginfo(debuginfo) - code, rt = Core.Compiler.typeinf_code(interp, mi.def::Method, mi.specTypes, mi.sparam_vals, optimize) + @static if VERSION < v"1.12.0-DEV.669" + code, rt = Core.Compiler.typeinf_code(interp, mi.def::Method, mi.specTypes, mi.sparam_vals, optimize) + else + code = Core.Compiler.typeinf_code(interp, mi.def::Method, mi.specTypes, mi.sparam_vals, optimize) + rt = code.rettype + end code isa CodeInfo || error("no code is available for ", mi) debuginfo === :none && Base.remove_linenums!(code) return Pair{CodeInfo,Any}(code, rt)