Skip to content

Commit 02734a5

Browse files
rdeitsdlfivefifty
authored andcommitted
fix inlining expression usage in generated functions (#51)
* fix inlining expression usage in generated functions * fix missing `@test`s * test for zero-allocation
1 parent d5710bb commit 02734a5

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/blockindices.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
@assert M < N
3939
α_ex = Expr(:tuple, [k <= M ? :(α[$k]) : :(1) for k = 1:N]...)
4040
return quote
41-
$Expr(:meta, :inline)
41+
$(Expr(:meta, :inline))
4242
@inbounds α2 = $α_ex
4343
BlockIndex(I, α2)
4444
end
@@ -54,7 +54,7 @@ Converts from global indices `inds` to a `BlockIndex`.
5454
I_ex = Expr(:tuple, [:(block_index[$k][1]) for k = 1:N]...)
5555
α_ex = Expr(:tuple, [:(block_index[$k][2]) for k = 1:N]...)
5656
return quote
57-
$Expr(:meta, :inline)
57+
$(Expr(:meta, :inline))
5858
@inbounds block_index = $block_index_ex
5959
@inbounds I = $I_ex
6060
@inbounds α = $α_ex
@@ -70,7 +70,7 @@ Converts from a block index to a tuple containing the global indices
7070
@generated function blockindex2global(block_sizes::BlockSizes{N}, block_index::BlockIndex{N}) where {N}
7171
ex = Expr(:tuple, [:(block_sizes[$k, block_index.I[$k]] + block_index.α[$k] - 1) for k = 1:N]...)
7272
return quote
73-
$Expr(:meta, :inline)
73+
$(Expr(:meta, :inline))
7474
@inbounds v = $ex
7575
return $ex
7676
end

src/blocksizes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ end
109109
@generated function globalrange(block_sizes::BlockSizes{N}, block_index::NTuple{N, Int}) where {N}
110110
indices_ex = Expr(:tuple, [:(block_sizes[$i, block_index[$i]]:block_sizes[$i, block_index[$i] + 1] - 1) for i = 1:N]...)
111111
return quote
112-
$Expr(:meta, :inline)
112+
$(Expr(:meta, :inline))
113113
@inbounds inds = $indices_ex
114114
return inds
115115
end

test/test_blockindices.jl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,29 @@ block_size = BlockSizes([1,2,3], [2, 3])
1818
@test nblocks(block_size, 1) == 3
1919
@test nblocks(block_size, 2) == 2
2020

21-
@inferred globalrange(block_size, (1,1)) == (1:1, 1:2)
22-
@inferred globalrange(block_size, (1,2)) == (1:1, 3:5)
23-
@inferred globalrange(block_size, (2,1)) == (2:3, 1:2)
24-
@inferred globalrange(block_size, (2,2)) == (2:3, 3:5)
21+
@test @inferred(globalrange(block_size, (1,1))) == (1:1, 1:2)
22+
@test @inferred(globalrange(block_size, (1,2))) == (1:1, 3:5)
23+
@test @inferred(globalrange(block_size, (2,1))) == (2:3, 1:2)
24+
@test @inferred(globalrange(block_size, (2,2))) == (2:3, 3:5)
2525

26-
@inferred global2blockindex(block_size, (3, 1)) == BlockIndex((2,1), (2,1))
27-
@inferred global2blockindex(block_size, (1, 4)) == BlockIndex((1,2), (1,2))
28-
@inferred global2blockindex(block_size, (4, 5)) == BlockIndex((3,2), (1,3))
26+
# Test for allocations inside a function to avoid noise due to global
27+
# variable references
28+
wrapped_allocations = (bs, i) -> @allocated(globalrange(bs, i))
29+
@test wrapped_allocations(block_size, (1, 1)) == 0
2930

30-
@inferred blockindex2global(block_size, BlockIndex((2,1), (2,1))) == (3, 1)
31-
@inferred blockindex2global(block_size, BlockIndex((1,2), (1,2))) == (1, 4)
32-
@inferred blockindex2global(block_size, BlockIndex((3,2), (1,3))) == (4, 5)
31+
@test @inferred(global2blockindex(block_size, (3, 1))) == BlockIndex((2,1), (2,1))
32+
@test @inferred(global2blockindex(block_size, (1, 4))) == BlockIndex((1,2), (1,2))
33+
@test @inferred(global2blockindex(block_size, (4, 5))) == BlockIndex((3,2), (1,3))
3334

35+
wrapped_allocations = (bs, i) -> @allocated(global2blockindex(bs, i))
36+
@test wrapped_allocations(block_size, (3, 1)) == 0
37+
38+
@test @inferred(blockindex2global(block_size, BlockIndex((2,1), (2,1)))) == (3, 1)
39+
@test @inferred(blockindex2global(block_size, BlockIndex((1,2), (1,2)))) == (1, 4)
40+
@test @inferred(blockindex2global(block_size, BlockIndex((3,2), (1,3)))) == (4, 5)
41+
42+
wrapped_allocations = (bs, i) -> @allocated(blockindex2global(bs, i))
43+
@test wrapped_allocations(block_size, BlockIndex((2,1), (2,1))) == 0
3444

3545
@test block_size == BlockSizes(1:3, 2:3)
3646

0 commit comments

Comments
 (0)