Commit Graph

2441 Commits

Author SHA1 Message Date
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Puyan Lotfi
84a69c4143 [C++-Interop] Import const &T function parameters as @in_guaranteed
When passing value types as a const-ref to a C++ API, ensure that the
caller is tasked with handling the lifetime of the instance passed in.
2022-09-12 16:27:27 -04:00
nate-chandler
53721774b8 Merge pull request #61042 from nate-chandler/fix-lexical-recursive-properties-bug
Fix bug with lexical recursive property.
2022-09-12 13:14:20 -07:00
Nate Chandler
f53e88b25b Workaround MSVC more. 2022-09-12 07:36:49 -07:00
Erik Eckstein
8e2e7a73c5 SIL: make argument effects more readable in textual SIL
So far, argument effects were printed in square brackets before the function name, e.g.
```
sil [escapes !%0.**, !%1, %1.c*.v** => %0.v**] @foo : $@convention(thin) (@guaranteed T) -> @out S {
bb0(%0 : $*S, %1 : @guaranteed $T):
...
```

As we are adding more argument effects, this becomes unreadable.
To make it more readable, print the effects after the opening curly brace, and print a separate line for each argument. E.g.
```
sil [ossa] @foo : $@convention(thin) (@guaranteed T) -> @out S {
[%0: noescape **]
[%1: noescape, escape c*.v** => %0.v**]
bb0(%0 : $*S, %1 : @guaranteed $T):
...
```
2022-09-12 09:14:54 +02:00
Nate Chandler
e327848b67 Workaround MSVC overload resolution. 2022-09-11 21:04:03 -07:00
Nate Chandler
af6749e84c [TypeLowering] Mark lexical when marking non-trivial.
In addition to starting all RecursiveProperties values which are
NonTrivial as Lexical, also mark instances of the structs which are
modified to become NonTrivial to simultaneously become Lexical.
2022-09-11 16:11:09 -07:00
Nate Chandler
f3cc0819c8 [NFC] Fix verification in TypeLowering. 2022-09-11 12:36:12 -07:00
Nate Chandler
82322705d8 [TypeLowering] Correct assertion.
When examining non-trivial types which were non-lexical, when a field
lacks a field decl (as happens with tuples), whether the type was
annotated @_eagerMove was overlooked, resulting in incorrectly
determining that a leaf was lexical.
2022-09-11 12:36:00 -07:00
Slava Pestov
c1b8690401 AST: Introduce special Builtin.TheTupleType singleton 2022-09-10 00:26:42 -04:00
Erik Eckstein
97b2354be6 SIL: add needsStackProtection flags for address_to_pointer and index_addr instructions.
Also add new "unprotected" variants of the `addressof` builtins:
* `Builtin.unprotectedAddressOf`
* `Builtin.unprotectedAddressOfBorrow`
2022-09-08 08:42:22 +02:00
Erik Eckstein
fdca208335 SIL: add the SILFunction.needsStackProtection flag
Indicates that stack protectors are inserted into this function to detect stack related buffer overflows.
2022-09-08 08:37:21 +02:00
Hamish Knight
b12015c343 [SIL] Introduce the increment_profiler_counter instruction
This is a dedicated instruction for incrementing a
profiler counter, which lowers to the
`llvm.instrprof.increment` intrinsic. This
replaces the builtin instruction that was
previously used, and ensures that its arguments
are statically known. This ensures that SIL
optimization passes do not invalidate the
instruction, fixing some code coverage cases in
`-O`.

rdar://39146527
2022-09-07 17:55:13 +01:00
Michael Gottesman
4dc6e6ecc4 [move-keyword] Remove old implementation.
By using the keyword instead of the function, we actually get a much simpler
implementation since we avoid all of the machinery of SILGenApply. Given that we
are going down that path, I am removing the old builtin implementation since it
is dead code.

The reason why I am removing this now is that in a subsequent commit, I want to
move all of the ownership checking passes to run /before/ mandatory inlining. I
originally placed the passes after mandatory inlining since the function version
of the move keyword was transparent and needing to be inlined before we could
process it. Since we use the keyword now, that is no longer an issue.
2022-09-04 01:19:01 -07:00
Hamish Knight
7e141f4b1c [Profiler] Emit coverage for literal initializers
It's not clear why these should be excluded when
other initializers receive coverage.
2022-09-01 19:29:17 +01:00
Joe Groff
18b2225435 Merge pull request #60604 from jckarter/silbuilder-dont-delete-operands-in-peepholes
SIL: Don't eagerly erase instructions in builder peepholes.
2022-08-26 12:27:05 -07:00
Hamish Knight
05e2309788 [Profiler] Fix coverage mapping for switches
Previously we assumed the exit counter was the
same as the entry counter. Update the logic such
that we track break statements (including
implicitly at the end of a case), as well as
early returns and jumps to parent statements. This
follows a similar logic to what we do for if
statements.

rdar://99141044
2022-08-25 17:30:34 +01:00
Hamish Knight
40bb60822f [Profiler] NFC: Add some comments 2022-08-25 17:30:33 +01:00
Slava Pestov
5c32f2136e AST: Introduce RequirementKind::SameCount 2022-08-23 11:12:00 -04:00
Nate Chandler
e9595ab845 [TypeLowering] Verify lexical property.
Check that if a type is non-trivial then either (1) it is lexical or (2)
its non-trivial leaves are @_eagerMove.
2022-08-22 15:28:00 -07:00
Nate Chandler
921c7f69c8 [SIL] Only lexical owned args are lexical.
Previously, every owned argument was considered lexical.  That included
owned arguments with eager move lifetimes.  Here, only owned arguments
whose lifetimes are lexical (i.e. not those whose lifetimes are eager
move) are considered lexical.
2022-08-22 15:28:00 -07:00
Nate Chandler
eb3f8002ed [SIL] Added SILType::getLifetime.
The function describes the lifetime that variables of that type have by
default (unless otherwise annotated).  This is done by looking at leaf
nodes until one that is not eager move is found, at which point the
lifetime is known to be lexical.  Otherwise, the lifetime is eager move
(i.e. if every leaf is eager move).
2022-08-22 15:27:59 -07:00
Nate Chandler
6f582336bf [TypeLowering] Record whether a type is lexical.
Used RecursiveProperties to keep track of whether a type is lexical.

This is done in the usual way by OR'ing together whether components are
lexicial.  The the values to be OR'd together come from primitives:
everything is non-lexical except reference types and resilient types
(because they could become reference types).  Those values can however
be affected by user annotations: marking a class @_lexical makes the
value for that primitive lexical; markinig an enum or struct @_eagerMove
overrides the value obtained by OR'ing together its components and makes
it be non-lexical.
2022-08-22 15:27:59 -07:00
Nate Chandler
93f6548950 [Textual SIL] Explicit function arg lifetimes.
Print and parse @_eagerMove and @_lexical on function arguments.
2022-08-21 21:44:31 -07:00
Michael Gottesman
3e52007562 [builtin] Remove "unsafeGuaranteed" and related code since Unmanaged now has an Ownership SSA based implementation that works completely in SILGen.
This isn't used in the stdlib anymore as well.
2022-08-21 01:22:36 -07:00
Allan Shortlidge
a66dabea03 Merge pull request #60625 from tshortli/weak-linked-import-inlinable
IRGen/SIL: Fix IR linkage computation for inlined function references from modules imported `@_weakLinked`
2022-08-19 16:56:46 -07:00
Hamish Knight
caf597212c Merge pull request #60645 from hamishknight/simon-says 2022-08-19 19:58:04 +01:00
Allan Shortlidge
40eb1422bb IRGen/SIL: Fix IR linkage computation for inlined function references from modules imported @_weakLinked.
Include the parent `ModuleDecl` when serializing a `SILFunction` so that it is available on deserialized functions even though the full `DeclContext` is not present. With the parent module always available we can reliably compute whether the `SILFunction` comes from a module that was imported `@_weakLinked`.

Serialize the `DeclContext` member of `SILFunction` so that it can be used to look up the module that a function belongs to in order to compute weak import status.

Resolves rdar://98521248
2022-08-19 09:56:45 -07:00
Hamish Knight
d8bab13469 [Profiler] Fix counters following DoStmts
Previously we weren't compensating for label
jumps and returns, i.e we assumed the exit count
is the same as the entry count. Ensure we follow
the same logic that other labeled statements
follow such that control flow is accounted for.

rdar://98881045
2022-08-19 15:06:11 +01:00
Hamish Knight
92a8fbd3a7 [Profiler] Assert that counters are present 2022-08-19 15:06:11 +01:00
Hamish Knight
64fcbd9412 [Profiler] Support lazy variable initializers
Start visiting LazyInitializerExpr for profiling,
such that we emit a profile counter when
initializing the initial value for the first time.

rdar://43393937
2022-08-19 14:03:40 +01:00
Hamish Knight
a1d3884b6a Merge pull request #60579 from hamishknight/never-say-never 2022-08-19 13:46:04 +01:00
Joe Groff
d3f482ab9e SIL: Don't eagerly erase instructions in builder peepholes.
Even if the operand doesn't currently have any uses after the peephole, the
caller may intend to use the operand for other purposes. If the operand is
really unused in the end, then dead code elimination ought to clean it up.
Fixes rdar://98418860.
2022-08-18 16:59:13 -07:00
Hamish Knight
e1bb0bb945 Merge pull request #60621 from hamishknight/pogo 2022-08-18 15:16:22 +01:00
Holly Borla
8713d78704 [PrintOptions] Print explicit 'any' in SIL. 2022-08-18 01:15:12 -04:00
Hamish Knight
68c80c57cf [Profiler] Simplify PGOMapping a bit
Rather than re-computing the counter indices as we
walk, use the indices that we've already computed
as a part of MapRegionCounters. This should be
NFC.

