Skip to content

Commit 538a96d

Browse files
authored
fix doctests (#123)
1 parent 7fab055 commit 538a96d

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

docs/src/dynamo.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,19 @@ julia> @code_ir roundtrip mul(1, 2)
8787

8888
Now we can recreate our `foo` macro. It's a little more verbose since simple symbols like `*` are resolved to `GlobalRef`s in lowered code, but it's broadly the same as our macro.
8989

90-
```jldoctest main
91-
julia> using MacroTools
90+
```@meta
91+
DocTestSetup = quote
92+
using IRTools
93+
using IRTools: @dynamo, IR
94+
95+
mul(a, b) = a * b
96+
end
97+
```
98+
99+
```jldoctest main2
100+
julia> using MacroTools, IRTools
101+
102+
julia> using IRTools: @dynamo, IR
92103
93104
julia> @dynamo function foo(a...)
94105
ir = IR(a...)
@@ -100,9 +111,13 @@ julia> @dynamo function foo(a...)
100111
end
101112
```
102113

114+
```@meta
115+
DocTestSetup = nothing
116+
```
117+
103118
It behaves identically, too.
104119

105-
```jldoctest main
120+
```jldoctest main2
106121
julia> foo() do
107122
10*5
108123
end
@@ -122,7 +137,7 @@ A key difference between macros and dynamos is that dynamos get passed *function
122137

123138
So what if `foo` actually inserted calls to itself when modifying a function? In other words, `prod([1, 2, 3])` would become `foo(prod, [1, 2, 3])`, and so on for each call inside a function. This lets us get the "dynamic extent" property that we talked about earlier.
124139

125-
```jldoctest main
140+
```jldoctest main2
126141
julia> using IRTools: xcall
127142
128143
julia> @dynamo function foo2(a...)
@@ -161,7 +176,7 @@ julia> @code_ir foo2 mul_wrapped(5, 10)
161176

162177
And that it works as expected:
163178

164-
```jldoctest main
179+
```jldoctest main2
165180
julia> foo() do # Does not work (since there is no literal `*` here)
166181
mul(5, 10)
167182
end
@@ -185,6 +200,8 @@ This, we have rewritten the `prod` function to actually calculate `sum`, by *int
185200
We can make our `foo2` dynamo simpler in a couple of ways. Firstly, IRTools provides a built-in utility `recurse!` which makes it easy to recurse into code.
186201

187202
```jldoctest main
203+
julia> using MacroTools
204+
188205
julia> using IRTools: recurse!
189206
190207
julia> @dynamo function foo2(a...)

docs/src/index.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Note that before even attempting to understand IRTools, you should have a good h
1414

1515
It's easiest to understand the IRTools IR by seeing some examples. We provide the macro `@code_ir` which behaves much like `@code_lowered`.
1616

17-
```jldoctest main
17+
```jldoctest
1818
julia> using IRTools
1919
2020
julia> f(x) = x+x
@@ -30,7 +30,9 @@ First things first. All variables are numbered (`%1`, `%2`, `%3` ...). IR will u
3030

3131
The main reason that there are a lot of intermediates is that, in IR, we only allow one function call per line. You can see how a nested Julia expression becomes a sequence of single instructions, kind of like an assembly language.
3232

33-
```jldoctest main
33+
```jldoctest
34+
julia> using IRTools
35+
3436
julia> f(x) = 3x*x + 2x + 1
3537
f (generic function with 1 method)
3638
@@ -51,7 +53,9 @@ Beyond that, this is essentially just very verbosely-written Julia code.
5153

5254
The most significant difference between `IR` and `Expr` is how control flow is handled. There are no such thing as nested if statements, while loops and so on in IR, only *branches*.
5355

54-
```jldoctest main
56+
```jldoctest
57+
julia> using IRTools
58+
5559
julia> f(x) = x > 0 ? x : 0
5660
f (generic function with 1 method)
5761
@@ -70,7 +74,9 @@ IR is composed of a series of *basic blocks* that jump between each other like t
7074

7175
Here's a more interesting example.
7276

73-
```jldoctest main
77+
```jldoctest
78+
julia> using IRTools
79+
7480
julia> function f(x)
7581
if x < 0
7682
x = -x
@@ -97,7 +103,9 @@ Why not just write this as `%2 = -%2`? It's important to understand that variabl
97103

98104
Loops work this way too: they are visible in the IR by branches that jump backwards, i.e. the `br 2` here. Variables that were modified inside the loop in the original code are explicitly passed between blocks.
99105

100-
```jldoctest main
106+
```jldoctest envpow
107+
julia> using IRTools
108+
101109
julia> function pow(x, n)
102110
r = 1
103111
while n > 0
@@ -129,7 +137,7 @@ julia> @code_ir pow(1, 1)
129137

130138
It's easy to get started by creating an empty fragment of IR.
131139

132-
```jldoctest main
140+
```jldoctest ir_example
133141
julia> using IRTools: IR, var, argument!, xcall
134142
135143
julia> ir = IR()
@@ -138,7 +146,7 @@ julia> ir = IR()
138146

139147
We can push new statements into the IR. `push!` returns a variable name that we can reuse later on.
140148

141-
```jldoctest main
149+
```jldoctest ir_example
142150
julia> x = argument!(ir)
143151
%1
144152
@@ -152,7 +160,7 @@ julia> ir
152160

153161
The IR can be viewed as a mapping from variables to statements, and indexing and iteration are consistent with that.
154162

155-
```julia
163+
```julia ir_example
156164
julia> ir[var(2)]
157165
IRTools.Statement(:(%1 * %1), Any, 0)
158166

@@ -167,7 +175,7 @@ There are a few other functions that do obvious things: `pushfirst!`, `insert!`,
167175

168176
In most cases you won't build IR from scratch, but will start from an existing function and modify its IR.
169177

170-
```jldoctest main
178+
```jldoctest envpow
171179
julia> ir = @code_ir pow(1, 1)
172180
1: (%1, %2, %3)
173181
br 2 (%3, 1)
@@ -187,7 +195,7 @@ julia> ir = @code_ir pow(1, 1)
187195

188196
You can work with a block at a time with `block(ir, n)` (all of them with `blocks(ir)`). Blocks similarly support functions like `push!`.
189197

190-
```jldoctest main
198+
```jldoctest envpow
191199
julia> using IRTools: block
192200
193201
julia> block(ir, 2)

0 commit comments

Comments
 (0)