-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
expr.jl
1209 lines (1004 loc) · 37.7 KB
/
expr.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
isexpr(@nospecialize(ex), heads) = isa(ex, Expr) && in(ex.head, heads)
isexpr(@nospecialize(ex), heads, n::Int) = isa(ex, Expr) && in(ex.head, heads) && length(ex.args) == n
const is_expr = isexpr
## symbols ##
"""
gensym([tag])
Generates a symbol which will not conflict with other variable names (in the same module).
"""
gensym() = ccall(:jl_gensym, Ref{Symbol}, ())
gensym(s::String) = ccall(:jl_tagged_gensym, Ref{Symbol}, (Ptr{UInt8}, Csize_t), s, sizeof(s))
gensym(ss::String...) = map(gensym, ss)
gensym(s::Symbol) = ccall(:jl_tagged_gensym, Ref{Symbol}, (Ptr{UInt8}, Csize_t), s, -1 % Csize_t)
"""
@gensym
Generates a gensym symbol for a variable. For example, `@gensym x y` is transformed into
`x = gensym("x"); y = gensym("y")`.
"""
macro gensym(names...)
blk = Expr(:block)
for name in names
push!(blk.args, :($(esc(name)) = gensym($(string(name)))))
end
push!(blk.args, :nothing)
return blk
end
## expressions ##
isexpr(@nospecialize(ex), head::Symbol) = isa(ex, Expr) && ex.head === head
isexpr(@nospecialize(ex), head::Symbol, n::Int) = isa(ex, Expr) && ex.head === head && length(ex.args) == n
copy(e::Expr) = exprarray(e.head, copy_exprargs(e.args))
# copy parts of an AST that the compiler mutates
function copy_exprs(@nospecialize(x))
if isa(x, Expr)
return copy(x)
elseif isa(x, PhiNode)
values = x.values
nvalues = length(values)
new_values = Vector{Any}(undef, nvalues)
@inbounds for i = 1:nvalues
isassigned(values, i) || continue
new_values[i] = copy_exprs(values[i])
end
return PhiNode(copy(x.edges), new_values)
elseif isa(x, PhiCNode)
values = x.values
nvalues = length(values)
new_values = Vector{Any}(undef, nvalues)
@inbounds for i = 1:nvalues
isassigned(values, i) || continue
new_values[i] = copy_exprs(values[i])
end
return PhiCNode(new_values)
end
return x
end
copy_exprargs(x::Array{Any,1}) = Any[copy_exprs(@inbounds x[i]) for i in 1:length(x)]
@eval exprarray(head::Symbol, arg::Array{Any,1}) = $(Expr(:new, :Expr, :head, :arg))
# create copies of the CodeInfo definition, and any mutable fields
function copy(c::CodeInfo)
cnew = ccall(:jl_copy_code_info, Ref{CodeInfo}, (Any,), c)
cnew.code = copy_exprargs(cnew.code)
cnew.slotnames = copy(cnew.slotnames)
cnew.slotflags = copy(cnew.slotflags)
cnew.codelocs = copy(cnew.codelocs)
cnew.linetable = copy(cnew.linetable::Union{Vector{Any},Vector{Core.LineInfoNode}})
cnew.ssaflags = copy(cnew.ssaflags)
cnew.edges = cnew.edges === nothing ? nothing : copy(cnew.edges::Vector)
ssavaluetypes = cnew.ssavaluetypes
ssavaluetypes isa Vector{Any} && (cnew.ssavaluetypes = copy(ssavaluetypes))
return cnew
end
==(x::Expr, y::Expr) = x.head === y.head && isequal(x.args, y.args)
==(x::QuoteNode, y::QuoteNode) = isequal(x.value, y.value)
==(stmt1::Core.PhiNode, stmt2::Core.PhiNode) = stmt1.edges == stmt2.edges && stmt1.values == stmt2.values
"""
macroexpand(m::Module, x; recursive=true)
Take the expression `x` and return an equivalent expression with all macros removed (expanded)
for executing in module `m`.
The `recursive` keyword controls whether deeper levels of nested macros are also expanded.
This is demonstrated in the example below:
```julia-repl
julia> module M
macro m1()
42
end
macro m2()
:(@m1())
end
end
M
julia> macroexpand(M, :(@m2()), recursive=true)
42
julia> macroexpand(M, :(@m2()), recursive=false)
:(#= REPL[16]:6 =# M.@m1)
```
"""
function macroexpand(m::Module, @nospecialize(x); recursive=true)
if recursive
ccall(:jl_macroexpand, Any, (Any, Any), x, m)
else
ccall(:jl_macroexpand1, Any, (Any, Any), x, m)
end
end
"""
@macroexpand
Return equivalent expression with all macros removed (expanded).
There are differences between `@macroexpand` and [`macroexpand`](@ref).
* While [`macroexpand`](@ref) takes a keyword argument `recursive`, `@macroexpand`
is always recursive. For a non recursive macro version, see [`@macroexpand1`](@ref).
* While [`macroexpand`](@ref) has an explicit `module` argument, `@macroexpand` always
expands with respect to the module in which it is called.
This is best seen in the following example:
```julia-repl
julia> module M
macro m()
1
end
function f()
(@macroexpand(@m),
macroexpand(M, :(@m)),
macroexpand(Main, :(@m))
)
end
end
M
julia> macro m()
2
end
@m (macro with 1 method)
julia> M.f()
(1, 1, 2)
```
With `@macroexpand` the expression expands where `@macroexpand` appears in the code (module `M` in the example).
With `macroexpand` the expression expands in the module given as the first argument.
"""
macro macroexpand(code)
return :(macroexpand($__module__, $(QuoteNode(code)), recursive=true))
end
"""
@macroexpand1
Non recursive version of [`@macroexpand`](@ref).
"""
macro macroexpand1(code)
return :(macroexpand($__module__, $(QuoteNode(code)), recursive=false))
end
## misc syntax ##
"""
Core.eval(m::Module, expr)
Evaluate an expression in the given module and return the result.
"""
Core.eval
"""
@inline
Give a hint to the compiler that this function is worth inlining.
Small functions typically do not need the `@inline` annotation,
as the compiler does it automatically. By using `@inline` on bigger functions,
an extra nudge can be given to the compiler to inline it.
`@inline` can be applied immediately before a function definition or within a function body.
```julia
# annotate long-form definition
@inline function longdef(x)
...
end
# annotate short-form definition
@inline shortdef(x) = ...
# annotate anonymous function that a `do` block creates
f() do
@inline
...
end
```
!!! compat "Julia 1.8"
The usage within a function body requires at least Julia 1.8.
---
@inline block
Give a hint to the compiler that calls within `block` are worth inlining.
```julia
# The compiler will try to inline `f`
@inline f(...)
# The compiler will try to inline `f`, `g` and `+`
@inline f(...) + g(...)
```
!!! note
A callsite annotation always has the precedence over the annotation applied to the
definition of the called function:
```julia
@noinline function explicit_noinline(args...)
# body
end
let
@inline explicit_noinline(args...) # will be inlined
end
```
!!! note
When there are nested callsite annotations, the innermost annotation has the precedence:
```julia
@noinline let a0, b0 = ...
a = @inline f(a0) # the compiler will try to inline this call
b = f(b0) # the compiler will NOT try to inline this call
return a, b
end
```
!!! warning
Although a callsite annotation will try to force inlining in regardless of the cost model,
there are still chances it can't succeed in it. Especially, recursive calls can not be
inlined even if they are annotated as `@inline`d.
!!! compat "Julia 1.8"
The callsite annotation requires at least Julia 1.8.
"""
macro inline(x)
return annotate_meta_def_or_block(x, :inline)
end
"""
@noinline
Give a hint to the compiler that it should not inline a function.
Small functions are typically inlined automatically.
By using `@noinline` on small functions, auto-inlining can be
prevented.
`@noinline` can be applied immediately before a function definition or within a function body.
```julia
# annotate long-form definition
@noinline function longdef(x)
...
end
# annotate short-form definition
@noinline shortdef(x) = ...
# annotate anonymous function that a `do` block creates
f() do
@noinline
...
end
```
!!! compat "Julia 1.8"
The usage within a function body requires at least Julia 1.8.
---
@noinline block
Give a hint to the compiler that it should not inline the calls within `block`.
```julia
# The compiler will try to not inline `f`
@noinline f(...)
# The compiler will try to not inline `f`, `g` and `+`
@noinline f(...) + g(...)
```
!!! note
A callsite annotation always has the precedence over the annotation applied to the
definition of the called function:
```julia
@inline function explicit_inline(args...)
# body
end
let
@noinline explicit_inline(args...) # will not be inlined
end
```
!!! note
When there are nested callsite annotations, the innermost annotation has the precedence:
```julia
@inline let a0, b0 = ...
a = @noinline f(a0) # the compiler will NOT try to inline this call
b = f(b0) # the compiler will try to inline this call
return a, b
end
```
!!! compat "Julia 1.8"
The callsite annotation requires at least Julia 1.8.
---
!!! note
If the function is trivial (for example returning a constant) it might get inlined anyway.
"""
macro noinline(x)
return annotate_meta_def_or_block(x, :noinline)
end
"""
@constprop setting [ex]
Control the mode of interprocedural constant propagation for the annotated function.
Two `setting`s are supported:
- `@constprop :aggressive [ex]`: apply constant propagation aggressively.
For a method where the return type depends on the value of the arguments,
this can yield improved inference results at the cost of additional compile time.
- `@constprop :none [ex]`: disable constant propagation. This can reduce compile
times for functions that Julia might otherwise deem worthy of constant-propagation.
Common cases are for functions with `Bool`- or `Symbol`-valued arguments or keyword arguments.
`@constprop` can be applied immediately before a function definition or within a function body.
```julia
# annotate long-form definition
@constprop :aggressive function longdef(x)
...
end
# annotate short-form definition
@constprop :aggressive shortdef(x) = ...
# annotate anonymous function that a `do` block creates
f() do
@constprop :aggressive
...
end
```
!!! compat "Julia 1.10"
The usage within a function body requires at least Julia 1.10.
"""
macro constprop(setting, ex)
sym = constprop_setting(setting)
isa(ex, Expr) && return esc(pushmeta!(ex, sym))
throw(ArgumentError(LazyString("Bad expression `", ex, "` in `@constprop settings ex`")))
end
macro constprop(setting)
sym = constprop_setting(setting)
return Expr(:meta, sym)
end
function constprop_setting(@nospecialize setting)
isa(setting, QuoteNode) && (setting = setting.value)
if setting === :aggressive
return :aggressive_constprop
elseif setting === :none
return :no_constprop
end
throw(ArgumentError(LazyString("@constprop "), setting, "not supported"))
end
"""
@assume_effects setting... [ex]
Override the compiler's effect modeling for the given method or foreign call.
`@assume_effects` can be applied immediately before a function definition or within a function body.
It can also be applied immediately before a `@ccall` expression.
!!! compat "Julia 1.8"
Using `Base.@assume_effects` requires Julia version 1.8.
# Examples
```jldoctest
julia> Base.@assume_effects :terminates_locally function pow(x)
# this :terminates_locally allows `pow` to be constant-folded
res = 1
1 < x < 20 || error("bad pow")
while x > 1
res *= x
x -= 1
end
return res
end
pow (generic function with 1 method)
julia> code_typed() do
pow(12)
end
1-element Vector{Any}:
CodeInfo(
1 ─ return 479001600
) => Int64
julia> code_typed() do
map((2,3,4)) do x
# this :terminates_locally allows this anonymous function to be constant-folded
Base.@assume_effects :terminates_locally
res = 1
1 < x < 20 || error("bad pow")
while x > 1
res *= x
x -= 1
end
return res
end
end
1-element Vector{Any}:
CodeInfo(
1 ─ return (2, 6, 24)
) => Tuple{Int64, Int64, Int64}
julia> Base.@assume_effects :total !:nothrow @ccall jl_type_intersection(Vector{Int}::Any, Vector{<:Integer}::Any)::Any
Vector{Int64} (alias for Array{Int64, 1})
```
!!! compat "Julia 1.10"
The usage within a function body requires at least Julia 1.10.
!!! warning
Improper use of this macro causes undefined behavior (including crashes,
incorrect answers, or other hard to track bugs). Use with care and only as a
last resort if absolutely required. Even in such a case, you SHOULD take all
possible steps to minimize the strength of the effect assertion (e.g.,
do not use `:total` if `:nothrow` would have been sufficient).
In general, each `setting` value makes an assertion about the behavior of the
function, without requiring the compiler to prove that this behavior is indeed
true. These assertions are made for all world ages. It is thus advisable to limit
the use of generic functions that may later be extended to invalidate the
assumption (which would cause undefined behavior).
The following `setting`s are supported.
- `:consistent`
- `:effect_free`
- `:nothrow`
- `:terminates_globally`
- `:terminates_locally`
- `:notaskstate`
- `:inaccessiblememonly`
- `:foldable`
- `:removable`
- `:total`
# Extended help
---
## `:consistent`
The `:consistent` setting asserts that for egal (`===`) inputs:
- The manner of termination (return value, exception, non-termination) will always be the same.
- If the method returns, the results will always be egal.
!!! note
This in particular implies that the method must not return a freshly allocated
mutable object. Multiple allocations of mutable objects (even with identical
contents) are not egal.
!!! note
The `:consistent`-cy assertion is made world-age wise. More formally, write
``fᵢ`` for the evaluation of ``f`` in world-age ``i``, then we require:
```math
∀ i, x, y: x ≡ y → fᵢ(x) ≡ fᵢ(y)
```
However, for two world ages ``i``, ``j`` s.t. ``i ≠ j``, we may have ``fᵢ(x) ≢ fⱼ(y)``.
A further implication is that `:consistent` functions may not make their
return value dependent on the state of the heap or any other global state
that is not constant for a given world age.
!!! note
The `:consistent`-cy includes all legal rewrites performed by the optimizer.
For example, floating-point fastmath operations are not considered `:consistent`,
because the optimizer may rewrite them causing the output to not be `:consistent`,
even for the same world age (e.g. because one ran in the interpreter, while
the other was optimized).
!!! note
If `:consistent` functions terminate by throwing an exception, that exception
itself is not required to meet the egality requirement specified above.
---
## `:effect_free`
The `:effect_free` setting asserts that the method is free of externally semantically
visible side effects. The following is an incomplete list of externally semantically
visible side effects:
- Changing the value of a global variable.
- Mutating the heap (e.g. an array or mutable value), except as noted below
- Changing the method table (e.g. through calls to eval)
- File/Network/etc. I/O
- Task switching
However, the following are explicitly not semantically visible, even if they
may be observable:
- Memory allocations (both mutable and immutable)
- Elapsed time
- Garbage collection
- Heap mutations of objects whose lifetime does not exceed the method (i.e.
were allocated in the method and do not escape).
- The returned value (which is externally visible, but not a side effect)
The rule of thumb here is that an externally visible side effect is anything
that would affect the execution of the remainder of the program if the function
were not executed.
!!! note
The `:effect_free` assertion is made both for the method itself and any code
that is executed by the method. Keep in mind that the assertion must be
valid for all world ages and limit use of this assertion accordingly.
---
## `:nothrow`
The `:nothrow` settings asserts that this method does not terminate abnormally
(i.e. will either always return a value or never return).
!!! note
It is permissible for `:nothrow` annotated methods to make use of exception
handling internally as long as the exception is not rethrown out of the
method itself.
!!! note
`MethodErrors` and similar exceptions count as abnormal termination.
---
## `:terminates_globally`
The `:terminates_globally` settings asserts that this method will eventually terminate
(either normally or abnormally), i.e. does not loop indefinitely.
!!! note
This `:terminates_globally` assertion covers any other methods called by the annotated method.
!!! note
The compiler will consider this a strong indication that the method will
terminate relatively *quickly* and may (if otherwise legal), call this
method at compile time. I.e. it is a bad idea to annotate this setting
on a method that *technically*, but not *practically*, terminates.
---
## `:terminates_locally`
The `:terminates_locally` setting is like `:terminates_globally`, except that it only
applies to syntactic control flow *within* the annotated method. It is thus
a much weaker (and thus safer) assertion that allows for the possibility of
non-termination if the method calls some other method that does not terminate.
!!! note
`:terminates_globally` implies `:terminates_locally`.
---
## `:notaskstate`
The `:notaskstate` setting asserts that the method does not use or modify the
local task state (task local storage, RNG state, etc.) and may thus be safely
moved between tasks without observable results.
!!! note
The implementation of exception handling makes use of state stored in the
task object. However, this state is currently not considered to be within
the scope of `:notaskstate` and is tracked separately using the `:nothrow`
effect.
!!! note
The `:notaskstate` assertion concerns the state of the *currently running task*.
If a reference to a `Task` object is obtained by some other means that
does not consider which task is *currently* running, the `:notaskstate`
effect need not be tainted. This is true, even if said task object happens
to be `===` to the currently running task.
!!! note
Access to task state usually also results in the tainting of other effects,
such as `:effect_free` (if task state is modified) or `:consistent` (if
task state is used in the computation of the result). In particular,
code that is not `:notaskstate`, but is `:effect_free` and `:consistent`
may still be dead-code-eliminated and thus promoted to `:total`.
---
## `:inaccessiblememonly`
The `:inaccessiblememonly` setting asserts that the method does not access or modify
externally accessible mutable memory. This means the method can access or modify mutable
memory for newly allocated objects that is not accessible by other methods or top-level
execution before return from the method, but it can not access or modify any mutable
global state or mutable memory pointed to by its arguments.
!!! note
Below is an incomplete list of examples that invalidate this assumption:
- a global reference or `getglobal` call to access a mutable global variable
- a global assignment or `setglobal!` call to perform assignment to a non-constant global variable
- `setfield!` call that changes a field of a global mutable variable
!!! note
This `:inaccessiblememonly` assertion covers any other methods called by the annotated method.
---
## `:foldable`
This setting is a convenient shortcut for the set of effects that the compiler
requires to be guaranteed to constant fold a call at compile time. It is
currently equivalent to the following `setting`s:
- `:consistent`
- `:effect_free`
- `:terminates_globally`
!!! note
This list in particular does not include `:nothrow`. The compiler will still
attempt constant propagation and note any thrown error at compile time. Note
however, that by the `:consistent`-cy requirements, any such annotated call
must consistently throw given the same argument values.
!!! note
An explicit `@inbounds` annotation inside the function will also disable
constant folding and not be overriden by `:foldable`.
---
## `:removable`
This setting is a convenient shortcut for the set of effects that the compiler
requires to be guaranteed to delete a call whose result is unused at compile time.
It is currently equivalent to the following `setting`s:
- `:effect_free`
- `:nothrow`
- `:terminates_globally`
---
## `:total`
This `setting` is the maximum possible set of effects. It currently implies
the following other `setting`s:
- `:consistent`
- `:effect_free`
- `:nothrow`
- `:terminates_globally`
- `:notaskstate`
- `:inaccessiblememonly`
!!! warning
`:total` is a very strong assertion and will likely gain additional semantics
in future versions of Julia (e.g. if additional effects are added and included
in the definition of `:total`). As a result, it should be used with care.
Whenever possible, prefer to use the minimum possible set of specific effect
assertions required for a particular application. In cases where a large
number of effect overrides apply to a set of functions, a custom macro is
recommended over the use of `:total`.
---
## Negated effects
Effect names may be prefixed by `!` to indicate that the effect should be removed
from an earlier meta effect. For example, `:total !:nothrow` indicates that while
the call is generally total, it may however throw.
"""
macro assume_effects(args...)
lastex = args[end]
inner = unwrap_macrocalls(lastex)
if is_function_def(inner)
ex = lastex
idx = length(args)-1
elseif isexpr(lastex, :macrocall) && lastex.args[1] === Symbol("@ccall")
ex = lastex
idx = length(args)-1
else # anonymous function case
ex = nothing
idx = length(args)
end
(consistent, effect_free, nothrow, terminates_globally, terminates_locally, notaskstate, inaccessiblememonly) =
(false, false, false, false, false, false, false, false)
for org_setting in args[1:idx]
(setting, val) = compute_assumed_setting(org_setting)
if setting === :consistent
consistent = val
elseif setting === :effect_free
effect_free = val
elseif setting === :nothrow
nothrow = val
elseif setting === :terminates_globally
terminates_globally = val
elseif setting === :terminates_locally
terminates_locally = val
elseif setting === :notaskstate
notaskstate = val
elseif setting === :inaccessiblememonly
inaccessiblememonly = val
elseif setting === :foldable
consistent = effect_free = terminates_globally = val
elseif setting === :removable
effect_free = nothrow = terminates_globally = val
elseif setting === :total
consistent = effect_free = nothrow = terminates_globally = notaskstate = inaccessiblememonly = val
else
throw(ArgumentError("@assume_effects $org_setting not supported"))
end
end
if is_function_def(inner)
return esc(pushmeta!(ex, :purity,
consistent, effect_free, nothrow, terminates_globally, terminates_locally, notaskstate, inaccessiblememonly))
elseif isexpr(ex, :macrocall) && ex.args[1] === Symbol("@ccall")
ex.args[1] = GlobalRef(Base, Symbol("@ccall_effects"))
insert!(ex.args, 3, Core.Compiler.encode_effects_override(Core.Compiler.EffectsOverride(
consistent, effect_free, nothrow, terminates_globally, terminates_locally, notaskstate, inaccessiblememonly,
)))
return esc(ex)
else # anonymous function case
return Expr(:meta, Expr(:purity,
consistent, effect_free, nothrow, terminates_globally, terminates_locally, notaskstate, inaccessiblememonly))
end
end
function compute_assumed_setting(@nospecialize(setting), val::Bool=true)
if isexpr(setting, :call) && setting.args[1] === :(!)
return compute_assumed_setting(setting.args[2], !val)
elseif isa(setting, QuoteNode)
return compute_assumed_setting(setting.value, val)
else
return (setting, val)
end
end
"""
@propagate_inbounds
Tells the compiler to inline a function while retaining the caller's inbounds context.
"""
macro propagate_inbounds(ex)
if isa(ex, Expr)
pushmeta!(ex, :inline)
pushmeta!(ex, :propagate_inbounds)
end
esc(ex)
end
"""
@polly
Tells the compiler to apply the polyhedral optimizer Polly to a function.
"""
macro polly(ex)
esc(isa(ex, Expr) ? pushmeta!(ex, :polly) : ex)
end
## some macro utilities ##
unwrap_macrocalls(@nospecialize(x)) = x
function unwrap_macrocalls(ex::Expr)
inner = ex
while inner.head === :macrocall
inner = inner.args[end]::Expr
end
return inner
end
function pushmeta!(ex::Expr, sym::Symbol, args::Any...)
if isempty(args)
tag = sym
else
tag = Expr(sym, args...)::Expr
end
inner = unwrap_macrocalls(ex)
idx, exargs = findmeta(inner)
if idx != 0
push!(exargs[idx].args, tag)
else
body = inner.args[2]::Expr
pushfirst!(body.args, Expr(:meta, tag))
end
ex
end
popmeta!(body, sym) = _getmeta(body, sym, true)
peekmeta(body, sym) = _getmeta(body, sym, false)
function _getmeta(body::Expr, sym::Symbol, delete::Bool)
body.head === :block || return false, []
_getmeta(body.args, sym, delete)
end
_getmeta(arg, sym, delete::Bool) = (false, [])
function _getmeta(body::Array{Any,1}, sym::Symbol, delete::Bool)
idx, blockargs = findmeta_block(body, args -> findmetaarg(args,sym)!=0)
if idx == 0
return false, []
end
metaargs = blockargs[idx].args
i = findmetaarg(blockargs[idx].args, sym)
if i == 0
return false, []
end
ret = isa(metaargs[i], Expr) ? (metaargs[i]::Expr).args : []
if delete
deleteat!(metaargs, i)
isempty(metaargs) && deleteat!(blockargs, idx)
end
true, ret
end
# Find index of `sym` in a meta expression argument list, or 0.
function findmetaarg(metaargs, sym)
for i = 1:length(metaargs)
arg = metaargs[i]
if (isa(arg, Symbol) && (arg::Symbol) == sym) ||
(isa(arg, Expr) && (arg::Expr).head == sym)
return i
end
end
return 0
end
function annotate_meta_def_or_block(@nospecialize(ex), meta::Symbol)
inner = unwrap_macrocalls(ex)
if is_function_def(inner)
# annotation on a definition
return esc(pushmeta!(ex, meta))
else
# annotation on a block
return Expr(:block,
Expr(meta, true),
Expr(:local, Expr(:(=), :val, esc(ex))),
Expr(meta, false),
:val)
end
end
function is_short_function_def(@nospecialize(ex))
isexpr(ex, :(=)) || return false
while length(ex.args) >= 1 && isa(ex.args[1], Expr)
(ex.args[1].head === :call) && return true
(ex.args[1].head === :where || ex.args[1].head === :(::)) || return false
ex = ex.args[1]
end
return false
end
is_function_def(@nospecialize(ex)) =
return isexpr(ex, :function) || is_short_function_def(ex) || isexpr(ex, :->)
function findmeta(ex::Expr)
if is_function_def(ex)
body = ex.args[2]::Expr
body.head === :block || error(body, " is not a block expression")
return findmeta_block(ex.args)
end
error(ex, " is not a function expression")
end
findmeta(ex::Array{Any,1}) = findmeta_block(ex)
function findmeta_block(exargs, argsmatch=args->true)
for i = 1:length(exargs)
a = exargs[i]
if isa(a, Expr)
if a.head === :meta && argsmatch(a.args)
return i, exargs
elseif a.head === :block
idx, exa = findmeta_block(a.args, argsmatch)
if idx != 0
return idx, exa
end
end
end
end
return 0, []
end
remove_linenums!(ex) = ex
function remove_linenums!(ex::Expr)
if ex.head === :block || ex.head === :quote
# remove line number expressions from metadata (not argument literal or inert) position
filter!(ex.args) do x
isa(x, Expr) && x.head === :line && return false
isa(x, LineNumberNode) && return false
return true
end
end
for subex in ex.args
subex isa Expr && remove_linenums!(subex)
end
return ex
end
function remove_linenums!(src::CodeInfo)
src.codelocs .= 0
length(src.linetable) > 1 && resize!(src.linetable, 1)
return src
end
macro generated()
return Expr(:generated)
end
"""
@generated f
`@generated` is used to annotate a function which will be generated.
In the body of the generated function, only types of arguments can be read
(not the values). The function returns a quoted expression evaluated when the
function is called. The `@generated` macro should not be used on functions mutating
the global scope or depending on mutable elements.
See [Metaprogramming](@ref) for further details.
# Examples
```jldoctest
julia> @generated function bar(x)
if x <: Integer
return :(x ^ 2)
else
return :(x)
end
end
bar (generic function with 1 method)
julia> bar(4)
16
julia> bar("baz")
"baz"
```
"""
macro generated(f)
if isa(f, Expr) && (f.head === :function || is_short_function_def(f))
body = f.args[2]
lno = body.args[1]
tmp = gensym("tmp")
return Expr(:escape,
Expr(f.head, f.args[1],
Expr(:block,
lno,
Expr(:if, Expr(:generated),
# https://github.com/JuliaLang/julia/issues/25678
Expr(:block,
:(local $tmp = $body),
:(if $tmp isa $(GlobalRef(Core, :CodeInfo)); return $tmp; else $tmp; end)),
Expr(:block,
Expr(:meta, :generated_only),
Expr(:return, nothing))))))
else
error("invalid syntax; @generated must be used with a function definition")
end
end
"""
@atomic var
@atomic order ex
Mark `var` or `ex` as being performed atomically, if `ex` is a supported expression.
@atomic a.b.x = new
@atomic a.b.x += addend
@atomic :release a.b.x = new
@atomic :acquire_release a.b.x += addend
Perform the store operation expressed on the right atomically and return the
new value.
With `=`, this operation translates to a `setproperty!(a.b, :x, new)` call.
With any operator also, this operation translates to a `modifyproperty!(a.b,
:x, +, addend)[2]` call.
@atomic a.b.x max arg2
@atomic a.b.x + arg2
@atomic max(a.b.x, arg2)
@atomic :acquire_release max(a.b.x, arg2)
@atomic :acquire_release a.b.x + arg2