Skip to content

better effects for iterate for Memory and Array #58755

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

Merged

Conversation

nsajko
Copy link
Contributor

@nsajko nsajko commented Jun 17, 2025

After PR #58754 the bounds checking is eliminated by LLVM even without @inbounds, so delete @inbounds to allow the noub effect to be inferred.

Also deduplicate and test for good effect inference.

@nsajko nsajko added the arrays [a, r, r, a, y, s] label Jun 17, 2025
@nsajko nsajko added iteration Involves iteration or the iteration protocol compiler:effects effect analysis labels Jun 17, 2025
@nsajko nsajko force-pushed the better_effects_for_array_iterate branch from 19ac0f2 to 7989b96 Compare June 17, 2025 12:32
@nsajko

This comment was marked as outdated.

@nsajko
Copy link
Contributor Author

nsajko commented Jun 17, 2025

NB: I didn't check yet, but if PR #58754 gets merged, it might be possible to just delete the @inbounds here instead of adding @_noub_meta.

EDIT: just checked, it's true, after #58754 the @inbounds doesn't seem necessary:

# no bounds checking, as expected
code_llvm(iterate, Tuple{Vector{Float32}, Int})
code_llvm(iterate, Tuple{Memory{Float32}, Int})

@eval Base function _iterate_array(A::Union{Memory, Array}, i::Int)
    @inline
    (i - 1)%UInt < length(A)%UInt ? (A[i], i + 1) : nothing
end

@eval Base iterate(A::Memory, i=1) = (@inline; _iterate_array(A, i))
@eval Base iterate(A::Array, i=1) = (@inline; _iterate_array(A, i))

# still no bounds checking!
code_llvm(iterate, Tuple{Vector{Float32}, Int})
code_llvm(iterate, Tuple{Memory{Float32}, Int})

@nsajko nsajko marked this pull request as draft June 18, 2025 05:44
@nsajko nsajko added the needs tests Unit tests are required for this change label Jun 18, 2025
@nsajko nsajko force-pushed the better_effects_for_array_iterate branch from 7989b96 to a8c8cae Compare June 18, 2025 19:26
@nsajko nsajko removed the needs tests Unit tests are required for this change label Jun 18, 2025
@nsajko nsajko marked this pull request as ready for review June 18, 2025 19:26
After PR JuliaLang#58754 the bounds checking is eliminated by LLVM even without
asserting `inbounds`, so delete `inbounds` to allow the `noub` effect
to be inferred.

Also deduplicate and test for good effect inference.
@nsajko nsajko force-pushed the better_effects_for_array_iterate branch from f5b3026 to 0e38f1b Compare June 18, 2025 19:27
@nsajko nsajko added needs nanosoldier run This PR should have benchmarks run on it status: waiting for PR reviewer labels Jun 18, 2025
@nsajko nsajko added merge me PR is reviewed. Merge when all tests are passing and removed needs nanosoldier run This PR should have benchmarks run on it status: waiting for PR reviewer labels Jun 19, 2025
@IanButterworth IanButterworth merged commit f855e4c into JuliaLang:master Jun 20, 2025
10 of 12 checks passed
iterate(A::Memory, i=1) = (@inline; (i - 1)%UInt < length(A)%UInt ? (@inbounds A[i], i + 1) : nothing)
function _iterate_array(A::Union{Memory, Array}, i::Int)
@inline
(i - 1)%UInt < length(A)%UInt ? (A[i], i + 1) : nothing
Copy link
Member

@mbauman mbauman Jun 20, 2025

Choose a reason for hiding this comment

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

Could (should?) this also become _checkbounds_array(Bool, A, i)?

@nsajko nsajko deleted the better_effects_for_array_iterate branch June 20, 2025 20:37
@nsajko nsajko removed the merge me PR is reviewed. Merge when all tests are passing label Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] compiler:effects effect analysis iteration Involves iteration or the iteration protocol
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants