Commit Graph

51 Commits

Author SHA1 Message Date
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Hamish Knight
a3848c62d5 [Profiler] Don't profile generated buffers
We may want to revisit this in the future, but for
now let's avoid profiling code in generated
buffers. To make this work we'll need to come up
with a scheme for writing out the generated buffers
such that tools like `llvm-cov` can reference them.

rdar://109562235
2023-05-20 00:32:20 +01:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Pavel Yaskevich
7e0f02ac40 Merge pull request #62724 from valeriyvan/Formatting
Add check against NULL before dereferencing pointer
2023-01-26 00:24:47 -08:00
Allan Shortlidge
76d1565a9d Sema: Lift restriction that declarations with @_backDeploy must have explicit availability.
Resolves rdar://103880356
2023-01-06 08:18:00 -08:00
Valeriy Van
694c99883f Remove AbstractClosureExpr::hasBody() not needed anymore as suggested in review 2023-01-04 12:38:10 +02:00
Hamish Knight
d59ff45468 [Profiler] Track the parent SourceFile for a SILCoverageMap
This tracks the source file that the profiled
entity was defined in.
2022-12-06 14:28:34 +00:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Hamish Knight
663c0b2f82 [Profiler] Fix over-eager assert
We may have a skipped function body when walking
over a SourceFile to profile top-level code,
adjust the assertion so we ignore cases that we
don't want to profile anyway.

rdar://102405053
2022-11-16 16:05:08 +00:00
Hamish Knight
350e28b4b7 [Profiler] Rework profiling of top-level code
Instead of creating and destroying a SILProfiler
per TopLevelCodeDecl, setup a single profiler
for the top-level entry point function, and visit
all the TopLevelCodeDecls when mapping regions.
2022-10-14 17:45:14 +01:00
Hamish Knight
337cbeffa3 [Profiler] Remove canCreateProfilerForAST
Preserve the assertions to make sure functions
and closures have bodies, but the other checks
seem redundant. SILGen already asserts that we
have a type-checked SourceFile, and we already
control what nodes we call `createProfiler` for.
2022-10-14 17:45:14 +01:00
Hamish Knight
b4d4137a5e [Profiler] NFC: Trim out unused imports
Also bump the copyright year.
2022-10-13 19:42:38 +01:00
Hamish Knight
809cfb7bf2 [Profiler] Introduce ProfileCounterRef
For now this just wraps an ASTNode, but in the
future it will allow us to model counters
that cannot simply hang off ASTNodes, e.g
error branch counters.
2022-10-13 19:42:37 +01:00
Hamish Knight
04ef945dbd [Profiler] Remove getProfilerContextForDecl
We can use `getInnermostDeclContext` instead. Also
tighten up the condition of `hasFileBeenTypeChecked`
a bit.
2022-10-10 14:11:55 +01:00
Hamish Knight
89145f39fc [Profiler] NFC: Rename assignCounter -> assignKnownCounter
Make it clearer that we expect an existing counter
to have been assigned by MapRegionCounters.
2022-10-10 14:11:54 +01:00
Hamish Knight
51946b16d5 [Profiler] Use proper SILDeclRef for top-level code
Use a main entry-point instead of a null
SILDeclRef. Eventually we'll want to unify the
emission here such that we visit all the
TopLevelDecls in one shot (and just use a single
profiler), but for now we can just hand the
SILProfiler the expected SILDeclRef.
2022-10-07 13:54:52 +01:00
Hamish Knight
072cdb5e87 [Profiler] Clean up CounterExpr allocation
Use a BumpPtrAllocator instead of a linked list,
and allocate internally within the CounterExpr so
we can just pass about values instead of pointers
and references. This allows us to eliminate
`CounterExpr::Ref`.

Unfortunately this means we can no longer store
CounterExprs in the SourceMappingRegions for
nodes, as we were previously using pointers to
propagate updates the counters made after the
region was popped. Instead we now have to query
the `CounterMap`. But IMO this is a worthwhile
tradeoff for code that's easier to reason about.
2022-10-06 19:25:09 +01:00
Hamish Knight
c9d96bcd01 [Profiler] Assert that we always have function bodies
It seems like this defensive logic was written
back when we could create SILProfilers for
declarations in addition to definitions. Now we
should only ever create SILProfilers for
definitions, so we can assert that we always have
a function body available.
2022-09-29 10:08:18 +01:00
Hamish Knight
5b1ca3f0e0 [SILGen] Avoid forcing function bodies for profiling
This is no longer necessary now that we ensure we
always SILGen functions that contain user code.
2022-09-29 10:08:17 +01:00
Hamish Knight
bd18c37b82 [Profiler] Adopt SILDeclRef::hasUserWrittenCode
This replaces some of the existing checks, and
ensures that we don't emit coverage maps for
property wrapper backing initializers that don't
contain user code.

