Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static_parameter expr #175

Merged
merged 12 commits into from
Oct 22, 2024
1 change: 1 addition & 0 deletions .github/workflows/Testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
matrix:
version:
- '1.7'
- '1.10'
- '1'
- 'nightly'
os:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
license = "MIT"
desc = "Tape based task copying in Turing"
repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.8.7"
version = "0.8.8"

[deps]
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
Expand Down
3 changes: 2 additions & 1 deletion perf/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ println("======= breakdown benchmark =======")
x = rand(100000)
tf = Libtask.TapedFunction(ackley, x, nothing)
tf(x, nothing);
ins = tf.tape[45]
idx = findlast((x)->isa(x, Libtask.Instruction), tf.tape)
ins = tf.tape[idx]
b = ins.input[1]

@show ins.input |> length
Expand Down
3 changes: 2 additions & 1 deletion src/Libtask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export TArray, tzeros, tfill, TRef # legacy types back compat


@static if isdefined(Core, :TypedSlot) || isdefined(Core.Compiler, :TypedSlot)
# Julia v1.10 removed Core.TypedSlot
# Julia v1.10 moved Core.TypedSlot to Core.Compiler
# Julia v1.11 removed Core.Compiler.TypedSlot
const TypedSlot = @static if isdefined(Core, :TypedSlot)
Core.TypedSlot
else
Expand Down
18 changes: 15 additions & 3 deletions src/tapedfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,10 @@ end

const IRVar = Union{Core.SSAValue, Core.SlotNumber}

function bind_var!(var_literal, bindings::Bindings, ir::Core.CodeInfo)
# for literal constants
push!(bindings, var_literal)
function bind_var!(var, bindings::Bindings, ir::Core.CodeInfo)
# for literal constants, and static parameters
var = Meta.isexpr(var, :static_parameter) ? ir.parent.sparam_vals[var.args[1]] : var
push!(bindings, var)
idx = length(bindings)
return idx
end
Expand Down Expand Up @@ -368,6 +369,14 @@ function translate!!(var::IRVar, line::Core.SlotNumber,
return Instruction(func, input, output)
end

function translate!!(var::IRVar, line::Number, # literal vars
bindings::Bindings, isconst::Bool, ir)
func = identity
input = (bind_var!(line, bindings, ir),)
output = bind_var!(var, bindings, ir)
return Instruction(func, input, output)
end

function translate!!(var::IRVar, line::NTuple{N, Symbol},
bindings::Bindings, isconst::Bool, ir) where {N}
# for syntax (; x, y, z), see Turing.jl#1873
Expand Down Expand Up @@ -439,6 +448,9 @@ function translate!!(var::IRVar, line::Expr,
end
return Instruction(identity, (_bind_fn(rhs),), _bind_fn(lhs))
end
elseif head === :static_parameter
v = ir.parent.sparam_vals[line.args[1]]
return Instruction(identity, (_bind_fn(v),), _bind_fn(var))
else
@error "Unknown Expression: " typeof(var) var typeof(line) line
throw(ErrorException("Unknown Expression"))
Expand Down
21 changes: 21 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,25 @@
r = tf(1, 2)
@test r == (c=3, x=1, y=2)
end

@testset "Issue-Libtask-174, SSAValue=Int and static parameter" begin
# SSAValue = Int
function f()
# this line generates: %1 = 1::Core.Const(1)
r = (a = 1)
return nothing
end
tf = Libtask.TapedFunction(f)
r = tf()
@test r == nothing

# static parameter
function g(::Type{T}) where {T}
a = zeros(T, 10)
end
tf = Libtask.TapedFunction(g, Float64)
r = tf(Float64)
@test r == zeros(Float64, 10)
end

end