Skip to content

Commit d7afd4f

Browse files
Merge pull request #735 from AayushSabharwal/as/cse-fix
fix: tuples and arrays of `Symbols` should not be considered symbolic in codegen
2 parents 91df94d + 3b5094e commit d7afd4f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/code.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,29 @@ function substitute_name(O, st)
176176
end
177177
end
178178

179+
function _is_tuple_or_array_of_symbolics(O)
180+
return O isa CodegenPrimitive ||
181+
(symbolic_type(O) != NotSymbolic() && !(O isa Union{Symbol, Expr})) ||
182+
_is_array_of_symbolics(O) ||
183+
_is_tuple_of_symbolics(O)
184+
end
185+
179186
function _is_array_of_symbolics(O)
180187
# O is an array, not a symbolic array, and either has a non-symbolic eltype or contains elements that are
181188
# symbolic or arrays of symbolics
182189
return O isa AbstractArray && symbolic_type(O) == NotSymbolic() &&
183-
(symbolic_type(eltype(O)) != NotSymbolic() ||
184-
any(x -> x isa CodegenPrimitive || symbolic_type(x) != NotSymbolic() || _is_array_of_symbolics(x), O))
190+
(symbolic_type(eltype(O)) != NotSymbolic() && !(eltype(O) <: Union{Symbol, Expr}) ||
191+
any(_is_tuple_or_array_of_symbolics, O))
185192
end
186193

187194
# workaround for https://github.com/JuliaSparse/SparseArrays.jl/issues/599
188195
function _is_array_of_symbolics(O::SparseMatrixCSC)
189-
return symbolic_type(eltype(O)) != NotSymbolic() ||
190-
any(x -> x isa CodegenPrimitive || symbolic_type(x) != NotSymbolic() || _is_array_of_symbolics(x), findnz(O)[3])
196+
return symbolic_type(eltype(O)) != NotSymbolic() && !(eltype(O) <: Union{Symbol, Expr}) ||
197+
any(_is_tuple_or_array_of_symbolics, findnz(O)[3])
191198
end
192199

193200
function _is_tuple_of_symbolics(O::Tuple)
194-
return any(x -> x isa CodegenPrimitive || symbolic_type(x) != NotSymbolic() || _is_array_of_symbolics(x) || _is_tuple_of_symbolics(x), O)
201+
return any(_is_tuple_or_array_of_symbolics, O)
195202
end
196203
_is_tuple_of_symbolics(O) = false
197204

test/cse.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,22 @@ end
229229
fn2 = @RuntimeGeneratedFunction(cse(toexpr(fnexpr)))
230230
@test fn1(ones(5)) == fn2(ones(5))
231231
end
232+
233+
@testset "Tuples and arrays of `Symbol`s aren't symbolic" begin
234+
@syms x y
235+
f(a, b, c) = a + b + length(c)
236+
ex = term(f, x, y, (:a, :b, :c))
237+
expr = quote
238+
let x = 1, y = 2
239+
$(toexpr(cse(ex)))
240+
end
241+
end
242+
@test eval(expr) == 6
243+
ex = term(f, x, y, [:a, :b, :c])
244+
expr = quote
245+
let x = 1, y = 2
246+
$(toexpr(cse(ex)))
247+
end
248+
end
249+
@test eval(expr) == 6
250+
end

0 commit comments

Comments
 (0)