Commit f10176c
committed
Implement new effect system
* TLDR
Before:
```
julia> let b = Expr(:block, (:(y += sin($x)) for x in randn(1000))...)
@eval function f_sin_perf()
y = 0.0
$b
y
end
end
f_sin_perf (generic function with 1 method)
julia> @time @code_typed f_sin_perf()
15.707267 seconds (25.95 M allocations: 1.491 GiB, 3.30% gc time)
[lots of junk]
```
After:
```
julia> @time @code_typed f_sin_perf()
0.016818 seconds (187.35 k allocations: 7.901 MiB, 99.73% compilation time)
CodeInfo(
1 ─ return 27.639138714768546
) => Float64
```
so roughly a 1000x improvement in compile time performance for const-prop heavy functions.
There are also run time improvements for functions that have patterns like:
```
function some_function_to_big_to_be_inlined_but_pure(x)
....
end
function foo(x)
some_function_to_big_to_be_inlined_but_pure(x)
return x
end
```
The inliner will now be able to see that some_function_to_big_to_be_inlined_but_pure is effect free, even without inlining it and just delete it, improving runtime performance (if some_function_to_big_to_be_inlined_but_pure is small enough to be inlined, there is a small compile time throughput win, by being able to delete it without inlining, but that's a smaller gain than the compile time gain above).
* Motivation / Overview
There are two motivations for this work. The first is the above mentioned improvement in compiler performance for const-prop heavy functions. This comes up a fair bit in various Modeling & Simulation codes we have where Julia code is often auto-generated from some combination of parameterized model codes and data. This ends up creating enormous functions with significant need for constant propagation (~50k statements with ~20k constant calls are not uncommon). Our current compiler was designed for people occasionally throwing a `sqrt(2)` or something in a function, not 20k of them, so performance is quite bad.
The second motivation is to have finer grained control over our purity modeling. We have `@Base.pure`, but that has somewhat nebulous semantics and is quite a big hammer that is not appropriate in most situations.
These may seem like orthogonal concerns at first, but they are not. The compile time issues fundamentally stem from us running constant propagation in inference's abstract interpreter. However, for simple, pure functions, that is entirely unnecessary, because we have a super-fast, JIT compiler version of that function just laying around in general. The issue is that we currently, we generally do not know when it is legal to run the JIT-compiled version of the function and when we need to abstractly interpret it. However, if the compiler were able to figure out an appropriate notion of purity, it could start doing that (which is what it does now for `@Base.pure` functions).
This PR adds that kind of notion of purity, converges it along with type information during inference and then makes use of it to speed up evaluation of constant propagation (where it is legal to do so), as well as improving the inliner.
* The new purity notions
The new purity model consists of four different kinds flags per code instance. For builtins and intrinsics the existing effect free and nothrow models are re-used. There is also a new macro `@Base.assume_effects` available, which can set the purity base case for methods or `:foreigncall`s. Here is the docstring for that macro, which also explains the semantics of the new purity flags:
```
@assume_effects setting... ex
@assume_effects(setting..., ex)
`@assume_effects` overrides the compiler's effect modeling for the given method.
`ex` must be a method definition.
WARNING: Improper use of this macro causes undefined behavior (including crashes,
incorrect answers, or other hard to track bugs). Use with care an only if absolutely
required.
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 `settings` are supported.
** `:idempotent`
The `:idempotent` 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 return value of the method must be
immutable. Multiple allocations of mutable objects (even with identical
contents) are not egal.
Note: The idempotency assertion is made world-arge wise. More formally, write
fₐ for the evaluation of `f` in world-age `a`, then we require:
∀ a, x, y: x === y → fₐ(x) === fₐ(y)
However, for two world ages `a, b` s.t. `a != b`, we may have `fₐ(x) !== fₐ(y)``
Note: A further implication is that idempontent 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 idempontency includes all legal rewrites performed by the optimizizer.
For example, floating-point fastmath operations are not considered idempotent,
because the optimizer may rewrite them causing the output to not be idempotent,
even for the same world age (e.g. because one ran in the interpreter, while
the other was optimized).
** `: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 infinite loop.
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.
Note: The `terminates_globally` assertion, covers any other methods called by
the annotated method.
** `: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 this
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`.
* `:total`
The `setting` combines the following other assertions:
- `:idempotent`
- `:effect_free`
- `:nothrow`
- `:terminates_globally`
and is a convenient shortcut.
Note: `@assume_effects :total` is similar to `@Base.pure` with the primary
distinction that the idempotency requirement applies world-age wise rather
than globally as described above. However, in particular, a method annotated
`@Base.pure` is always total.
```
* Changes to data structures
- Each CodeInstance gains two sets of four flags corresponding to the notions above (except terminates_locally, which is just a type inference flag). One set of flags tracks IPO-valid information (as determined by inference), the other set of flags tracks optimizer-valid information (as determined after optimization). Otherwise they have identical semantics.
- Method and CodeInfo each gain 5 bit flags corresponding 1:1 to the purity notions defined above. No separate distinction is made between IPO valid and optimizer valid flags here. We might in the future want such a distinction, but I'm hoping to get away without it for now, since the IPO-vs-optimizer distinction is a bit subtle and I don't really want to expose that to the user.
- `:foreigncall` gains an extra argument (after `cconv`) to describe the effects of the call.
* Algorithm
Relatively straightforward.
- Every call or builtin accumulates its effect information into the current frame.
- Finding an effect (throw/global side effect/non-idempotenct, etc.) taints the entire frame. Idempotency is technically a dataflow property, but that is not modeled here and any non-idempotent intrinsic will taint the idempotency flag, even if it does not contribute to the return value. I don't think that's a huge problem in practice, because currently we only use idempotency if effect-free is also set and in effect-free functions you'd generally expect every statement to contribute to the return value.
- Any backedge taints the termination effect, as does any recursion
- Unknown statements (generic calls, things I haven't gotten around to) taint all effects1 parent 816c6a2 commit f10176c
File tree
36 files changed
+813
-149
lines changed- base
- compiler
- ssair
- strings
- doc/src/devdocs
- src
- stdlib
- InteractiveUtils/test
- Serialization/src
- test
- compiler
36 files changed
+813
-149
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
418 | 418 | | |
419 | 419 | | |
420 | 420 | | |
421 | | - | |
422 | | - | |
423 | | - | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
424 | 424 | | |
425 | 425 | | |
426 | 426 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
676 | 676 | | |
677 | 677 | | |
678 | 678 | | |
| 679 | + | |
679 | 680 | | |
680 | 681 | | |
681 | 682 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
| |||
126 | 129 | | |
127 | 130 | | |
128 | 131 | | |
| 132 | + | |
129 | 133 | | |
130 | 134 | | |
131 | 135 | | |
132 | 136 | | |
133 | 137 | | |
134 | 138 | | |
135 | 139 | | |
| 140 | + | |
136 | 141 | | |
137 | 142 | | |
138 | 143 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | 152 | | |
163 | 153 | | |
164 | 154 | | |
| |||
292 | 282 | | |
293 | 283 | | |
294 | 284 | | |
295 | | - | |
296 | | - | |
| 285 | + | |
| 286 | + | |
297 | 287 | | |
298 | 288 | | |
299 | | - | |
| 289 | + | |
300 | 290 | | |
301 | 291 | | |
302 | 292 | | |
| |||
306 | 296 | | |
307 | 297 | | |
308 | 298 | | |
309 | | - | |
310 | | - | |
311 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
312 | 302 | | |
313 | 303 | | |
314 | 304 | | |
| |||
612 | 602 | | |
613 | 603 | | |
614 | 604 | | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | 605 | | |
631 | 606 | | |
632 | 607 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
58 | | - | |
| 65 | + | |
59 | 66 | | |
60 | 67 | | |
61 | 68 | | |
| |||
506 | 513 | | |
507 | 514 | | |
508 | 515 | | |
509 | | - | |
| 516 | + | |
| 517 | + | |
510 | 518 | | |
511 | | - | |
| 519 | + | |
| 520 | + | |
512 | 521 | | |
513 | 522 | | |
514 | 523 | | |
| |||
715 | 724 | | |
716 | 725 | | |
717 | 726 | | |
718 | | - | |
| 727 | + | |
719 | 728 | | |
720 | 729 | | |
721 | | - | |
| 730 | + | |
| 731 | + | |
722 | 732 | | |
723 | 733 | | |
724 | | - | |
| 734 | + | |
725 | 735 | | |
726 | 736 | | |
727 | | - | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
728 | 743 | | |
729 | 744 | | |
730 | 745 | | |
| |||
742 | 757 | | |
743 | 758 | | |
744 | 759 | | |
| 760 | + | |
745 | 761 | | |
746 | 762 | | |
747 | 763 | | |
| |||
752 | 768 | | |
753 | 769 | | |
754 | 770 | | |
| 771 | + | |
755 | 772 | | |
| 773 | + | |
756 | 774 | | |
757 | 775 | | |
758 | 776 | | |
759 | 777 | | |
760 | 778 | | |
761 | 779 | | |
762 | 780 | | |
763 | | - | |
| 781 | + | |
764 | 782 | | |
765 | 783 | | |
766 | 784 | | |
767 | 785 | | |
768 | 786 | | |
769 | | - | |
| 787 | + | |
770 | 788 | | |
771 | 789 | | |
772 | 790 | | |
773 | 791 | | |
774 | 792 | | |
775 | 793 | | |
776 | 794 | | |
777 | | - | |
| 795 | + | |
778 | 796 | | |
779 | 797 | | |
780 | 798 | | |
| |||
816 | 834 | | |
817 | 835 | | |
818 | 836 | | |
819 | | - | |
820 | | - | |
821 | | - | |
822 | | - | |
823 | 837 | | |
824 | 838 | | |
825 | | - | |
| 839 | + | |
826 | 840 | | |
827 | 841 | | |
828 | 842 | | |
| |||
831 | 845 | | |
832 | 846 | | |
833 | 847 | | |
834 | | - | |
835 | | - | |
| 848 | + | |
| 849 | + | |
836 | 850 | | |
837 | 851 | | |
838 | | - | |
| 852 | + | |
839 | 853 | | |
840 | 854 | | |
841 | 855 | | |
842 | 856 | | |
843 | | - | |
844 | | - | |
| 857 | + | |
| 858 | + | |
845 | 859 | | |
846 | 860 | | |
847 | 861 | | |
| |||
850 | 864 | | |
851 | 865 | | |
852 | 866 | | |
853 | | - | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
854 | 871 | | |
855 | 872 | | |
856 | | - | |
| 873 | + | |
857 | 874 | | |
858 | 875 | | |
859 | 876 | | |
| |||
1100 | 1117 | | |
1101 | 1118 | | |
1102 | 1119 | | |
1103 | | - | |
| 1120 | + | |
1104 | 1121 | | |
1105 | 1122 | | |
1106 | | - | |
| 1123 | + | |
1107 | 1124 | | |
1108 | 1125 | | |
1109 | 1126 | | |
| |||
1220 | 1237 | | |
1221 | 1238 | | |
1222 | 1239 | | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
1223 | 1255 | | |
1224 | 1256 | | |
1225 | 1257 | | |
| |||
1232 | 1264 | | |
1233 | 1265 | | |
1234 | 1266 | | |
1235 | | - | |
| 1267 | + | |
1236 | 1268 | | |
1237 | 1269 | | |
1238 | 1270 | | |
| |||
1309 | 1341 | | |
1310 | 1342 | | |
1311 | 1343 | | |
| 1344 | + | |
1312 | 1345 | | |
1313 | 1346 | | |
1314 | 1347 | | |
| |||
1447 | 1480 | | |
1448 | 1481 | | |
1449 | 1482 | | |
1450 | | - | |
| 1483 | + | |
1451 | 1484 | | |
1452 | 1485 | | |
1453 | 1486 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
668 | 668 | | |
669 | 669 | | |
670 | 670 | | |
671 | | - | |
| 671 | + | |
672 | 672 | | |
673 | 673 | | |
674 | 674 | | |
| |||
960 | 960 | | |
961 | 961 | | |
962 | 962 | | |
963 | | - | |
| 963 | + | |
964 | 964 | | |
965 | 965 | | |
966 | | - | |
| 966 | + | |
967 | 967 | | |
968 | 968 | | |
969 | 969 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
788 | 788 | | |
789 | 789 | | |
790 | 790 | | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
791 | 806 | | |
0 commit comments