From 92b3a6d70b04313cf69e8f1cc9bf43d6f57b928f Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 08:59:27 +0800 Subject: [PATCH 01/12] comment about TypedSlot --- .github/workflows/Testing.yaml | 1 + src/Libtask.jl | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/Testing.yaml b/.github/workflows/Testing.yaml index 48d03b62..224c879e 100644 --- a/.github/workflows/Testing.yaml +++ b/.github/workflows/Testing.yaml @@ -13,6 +13,7 @@ jobs: version: - '1.7' - '1' + - '1.11.1' - 'nightly' os: - ubuntu-latest diff --git a/src/Libtask.jl b/src/Libtask.jl index ac349a80..1c6c676e 100644 --- a/src/Libtask.jl +++ b/src/Libtask.jl @@ -10,6 +10,7 @@ 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.11 removed Core.Compiler.TypedSlot const TypedSlot = @static if isdefined(Core, :TypedSlot) Core.TypedSlot else From 38376c379ff737816606ea2e8174581d597ddf52 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 09:43:18 +0800 Subject: [PATCH 02/12] find an Instruction dynamically --- .github/workflows/Testing.yaml | 1 - perf/benchmark.jl | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Testing.yaml b/.github/workflows/Testing.yaml index 224c879e..48d03b62 100644 --- a/.github/workflows/Testing.yaml +++ b/.github/workflows/Testing.yaml @@ -13,7 +13,6 @@ jobs: version: - '1.7' - '1' - - '1.11.1' - 'nightly' os: - ubuntu-latest diff --git a/perf/benchmark.jl b/perf/benchmark.jl index 2b2312c0..dcfb3638 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -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 From 7d73c270d71cb48f54db552b813980456424d4d9 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 10:35:19 +0800 Subject: [PATCH 03/12] translate static_parameter expr --- src/tapedfunction.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tapedfunction.jl b/src/tapedfunction.jl index 2d947aaf..2059fd18 100644 --- a/src/tapedfunction.jl +++ b/src/tapedfunction.jl @@ -439,6 +439,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")) From b18752b4c52c077312684e0375f0a4728dad7be3 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 15:21:57 +0800 Subject: [PATCH 04/12] translate literal variables --- src/tapedfunction.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tapedfunction.jl b/src/tapedfunction.jl index 2059fd18..bac08e01 100644 --- a/src/tapedfunction.jl +++ b/src/tapedfunction.jl @@ -368,6 +368,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 From 7349f36b47cf4ecfeb89a410ec7bbad2512da250 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 18:12:41 +0800 Subject: [PATCH 05/12] test case --- src/Libtask.jl | 2 +- test/issues.jl | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Libtask.jl b/src/Libtask.jl index 1c6c676e..8fa79533 100644 --- a/src/Libtask.jl +++ b/src/Libtask.jl @@ -9,7 +9,7 @@ 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 diff --git a/test/issues.jl b/test/issues.jl index f9deeffd..a559c981 100644 --- a/test/issues.jl +++ b/test/issues.jl @@ -59,4 +59,17 @@ 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 + end + end From 2175446d47fefa4d3a68cf58d6f52910231f6778 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 18:36:44 +0800 Subject: [PATCH 06/12] test case for static parameter --- test/issues.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/issues.jl b/test/issues.jl index a559c981..370c0235 100644 --- a/test/issues.jl +++ b/test/issues.jl @@ -70,6 +70,14 @@ 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 From 2233b3a5168effab387594af3a1d869b41a61c03 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:51:12 +0100 Subject: [PATCH 07/12] drop compat for Julia < 1.10; mark as breaking release --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index bc778669..f4890767 100644 --- a/Project.toml +++ b/Project.toml @@ -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.9" [deps] FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" @@ -14,7 +14,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" [compat] FunctionWrappers = "1.1" LRUCache = "1.3" -julia = "1.7" +julia = "1.10" [extras] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" From 3bc973a0ec02d79769de71704b006fd998f56725 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 12:03:26 +0100 Subject: [PATCH 08/12] Restrict CI to 1.10 --- .github/workflows/Testing.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Testing.yaml b/.github/workflows/Testing.yaml index 48d03b62..2b8b8685 100644 --- a/.github/workflows/Testing.yaml +++ b/.github/workflows/Testing.yaml @@ -11,7 +11,7 @@ jobs: strategy: matrix: version: - - '1.7' + - '1.10' - '1' - 'nightly' os: From ee50a93956f51f5a9b1df6fcea71edaaaf246c64 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 19:34:27 +0800 Subject: [PATCH 09/12] eval static parameters in args --- src/tapedfunction.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tapedfunction.jl b/src/tapedfunction.jl index bac08e01..cf9f74c0 100644 --- a/src/tapedfunction.jl +++ b/src/tapedfunction.jl @@ -412,8 +412,12 @@ function translate!!(var::IRVar, line::Expr, bindings::Bindings, isconst::Bool, ir::Core.CodeInfo) head = line.head _bind_fn = (x) -> bind_var!(x, bindings, ir) + # expand static parameter + _expand_sp = (x) -> begin + Meta.isexpr(x, :static_parameter) ? ir.parent.sparam_vals[x.args[1]] : x + end if head === :new - args = map(_bind_fn, line.args) + args = map(_bind_fn ∘ _expand_sp, line.args) return Instruction(__new__, args |> Tuple, _bind_fn(var)) elseif head === :call # Only some of the function calls can be optimized even though many of their results are @@ -423,7 +427,7 @@ function translate!!(var::IRVar, line::Expr, v = ir.ssavaluetypes[var.id].val _canbeoptimized(v) && return _const_instruction(var, v, bindings, ir) end - args = map(_bind_fn, line.args) + args = map(_bind_fn ∘ _expand_sp, line.args) # args[1] is the function func = line.args[1] if Meta.isexpr(func, :static_parameter) # func is a type parameter From 7cab32453c353bd7427c949f663b8380835e267d Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 12:43:26 +0100 Subject: [PATCH 10/12] Test whether Julia 1.7 still works --- .github/workflows/Testing.yaml | 2 +- Project.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Testing.yaml b/.github/workflows/Testing.yaml index 2b8b8685..48d03b62 100644 --- a/.github/workflows/Testing.yaml +++ b/.github/workflows/Testing.yaml @@ -11,7 +11,7 @@ jobs: strategy: matrix: version: - - '1.10' + - '1.7' - '1' - 'nightly' os: diff --git a/Project.toml b/Project.toml index f4890767..41385419 100644 --- a/Project.toml +++ b/Project.toml @@ -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.9" +version = "0.8.8" [deps] FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" @@ -14,7 +14,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" [compat] FunctionWrappers = "1.1" LRUCache = "1.3" -julia = "1.10" +julia = "1.7" [extras] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" From 08ebc4815bc00651ee5bf707eeba888ce4521022 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 19:58:59 +0800 Subject: [PATCH 11/12] eval static parameter when binding vars --- .github/workflows/Testing.yaml | 1 + src/tapedfunction.jl | 15 ++++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.github/workflows/Testing.yaml b/.github/workflows/Testing.yaml index 48d03b62..728f92e1 100644 --- a/.github/workflows/Testing.yaml +++ b/.github/workflows/Testing.yaml @@ -12,6 +12,7 @@ jobs: matrix: version: - '1.7' + - '1.10' - '1' - 'nightly' os: diff --git a/src/tapedfunction.jl b/src/tapedfunction.jl index cf9f74c0..520468f2 100644 --- a/src/tapedfunction.jl +++ b/src/tapedfunction.jl @@ -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 statuc parameter + var = Meta.isexpr(var, :static_parameter) ? ir.parent.sparam_vals[var.args[1]] : var + push!(bindings, var) idx = length(bindings) return idx end @@ -412,12 +413,8 @@ function translate!!(var::IRVar, line::Expr, bindings::Bindings, isconst::Bool, ir::Core.CodeInfo) head = line.head _bind_fn = (x) -> bind_var!(x, bindings, ir) - # expand static parameter - _expand_sp = (x) -> begin - Meta.isexpr(x, :static_parameter) ? ir.parent.sparam_vals[x.args[1]] : x - end if head === :new - args = map(_bind_fn ∘ _expand_sp, line.args) + args = map(_bind_fn, line.args) return Instruction(__new__, args |> Tuple, _bind_fn(var)) elseif head === :call # Only some of the function calls can be optimized even though many of their results are @@ -427,7 +424,7 @@ function translate!!(var::IRVar, line::Expr, v = ir.ssavaluetypes[var.id].val _canbeoptimized(v) && return _const_instruction(var, v, bindings, ir) end - args = map(_bind_fn ∘ _expand_sp, line.args) + args = map(_bind_fn, line.args) # args[1] is the function func = line.args[1] if Meta.isexpr(func, :static_parameter) # func is a type parameter From d4f7ac77de0a5ca96587bc75fbee4c6609fd5be3 Mon Sep 17 00:00:00 2001 From: KDr2 Date: Tue, 22 Oct 2024 20:03:14 +0800 Subject: [PATCH 12/12] [skip ci] fix typos --- src/tapedfunction.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tapedfunction.jl b/src/tapedfunction.jl index 520468f2..5e85b87b 100644 --- a/src/tapedfunction.jl +++ b/src/tapedfunction.jl @@ -270,7 +270,7 @@ end const IRVar = Union{Core.SSAValue, Core.SlotNumber} function bind_var!(var, bindings::Bindings, ir::Core.CodeInfo) - # for literal constants, and statuc parameter + # 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)