From 36aab14a97a6ae6fb937577601e44134c24a0e37 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Thu, 25 Aug 2022 19:17:38 +0900 Subject: [PATCH] IRShow: print invalid `SSAValue` in red (#46472) --- base/compiler/ssair/show.jl | 17 ++++++++++++----- base/show.jl | 9 ++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/base/compiler/ssair/show.jl b/base/compiler/ssair/show.jl index f58a1cc6e0d49..b6b33c422b651 100644 --- a/base/compiler/ssair/show.jl +++ b/base/compiler/ssair/show.jl @@ -810,7 +810,9 @@ function show_ir(io::IO, ir::IRCode, config::IRShowConfig=default_config(ir); pop_new_node! = new_nodes_iter(ir)) used = stmts_used(io, ir) cfg = ir.cfg - show_ir_stmts(io, ir, 1:length(ir.stmts), config, used, cfg, 1; pop_new_node!) + let io = IOContext(io, :maxssaid=>length(ir.stmts)) + show_ir_stmts(io, ir, 1:length(ir.stmts), config, used, cfg, 1; pop_new_node!) + end finish_show_ir(io, cfg, config) end @@ -818,7 +820,9 @@ function show_ir(io::IO, ci::CodeInfo, config::IRShowConfig=default_config(ci); pop_new_node! = Returns(nothing)) used = stmts_used(io, ci) cfg = compute_basic_blocks(ci.code) - show_ir_stmts(io, ci, 1:length(ci.code), config, used, cfg, 1; pop_new_node!) + let io = IOContext(io, :maxssaid=>length(ci.code)) + show_ir_stmts(io, ci, 1:length(ci.code), config, used, cfg, 1; pop_new_node!) + end finish_show_ir(io, cfg, config) end @@ -837,7 +841,9 @@ function show_ir(io::IO, compact::IncrementalCompact, config::IRShowConfig=defau end end pop_new_node! = new_nodes_iter(compact) - bb_idx = show_ir_stmts(io, compact, 1:compact.result_idx-1, config, used_compacted, cfg, 1; pop_new_node!) + bb_idx = let io = IOContext(io, :maxssaid=>length(compact.result)) + show_ir_stmts(io, compact, 1:compact.result_idx-1, config, used_compacted, cfg, 1; pop_new_node!) + end # Print uncompacted nodes from the original IR stmts = compact.ir.stmts @@ -847,8 +853,9 @@ function show_ir(io::IO, compact::IncrementalCompact, config::IRShowConfig=defau # config.line_info_preprinter(io, "", compact.idx) printstyled(io, "─"^(width-indent-1), '\n', color=:red) end - - show_ir_stmts(io, compact.ir, compact.idx:length(stmts), config, used_uncompacted, cfg, bb_idx; pop_new_node!) + let io = IOContext(io, :maxssaid=>length(compact.ir.stmts)) + show_ir_stmts(io, compact.ir, compact.idx:length(stmts), config, used_uncompacted, cfg, bb_idx; pop_new_node!) + end finish_show_ir(io, cfg, config) end diff --git a/base/show.jl b/base/show.jl index 6f17fe6ee2ef6..22f3f8ec0786f 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1686,7 +1686,14 @@ end ## AST printing ## -show_unquoted(io::IO, val::SSAValue, ::Int, ::Int) = print(io, "%", val.id) +function show_unquoted(io::IO, val::SSAValue, ::Int, ::Int) + if get(io, :maxssaid, typemax(Int))::Int < val.id + # invalid SSAValue, print this in red for better recognition + printstyled(io, "%", val.id; color=:red) + else + print(io, "%", val.id) + end +end show_unquoted(io::IO, sym::Symbol, ::Int, ::Int) = show_sym(io, sym, allow_macroname=false) show_unquoted(io::IO, ex::LineNumberNode, ::Int, ::Int) = show_linenumber(io, ex.line, ex.file) show_unquoted(io::IO, ex::GotoNode, ::Int, ::Int) = print(io, "goto %", ex.label)