rdar://99931619
2022-09-29 10:08:15 +01:00
Hamish Knight
bca941b152 [AST] NFC: Rename IfExpr -> TernaryExpr
This matches what we call it in SwiftSyntax, and
is just generally clearer.
2022-09-28 10:33:31 +01:00
Hamish Knight
20830cc2fb [Profiler] Avoid profiling unavailable decls
Such decls don't provide useful coverage info.

rdar://83253091
2022-09-19 10:06:36 +01:00
Hamish Knight
50367ecfa6 [Profiler] Don't walk into property initializer closures
These don't have a parent, but still shouldn't be
walked into. Be sure to check that the SILDeclRef
is for a closure.

rdar://99963912
2022-09-15 17:01:44 +01:00
Hamish Knight
9bb87a5f50 [Profiler] NFC: Invert isUnmapped
IMO it's clearer this way.
2022-09-15 17:01:43 +01:00
Hamish Knight
b7d3f502fe [Profiler] Limit decl walking behavior
Unify the mapped behavior with the unmapped
behavior and only ever walk into a pattern binding
decls, top-level code decls, and non-nested
functions. This avoids walking into e.g nested
types, leading to duplicate coverage.

We don't need to handle the unmapped behavior
separately, as top-level code decls are always
explicit, and non-nested functions are always
checked when we create the profiler.

rdar://99963912
2022-09-15 17:01:43 +01:00
Hamish Knight
30568a60a8 [Profiler] Don't run PGO on unmapped decls
This avoids an unnecessary walk, as we don't have
any interesting profile data for such decls.
2022-09-15 17:01:42 +01:00
Hamish Knight
bf9b4b92fc [Profiler] Only map implicit getters for lazy vars
This is the only implicit getter case we care
about, the rest do not contain user-written code.
This means that we now otherwise only ever map
explicit decls, which we need to SILGen anyway. As
such I believe this should fix rdar://39332957.

We do still potentially map implicit expressions,
but should only do so for explicit property
initializers. There is one remaining bug where we
can map an implicit property wrapper backing
initalizer, which I'm tracking in rdar://99931619.
2022-09-14 20:51:52 +01:00
Hamish Knight
5039c1c3f1 [Profiler] NFC: Remove unnecessary parameter
`SILProfiler::create` is only ever called when
emitting a definition, so this check is redundant.
2022-09-14 20:51:51 +01:00
Hamish Knight
b582dbfa26 [Profiler] Clean up walking code a little
Use the new explicit ASTWalker actions to factor
out the skipping logic, ensuring to apply it in
post-visitation too, which fixes a PCMacro crash
(though I don't believe this is a configuration
that ever actually comes up). Also tighten up
invariants such that an `IfExpr` must appear
within an existing region.
2022-09-13 15:41:36 +01:00
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
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
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
Hamish Knight
caf597212c Merge pull request #60645 from hamishknight/simon-says 2022-08-19 19:58:04 +01: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
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
Vedant Kumar
cfd38c42a1 [Profiler] Assign counter to IfStmt condition
Push a region to cover any expressions within an IfStmt condition.

rdar://67280997
2020-08-18 14:05:24 -07:00
Owen Voorhees
8b4d9ef3eb [Profiler] Don't use presumed line numbers for profiling 2020-05-26 11:19:30 -07:00
Owen Voorhees
45bc578ae5 [SourceManager] Rename line and column APIs for clarity 2020-05-21 12:54:07 -05:00
Saleem Abdulrasool
fa46f7131c sprinkle some llvm_unreachable for MSVC (NFC)
MSVC does not realize that the switch is exhaustive and requires that
the path is explicitly marked as unreachable.  This silences the C4715
warning ("not all control paths return a value").
2020-04-24 18:59:07 -07:00
Doug Gregor
78880ffc1a Merge pull request #27776 from owenv/catch_revamp_take_4
[SE-0276] Support multiple patterns in catch clauses
2020-04-07 12:31:33 -07:00
Owen Voorhees
43e2d107e1 [SE-0276] Implement multi-pattern catch clauses
Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
2020-04-04 09:28:26 -07:00
Slava Pestov
5f51546480 Sema: Fix source range for curry thunks
Fixes <rdar://problem/61117301> / <https://bugs.swift.org/browse/SR-12496>.
2020-04-03 23:26:04 -04:00