Missed optimization on value range of slice::len
#67186
Labels
A-codegen
Area: Code generation
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
I-slow
Issue: Problems and improvements with respect to performance of generated code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
The internals of llvm do not permit creation of slices spanning more
than
isize::MAX
bytes, as a limit resulting from pointer offsettinginbounds. It must be both possible to represent arbitrary pointer
differences of the same allocation within an
isize
and there must beno wrapping in the
ptr::offset
and array indexing. This has influencedthe surface documentation for
slice::from_raw_parts
:However, the implementation in the
core
library can not leverage thisproperty for optimization. To illustrate this, the following code should never
require an overflow check and never panic but the compiler is unable to
remove the panic path.
play
The
core
library could add an optimization hint in its implementationof
slice::len
asserting that the value range is in fact at most as large asthe maximum possible values of
isize
. This information should then bepropagated automatically at each call site in optimizer passes.
This currently allows the optimizer to remove the bounds check, as can be seen in this implementation: https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=6e46ce72b9affea080a33eee72b53267
I don't see a way to enable this on current nightly as
slice::len
is aconst fn
and several parts of the above implementation will consequently not yet work.The text was updated successfully, but these errors were encountered: