Skip to content

Commit 26e3cca

Browse files
committed
Add action to show system structure
1 parent cd99426 commit 26e3cca

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

ext/DAECompilerCthulhuExt.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module DAECompilerCthulhuExt
22

33
using Core.IR
4-
using DAECompiler: DAECompiler, DAEIPOResult, UncompilableIPOResult, Settings, ADAnalyzer, structural_analysis!, find_matching_ci, StructureCache, ir_to_src, get_method_instance, MappingInfo, AnalyzedSource
4+
using DAECompiler: DAECompiler, DAEIPOResult, UncompilableIPOResult, Settings, ADAnalyzer, structural_analysis!, find_matching_ci, matched_system_structure, StructureCache, ir_to_src, get_method_instance, MappingInfo, AnalyzedSource
55
using Compiler: Compiler, InferenceResult, NativeInterpreter, SOURCE_MODE_GET_SOURCE, get_inference_world, typeinf_ext, Effects, get_ci_mi, NoCallInfo
66
using Accessors: setproperties
77
using Diffractor: FRuleCallInfo
@@ -105,9 +105,21 @@ function Cthulhu.menu_commands(provider::DAEProvider)
105105
commands = Cthulhu.default_menu_commands(provider)
106106
filter!(x -> !in(x.name, (:optimize, :dump_params, :llvm, :native)), commands)
107107
push!(commands, toggle_setting(provider, 'f', :force_inline_all, "force inline all"))
108+
push!(commands, Cthulhu.perform_action(show_mss, 'm', :show_mss, :actions, "Show system structure"))
108109
return commands
109110
end
110111

112+
function show_mss(state::CthulhuState)
113+
result = state.ci.inferred::DAEIPOResult
114+
terminal = state.terminal
115+
io = terminal.out_stream::IO
116+
mss = matched_system_structure(result, state.provider.settings.mode)
117+
(_, width) = displaysize(terminal)
118+
printstyled(io, '\n', '-'^((width - 26) ÷ 2), " Showing system structure ", '-'^((width - 26) ÷ 2), '\n'; color = :light_black)
119+
show(io, MIME"text/plain"(), mss)
120+
printstyled(io, '\n', '-'^width, "\n\n"; color = :light_black)
121+
end
122+
111123
function toggle_setting(provider::DAEProvider, key::Char, name::Symbol, description::String = string(name))
112124
callback = state -> toggle_setting!(state, name)
113125
Command(callback, key, name, description, :toggles)

src/analysis/cache.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,20 @@ function make_structure_from_ipo(ipo::DAEIPOResult)
115115

116116
structure = DAESystemStructure(StateSelection.complete(var_to_diff), StateSelection.complete(eq_to_diff), graph, solvable_graph)
117117
end
118+
119+
function matched_system_structure(result::DAEIPOResult, mode)
120+
structure = make_structure_from_ipo(result)
121+
122+
tstate = TransformationState(result, structure)
123+
err = StateSelection.check_consistency(tstate, nothing)
124+
err !== nothing && throw(err)
125+
126+
ret = top_level_state_selection!(tstate)
127+
isa(ret, UncompilableIPOResult) && throw(ret.error)
128+
129+
(diff_key, init_key) = ret
130+
key = in(mode, (DAE, DAENoInit, ODE, ODENoInit)) ? diff_key : init_key
131+
132+
var_eq_matching = matching_for_key(tstate, key)
133+
return StateSelection.MatchedSystemStructure(result, structure, var_eq_matching)
134+
end

src/reflection.jl

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,7 @@ function code_structure_by_type(@nospecialize(tt::Type); world::UInt = Base.tls_
3232
_result = structural_analysis!(ci, world, settings)
3333
isa(_result, UncompilableIPOResult) && throw(_result.error)
3434
!matched && return result ? _result : _result.ir
35-
result = _result
36-
37-
structure = make_structure_from_ipo(result)
38-
39-
tstate = TransformationState(result, structure)
40-
err = StateSelection.check_consistency(tstate, nothing)
41-
err !== nothing && throw(err)
42-
43-
ret = top_level_state_selection!(tstate)
44-
isa(ret, UncompilableIPOResult) && throw(ret.error)
45-
46-
(diff_key, init_key) = ret
47-
key = in(mode, (DAE, DAENoInit, ODE, ODENoInit)) ? diff_key : init_key
48-
49-
var_eq_matching = matching_for_key(tstate, key)
50-
return StateSelection.MatchedSystemStructure(result, structure, var_eq_matching)
35+
return matched_system_structure(_result, mode)
5136
end
5237

5338
"""

0 commit comments

Comments
 (0)