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

Remove @inbounds in tuple iteration #48297

Merged
merged 3 commits into from
Jan 17, 2023
Merged

Remove @inbounds in tuple iteration #48297

merged 3 commits into from
Jan 17, 2023

Conversation

Keno
Copy link
Member

@Keno Keno commented Jan 16, 2023

LLVM can prove this inbounds and the annotation weakens the inferable effects for tuple iteration, which has a surprisingly large inference performance and precision impact.

Unfortunately, my previous changes to :inbounds tainting weren't quite strong enough yet, because getfield was still tainting consistency on unknown boundscheck arguments. To fix that, we pass through the fargs into the fetfield effects to check if we're getting a literal :boundscheck, in which case the :noinbounds consistency-tainting logic I added in #48246 is sufficient to not require additional consistency tainting.

Also add a test for both effects and codegen to make sure this doens't regress.

@Keno
Copy link
Member Author

Keno commented Jan 16, 2023

This was #48260, but CI didn't like that PR anymore.

@aviatesk
Copy link
Member

@nanosoldier runbenchmarks("inference", vs=":master")

# The inbounds-ness assertion requires dynamic reachability, while
# :consistent needs to be true for all input values.
# However, as a special exception, we do allow literal `:boundscheck`.
# `:consitency`-will be tainted in any caller using `@inbounds` based
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# `:consitency`-will be tainted in any caller using `@inbounds` based
# `:consistency`-will be tainted in any caller using `@inbounds` based

Copy link
Member Author

Choose a reason for hiding this comment

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

Wait, we fixed that already. Did I accidentally drop the fixup commit when rebasing?

base/compiler/tfuncs.jl Outdated Show resolved Hide resolved
base/compiler/tfuncs.jl Show resolved Hide resolved
@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

LLVM can prove this inbounds and the annotation weakens the
inferable effects for tuple iteration, which has a surprisingly
large inference performance and precision impact.

Unfortunately, my previous changes to :inbounds tainting weren't
quite strong enough yet, because `getfield` was still tainting
consistency on unknown boundscheck arguments. To fix that, we
pass through the fargs into the fetfield effects to check if
we're getting a literal `:boundscheck`, in which case the
`:noinbounds` consistency-tainting logic I added in #48246
is sufficient to not require additional consistency tainting.

Also add a test for both effects and codegen to make sure this doens't regress.
@Keno
Copy link
Member Author

Keno commented Jan 16, 2023

many_const_calls

This got exceedingly faster. I'm assuming this means we regressed it at some point and didn't notice. Some memory regressions elsewhere. Probably from having to allocate more ArgInfo objects in the optimizer, but we shouldn't really be calling this from there anymore anyway, since we now annotate effectfulness flags in inference. But that's a spearate change. I think this is good to go if CI comes back green.

@aviatesk
Copy link
Member

So we don't need the previous changes on the :noinbounds effect bit?

@Keno
Copy link
Member Author

Keno commented Jan 16, 2023

We need both

@aviatesk
Copy link
Member

Ah, I just forgot we already merged that PR.

elseif length(argtypes) != 2
if length(argtypes) == 4
isvarargtype(argtypes[4]) && return false
if widenconst(argtypes[4]) !== Bool
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if widenconst(argtypes[4]) !== Bool
if !hasintersect(widenconst(argtypes[4]), Bool)

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 agree with you that this condition is suspect, but I'm not sure your suggested replacement is correct either, because that would just set the ordering to :not_atomic, which seems wrong potentially. However, this is pre-existing code, so I think we should punt that to another PR.

@@ -794,3 +794,6 @@ end
f48085(@nospecialize x...) = length(x)
@test Core.Compiler.get_compileable_sig(which(f48085, (Vararg{Any},)), Tuple{typeof(f48085), Vararg{Int}}, Core.svec()) === nothing
@test Core.Compiler.get_compileable_sig(which(f48085, (Vararg{Any},)), Tuple{typeof(f48085), Int, Vararg{Int}}, Core.svec()) === Tuple{typeof(f48085), Any, Vararg{Any}}

# Make sure that the bounds check is elided in tuple iteration
@test !occursin("call void @", get_llvm(iterate, Tuple{NTuple{4, Float64}, Int64}))
Copy link
Member

Choose a reason for hiding this comment

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

These sorts of checks usually break our coverage and debug tests (since -g2 may add void @llvm.dbg.value calls, for example). It is preexisting, so does not need to block merging. But can we do any better though?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could maybe check for the jl_ prefix, since the boundscheck we're trying to avoid here is an intrinsic function, but I did want to test that we don't call any other non-inlined function that might be hiding the call either.

@vtjnash
Copy link
Member

vtjnash commented Jan 16, 2023

Error in testset compiler/codegen:
Error During Test at C:\buildkite-agent\builds\win2k22-amdci6-0\julialang\julia-master\julia-7a211727c2\share\julia\test\compiler\codegen.jl:799
  Test threw exception
  Expression: !(occursin("call void @", get_llvm(iterate, Tuple{NTuple{4, Float64}, Int64})))
  no unique matching method found for the specified argument types

s/Int64/Int/

@Keno Keno merged commit 9582937 into master Jan 17, 2023
@Keno Keno deleted the kf/rminbounds2 branch January 17, 2023 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants