Why does `@inbounds` disable constant propagation?

nsajko
Sat, 04 Nov 2023 09:15:10 +0000
Internals & Design

The Julia manual says:

An explicit @inbounds annotation inside the function will also disable constant folding and not be overridden by :foldable.

Some other references of this fact:

Issue #48245 says:

turning on --check-bounds=no on current master can actually result in significantly
worse performance because it removes inference’s ability to do concrete evaluation (constant folding)

PR #50239 says:

Yes, having anything other than Const(true) on the boundscheck argument of the intrinsic taints :consistent.

PR #50641 says:

In the whole, --check-bounds removal discussion, we had assumed that we did not want to keep two copies of all code just for the purposes of --check-bounds which thus required us disable constant propagation.

I still don’t understand why would constant propagation need to be disabled where @inbounds is used. My first guess was that one wouldn’t want to disable bounds checking during constant propagation so as to prevent an internal compiler error because of an out-of-bounds access? But, in that case, wouldn’t it be better to just disable @inbounds during constant propagation (i.e., run all bounds checks during constant propagation, as with --check-bounds=yes)?

EDIT: issue on Julia’s Github: make `@inbounds` not taint consistency effect/disable constant propagation · Issue #52041 · JuliaLang/julia · GitHub

26 posts - 10 participants