Skip to content

Bounds checking in dot #650

Closed
Closed
@wraith1995

Description

@wraith1995

The dot product of two static arrays has a bounds check in the inner loop as

using StaticArrays
using LinearAlgebra
a = @SVector rand(4)
b = @SVector rand(4)
@code_typed dot(a,b)

produces

CodeInfo(
1 ──       goto #18 if not true
2 ──       goto #17 if not true
3 ──       nothing::Nothing
4 ┄─ %4  = φ (#3 => 0.0, #16 => %32)::Float64
│    %5  = φ (#3 => 0, #16 => %33)::Int64
│    %6  = (Base.slt_int)(%5, 4)::Bool
└───       goto #17 if not %6
5 ── %8  = (Base.add_int)(%5, 1)::Int64
│    %9  = (Base.sub_int)(%8, 1)::Int64
│    %10 = (Base.add_int)(1, %9)::Int64
└───       goto #14 if not false
6 ── %12 = (Base.slt_int)(0, %8)::Bool
└───       goto #10 if not %12
7 ── %14 = (Base.sle_int)(%10, 4)::Bool
└───       goto #9 if not %14
8 ── %16 = (Base.sle_int)(1, %10)::Bool
└───       goto #11
9 ──       goto #11
10 ─       goto #11
11 ┄ %20 = φ (#8 => %16, #9 => false, #10 => false)::Bool
└───       goto #13 if not %20
12 ─       goto #14
13 ─       invoke Base.throw_boundserror($(QuoteNode(1:4))::UnitRange{Int64}, %8::Int64)::Union{}
└───       $(Expr(:unreachable))::Union{}
14 ┄       goto #15
15 ─       goto #16
16 ─ %27 = (Base.getfield)(a, :data)::NTuple{4,Float64}
│    %28 = (Base.getfield)(%27, %10, false)::Float64
│    %29 = (Base.getfield)(b, :data)::NTuple{4,Float64}
│    %30 = (Base.getfield)(%29, %10, false)::Float64
│    %31 = (Base.mul_float)(%28, %30)::Float64
│    %32 = (Base.add_float)(%4, %31)::Float64
│    %33 = (Base.add_int)(%5, 1)::Int64
│          $(Expr(:simdloop, false))::Any
└───       goto #4
17 ┄ %36 = φ (#4 => %4, #2 => 0.0)::Float64
18 ┄ %37 = φ (#17 => %36, #1 => 0.0)::Float64
└───       goto #19
19 ─       return %37
) => Float64

This seems surely wrong to me, but the dot function uses @inbounds correctly and as far as I can tell, all the needed @propagate_inbounds are in place! What on earth is going wrong?

For reference, I'm on julia v1.1.1 and StaticArrays v0.11.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions