|
94 | 94 |
|
95 | 95 | # https://github.com/QuantumKitHub/TensorOperations.jl/issues/280: the generated code must keep |
96 | 96 | # the user's `LineNumberNode`s (so `@tensor` lines show up in code coverage) while dropping the |
97 | | -# parser's own internal ones (which would otherwise pollute the package's coverage). |
| 97 | +# parser's own, which would steal that attribution. See `statementlinenumbernodes` in |
| 98 | +# `runtests.jl` for why only block-statement positions are inspected. `@macroexpand1` is used |
| 99 | +# throughout so that we look at what `@tensor` itself produced, and not at the expansion of |
| 100 | +# macros that it merely passes through (e.g. the `@warn` of the `costcheck` path). |
98 | 101 | @testset "line numbers (issue #280)" begin |
99 | | - collectlinenumbernodes(ex, acc = LineNumberNode[]) = |
100 | | - ( |
101 | | - ex isa LineNumberNode ? push!(acc, ex) : |
102 | | - ex isa Expr && foreach(e -> collectlinenumbernodes(e, acc), ex.args); acc |
103 | | - ) |
104 | | - pkgsrc = dirname(pathof(TensorOperations)) |
105 | | - pkglnns(lnns) = filter(l -> startswith(String(l.file), pkgsrc), lnns) |
106 | | - userlines(lnns) = sort!(unique!([l.line for l in lnns if String(l.file) == @__FILE__])) |
107 | | - |
108 | | - @testset "no internal LineNumberNodes leak into generated code" begin |
109 | | - # covers the scalar, dst-reuse and checkpoint `quote` paths in the parser |
| 102 | + thisfile = Symbol(@__FILE__) |
| 103 | + |
| 104 | + @testset "single-statement expressions carry no LineNumberNodes" begin |
| 105 | + # nothing to preserve here: the input has no `LineNumberNode`s of its own, and the |
| 106 | + # statement is attributed to the line of the `@tensor` call by the surrounding scope |
110 | 107 | exprs = [ |
111 | | - @macroexpand(@tensor T[a, b] := A[a, c] * B[c, b]), |
112 | | - @macroexpand(@tensor R[a, b] := A[a, c] * B[c, d] * C[d, e] * E[e, f] * F[f, b]), |
113 | | - @macroexpand(@tensor s = X[a, b] * Y[a, b]), |
114 | | - @macroexpand(@tensoropt R[a, b] := A[a, c] * B[c, d] * C[d, e] * E[e, b]), |
115 | | - @macroexpand(@tensor allocator = alloc R[a, b] := A[a, c] * B[c, d] * C[d, b]), |
116 | | - @macroexpand(@tensor costcheck = warn R[a, b] := A[a, c] * B[c, d] * C[d, b]), |
117 | | - @macroexpand(@tensor contractcheck = true R[a, b] := A[a, c] * B[c, b]), |
| 108 | + @macroexpand1(@tensor T[a, b] := A[a, c] * B[c, b]), |
| 109 | + @macroexpand1(@tensor R[a, b] := A[a, c] * B[c, d] * C[d, e] * E[e, f] * F[f, b]), |
| 110 | + @macroexpand1(@tensor s = X[a, b] * Y[a, b]), |
| 111 | + @macroexpand1(@tensoropt R[a, b] := A[a, c] * B[c, d] * C[d, e] * E[e, b]), |
| 112 | + @macroexpand1(@tensor allocator = alloc R[a, b] := A[a, c] * B[c, d] * C[d, b]), |
| 113 | + @macroexpand1(@tensor costcheck = warn R[a, b] := A[a, c] * B[c, d] * C[d, b]), |
| 114 | + @macroexpand1(@tensor contractcheck = true R[a, b] := A[a, c] * B[c, b]), |
118 | 115 | ] |
119 | 116 | for ex in exprs |
120 | | - @test isempty(pkglnns(collectlinenumbernodes(ex))) |
| 117 | + @test isempty(statementlinenumbernodes(ex)) |
121 | 118 | end |
122 | 119 | end |
123 | 120 |
|
124 | | - @testset "user LineNumberNodes are preserved per statement" begin |
125 | | - # multi-statement block, including a nested contraction whose intermediate is reused |
126 | | - block = @macroexpand @tensor begin |
| 121 | + @testset "one user LineNumberNode per statement of a block" begin |
| 122 | + # multi-statement block, including a scalar assignment; `s = ...` additionally |
| 123 | + # exercises the `_flatten` path that hoists a block into the right hand side |
| 124 | + firstline = @__LINE__() + 2 |
| 125 | + block = @macroexpand1 @tensor begin |
127 | 126 | T[a, e] := A[a, c] * B[c, d] * C[d, e] |
128 | 127 | D[a, b] := T[a, e] * E[e, b] |
129 | 128 | s = D[a, b] * F[a, b] |
130 | 129 | end |
131 | | - lnns = collectlinenumbernodes(block) |
132 | | - @test isempty(pkglnns(lnns)) |
133 | | - @test length(userlines(lnns)) >= 3 # one distinct user line per statement |
| 130 | + lnns = statementlinenumbernodes(block) |
| 131 | + @test all(l -> l.file === thisfile, lnns) |
| 132 | + @test sort!(unique(l.line for l in lnns)) == collect(firstline .+ (0:2)) |
| 133 | + |
| 134 | + # dst-reuse: `tensorify` wraps this in a `quote` of its own |
| 135 | + reuseline = @__LINE__() + 2 |
| 136 | + reuseblock = @macroexpand1 @tensor begin |
| 137 | + T[a, b] := A[a, c] * B[c, b] |
| 138 | + T[a, b] := T[a, c] * C[c, b] |
| 139 | + end |
| 140 | + reuselnns = statementlinenumbernodes(reuseblock) |
| 141 | + @test all(l -> l.file === thisfile, reuselnns) |
| 142 | + @test sort!(unique(l.line for l in reuselnns)) == collect(reuseline .+ (0:1)) |
134 | 143 |
|
135 | | - optblock = @macroexpand @tensoropt begin |
| 144 | + optline = @__LINE__() + 2 |
| 145 | + optblock = @macroexpand1 @tensoropt begin |
136 | 146 | T[a, e] := A[a, c] * B[c, d] * C[d, e] |
137 | 147 | D[a, b] := T[a, e] * E[e, b] |
138 | 148 | end |
139 | | - optlnns = collectlinenumbernodes(optblock) |
140 | | - @test isempty(pkglnns(optlnns)) |
141 | | - @test length(userlines(optlnns)) >= 2 |
| 149 | + optlnns = statementlinenumbernodes(optblock) |
| 150 | + @test all(l -> l.file === thisfile, optlnns) |
| 151 | + @test sort!(unique(l.line for l in optlnns)) == collect(optline .+ (0:1)) |
| 152 | + end |
| 153 | + |
| 154 | + @testset "kwargs that generate extra code preserve user line numbers" begin |
| 155 | + # `allocator` inserts a checkpoint `quote`, `costcheck` inserts a `@notensor` block |
| 156 | + # containing a `@warn` whose own `LineNumberNode` is structurally required |
| 157 | + allocline = @__LINE__() + 2 |
| 158 | + allocblock = @macroexpand1 @tensor allocator = alloc begin |
| 159 | + T[a, b] := A[a, c] * B[c, b] |
| 160 | + Z[a, b] := T[a, c] * C[c, b] |
| 161 | + end |
| 162 | + alloclnns = statementlinenumbernodes(allocblock) |
| 163 | + @test all(l -> l.file === thisfile, alloclnns) |
| 164 | + @test sort!(unique(l.line for l in alloclnns)) == collect(allocline .+ (0:1)) |
| 165 | + |
| 166 | + costline = @__LINE__() + 2 |
| 167 | + costblock = @macroexpand1 @tensor costcheck = warn begin |
| 168 | + T[a, b] := A[a, c] * B[c, b] |
| 169 | + Z[a, b] := T[a, c] * C[c, b] |
| 170 | + end |
| 171 | + costlnns = statementlinenumbernodes(costblock) |
| 172 | + @test all(l -> l.file === thisfile, costlnns) |
| 173 | + @test sort!(unique(l.line for l in costlnns)) == collect(costline .+ (0:1)) |
| 174 | + # the `@warn` macrocall must have kept its (TensorOperations-internal) LineNumberNode, |
| 175 | + # otherwise the expression cannot be expanded at all |
| 176 | + @test macroexpand(@__MODULE__, costblock; recursive = true) isa Expr |
142 | 177 | end |
143 | 178 | end |
144 | 179 |
|
|
0 commit comments