|
92 | 92 | end |
93 | 93 | end |
94 | 94 |
|
| 95 | +# https://github.com/QuantumKitHub/TensorOperations.jl/issues/280: the generated code must keep |
| 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). |
| 98 | +@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 |
| 110 | + 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]), |
| 118 | + ] |
| 119 | + for ex in exprs |
| 120 | + @test isempty(pkglnns(collectlinenumbernodes(ex))) |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 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 |
| 127 | + T[a, e] := A[a, c] * B[c, d] * C[d, e] |
| 128 | + D[a, b] := T[a, e] * E[e, b] |
| 129 | + s = D[a, b] * F[a, b] |
| 130 | + end |
| 131 | + lnns = collectlinenumbernodes(block) |
| 132 | + @test isempty(pkglnns(lnns)) |
| 133 | + @test length(userlines(lnns)) >= 3 # one distinct user line per statement |
| 134 | + |
| 135 | + optblock = @macroexpand @tensoropt begin |
| 136 | + T[a, e] := A[a, c] * B[c, d] * C[d, e] |
| 137 | + D[a, b] := T[a, e] * E[e, b] |
| 138 | + end |
| 139 | + optlnns = collectlinenumbernodes(optblock) |
| 140 | + @test isempty(pkglnns(optlnns)) |
| 141 | + @test length(userlines(optlnns)) >= 2 |
| 142 | + end |
| 143 | +end |
| 144 | + |
95 | 145 | @testset "opt" begin |
96 | 146 | A = randn(5, 5, 5, 5) |
97 | 147 | B = randn(5, 5, 5) |
|
0 commit comments