by Animats · 6mo ago · 0 comments
let mut tab: [usize;100] = [0;100];
...
for i in 0..101 {
tab[i] = i;
}
This must panic at i=100. Panic becomes inevitable at entry to the loop.
Is the compiler entitled to generate a check that will panic at loop entry?
The slides suggest that Rust does not hoist such checks, and, so, with nested
loops, it has trouble getting checks out of the loop, which prevents vectorization.Even if the panic message would not include the index, LLVM was unable to do that if the previous iterations had side effects (for example if `tab` is not a local variable).
function Square(num : Integer) return Integer is
tab : array (0..100) of integer;
begin
for i in 0..101 loop
tab(i):=i;
end loop;
return tab(100);
end Square;
The assembly code generated is : sub rsp, 8 #,
mov esi, 11 #,
mov edi, OFFSET FLAT:.LC0 #,
call "__gnat_rcheck_CE_Index_Check" #
Loop is not run and exeption handler is called directly.Need to try an example where the size isn't known until run time.
What the compiler is allowed to do is to shorten the loop by one and unconditionally panic after the loop, but this falls under the purview of the LLVM optimizer.
That does create a problem for early panics, panicking when panic becomes inevitable but has not happened yet. This deserves more thought.