-
Notifications
You must be signed in to change notification settings - Fork 20
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
[WIP] control flow if #735
Open
glou-nes
wants to merge
5
commits into
EnzymeAD:main
Choose a base branch
from
glou-nes:control_flow_fun
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.
Open
Changes from 2 commits
Commits
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 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,87 @@ | ||
CC = Core.Compiler | ||
ReactantInter = Enzyme.Compiler.Interpreter.EnzymeInterpreter{ | ||
typeof(Reactant.set_reactant_abi) | ||
} | ||
EnzymeInter = Enzyme.Compiler.Interpreter.EnzymeInterpreter | ||
|
||
shift_off(s, _) = s | ||
shift_off(s::Core.SSAValue, new_index::Vector) = Core.SSAValue(new_index[s.id]) | ||
|
||
#add a conversion to Bool before a lowered if | ||
goto_if_not_protection(src::Core.CodeInfo) = begin | ||
new_index = [] | ||
offset = 0 | ||
for (i, t) in enumerate(typeof.(src.code)) | ||
t == Core.GotoIfNot && (offset += 2) | ||
push!(new_index, i + offset) | ||
end | ||
|
||
nc = [] | ||
ncl = [] | ||
for (i, c) in enumerate(src.code) | ||
v = nothing | ||
if c isa Core.GotoIfNot | ||
push!(nc, GlobalRef(Main, :convert)) | ||
push!(nc, Expr(:call, (Core.SSAValue(new_index[i] - 2), GlobalRef(Main, :Bool), shift_off(c.cond, new_index))...)) | ||
append!(ncl, [src.codelocs[i] for _ in 1:2]) | ||
v = Core.GotoIfNot(Core.SSAValue(new_index[i] - 1), new_index[c.dest]) | ||
elseif c isa Core.GotoNode | ||
v = Core.GotoNode(new_index[c.label]) | ||
elseif c isa Core.ReturnNode | ||
v = Core.ReturnNode(shift_off(c.val, new_index)) | ||
elseif c isa Expr | ||
v = Expr(c.head, (shift_off(a, new_index) for a in c.args)...) | ||
else | ||
v = c | ||
end | ||
push!(nc, v) | ||
push!(ncl, src.codelocs[i]) | ||
end | ||
new = copy(src) | ||
new.code = nc | ||
new.codelocs = ncl | ||
for _ in 1:offset | ||
push!(new.ssaflags, 0x00000000) | ||
end | ||
new.ssavaluetypes = src.ssavaluetypes + offset | ||
return new | ||
end | ||
|
||
vec = [] | ||
vec2 = [] | ||
function CC.inlining_policy( | ||
interp::ReactantInter, | ||
@nospecialize(src), | ||
@nospecialize(info::CC.CallInfo), | ||
stmt_flag::UInt32, | ||
) | ||
#typeof(src) in [CC.IRCode, Core.CodeInfo] || return; | ||
#push!(vec, (CC.copy(src), info)) | ||
#push!(vec2, stacktrace()) | ||
#=info isa CC.ConstCallInfo && (info = info.call) | ||
push!(vec, info) | ||
if info isa MethodMatchInfo | ||
mm::Core.MethodMatch = first(info.results.matches) | ||
m::Method = mm.method | ||
if m.name == :convert && m.sig isa DataType | ||
if m.sig.types[3] == Reactant.TracedRNumber{Bool} | ||
return true | ||
end | ||
end | ||
end | ||
#push!(vec2, info) | ||
=# | ||
return nothing | ||
@invoke CC.inlining_policy( | ||
interp::EnzymeInter, src, info::CC.CallInfo, stmt_flag::UInt32 | ||
) | ||
end | ||
|
||
#=vec2 = [] | ||
CC.finish!(ji::ReactantInter, caller::CC.InferenceState) = begin | ||
res = @invoke CC.finish!(ji::EnzymeInter, caller::CC.InferenceState) | ||
push!(vec2, res) | ||
end | ||
=# | ||
|
||
|
This file contains 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.
Billy's concern was that since the transformation here happens before inlining, the code that will later get doesn't have the needed conversions inserted.
I'm wondering if you run into this issue?
If so, the idea was to do the transformation by specializing the abstract interpreter.
If that's necessary, the correct place to do it would be
InferenceState
like I did here:https://github.com/JuliaLLVM/MLIR.jl/blob/cf22c54222033ed4f5f3d68a620448bcaf766ac4/src/Generate/absint.jl#L151C1-L160C4
based on an old pr from valentin:
JuliaGPU/GPUCompiler.jl#311
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.
I don't have an example of such a case in mind! I will try with this one firstly (seem easier to handle).