bounds checking and optimization

Johan j at j.nl
Tue Jun 29 11:20:19 UTC 2021


On Sunday, 27 June 2021 at 17:49:22 UTC, Bruce Carneal wrote:
> The following code auto vectorizes nicely, as you'd hope:
>
>     void abc(const(int)[] a, const(int)[] b, int[] c) @nogc 
> nothrow pure @trusted {
>         const bound = c.length;
>         (a.length == bound) || assert(0);
>         (b.length == bound) || assert(0);
>         foreach(i; 0..bound)
>             c.ptr[i] = a.ptr[i] + b.ptr[i];
>     }
>
> If you drop the .ptr suffixes the bounds checks are elided, 
> correctly, but the code is not auto vectorized.
>
> Similarly, if you drop the .ptr suffixes and compile @safe, the 
> bounds checks are elided but the code is not auto vectorized.
>
> It would be great if known-within-bound indexing could yield 
> fast code without requiring either .ptr or @trusted.  IOW, I'm 
> hoping for faster safe code.  Is that easily achieved in cases 
> like the above or is it quite hard?

Vectorization does happen when running it again through the 
optimization pipeline:
D --> LLVM IR: https://d.godbolt.org/
IR --> opt --> IR: https://opt.godbolt.org/

This means that we can solve it by reordering/optimizing our 
optimization pipeline in LDC. It is non-trivial work and I would 
suggest not to undertake it before we have switched to LLVM's new 
PassManager (because I think that also tweaks the order of 
optimization passes)

-Johan



More information about the digitalmars-d-ldc mailing list