Eventually we should also stop duplicating the
counter arithmetic, and instead rely on the
CounterExprs computed by CoverageMapping. For now
I'm leaving that as follow-up work.
2022-08-17 17:27:36 +01:00
Hamish Knight
3a3a98e06f [Profiler] NFC: Constify LoadedCounts 2022-08-17 17:27:24 +01:00
Hamish Knight
d1eb6f9465 [Profiler] Avoid introducing empty unreachable regions
When computing the counter for the region
following a labeled statement such as `if`, avoid
adding a new empty region if the counter for such
a region is known to be zero, i.e unreachable.
This avoids adding spurious unreachable regions
after an if statements where each branch returns,
throws, or otherwise jumps to a parent statement.

We will however still add the region if any code
follows such a statement, making it non-empty.

rdar://29390569
2022-08-16 20:27:30 +01:00
Hamish Knight
c1ae4ed6bf [Profiler] Introduce CounterExpr::isSemanticallyZero
Refactor `CounterExpr::expand` to accept a function
lookup for the counter indices, and add
`isSemanticallyZero` to allow for checking whether
a simplified counter is 0.
2022-08-16 20:27:29 +01:00
Michael Gottesman
accc2820f8 Merge pull request #60511 from gottesmm/pr-0796eed23b86f5812f817b15b928dbe11485c361
[move-only-addresses] Slicing some patches off of the larger patch.
2022-08-11 16:25:10 -07:00
Michael Gottesman
5baf2af88a [sil] Add a new instruction called explicit_copy_addr.
This is exactly like copy_addr except that it is not viewed from the verifiers
perspective as an "invalid" copy of a move only value. It is intended to be used
in two contexts:

1. When the move checker emits a diagnostic since it could not eliminate a copy,
we still need to produce valid SIL without copy_addr on move only types since we
will hit canonical SIL eventually even if we don't actually codegen the SIL. The
pass can just convert said copy_addr to explicit_copy_addr and everyone is
happy.

2. To implement the explicit copy function for address only types.
2022-08-11 11:40:53 -07:00
Allan Shortlidge
f71a3232cf IRGen: Use extern_weak linkage for Clang symbols from @_weakLinked imported modules.
rdar://96098097
2022-08-11 11:02:57 -07:00
Allan Shortlidge
bc5f13cb6b AST: Accept @_weakLinked on import decls to force weak linkage of symbols from a module.
The effect of declaring an import `@_weakLinked` is to treat every declaration from the module as if it were declared with `@_weakLinked`. This is useful in environments where entire modules may not be present at runtime. Although it is already possible to instruct the linker to weakly link an entire dylib, a Swift attribute provides a way to declare intent in source code and also opens the door to diagnostics and other compiler behaviors that depend on knowing that all the module's symbols will be weakly linked.

rdar://96098097
2022-08-11 11:02:57 -07:00
Ben Barham
a59fa46813 Merge pull request #60426 from bnbarham/include-smallvec
[Basic] Include SmallVector.h for Clang <= 5
2022-08-10 15:54:48 -07:00
Slava Pestov
9d96ed940f AST: Rename 'canonical wrt. generic signature' to 'reduced'
We had two notions of canonical types, one is the structural property
where it doesn't contain sugared types, the other one where it does
not contain reducible type parameters with respect to a generic
signature.

Rename the second one to a 'reduced type'.
2022-08-09 12:46:31 -04:00
Ben Barham
6de34f37e5 [NFC] Revert SmallVector<T> -> SmallVector<T, N> fixes
With the change to include `SmallVector.h` directly in `LLVM.h` rather
than forward declaring in the only case it matters (ie. Clang <= 5),
these fixes are no longer needed. Since defaulted version is preferred
when there's no better choice (which is presumably the case if that's
how they were originally added), use it instead. Some uses were instead
changed to add `llvm::` so remove that too.
2022-08-05 21:25:55 -07:00
Sima Nerush
7b00eb67cf SIL: Remove unused parameter 2022-08-04 21:25:43 -06:00
Hamish Knight
9da53193da [AST] Remove ParameterTypeFlags from ParenType and TupleType
The last clients that relied on stashing parameter
type flags on these types are now gone.
2022-08-02 13:56:32 +01:00
Allan Shortlidge
34012dbc68 Frontend: Rename -enable-ad-hoc-availability to -weak-link-at-target.
I am separating the concern of weakly linking symbols that are introduced at the deployment target from the concern of type checking this new type of potential unavailability.

Resolves rdar://97925900
2022-08-01 11:57:06 -07:00
Zoe Carver
84b359e332 Merge pull request #60283 from zoecarver/frt-static-extension
[cxx-interop] Fix static extensions on foreign reference types.
2022-07-28 17:42:34 -07:00