Skip to content

Commit 9dcea85

Browse files
Merge branch 'master' into ox/colprac
2 parents 62a1f6c + dd7e317 commit 9dcea85

File tree

6 files changed

+38
-42
lines changed

6 files changed

+38
-42
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Mjolnir"
22
uuid = "1154507a-7ac2-44fe-9f15-34617bca9db5"
33
authors = ["Mike J Innes <[email protected]>"]
4-
version = "0.1.0"
4+
version = "0.2.1"
55

66
[deps]
77
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Mjolnir
22

3+
[![Build Status](https://travis-ci.org/FluxML/Mjolnir.jl.svg?branch=master)](https://travis-ci.org/FluxML/Mjolnir.jl)
34
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
4-
[![Build Status](https://travis-ci.org/MikeInnes/Mjolnir.jl.svg?branch=master)](https://travis-ci.org/MikeInnes/Mjolnir.jl)
55

66
Mjolnir is a hybrid approach to partial evaluation / abstract interpretation,
77
with an implementation in Julia. It can be thought of as a blend of
@@ -26,7 +26,7 @@ pow (generic function with 1 method)
2626

2727
julia> using Mjolnir
2828

29-
julia> @trace pow(Int, 3)
29+
julia> @trace pow(::Int, 3)
3030
1: (%1 :: const(pow), %2 :: Int64, %3 :: const(3))
3131
%4 = (*)(1, %2) :: Int64
3232
%5 = (*)(%4, %2) :: Int64
@@ -44,7 +44,7 @@ can generate diagnostics when there are issues. Mjolnir can thus compile a much
4444
wider range of Julia programs than OO approaches.
4545

4646
```julia
47-
julia> @trace pow(Int, Int)
47+
julia> @trace pow(::Int, ::Int)
4848
1: (%1 :: const(pow), %2 :: Int64, %3 :: Int64)
4949
%4 = (>)(%3, 0) :: Bool
5050
br 3 (1) unless %4

docs/types.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ functions are called _primitives_.
4747
julia> f(x) = 3x^2 + 2x + 1
4848
f (generic function with 1 method)
4949

50-
julia> @trace f(Int)
50+
julia> @trace f(::Int)
5151
1: (%1 :: const(f), %2 :: Int64)
5252
%3 = (*)(%2, %2) :: Int64
5353
%4 = (*)(3, %3) :: Int64
@@ -144,7 +144,7 @@ julia> @abstract MyPrimitives (a::AType{T} * b::AType{T}) where T<:Union{Float64
144144

145145
julia> MyDefaults() = Multi(MyPrimitives(), Mjolnir.Basic())
146146

147-
julia> @trace MyDefaults() Int * Float64
147+
julia> @trace MyDefaults() ::Int * ::Float64
148148
1: (%1 :: const(*), %2 :: Int64, %3 :: Float64)
149149
%4 = ($(QuoteNode(Float64)))(%2) :: Float64
150150
%5 = (*)(%4, %3) :: Float64

src/infer.jl

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ mutable struct Frame
8686
edges::Vector{Any}
8787
stmts::Vector{Vector{Variable}}
8888
rettype::AType
89-
outer::Bool
9089
end
9190

9291
getblock(fr::Frame, b, follow) =
@@ -96,7 +95,7 @@ getblock(fr::Frame, b, follow) =
9695
# TODO clear up inlined edges
9796
uninline!(fr::Frame, b, follow) = deleteat!(fr.inlined[b], follow+1:length(fr.inlined[b]))
9897

99-
Frame(ir::IR; outer = false) = Frame(ir, [IR[] for _ = 1:length(blocks(ir))], [], keys.(blocks(ir)), Union{}, outer)
98+
Frame(ir::IR) = Frame(ir, [IR[] for _ = 1:length(blocks(ir))], [], keys.(blocks(ir)), Union{})
10099

101100
function frame(ir::IR, args...)
102101
prepare_ir!(ir)
@@ -201,7 +200,7 @@ function step!(inf::Inference)
201200
error("Unrecognised expression $(st.expr)")
202201
end
203202
elseif (brs = openbranches(block); length(brs) == 1 && !isreturn(brs[1])
204-
&& !(frame.outer && brs[1].block == length(frame.ir.blocks)))
203+
&& !(brs[1].block == length(frame.ir.blocks)))
205204
inferbranch!(inf, frame, b, f, brs[1])
206205
else
207206
for br in brs
@@ -250,19 +249,3 @@ function return_type(ir::IR, args...)
250249
infer!(inf)
251250
return fr.rettype
252251
end
253-
254-
function inlineable(ir::IR, inf::Inference)
255-
for (v, st) in ir
256-
Ts = exprtype.((ir,), st.expr.args)
257-
haskey(inf.frames, Ts) && return (v, Ts)
258-
end
259-
end
260-
261-
function inlineall(ir::IR, inf::Inference)
262-
while (next = inlineable(ir, inf)) != nothing
263-
v, Ts = next
264-
subir = inf.frames[Ts].ir
265-
ir = inline(ir, v, subir)
266-
end
267-
return ir
268-
end

src/trace.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ function abstract!(tr, bl, env)
133133
for i = 1:length(args)
134134
argtypes(ir)[i] = exprtype(tr.ir, rename(env, args[i]))
135135
end
136-
inf = Inference(Frame(ir, outer = true), tr.primitives)
137-
infer!(inf)
138-
# TODO weird that we need to expand here; perhaps inlining is impicitly pruning
139-
# somehow.
140-
ir = inlineall(ir, inf) |> expand!
136+
infer!(Inference(Frame(ir), tr.primitives))
141137
inline!(tr.ir, ir, rename.((env,), args))
142138
for (k, v) in zip(arguments(block(bl.ir, after)), arguments(blocks(tr.ir)[end]))
143139
env[k] = v
@@ -273,12 +269,17 @@ function trace(P, Ts...)
273269
end
274270
end
275271

272+
to_type_level(x) = :(Mjolnir.Const($x))
273+
to_type_level(ex::Expr) = isexpr(ex, :(::)) && length(ex.args) == 1 ? (ex.args[1]) : :(Mjolnir.Const($ex))
274+
276275
atype(T::AType) = T
277276
atype(x) = Const(x)
277+
#atype.(($(esc(f)), $(esc.(args)...)))...)
278278

279279
function tracem(P, ex)
280280
@capture(ex, f_(args__)) || error("@trace f(args...)")
281-
:(trace($(esc(P)), atype.(($(esc(f)), $(esc.(args)...)))...))
281+
sig = ((esc to_type_level).((f, args...)))
282+
:(trace($(esc(P)), $(sig...)))
282283
end
283284

284285
"""
@@ -288,7 +289,7 @@ end
288289
Get a typed trace for `f`, analagous to `@code_typed`. Note that unlike
289290
`@code_typed`, you probably want to pass types rather than values, e.g.
290291
291-
julia> @trace Int+Int
292+
julia> @trace ::Int + ::Int
292293
1: (%1 :: const(+), %2 :: Int64, %3 :: Int64)
293294
%4 = (+)(%2, %3) :: Int64
294295
return %4

test/trace.jl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,24 @@ ir = @code_ir fact(1)
6464
tr = @trace f()
6565
@test returntype(tr) == String
6666

67-
tr = @trace pow(Int, 3)
67+
tr = @trace pow(::Int, 3)
6868
@test length(tr.blocks) == 1
6969
@test returntype(tr) == Int
7070

7171
tr = @trace pow(2, 3)
7272
@test length(tr.blocks) == 1
7373
@test returntype(tr) == Const(8)
7474

75-
tr = @trace pow(2, Int)
75+
tr = @trace pow(2, ::Int)
7676
@test returntype(tr) == Int
7777

78-
tr = @trace pow(2.0, Int)
78+
tr = @trace pow(2.0, ::Int)
7979
@test returntype(tr) == Union{Float64,Int}
8080

81-
tr = @trace pow(1, Int)
81+
tr = @trace pow(1, ::Int)
8282
@test returntype(tr) == Const(1)
8383

84-
tr = @trace bar(Int, Int)
84+
tr = @trace bar(::Int, ::Int)
8585
@test returntype(tr) == Int
8686

8787
function foo(x)
@@ -93,7 +93,7 @@ end
9393
tr = @trace foo(1)
9494
@test returntype(tr) == Const(1)
9595

96-
tr = @trace foo(Int)
96+
tr = @trace foo(::Int)
9797
@test returntype(tr) == Int
9898

9999
function foo(x)
@@ -142,7 +142,7 @@ end
142142
tr = @trace pow(2, 3)
143143
@test returntype(tr) == Const(8)
144144

145-
tr = @trace pow(Int, Int)
145+
tr = @trace pow(::Int, ::Int)
146146
@test returntype(tr) == Int
147147

148148
function sumabs2(xs)
@@ -153,14 +153,26 @@ function sumabs2(xs)
153153
return s
154154
end
155155

156-
tr = @trace sumabs2(arrayshape(Float64, 3))
156+
tr = @trace sumabs2(::arrayshape(Float64, 3))
157157
@test length(blocks(tr)) == 1
158158
@test returntype(tr) == Float64
159159

160160
f(xs) = sum(xs)
161-
tr = @trace f(Matrix{Int32})
161+
tr = @trace f(::Matrix{Int32})
162162
@test returntype(tr) == Int32
163163

164164
f(xs) = sum(xs, dims = 1)
165-
tr = @trace f(Matrix{Int32})
165+
tr = @trace f(::Matrix{Int32})
166166
@test returntype(tr) == Matrix{Int32}
167+
168+
169+
function negsquare(x)
170+
if x > 0
171+
return x^2
172+
else
173+
return -x^2
174+
end
175+
end
176+
177+
tr = @trace negsquare(::Float64)
178+
@test returntype(tr) == Float64

0 commit comments

Comments
 (0)