|
| 1 | +`@_transparent` |
| 2 | +================= |
| 3 | + |
| 4 | +Semantically, `@_transparent` means something like "treat this operation as |
| 5 | +if it were a primitive operation". The name is meant to imply that both the |
| 6 | +compiler and the compiled program will "see through" the operation to its |
| 7 | +implementation. |
| 8 | + |
| 9 | +This has several consequences: |
| 10 | + |
| 11 | +- Any calls to a function marked `@_transparent` MUST be inlined prior to |
| 12 | + doing dataflow-related diagnostics, even under `-Onone`. This may be |
| 13 | + necessary to *catch* dataflow errors. |
| 14 | + |
| 15 | +- Because of this, a `@_transparent` function is implicitly inlinable, in |
| 16 | + that changing its implementation most likely will not affect callers in |
| 17 | + existing compiled binaries. |
| 18 | + |
| 19 | +- Because of this, a public or `@usableFromInline` `@_transparent` function |
| 20 | + MUST only reference public symbols, and MUST not be optimized based on |
| 21 | + knowledge of the module it's in. [The former is caught by checks in Sema.] |
| 22 | + |
| 23 | +- Debug info SHOULD skip over the inlined operations when single-stepping |
| 24 | + through the calling function. |
| 25 | + |
| 26 | +This is all that `@_transparent` means. |
| 27 | + |
| 28 | + |
| 29 | +When should you use `@_transparent`? |
| 30 | +-------------------------------------- |
| 31 | + |
| 32 | +- Does the implementation of this function ever have to change? Then you can't |
| 33 | + allow it to be inlined. |
| 34 | + |
| 35 | +- Does the implementation need to call private things---either true-`private` |
| 36 | + functions, or `internal` functions that might go away in the next release? |
| 37 | + Then you can't allow it to be inlined. |
| 38 | + |
| 39 | +- Is it okay if the function is *not* inlined? You'd just prefer that it were? |
| 40 | + Then you should use `@inlinable`, rather than `@_transparent`. (If you |
| 41 | + really need this, you can add `@inline(__always)` as well.) |
| 42 | + |
| 43 | +- Is it a problem if the function is inlined even under `-Onone`? Then you're |
| 44 | + really in the previous case. Trust the compiler. |
| 45 | + |
| 46 | +- Is it a problem if you can't step through the function that's been inlined? |
| 47 | + Then you don't want `@_transparent`; you just want `@inline(__always)` |
| 48 | + (and probably `@inlinable` as well, for cross-module inlining). |
| 49 | + |
| 50 | +- Is it okay if the inlining happens after all the dataflow diagnostics? Then |
| 51 | + you don't want `@_transparent`; you just want `@inline(__always)`. |
| 52 | + |
| 53 | +If you made it this far, it sounds like `@_transparent` is the right choice. |
| 54 | + |
| 55 | + |
| 56 | +Interaction with other annotations |
| 57 | +---------------------------------- |
| 58 | + |
| 59 | +- As mentioned above, putting `@_transparent` on a function that is |
| 60 | + `public` or `@usableFromInline` exposes its body to other modules. It is |
| 61 | + not necessary to additionally include `@inlinable`. |
| 62 | + |
| 63 | +- Unlike `@inlinable`, however, `@_transparent` does not imply |
| 64 | + `@usableFromInline`. It is possible to have functions marked |
| 65 | + `@_transparent` that are only meant for use within the current module or |
| 66 | + even the current file. |
| 67 | + |
| 68 | + |
| 69 | +Current implementation limitations |
| 70 | +---------------------------------- |
| 71 | + |
| 72 | +- When compiling in non-single-frontend mode, no SIL is generated for any |
| 73 | + functions but those in the primary file (for each frontend invocation), |
| 74 | + including `@inline(__always)` and `@_transparent` functions, which means |
| 75 | + they will not be inlined. This is semantically a bug. rdar://problem/15366167 |
0 commit comments