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

Add propagate_inbounds to new getindex method #39996

Closed
jakobnissen opened this issue Mar 12, 2021 · 6 comments
Closed

Add propagate_inbounds to new getindex method #39996

jakobnissen opened this issue Mar 12, 2021 · 6 comments
Labels
domain:arrays [a, r, r, a, y, s] kind:regression Regression in behavior compared to a previous version

Comments

@jakobnissen
Copy link
Contributor

MWE:

struct TextKmerIterator
    data::Vector{UInt8}
    stop::UInt # inclusive
end

function foo(x::TextKmerIterator)
    kmer, i, k = UInt(0), UInt(1), UInt(0)
    @inbounds while i  x.stop
        byte = x.data[i]
        k += one(UInt)
        i += 1
        kmer = ((kmer << 2) & UInt(0x3f)) | 0x01
        if k == 3
            return kmer, (kmer, i, k-one(UInt))
        end
    end
    return nothing
end

And then

julia> it = TextKmerIterator(collect(codeunits("T")), 4)
Main.TextKmerIterator(UInt8[0x54], 0x0000000000000004)

julia> foo(it)
ERROR: BoundsError: attempt to access 1-element Vector{UInt8} at index [2]

According to Nicholas Bauer on Slack, the culprit is

getindex(A::Array, i1::Integer, I::Integer...) = A[to_indices(A, (i1, I...))...]

added in #36427 which lacks propagate inbounds.

@simeonschaub
Copy link
Member

Ah, that looks like it was missed. Note however that reading arrays out of bounds should always be avoided nevertheless and can lead to segfaults.

@simeonschaub simeonschaub added domain:arrays [a, r, r, a, y, s] kind:regression Regression in behavior compared to a previous version labels Mar 12, 2021
@sanjibansg
Copy link
Contributor

I would like to contribute to this issue. Do guide me on approaching this @simeonschaub @jakobnissen

@jakobnissen
Copy link
Contributor Author

@Kahanikaar Nice! You would need to clone the Julia repo. Then go to the relevant file and add @_propagate_inbounds_meta right before the function call. I think that should do the trick.

To verify it worked, you can run my test code. If you don't see a BoundsError, it probably worked. To be sure, you can run @code_native foo(it) and check that there is no boundschecking branch.

@sanjibansg
Copy link
Contributor

Initial PR made. Do review. @jakobnissen @simeonschaub

@KristofferC
Copy link
Sponsor Member

Closed by #40281?

@jakobnissen
Copy link
Contributor Author

Yes, looks like it! Thanks for your PR anyway, @Kahanikaar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] kind:regression Regression in behavior compared to a previous version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants