-
Notifications
You must be signed in to change notification settings - Fork 2
Add Cthulhu
integration using the new provider API
#56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
serenity4
wants to merge
5
commits into
JuliaComputing:main
Choose a base branch
from
serenity4:cthulhu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−9
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68c403f
[deprecated] Initial implementation with AbstractInterpreter
serenity4 7f68316
Update to new Cthulhu API
serenity4 ec58bdd
Update to latest Cthulhu changes
serenity4 a612142
Remove Tracy
serenity4 c5a713e
Move Cthulhu integration into extension
serenity4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
module DAECompilerCthulhuExt | ||
|
||
using Core.IR | ||
using DAECompiler: DAECompiler, DAEIPOResult, UncompilableIPOResult, Settings, ADAnalyzer, structural_analysis!, find_matching_ci, StructureCache, ir_to_src, get_method_instance, MappingInfo, AnalyzedSource | ||
using Compiler: Compiler, InferenceResult, NativeInterpreter, SOURCE_MODE_GET_SOURCE, get_inference_world, typeinf_ext, Effects, get_ci_mi, NoCallInfo | ||
using Accessors: setproperties | ||
using Diffractor: FRuleCallInfo | ||
|
||
import Cthulhu as _Cthulhu | ||
const Cthulhu = Base.get_extension(_Cthulhu, :CthulhuCompilerExt) | ||
using .Cthulhu: CthulhuState, AbstractProvider, Command, InferenceKey, InferenceDict, PC2Remarks, PC2CallMeta, PC2Effects, PC2Excts, LookupResult, generate_code_instance, value_for_default_command | ||
|
||
mutable struct DAEProvider <: AbstractProvider | ||
world::UInt | ||
settings::Settings | ||
remarks::InferenceDict{PC2Remarks} | ||
calls::InferenceDict{PC2CallMeta} | ||
effects::InferenceDict{PC2Effects} | ||
exception_types::InferenceDict{PC2Excts} | ||
end | ||
DAEProvider(; world = Base.tls_world_age(), settings = Settings()) = DAEProvider(world, settings, InferenceDict{PC2Remarks}(), InferenceDict{PC2CallMeta}(), InferenceDict{PC2Effects}(), InferenceDict{PC2Excts}()) | ||
|
||
Cthulhu.get_inference_world(provider::DAEProvider) = provider.world | ||
|
||
function Cthulhu.find_method_instance(provider::DAEProvider, @nospecialize(tt::Type{<:Tuple}), world::UInt) | ||
return get_method_instance(tt, world) | ||
end | ||
|
||
function check_result(ci::CodeInstance) | ||
isa(ci.inferred, UncompilableIPOResult) && throw(ci.inferred.error) | ||
return true | ||
end | ||
|
||
function Cthulhu.generate_code_instance(provider::DAEProvider, mi::MethodInstance) | ||
world = get_inference_world(provider) | ||
ci = find_matching_ci(ci->ci.owner == StructureCache(), mi, world) | ||
# XXX: We should not cache the CodeInstance this way, or at least invalidate in the provider in `toggle_setting!`. | ||
if ci !== nothing | ||
haskey(provider.remarks, ci) && return ci | ||
else | ||
provider.settings.force_inline_all && @warn "`force_inline_all=true` is not supported yet; this setting will be ignored" | ||
analyzer = ADAnalyzer(; world) | ||
ci_pre = typeinf_ext(analyzer, mi, SOURCE_MODE_GET_SOURCE) | ||
result = structural_analysis!(ci_pre, world, provider.settings) | ||
ci = find_matching_ci(ci->ci.owner == StructureCache(), mi, world)::CodeInstance | ||
end | ||
|
||
check_result(ci) | ||
provider.remarks[ci] = PC2Remarks() | ||
provider.calls[ci] = PC2CallMeta() | ||
provider.effects[ci] = PC2Effects() | ||
provider.exception_types[ci] = PC2Excts() | ||
|
||
@eval Main global result = $(ci.inferred) | ||
|
||
return ci | ||
end | ||
|
||
Cthulhu.get_override(provider::DAEProvider, @nospecialize(info)) = nothing | ||
|
||
Cthulhu.get_pc_remarks(provider::DAEProvider, key::CodeInstance) = get(provider.remarks, key, nothing) | ||
Cthulhu.get_pc_effects(provider::DAEProvider, key::CodeInstance) = get(provider.effects, key, nothing) | ||
Cthulhu.get_pc_excts(provider::DAEProvider, key::CodeInstance) = get(provider.exception_types, key, nothing) | ||
|
||
function Cthulhu.LookupResult(provider::DAEProvider, ci::CodeInstance, optimize::Bool) | ||
if isa(ci.inferred, AnalyzedSource) | ||
mi = get_ci_mi(ci) | ||
new_ci = generate_code_instance(provider, mi) | ||
check_result(new_ci) | ||
@assert isa(new_ci.inferred, DAEIPOResult) "Inferred type of newly generated `CodeInstance` must be `DAEIPOResult`, got `$(typeof(new_ci.inferred))`" | ||
return LookupResult(provider, new_ci, optimize) | ||
end | ||
result = ci.inferred::DAEIPOResult | ||
ir = copy(result.ir) | ||
pushfirst!(ir.argtypes, Tuple) | ||
src = ir_to_src(ir, provider.settings; widen = false) | ||
src.ssavaluetypes = copy(ir.stmts.type) | ||
src.min_world = @atomic ci.min_world | ||
src.max_world = @atomic ci.max_world | ||
optimized = true | ||
rt = Cthulhu.cached_return_type(ci) | ||
exct = Cthulhu.cached_exception_type(ci) | ||
infos = widen_call_infos(ir.stmts.info) | ||
return LookupResult(ir, src, rt, exct, infos, src.slottypes, Cthulhu.get_effects(ci), optimized) | ||
end | ||
|
||
function widen_call_infos(infos) | ||
infos = copy(infos) | ||
for (i, info) in enumerate(infos) | ||
while true | ||
isa(info, FRuleCallInfo) && (info = info.info; continue) | ||
isa(info, MappingInfo) && (info = info.info; continue) | ||
break | ||
end | ||
infos[i] = info | ||
end | ||
return infos | ||
end | ||
|
||
function toggle_setting(provider::DAEProvider, setting::Symbol, value) | ||
return setproperties(provider.settings, NamedTuple((setting => value,))) | ||
end | ||
|
||
function Cthulhu.menu_commands(provider::DAEProvider) | ||
commands = Cthulhu.default_menu_commands(provider) | ||
filter!(x -> !in(x.name, (:optimize, :dump_params, :llvm, :native)), commands) | ||
push!(commands, toggle_setting(provider, 'f', :force_inline_all, "force inline all")) | ||
return commands | ||
end | ||
|
||
function toggle_setting(provider::DAEProvider, key::Char, name::Symbol, description::String = string(name)) | ||
callback = state -> toggle_setting!(state, name) | ||
Command(callback, key, name, description, :toggles) | ||
end | ||
|
||
function Cthulhu.value_for_command(provider::DAEProvider, state::CthulhuState, command::Command) | ||
hasproperty(provider.settings, command.name) && | ||
return getproperty(provider.settings, command.name) | ||
return value_for_default_command(provider, state, command) | ||
end | ||
|
||
function toggle_setting!(state::CthulhuState, name::Symbol) | ||
(; provider) = state | ||
(; settings) = provider | ||
value = !getproperty(settings, name)::Bool | ||
provider.settings = setproperties(settings, NamedTuple((name => value,))) | ||
state.display_code = true | ||
end | ||
|
||
DAECompiler.dae_provider(args...; kwargs...) = DAEProvider(args...; kwargs...) | ||
|
||
end # module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing file?