Skip to content

Commit f7dde1d

Browse files
committed
Make constant propagation unconditional (#515)
1 parent 955c378 commit f7dde1d

File tree

8 files changed

+29
-117
lines changed

8 files changed

+29
-117
lines changed

ApproxFunBaseTest/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1414
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1515

1616
[compat]
17-
ApproxFunBase = "0.5, 0.6, 0.7, 0.8"
17+
ApproxFunBase = "0.5, 0.6, 0.7, 0.8, 0.9"
1818
BandedMatrices = "0.16, 0.17"
1919
BlockArrays = "0.14, 0.15, 0.16"
2020
BlockBandedMatrices = "0.10, 0.11, 0.12"

src/Fun.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,7 @@ end
594594

595595
\(c::Number, f::Fun) = Fun(f.space, c \ f.coefficients)
596596

597-
@static if VERSION >= v"1.8"
598-
Base.@constprop :aggressive intpow(f::Fun, k::Integer) = _intpow(f, k)
599-
else
600-
intpow(f::Fun, k::Integer) = _intpow(f, k)
601-
end
602-
@inline function _intpow(f, k)
597+
Base.@constprop :aggressive function intpow(f, k)
603598
if k == 0
604599
ones(cfstype(f), space(f))
605600
elseif k==1

src/Multivariate/TensorSpace.jl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,14 @@ Space(sp::ProductDomain) = TensorSpace(sp)
358358
setdomain(sp::TensorSpace, d::ProductDomain) = TensorSpace(setdomain.(factors(sp), factors(d)))
359359

360360
*(A::Space, B::Space) = A B
361-
@inline function _powspace(A, p)
361+
Base.@constprop :aggressive function ^(A::Space, p::Integer)
362362
p >= 1 || throw(ArgumentError("exponent must be >= 1, received $p"))
363363
# Enumerate common cases to help with constant propagation
364364
p == 1 ? A :
365365
p == 2 ? A * A :
366366
p == 3 ? A * A * A :
367367
foldl(*, ntuple(_ -> A, p))
368368
end
369-
@static if VERSION >= v"1.8"
370-
Base.@constprop :aggressive function ^(A::Space, p::Integer)
371-
_powspace(A, p)
372-
end
373-
else
374-
^(A::Space, p::Integer) = _powspace(A, p)
375-
end
376369

377370

378371
## TODO: generalize

src/Operators/banded/CalculusOperator.jl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ end
191191

192192

193193
## Map to canonical
194-
@inline function _DefaultDerivative(sp::Space, k::Number)
194+
Base.@constprop :aggressive function DefaultDerivative(sp::Space, k::Number)
195195
assert_integer(k)
196196
if nameof(typeof(canonicaldomain(sp))) == nameof(typeof(domain(sp)))
197197
# this is the normal default constructor
@@ -222,19 +222,7 @@ end
222222
end
223223
end
224224

225-
@static if VERSION >= v"1.8"
226-
for f in (:DefaultDerivative, :DefaultIntegral)
227-
_f = Symbol(:_, f)
228-
@eval Base.@constprop :aggressive $f(sp::Space, k::Number) = $_f(sp, k)
229-
end
230-
else
231-
for f in (:DefaultDerivative, :DefaultIntegral)
232-
_f = Symbol(:_, f)
233-
@eval $f(sp::Space, k::Number) = $_f(sp, k)
234-
end
235-
end
236-
237-
@inline function _DefaultIntegral(sp::Space, k::Number)
225+
Base.@constprop :aggressive function DefaultIntegral(sp::Space, k::Number)
238226
assert_integer(k)
239227
if nameof(typeof(canonicaldomain(sp))) == nameof(typeof(domain(sp)))
240228
# this is the normal default constructor

src/Operators/general/CachedOperator.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@ CachedOperator(op::Operator,data::AbstractMatrix,sz::Tuple{Int,Int},pd=false) =
2424
CachedOperator(op::Operator,data::AbstractMatrix,padding=false) = CachedOperator(op,data,size(data),padding)
2525

2626

27-
@static if VERSION >= v"1.8"
28-
Base.@constprop :aggressive function default_CachedOperator(op::Operator;padding::Bool=false)
29-
_default_CachedOperator(op, padding)
30-
end
31-
else
32-
function default_CachedOperator(op::Operator;padding::Bool=false)
33-
_default_CachedOperator(op, padding)
34-
end
35-
end
36-
@inline function _default_CachedOperator(op::Operator, padding)
27+
Base.@constprop :aggressive function default_CachedOperator(op::Operator; padding::Bool=false)
3728
if isbanded(op)
3829
CachedOperator(BandedMatrix, op; padding=padding)
3930
elseif isbandedblockbanded(op) && !padding

src/Operators/general/InterlaceOperator.jl

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ end
9090
const VectorInterlaceOperator = InterlaceOperator{T,1,DS,RS} where {T,DS,RS<:Space{D,R}} where {D,R<:AbstractVector}
9191
const MatrixInterlaceOperator = InterlaceOperator{T,2,DS,RS} where {T,DS,RS<:Space{D,R}} where {D,R<:AbstractVector}
9292

93-
@static if VERSION >= v"1.8"
94-
Base.@constprop :aggressive interlace_bandwidths(args...) = _interlace_bandwidths(args...)
95-
else
96-
interlace_bandwidths(args...) = _interlace_bandwidths(args...)
97-
end
98-
9993
__interlace_ops_bandwidths(ops::AbstractMatrix) = bandwidths.(ops)
10094
__interlace_ops_bandwidths(ops::Diagonal) = bandwidths.(parent(ops))
10195
function __interlace_bandwidths_square(ops::AbstractMatrix, bw = __interlace_ops_bandwidths(ops))
@@ -119,8 +113,11 @@ function __interlace_bandwidths_square(ops::Diagonal, bw = __interlace_ops_bandw
119113
l,u
120114
end
121115

122-
@inline function _interlace_bandwidths(ops::AbstractMatrix{<:Operator}, ds, rs,
123-
allbanded = all(isbanded,ops), bw = allbanded ? __interlace_ops_bandwidths(ops) : nothing)
116+
Base.@constprop :aggressive function interlace_bandwidths(ops::AbstractMatrix{<:Operator},
117+
ds, rs,
118+
allbanded = all(isbanded, ops),
119+
bw = allbanded ? __interlace_ops_bandwidths(ops) : nothing)
120+
124121
p=size(ops,1)
125122
dsi = interlacer(ds)
126123
rsi = interlacer(rs)
@@ -159,7 +156,7 @@ function InterlaceOperator(ops::AbstractMatrix{<:Operator},ds::Space,rs::Space;
159156
israggedbelow)
160157
end
161158

162-
@inline function _interlace_bandwidths(ops::VectorOrTupleOfOp, ds, rs, allbanded = all(isbanded, ops))
159+
Base.@constprop :aggressive function interlace_bandwidths(ops::VectorOrTupleOfOp, ds, rs, allbanded = all(isbanded, ops))
163160
p=size(ops,1)
164161
ax1 = first(axes(ops))
165162
if allbanded
@@ -210,19 +207,10 @@ function InterlaceOperator(opsin::AbstractVector{<:Operator})
210207
ops = convert_vector(promotedomainspace(opsin))
211208
InterlaceOperator(ops, domainspace(first(ops)), rangespace(ops))
212209
end
213-
@inline function _InterlaceOperator(opsin, promotedomain)
210+
Base.@constprop :aggressive function InterlaceOperator(opsin, promotedomain = true)
214211
ops = promotedomain ? promotedomainspace(opsin) : opsin
215212
InterlaceOperator(ops, domainspace(first(ops)), rangespace(ops))
216213
end
217-
@static if VERSION >= v"1.8"
218-
Base.@constprop :aggressive function InterlaceOperator(opsin::Tuple{Operator, Vararg{Operator}}, promotedomain = true)
219-
_InterlaceOperator(opsin, promotedomain)
220-
end
221-
else
222-
function InterlaceOperator(opsin::Tuple{Operator, Vararg{Operator}}, promotedomain = true)
223-
_InterlaceOperator(opsin, promotedomain)
224-
end
225-
end
226214

227215
InterlaceOperator(ops::AbstractArray, ds=NoSpace, rs=ds) =
228216
InterlaceOperator(Array{Operator{promote_eltypeof(ops)}, ndims(ops)}(ops), ds, rs)

src/Operators/general/algebra.jl

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -250,34 +250,18 @@ struct TimesOperator{T,BW,SZ,O<:Operator{T},BBW,SBBW} <: Operator{T}
250250
israggedbelow::Bool
251251
isafunctional::Bool
252252

253-
@static if VERSION >= v"1.8"
254-
Base.@constprop :aggressive function TimesOperator{T,BW,SZ,O,BBW,SBBW}(ops::Vector{O}, bw::BW,
255-
sz::SZ, bbw::BBW, sbbw::SBBW,
256-
ibbb::Bool, irb::Bool, isaf::Bool;
257-
anytimesop = any(x -> x isa TimesOperator, ops)) where {T,O<:Operator{T},BW,SZ,BBW,SBBW}
258-
259-
# check compatible
260-
check_times(ops)
261-
262-
# remove TimesOperators buried inside ops
263-
newops = anytimesop ? splice_times(ops) : ops
264-
265-
new{T,BW,SZ,O,BBW,SBBW}(newops, bw, sz, bbw, sbbw, ibbb, irb, isaf)
266-
end
267-
else
268-
function TimesOperator{T,BW,SZ,O,BBW,SBBW}(ops::Vector{O}, bw::BW,
269-
sz::SZ, bbw::BBW, sbbw::SBBW,
270-
ibbb::Bool, irb::Bool, isaf::Bool;
271-
anytimesop = any(x -> x isa TimesOperator, ops)) where {T,O<:Operator{T},BW,SZ,BBW,SBBW}
253+
Base.@constprop :aggressive function TimesOperator{T,BW,SZ,O,BBW,SBBW}(ops::Vector{O}, bw::BW,
254+
sz::SZ, bbw::BBW, sbbw::SBBW,
255+
ibbb::Bool, irb::Bool, isaf::Bool;
256+
anytimesop = any(x -> x isa TimesOperator, ops)) where {T,O<:Operator{T},BW,SZ,BBW,SBBW}
272257

273-
# check compatible
274-
check_times(ops)
258+
# check compatible
259+
check_times(ops)
275260

276-
# remove TimesOperators buried inside ops
277-
newops = anytimesop ? splice_times(ops) : ops
261+
# remove TimesOperators buried inside ops
262+
newops = anytimesop ? splice_times(ops) : ops
278263

279-
new{T,BW,SZ,O,BBW,SBBW}(newops, bw, sz, bbw, sbbw, ibbb, irb, isaf)
280-
end
264+
new{T,BW,SZ,O,BBW,SBBW}(newops, bw, sz, bbw, sbbw, ibbb, irb, isaf)
281265
end
282266
end
283267

@@ -334,13 +318,7 @@ function convert(::Type{Operator{T}}, P::TimesOperator) where {T}
334318
end
335319
end
336320

337-
338-
@static if VERSION > v"1.8"
339-
Base.@constprop :aggressive promotetimes(args...) = _promotetimes(args...)
340-
else
341-
promotetimes(args...) = _promotetimes(args...)
342-
end
343-
@inline function _promotetimes(opsin,
321+
Base.@constprop :aggressive function promotetimes(opsin,
344322
dsp=domainspace(last(opsin)),
345323
anytimesop=true)
346324

@@ -666,22 +644,14 @@ end
666644
-(A::Operator) = ConstantTimesOperator(-1, A)
667645
-(A::Operator, B::Operator) = A + (-B)
668646

669-
@inline function _mulop(f::Fun, A::Operator)
647+
Base.@constprop :aggressive function *(f::Fun, A::Operator)
670648
if isafunctional(A) && (isinf(bandwidth(A, 1)) || isinf(bandwidth(A, 2)))
671649
LowRankOperator(f, A)
672650
else
673651
TimesOperator(Multiplication(f, rangespace(A)), A)
674652
end
675653
end
676654

677-
@static if VERSION >= v"1.8"
678-
Base.@constprop :aggressive function *(f::Fun, A::Operator)
679-
_mulop(f, A)
680-
end
681-
else
682-
*(f::Fun, A::Operator) = _mulop(f, A)
683-
end
684-
685655
*(c::Number, A::Operator) = ConstantTimesOperator(c, A)
686656
*(A::Operator, c::Number) = c * A
687657

src/Spaces/ProductSpaceOperators.jl

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,32 +245,24 @@ _spacename(::PiecewiseSpace) = PiecewiseSpace
245245
end
246246

247247
for (Op,OpWrap) in ((:Derivative,:DerivativeWrapper),(:Integral,:IntegralWrapper))
248-
_Op = Symbol(:_, Op)
249248
@eval begin
250-
@inline function $_Op(S::PiecewiseSpace, k::Number)
249+
Base.@constprop :aggressive function $Op(S::PiecewiseSpace, k::Number)
251250
assert_integer(k)
252251
t = map(s->$Op(s,k),components(S))
253252
O = InterlaceOperator_Diagonal(t, S)
254253
$OpWrap(O,k)
255254
end
256-
@inline function $_Op(S::ArraySpace, k::Number)
255+
Base.@constprop :aggressive function $Op(S::ArraySpace, k::Number)
257256
assert_integer(k)
258257
ops = map(s->$Op(s,k),S)
259258
RS = ArraySpace(reshape(map(rangespace, ops), size(S)))
260259
O = InterlaceOperator(Diagonal(ops), S, RS)
261260
$OpWrap(O,k)
262261
end
263-
@static if VERSION >= v"1.8"
264-
Base.@constprop :aggressive $Op(s::PiecewiseSpace, k::Number) = $_Op(s, k)
265-
Base.@constprop :aggressive $Op(s::ArraySpace, k::Number) = $_Op(s, k)
266-
else
267-
$Op(s::PiecewiseSpace, k::Number) = $_Op(s, k)
268-
$Op(s::ArraySpace, k::Number) = $_Op(s, k)
269-
end
270262
end
271263
end
272264

273-
@inline function _Derivative(S::SumSpace, k::Number)
265+
Base.@constprop :aggressive function Derivative(S::SumSpace, k::Number)
274266
assert_integer(k)
275267
# we want to map before we decompose, as the map may introduce
276268
# mixed bases.
@@ -283,13 +275,8 @@ end
283275
end
284276
end
285277

286-
@static if VERSION >= v"1.8"
287-
Base.@constprop :aggressive Derivative(s::SumSpace, k::Number) = _Derivative(s, k)
288-
else
289-
Derivative(s::SumSpace, k::Number) = _Derivative(s, k)
290-
end
291-
292-
choosedomainspace(M::CalculusOperator{UnsetSpace},sp::SumSpace)=mapreduce(s->choosedomainspace(M,s),union,sp.spaces)
278+
choosedomainspace(M::CalculusOperator{UnsetSpace}, sp::SumSpace) =
279+
mapreduce(s->choosedomainspace(M,s),union,sp.spaces)
293280

294281
## Multiplcation for Array*Vector
295282

0 commit comments

Comments
 (0)