Skip to content
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

[NewOptimizer] Union Split during Inlining #26900

Merged
merged 3 commits into from
May 2, 2018
Merged

[NewOptimizer] Union Split during Inlining #26900

merged 3 commits into from
May 2, 2018

Conversation

Keno
Copy link
Member

@Keno Keno commented Apr 25, 2018

This adds union splitting optimizations to the inliner.
This works a bit differently from the old optimizer, which
did union splitting on the bail out path. Instead, we try
to run the full inliner on any union split case that's a
dispatch tuple (and record what to do in that case). Doing
that allows an effective resolution of #23338, though this
PR doesn't quite do that yet, since some further (minor) fixes
are needed.

@Keno Keno mentioned this pull request Apr 25, 2018
18 tasks
opt_check_bounds == 0 && return :default
opt_check_bounds == 1 && return :on
return :off
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

editor issue?

case === nothing
@assert !isa(metharg, UnionAll)
cond = true
for (i, (a, m)) in Iterators.drop(enumerate(zip(atype.parameters, metharg.parameters)), 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't put Types in Tuples

return_value
end

function ir_inline_unionsplit!(compact, idx, argexprs, linetable, item, boundscheck, todo_bbs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotate your argument types

cond = insert_node_here!(compact, and_expr, Bool, line)
end
end
atype = typesubtract(atype, metharg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attempting this operation is wrong, although since atype is a DataType, it's at least a no-op

has_generic = false
@assert length(item.bbs) > length(item.cases)
for ((metharg, case), next_cond_bb) in zip(item.cases, item.bbs)
case === nothing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code

val = ir_inline_item!(compact, idx, argexprs, linetable, case, boundscheck, todo_bbs)
elseif isa(case, MethodInstance)
# Make it an Expr(:invoke)
error()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to fix error() this

elseif isa(case, ConstantCase)
val = case.val
else
error()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to fix this

@ararslan ararslan added the compiler:optimizer Optimization passes (mostly in base/compiler/ssair/) label Apr 25, 2018
@Keno Keno force-pushed the kf/unionsplit branch 4 times, most recently from 2cfee6c to 6a6b96e Compare May 1, 2018 15:25
@Keno
Copy link
Member Author

Keno commented May 1, 2018

I've been running this branch locally and it seems to work fine, so I'm planning to merge this in a bit. As always comments are still open afterwards.

@vtjnash
Copy link
Member

vtjnash commented May 1, 2018

Seems like this design will probably regress our ability to do union-splitting a bit (this can only handle cases that intersect to create dispatch tuples, while the approach of the existing code can handle more general cases with fewer isa tests), but I understand most review is being deferred for later

isa(methsp[i], TypeVar) && return nothing
end

# Find the linfo for this methods (Generated functions are expanded here if necessary)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope not: the last parameter (true) means "do not expand generated functions"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we discussed that on a past review. I'll separately fix the comment.

ast = copy_exprargs(inferred.code)
else
src = ccall(:jl_uncompress_ast, Any, (Any, Any), method, inferred::Vector{UInt8})::CodeInfo
# TODO: It seems like PhiNodes are shared between compressed codeinfos, making this copy necessary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a serious bug with the serializer, if (still) true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this got fixed.

found_any = false
for (i, match) in enumerate(meth)
(metharg, methsp, method) = (match[1]::Type, match[2]::SimpleVector, match[3]::Method)
metharg′ <: metharg || continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type-intersect

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that equivalent, since metharg′ is a dispatch tuple?

Copy link
Member

@vtjnash vtjnash May 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, although note that metharg is not relevant to whether the method will actually match at runtime

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate that statement?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metharg is typeintersect(method.sig, atypes), so it's unpredictable whether it's method.sig, atypes, or something wider or narrower. basically, just assume that it's Tuple and you should be OK.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so should that be method.sig on the RHS of this check then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that should work

@Keno
Copy link
Member Author

Keno commented May 1, 2018

Seems like this design will probably regress our ability to do union-splitting a bit (this can only handle cases that intersect to create dispatch tuples, while the approach of the existing code can handle more general cases with fewer isa tests), but I understand most review is being deferred for later

Examples of which cases you don't like with the new behavior would be good. Last time we discussed this you and @JeffBezanson told me that you'd prefer to go through the dynamic dispatch for everything that's not a dispatchtuple. In any case, we can do all of that later.

@vtjnash
Copy link
Member

vtjnash commented May 1, 2018

We'd like to also be able to generate direct calls in the presence of @nospecialize cases (predicated on whether inlining succeeds instead – there's a partial predicate for this "jl_is_cacheable_sig" in src).

Keno added 3 commits May 1, 2018 17:19
This adds union splitting optimizations to the inliner.
This works a bit differently from the old optimizer, which
did union splitting on the bail out path. Instead, we try
to run the full inliner on any union split case that's a
dispatch tuple (and record what to do in that case). Doing
that allows an effective resolution of #23338, though this
PR doesn't quite do that yet, since some further (minor) fixes
are needed.
@Keno Keno force-pushed the kf/unionsplit branch from 6a6b96e to 8f4ff91 Compare May 1, 2018 21:27
@Keno Keno merged commit 7d48ecb into master May 2, 2018
@martinholters martinholters deleted the kf/unionsplit branch May 2, 2018 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:optimizer Optimization passes (mostly in base/compiler/ssair/)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants