Commit d3b2e82
committed
Use unchecked indexing in integer
The `_fmt_inner` helper in `core::fmt::num` wrote into its
`MaybeUninit<u8>` buffer through regular `[]` indexing and then relied
on surrounding `core::hint::assert_unchecked` hints to convince LLVM to
elide the resulting bounds checks. Under `-Copt-level=z` with fat LTO
(and other configurations where the range information fails to
propagate) the hints were dropped and the optimizer left a
`panic_bounds_check` call on the hot integer-formatting path.
Switch the per-pair buffer writes to `get_unchecked_mut` / `get_unchecked`
on both `buf` and `DECIMAL_PAIRS`. Each `unsafe` block carries a short
rationale comment (why unchecked indexing) and a tight `SAFETY` comment
stating the invariants (`offset + N <= buf.len()`, and the numeric bound
on the `DECIMAL_PAIRS` index). The existing `assert_unchecked`s are
retained because they still document the `offset` / `buf.len()`
invariants and help downstream codegen for the rest of the routine.
Also add a codegen-llvm regression test with `opt-3` and `opt-z`
revisions that checks the `Display` path for `usize` and `u64` does
not contain any `panic_bounds_check`, with a paired sanity check to
make sure the symbol itself is still emitted by the compiler for a
real out-of-bounds index so the `CHECK-NOT`s cannot pass vacuously._fmt_inner to keep bounds checks elided1 parent 12f35ad commit d3b2e82
2 files changed
Lines changed: 76 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
217 | 226 | | |
218 | 227 | | |
219 | 228 | | |
| |||
228 | 237 | | |
229 | 238 | | |
230 | 239 | | |
231 | | - | |
232 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
233 | 248 | | |
234 | 249 | | |
235 | 250 | | |
| |||
242 | 257 | | |
243 | 258 | | |
244 | 259 | | |
245 | | - | |
246 | | - | |
247 | 260 | | |
248 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
249 | 268 | | |
250 | 269 | | |
251 | 270 | | |
| |||
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
0 commit comments