|
| 1 | +# Pragma Annotations understood by dart2js |
| 2 | + |
| 3 | +## Pragmas for general use |
| 4 | + |
| 5 | +| Pragma | Meaning | |
| 6 | +| --- | --- | |
| 7 | +| `dart2js:noInline` | [Never inline a function or method](#requesting-a-function-never-be-inlined) | |
| 8 | +| `dart2js:never-inline` | Alias for `dart2js:noInline` | |
| 9 | +| `dart2js:tryInline` | [Inline a function or method when possible](#requesting-a-function-be-inlined) | |
| 10 | +| `dart2js:prefer-inline` | Alias for `dart2js:tryInline` | |
| 11 | +| `dart2js:disable-inlining` | [Disable inlining within a method](#disabling-inlining) | |
| 12 | +| `dart2js:noElision` | Disables an optimization whereby unused fields or unused parameters are removed | |
| 13 | + |
| 14 | +## Unsafe pragmas for general use |
| 15 | + |
| 16 | +These pragmas are available for use in third-party code but are potentially |
| 17 | +unsafe. The use of these pragmas is discouraged unless the developer fully |
| 18 | +understands potential repercussions. |
| 19 | + |
| 20 | +| Pragma | Meaning | |
| 21 | +| --- | --- | |
| 22 | +| `dart2js:as:check` | [Check `as` casts](#casts) | |
| 23 | +| `dart2js:as:trust` | [Trust `as` casts](#casts) | |
| 24 | +| `dart2js:downcast:check` | [Check downcasts](#downcasts) | |
| 25 | +| `dart2js:downcast:trust` | [Trust downcasts](#downcasts) | |
| 26 | +| `dart2js:index-bounds:check` | TBD | |
| 27 | +| `dart2js:index-bounds:trust` | TBD | |
| 28 | +| `dart2js:late:check` | [Check late fields are used correctly](#late-checks) | |
| 29 | +| `dart2js:late:trust` | [Trust late fields are used correctly](#late-checks) | |
| 30 | +| `dart2js:parameter:check` | TBD | |
| 31 | +| `dart2js:parameter:trust` | TBD | |
| 32 | +| `dart2js:types:check` | TBD | |
| 33 | +| `dart2js:types:trust` | TBD | |
| 34 | + |
| 35 | +## Pragmas for internal use |
| 36 | + |
| 37 | +These pragmas can cause unsound behavior if used incorrectly and therefore are |
| 38 | +only allowed within the core SDK libraries. |
| 39 | + |
| 40 | +| Pragma | Meaning | |
| 41 | +| --- | --- | |
| 42 | +| `dart2js:assumeDynamic` | TBD | |
| 43 | +| `dart2js:disableFinal` | TBD | |
| 44 | +| `dart2js:noSideEffects` | Requires `dart2js:noInline` to work properly | |
| 45 | +| `dart2js:noThrows` | Requires `dart2js:noInline` to work properly | |
| 46 | + |
| 47 | +## Detailed descriptions |
| 48 | + |
| 49 | +### Annotations related to function inlining |
| 50 | + |
| 51 | +Function (method) inlining is a compiler optimization where a call to a function |
| 52 | +is replaced with the body of the function. To perform function inlining, the |
| 53 | +compiler needs to determine that the call site calls exactly one function, the |
| 54 | +target. This is trivial for top-level methods, static methods and |
| 55 | +constructors. For calls to instance methods, the compiler does an analysis of |
| 56 | +the possible types of the receiver and uses that to reduce the set of potential |
| 57 | +targets. If there is a single target, it can potentially be inlined. |
| 58 | + |
| 59 | +Not all functions can be inlined. For example, a recursive function cannot be |
| 60 | +expanded by inlining indefinitely. `dart2js` will not inline functions complex |
| 61 | +control flow, such as methods with exception handling (`try`-`catch`-`finally`) |
| 62 | +or many return or throw exit points. |
| 63 | + |
| 64 | +We say a function is a _viable inlining candidate_ when it is the single target |
| 65 | +and it is possible to perform the inlining. |
| 66 | + |
| 67 | +One benefit of inlining is that the execution cost of performing the call is |
| 68 | +avoided, which can be a substantial part of the total cost of the call when the |
| 69 | +body of the callee is simple. Copying instructions from the callee into the |
| 70 | +caller can create more opportunities for optimization, for example, it becomes |
| 71 | +possible to recognize and remove repeated operations. |
| 72 | + |
| 73 | +The compiler automatically makes a decision whether or not to inline a function |
| 74 | +or method based on heuristics. One heuristic is to inline if the the inlined |
| 75 | +code is likely to be smaller that the call, as this results in a smaller _and_ |
| 76 | +faster program. Another heuristic is to inline even if the code is likely to be |
| 77 | +slightly larger when the call is in a loop, as loops here is a chance that some |
| 78 | +of the code can be hoisted out of the loop. |
| 79 | + |
| 80 | +The annotations described below allow the developer to override the default |
| 81 | +decisions. They should be used sparingly since it is likely that over time |
| 82 | +manual overrides will become increasingly out of date and mismatched with the |
| 83 | +evolving capabilities of the compiler. |
| 84 | + |
| 85 | +#### Requesting a function be inlined |
| 86 | + |
| 87 | +```dart |
| 88 | +@pragma('dart2js:tryInline') |
| 89 | +``` |
| 90 | + |
| 91 | +```dart |
| 92 | +@pragma('dart2js:prefer-inline) // Alias for the above annotation. |
| 93 | +``` |
| 94 | + |
| 95 | +This annotation may be placed on a function or method. |
| 96 | + |
| 97 | +The compiler will inline the annotated function wherever it is a viable inlining |
| 98 | +candidate. |
| 99 | + |
| 100 | + |
| 101 | +#### Requesting a function never be inlined |
| 102 | + |
| 103 | +```dart |
| 104 | +@pragma('dart2js:noInline') |
| 105 | +``` |
| 106 | + |
| 107 | +```dart |
| 108 | +@pragma('dart2js:never-inline) // Alias for the above annotation. |
| 109 | +``` |
| 110 | + |
| 111 | +This annotation may be placed on a function or method to prevent the function |
| 112 | +from being inlined. |
| 113 | + |
| 114 | +#### Disabling inlining |
| 115 | + |
| 116 | +```dart |
| 117 | +@pragma('dart2js:disable-inlining') |
| 118 | +``` |
| 119 | + |
| 120 | +This annotation may be placed on a function or method. |
| 121 | + |
| 122 | +Function inlining is disabled at call sites within the annotated function. |
| 123 | +Inlining is disabled even when the call site has a viable inlining candidate |
| 124 | +that is annotated with `@pragma('dart2js:tryInline')`. |
| 125 | + |
| 126 | + |
| 127 | +### Annotations related to run-time checks |
| 128 | + |
| 129 | +The Dart language and runtime libraries mandate checks in various places. Checks |
| 130 | +result in some kind of `Error` exception being thrown. If a program has a high |
| 131 | +degree of test coverage, the developer might have some confidence that the |
| 132 | +checks will never fail. If this is the case, the checks can be disabled via |
| 133 | +command line options or annotations. Annotations override the command line |
| 134 | +settings. |
| 135 | + |
| 136 | +Trusting (i.e. disabling) checks can lead to a smaller and faster program. The |
| 137 | +cost is highly confusing unspecified behavior in place of the `Error`s that |
| 138 | +would otherwise have been thrown. The unspecified behavior is not necessarily |
| 139 | +consistent between runs and includes the program execution reaching statements |
| 140 | +that are 'impossible' to reach and variables being assigned values of an |
| 141 | +'impossible' type. |
| 142 | + |
| 143 | +#### Casts |
| 144 | + |
| 145 | +```dart |
| 146 | +@pragma('dart2js:as:check') |
| 147 | +@pragma('dart2js:as:trust') |
| 148 | +``` |
| 149 | + |
| 150 | +These annotations may be placed on a function or method to control whether `as` |
| 151 | +casts in the body of the function are checked. |
| 152 | + |
| 153 | +One use of `dart2js:as:trust` is to construct an `unsafeCast` method. |
| 154 | + |
| 155 | +```dart |
| 156 | +@pragma('dart2js:tryInline') |
| 157 | +@pragma('dart2js:as:trust') |
| 158 | +T unsafeCast<T>(Object? o) => o as T; |
| 159 | +``` |
| 160 | + |
| 161 | +The `tryInline` pragma ensures that the function is inlined, removing the cost |
| 162 | +of the call and passing the type parameter `T`, and the `as:trust` pragma |
| 163 | +removes the code that does the check. |
| 164 | + |
| 165 | +#### Downcasts |
| 166 | + |
| 167 | +```dart |
| 168 | +@pragma('dart2js:downcast:check') |
| 169 | +@pragma('dart2js:downcast:trust') |
| 170 | +``` |
| 171 | + |
| 172 | +These annotations may be placed on a function or method to control whether |
| 173 | +implicit downcasts in the body of the function are checked. |
| 174 | + |
| 175 | +This is similar to the `dart2js:as:check` and `dart2js:as:trust` pragmas except |
| 176 | +it applies to implicit downcasts. Implicit downcasts are `as` checks that are |
| 177 | +inserted to cast from `dynamic`. |
| 178 | + |
| 179 | +The `unsafeCast` method described above could also be written by trusting |
| 180 | +implicit downcasts. |
| 181 | + |
| 182 | +```dart |
| 183 | +@pragma('dart2js:tryInline') |
| 184 | +@pragma('dart2js:downcast:trust') |
| 185 | +T unsafeCast<T>(dynamic o) => o; // implicit downcast `as T`. |
| 186 | +``` |
| 187 | + |
| 188 | +Trusting implicit downcasts is part of the `-O3` and `-O4` optimization level |
| 189 | +command line options. `dart2js:downcast:check` can be used to enable checking of |
| 190 | +implicit downcasts in a method when it would otherwise be trusted due to the |
| 191 | +command line options. |
| 192 | + |
| 193 | +#### Late checks |
| 194 | + |
| 195 | +Late checks - checking whether a late variable has been initialized - occur on |
| 196 | +all late variables. The checks on late instance variables (i.e. late fields) |
| 197 | +can be controlled via the following annotations. |
| 198 | + |
| 199 | +```dart |
| 200 | +@pragma('dart2js:late:check') |
| 201 | +@pragma('dart2js:late:trust') |
| 202 | +``` |
| 203 | + |
| 204 | +These annotations may be placed on the declaration of a late field, class, or |
| 205 | +library. When placed on a class, the annotation applies to all late fields of |
| 206 | +the class. When placed on a library, the annotation applies to all late fields |
| 207 | +of all classes in the library. `dart2js:late` annotations are _scoped_: when |
| 208 | +there are multiple annotations, the one nearest the late field wins. |
| 209 | + |
| 210 | +In the future this annotation might be extended to apply to `late` local |
| 211 | +variables, static variables, and top-level variables. |
0 commit comments