-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Allow constant-folding intrinsics that are non-pure for inference only #31193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1077,6 +1077,21 @@ function ispuretopfunction(@nospecialize(f)) | |
istopfunction(f, :promote_type) | ||
end | ||
|
||
function intrinsic_tfunc_optimize(f::IntrinsicFunction, atypes::Vector{Any}) | ||
is_pure_intrinsic_optimize(f) || return nothing | ||
args = atypes[2:end] | ||
_all(@nospecialize(a) -> isa(a, Const), args) || return nothing | ||
iidx = Int(reinterpret(Int32, f::IntrinsicFunction)) + 1 | ||
if iidx < 0 || iidx > length(T_IFUNC) | ||
# invalid intrinsic | ||
return nothing | ||
end | ||
(min, max, _) = T_IFUNC[iidx] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like you're trying to do error checking here, then only get around to doing half of it? I think we can reuse most of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, oops that code got lost somewhere. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh, that seems sort of a little bit silly. I didn't realize we had a third copy of that code sitting around too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So should I add support for intrinsics to pure_eval_call then? I would kind of like to avoid the try/catch since we do have nothrow modeling for intrinsics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In any case, I've just added the error check here for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's probably OK to reuse that support, although a bit less general (can't handle some functions where it might throw, but computing that set is pretty tedious—e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to still be missing a call to the nothrow-test or a try/catch to handle it? |
||
min <= length(atypes) - 1 <= max || return nothing | ||
argvals = anymap(a::Const -> a.val, atypes[2:end]) | ||
return f(argvals...) | ||
end | ||
|
||
function early_inline_special_case(ir::IRCode, @nospecialize(f), @nospecialize(ft), e::Expr, atypes::Vector{Any}, sv::OptimizationState, | ||
@nospecialize(etype)) | ||
if (f === typeassert || ft ⊑ typeof(typeassert)) && length(atypes) == 3 | ||
|
@@ -1106,6 +1121,12 @@ function early_inline_special_case(ir::IRCode, @nospecialize(f), @nospecialize(f | |
return quoted(val) | ||
end | ||
end | ||
elseif isa(f, IntrinsicFunction) && is_pure_intrinsic_optimize_only(f) | ||
# If this intrinsic was not eligible for constant propagation during | ||
# inference, but is now, try to do that here. | ||
val = intrinsic_tfunc_optimize(f, atypes) | ||
val === nothing && return nothing | ||
return quoted(val) | ||
end | ||
end | ||
|
||
|
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.
This list should have all of the
_fast
intrinsics too plusmuladd_float
. I thought I had remembered to add those to theis_pure_intrinsic_infer
list originally, but seems like somehow onlysqrt_llvm
actually made it on the list—which is arguably the least likely one to actually be a problem.