You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_09/lecture.md
+171Lines changed: 171 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,177 @@ In theory, we can do all the above by directly modifying the code or introducing
129
129
130
130
The technique we desire is called contextual dispatch, which means that under some context, we invoke a different function. The library `Casette.jl` provides a high-level api for overdubbing, but it is by no means interesting to see, how it works, as it shows, how we can "interact" with the lowered code before the code is typed.
131
131
132
+
## insertion of the code
133
+
134
+
Imagine that julia has compiled some function. For example
135
+
```julia
136
+
foo(x,y) = x * y +sin(x)
137
+
```
138
+
Can I get access to lowered form?
139
+
```julia
140
+
julia>@code_loweredfoo(1.0, 1.0)
141
+
CodeInfo(
142
+
1 ─ %1= x * y
143
+
│ %2= Main.sin(x)
144
+
│ %3=%1+%2
145
+
└── return%3
146
+
)
147
+
```
148
+
The lowered form is very nice, because on the left hand, there is **always** one parameter. Such form would be very nice for example for computation of automatic gradients, because it is very close to a computation graph. It is built for you by the compiler. Swell.
149
+
150
+
The answer is affirmative. with a little bit of effort (copied from `Cassette.jl`), you can have it
151
+
```julia
152
+
functionretrieve_code_info(sigtypes, world = Base.get_world_counter())
153
+
S = Tuple{map(s -> Core.Compiler.has_free_typevars(s) ?typeof(s.parameters[1]) : s, sigtypes)...}
154
+
_methods = Base._methods_by_ftype(S, -1, world)
155
+
isempty(_methods) &&@error("method $(sigtypes) does not exist, may-be run it once")
156
+
type_signature, raw_static_params, method = _methods[1] # method is the same as we would get by invoking methods(+, (Int, Int)).ms[1]
A further complication is that we need to change variables back and also gives them names. if names have existed before. Recall that lowered form introduces additional variables while converting the code to SSA. The variables defined in the source code (argument names and user-defined variables) can be found in `ci.slotnames`,
214
+
whereas the variables introduces during lowering to SSA are named by the line number.
0 commit comments