Skip to content

Commit 2761415

Browse files
authored
Merge pull request #12 from MasonProtter/at-trace_change
use ::T for types in @trace
2 parents dd136dc + 02c63d9 commit 2761415

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
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.0"
55

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

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pow (generic function with 1 method)
2525

2626
julia> using Mjolnir
2727

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

4545
```julia
46-
julia> @trace pow(Int, Int)
46+
julia> @trace pow(::Int, ::Int)
4747
1: (%1 :: const(pow), %2 :: Int64, %3 :: Int64)
4848
%4 = (>)(%3, 0) :: Bool
4949
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/trace.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,17 @@ function trace(P, Ts...)
273273
end
274274
end
275275

276+
to_type_level(x) = :(Mjolnir.Const($x))
277+
to_type_level(ex::Expr) = isexpr(ex, :(::)) && length(ex.args) == 1 ? (ex.args[1]) : :(Mjolnir.Const($ex))
278+
276279
atype(T::AType) = T
277280
atype(x) = Const(x)
281+
#atype.(($(esc(f)), $(esc.(args)...)))...)
278282

279283
function tracem(P, ex)
280284
@capture(ex, f_(args__)) || error("@trace f(args...)")
281-
:(trace($(esc(P)), atype.(($(esc(f)), $(esc.(args)...)))...))
285+
sig = ((esc to_type_level).((f, args...)))
286+
:(trace($(esc(P)), $(sig...)))
282287
end
283288

284289
"""
@@ -288,7 +293,7 @@ end
288293
Get a typed trace for `f`, analagous to `@code_typed`. Note that unlike
289294
`@code_typed`, you probably want to pass types rather than values, e.g.
290295
291-
julia> @trace Int+Int
296+
julia> @trace ::Int + ::Int
292297
1: (%1 :: const(+), %2 :: Int64, %3 :: Int64)
293298
%4 = (+)(%2, %3) :: Int64
294299
return %4

test/trace.jl

Lines changed: 10 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,14 @@ 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}

0 commit comments

Comments
 (0)