Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly support offset indices #992

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/array-lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function Base.getindex(x::SymArray, idx...)
meta = metadata(unwrap(x))
if shape(x) !== Unknown() && all(i -> i isa Integer, idx)
II = CartesianIndices(axes(x))
ii = CartesianIndex(idx)
@boundscheck begin
if !checkbounds(Bool, II, idx...)
if !in(ii, II)
throw(BoundsError(x, idx))
end
end
ii = II[idx...]
res = Term{eltype(symtype(x))}(getindex, [x, Tuple(ii)...]; metadata = meta)
elseif all(i -> symtype(i) <: Integer, idx)
shape(x) !== Unknown() && @boundscheck begin
Expand Down
13 changes: 11 additions & 2 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ function Base.show(io::IO, aop::ArrayOp)
end
end

Base.summary(io::IO, aop::ArrayOp) = Base.array_summary(io, aop, shape(aop))
function Base.showarg(io::IO, aop::ArrayOp, toplevel)
show(io, aop)
toplevel && print(io, "::", typeof(aop))
return nothing
end

symtype(a::ArrayOp{T}) where {T} = T
istree(a::ArrayOp) = true
function operation(a::ArrayOp)
Expand Down Expand Up @@ -208,7 +215,9 @@ function make_shape(output_idx, expr, ranges=Dict())
end
mi = matches[i]
@assert !isempty(mi)
return get_extents(mi)
ext = get_extents(mi)
ext isa Unknown && return Unknown()
return Base.OneTo(length(ext))
elseif i isa Integer
return Base.OneTo(1)
end
Expand Down Expand Up @@ -526,7 +535,7 @@ end
function axes(A::Union{Arr, SymArray})
s = shape(unwrap(A))
s === Unknown() && error("axes of $A not known")
return map(x->1:length(x), s)
return s
end


Expand Down
16 changes: 16 additions & 0 deletions test/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,19 @@ end
@test !isequal(a, b) && !isequal(b, c) && !isequal(a, c)
@test hash(a) != hash(b) && hash(b) != hash(c) && hash(a) != hash(c)
end

@testset "Offset Indices" begin
@variables k[0:3]

@testset "i = $i" for i in 0:3
sym = unwrap(k[i])
@test operation(sym) === getindex
args = arguments(sym)
@test length(args) == 2
@test args[1] === unwrap(k)
@test args[2] === i
end

@test_throws BoundsError k[-1]
@test_throws BoundsError k[4]
end
4 changes: 2 additions & 2 deletions test/build_function_tests/stencil-broadcast-inplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
ˍ₋out_1 = (view)(ˍ₋out, 1:6, 1:6)
ˍ₋out_1 .= 0
ˍ₋out_2 = (view)(ˍ₋out, 2:5, 2:5)
for (j, j′) = zip(1:4, reset_to_one(1:4))
for (i, i′) = zip(1:4, reset_to_one(1:4))
for (j, j′) = zip(Base.OneTo(4), reset_to_one(Base.OneTo(4)))
for (i, i′) = zip(Base.OneTo(4), reset_to_one(Base.OneTo(4)))
ˍ₋out_2[i′, j′] = (+)(ˍ₋out_2[i′, j′], (+)(1, (getindex)(ˍ₋out_2_input_1, i, j)))
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/build_function_tests/stencil-broadcast-outplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
ˍ₋out_1 = (view)(ˍ₋out, 1:6, 1:6)
ˍ₋out_1 .= 0
ˍ₋out_2 = (view)(ˍ₋out, 2:5, 2:5)
for (j, j′) = zip(1:4, reset_to_one(1:4))
for (i, i′) = zip(1:4, reset_to_one(1:4))
for (j, j′) = zip(Base.OneTo(4), reset_to_one(Base.OneTo(4)))
for (i, i′) = zip(Base.OneTo(4), reset_to_one(Base.OneTo(4)))
ˍ₋out_2[i′, j′] = (+)(ˍ₋out_2[i′, j′], (+)(1, (getindex)(ˍ₋out_2_input_1, i, j)))
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/build_function_tests/transpose-inplace.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:(function (x,)
let ˍ₋out = zeros(Float64, map(length, (1:4, 1:4)))
let ˍ₋out = zeros(Float64, map(length, (Base.OneTo(4), Base.OneTo(4))))
begin
for (j, j′) = zip(1:4, reset_to_one(1:4))
for (i, i′) = zip(1:4, reset_to_one(1:4))
Expand Down
Loading