Skip to content

Commit cbf9639

Browse files
committed
linenumbers
1 parent 574b16d commit cbf9639

6 files changed

Lines changed: 114 additions & 11 deletions

File tree

‎ext/TensorOperationsBumperExt.jl‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ function TensorOperations._butensor(src, ex...)
2727
buf_sym = gensym("buffer")
2828

2929
# TODO: there is no check for doubled tensor kwargs
30-
newex = quote
31-
$buf_sym = $(Expr(:call, GlobalRef(Bumper, :default_buffer)))
32-
$(
33-
Expr(
34-
:macrocall, GlobalRef(TensorOperations, Symbol("@tensor")),
35-
src, :(allocator = $buf_sym), ex...
36-
)
30+
return Expr(
31+
:block,
32+
Expr(:(=), buf_sym, Expr(:call, GlobalRef(Bumper, :default_buffer))),
33+
Expr(
34+
:macrocall, GlobalRef(TensorOperations, Symbol("@tensor")),
35+
src, :(allocator = $buf_sym), ex...
3736
)
38-
end
39-
return Base.remove_linenums!(newex)
37+
)
4038
end
4139

4240
end

‎src/indexnotation/contractiontrees.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function insertcontractiontrees!(
163163
end
164164
)
165165
end
166-
push!(postexprs, removelinenumbernode(costcompareex))
166+
push!(postexprs, removeinternallinenumbernodes(costcompareex))
167167
return treeex
168168
end
169169

‎src/indexnotation/parser.jl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mutable struct TensorParser
1212
contractiontreebuilder = defaulttreebuilder
1313
contractiontreesorter = defaulttreesorter
1414
contractioncostcheck = nothing
15-
postprocessors = [_flatten, removelinenumbernode, addtensoroperations]
15+
postprocessors = [_flatten, addtensoroperations]
1616
return new(
1717
preprocessors,
1818
contractiontreebuilder, contractiontreesorter, contractioncostcheck,
@@ -34,6 +34,7 @@ function (parser::TensorParser)(ex::Expr)
3434
for p in parser.postprocessors
3535
ex = p(ex)::Expr
3636
end
37+
ex = removeinternallinenumbernodes(ex)::Expr
3738
return ex
3839
end
3940

‎src/indexnotation/postprocessors.jl‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,47 @@ function _flatten(ex)
3030
end
3131
end
3232

33+
# package source directory (with trailing separator), used to recognize `LineNumberNode`s that
34+
# point into the parser's own `quote` blocks rather than into user code.
35+
const _PARSER_SRCDIR = joinpath(dirname(@__DIR__), "")
36+
37+
_isinternallinenumber(@nospecialize(x)) =
38+
x isa LineNumberNode && startswith(String(x.file), _PARSER_SRCDIR)
39+
40+
"""
41+
removeinternallinenumbernodes(ex)
42+
43+
Remove all `LineNumberNode`s that point into the TensorOperations source tree, i.e. the ones
44+
introduced by the parser's own `quote` blocks. `LineNumberNode`s originating from user code are
45+
kept, so that the generated code remains attributable to the user's source lines (e.g. for code
46+
coverage).
47+
"""
48+
function removeinternallinenumbernodes(ex)
49+
if isexpr(ex, :block)
50+
# within a block, `LineNumberNode`s are statement markers: drop the internal ones
51+
args = Any[
52+
removeinternallinenumbernodes(e) for e in ex.args
53+
if !_isinternallinenumber(e)
54+
]
55+
return Expr(:block, args...)
56+
elseif isa(ex, Expr)
57+
# elsewhere (e.g. the mandatory 2nd argument of a `:macrocall`) a `LineNumberNode` may
58+
# be structurally required, so keep all positions and only recurse into nested blocks
59+
return Expr(ex.head, Any[removeinternallinenumbernodes(e) for e in ex.args]...)
60+
else
61+
return ex
62+
end
63+
end
64+
3365
"""
3466
removelinenumbernode(ex)
3567
3668
Remove all `LineNumberNode`s from an expression.
69+
70+
!!! note
71+
Kept for backwards compatibility. The parser now uses
72+
[`removeinternallinenumbernodes`](@ref), which preserves user `LineNumberNode`s so that
73+
generated code stays attributable to the user's source lines (e.g. for code coverage).
3774
"""
3875
function removelinenumbernode(ex)
3976
if isexpr(ex, :block)

‎test/butensor.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
end
77

88
using Bumper
9+
@testset "@butensor preserves user line numbers (issue #280)" begin
10+
# `@butensor` wraps the block in an inner `@tensor`; make sure it does not strip the user's
11+
# line numbers, and does not leak TensorOperations-internal ones.
12+
pkgsrc = dirname(pathof(TensorOperations))
13+
lnns = LineNumberNode[]
14+
collect_lnns(x) = x isa LineNumberNode ? push!(lnns, x) :
15+
x isa Expr && foreach(collect_lnns, x.args)
16+
collect_lnns(
17+
@macroexpand @butensor begin
18+
T[a, b] := X[a, c] * Y[c, b]
19+
Z[a, b] := T[a, c] * W[c, b]
20+
end
21+
)
22+
@test !any(l -> startswith(String(l.file), pkgsrc), lnns)
23+
@test count(l -> String(l.file) == @__FILE__, lnns) >= 2
24+
end
25+
926
@testset "Bumper tests with eltype $T" for T in (Float32, ComplexF64)
1027
D1, D2, D3 = 30, 40, 20
1128
d1, d2 = 2, 3

‎test/macro_kwargs.jl‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,56 @@ end
9292
end
9393
end
9494

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+
95145
@testset "opt" begin
96146
A = randn(5, 5, 5, 5)
97147
B = randn(5, 5, 5)

0 commit comments

Comments
 (0)