Skip to content

Commit

Permalink
static_parameter expr (#175)
Browse files Browse the repository at this point in the history
* comment about TypedSlot

* find an Instruction dynamically

* translate static_parameter expr

* translate literal variables

* test case

* test case for static parameter

* drop compat for Julia < 1.10; mark as breaking release

* Restrict CI to 1.10

* eval static parameters in args

* Test whether Julia 1.7 still works

* eval static parameter when binding vars

* [skip ci] fix typos

---------

Co-authored-by: Hong Ge <[email protected]>
Co-authored-by: Penelope Yong <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent d176f19 commit 6264afd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
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

2 comments on commit 6264afd

@penelopeysm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117816

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.8 -m "<description of version>" 6264afd33f9441697d2f5ed4a75bbcc37f15feba
git push origin v0.8.8

Please sign in to comment.