Commit Graph

270 Commits

Author SHA1 Message Date
swift-ci
e0e2634b84 Merge remote-tracking branch 'origin/main' into rebranch 2021-07-06 09:35:07 -07:00
hiro
923fba78e1 Fixed typos (#38247)
* fix: typo occured -> occurred

* fix: typo occured -> occurred

* fix: typo depenedency -> dependency
2021-07-03 14:56:44 -03:00
Evan Wilde
e316724243 Fix reference to in_place_t
in_place_t was pulled out of optional_detail and moved up to the llvm
namespace.
2021-06-07 11:52:40 -07:00
Alex Hoppen
294977534c [libSyntax] Remove incremental JSON transfer option
We were only keeping track of `RawSyntax` node IDs to incrementally transfer a syntax tree via JSON. However, AFAICT the incremental JSON transfer option has been superceeded by `SyntaxParseActions`, which are more efficient.

So, let’s clean up and remove the `RawSyntax` node ID and JSON incremental transfer option.

In places that still need a notion of `RawSyntax` identity (like determining the reused syntax regions), use the `RawSyntax`’s pointer instead of the manually created ID.

In `incr_transfer_round_trip.py` always use the code path that uses the `SyntaxParseActions` and remove the transitional code that was still using the incremental JSON transfer but was never called.
2021-04-07 10:01:34 +02:00
Alex Hoppen
d0cb7ad624 [libSyntax] Eliminate loop in RawSyntax constructor
Currently, when creating a `RawSyntax` layout node, the `RawSyntax` constructor needs to iterate over all child nodes to
a) sum up their sub node count
b) add their arena as a child arena of the new node's arena

But we are already iterating over all child nodes in every place that calls these constructors. So instead of looping twice, we can perform the above operations in the loop that already exists and pass the parameters to the `RawSyntax` constructor, which spees up `RawSyntax` node creation.

To ensure the integrity of the `RawSyntax` tree, the passed in values are still validated in release builds.
2021-03-26 18:30:46 +01:00
Alex Hoppen
e1e0f544d4 Merge pull request #36350 from ahoppen/pr/syntaxref
[libSyntax] Create SyntaxRef, which uses SyntaxDataRef internally
2021-03-12 08:17:34 +01:00
Alex Hoppen
3fb5dd1c68 [libSyntax] Create SyntaxRef, which uses SyntaxDataRef internally
Now that we have a fast SyntaxDataRef, create a corresponding SyntaxRef hierarchy. In contrast to the Syntax, SyntaxRef does *not* own the backing SyntaxDataRef, but merely has a pointer to the SyntaxDataRef.
In addition to the requirements imposed by SyntaxDataRef, the user of the SyntaxRef hierarchy needs to make sure that the backing SyntaxDataRef of a SyntaxRef node stays alive. While this sounds like a lot of requirements, it has performance advantages:
 - Passing a SyntaxRef node around is just passing a pointer around.
 - When casting a SyntaxRef node, we only need to create a new SyntaxRef (aka. pointer) that points to the same underlying SyntaxDataRef - there's no need to duplicate the SyntaxDataRef.
 - As SyntaxDataRef is not ref-counted, there's no ref-counting overhead involved.

Furthermore, the requirements are typically fulfilled. The getChild methods on SyntaxRef return an OwnedSyntaxRef, which stores the SyntaxDataRef. As long as this variable is stored somewhere on the stack, the corresponding SyntaxRef can safely be used for the duration of the stack frame. Even calls like the following are possible, because OwnedSyntaxRef returned by getChild stays alive for the duration of the entire statement.
```
useSyntaxRef(mySyntaxRef.getChild(0).getRef())
```
2021-03-11 19:34:49 +01:00
Alex Hoppen
ab7c51b9e2 [libSyntax] Improve data structure in RawSyntax
It turns out that the bitpacked Commons struct is actually fairly expensive because the CPU needs to apply bitmasks to fetch the IsToken and Presence flag. We've got padding space available, so we might as well properly align these boolean flags.

Also on a source level, replace a couple of bit-restricted unsigned fields by their representing type (e.g. SyntaxKind).

Finally, we can pull out the common bits to RawSyntax and have the Bits union only contain the token- or layout-specific fields.
This also allows us to initialise these fields in the constructor's initialiser list (instead of in the initialiser body).

Lastly, change copyToArenaIfNecessary to work on a char *& and length, which allows us to initialise leading/trailing trivia/token text in the initialiser list and adjust if necessary later.
2021-03-11 07:56:47 +01:00
Alex Hoppen
06bce44cf6 [libSyntax] Add validate method to all syntax nodes
For syntax nodes that previously didn’t have a `validate` method, the newly added `validate` method is a no-op. This will make validation easier in upcoming generic code.
2021-03-09 11:16:50 +01:00
Alex Hoppen
c5fd7eb594 [libSyntax] Add getChild accessors for SyntaxDataRef 2021-03-09 10:37:05 +01:00
Alex Hoppen
aa74b1bbe0 Merge pull request #36313 from ahoppen/pr/custom-opt-storage
[libSyntax] Hide null AbsoluteRawSyntax behind a custom OptionalStorage implementation
2021-03-09 09:44:20 +01:00
Alex Hoppen
107add7fd7 Merge pull request #36278 from ahoppen/pr/inline-rawsyntax-methods
[libSyntax] Inline commonly called methods in RawSyntax and AbsoluteRawSyntax
2021-03-08 10:21:40 +01:00
Alex Hoppen
da8357157d [libSyntax] Hide null AbsoluteRawSyntax behind a custom OptionalStorage implementation
This splits the previous null AbsoluteRawSyntax type into two categories
and removes the public null initializer and isNull method.

1. The default initializer of AbsoluteRawSyntax now create uninitialized
   memory. This is exactly what we need since we just need to allocate
   the memory to initialise  it using the SyntaxDataRef::getChild method
   wherever we use it.
2. Make Optional<AbsoluteRawSyntax> and Optional<SyntaxDataRef> zero-cost
   wrappers around their underlying type. These use the old null type
   to indicate a missing optional value.

Overall, I believe this makes the code both safer (we now enforce null
types properly in the type system) and potentially faster (although I
haven't been able to measure an improvement)
2021-03-05 16:59:54 +01:00
Alex Hoppen
c6a04392fa [libSyntax] Model IsRef in SyntaxDataRef by a a virtual isRef method 2021-03-05 16:59:54 +01:00
Alex Hoppen
f284529bd3 [libSyntax] Add a unsafe but fast SyntaxDataRef version of SyntaxData
In contrast to SyntaxData, SyntaxDataRef is not memory-safe, but
designed to be fast. In particular, the following guarantees from
SyntaxData are being dropped:
 - SyntaxDataRef does not retain the SyntaxArena containing its
   RawSyntax. The user of SyntaxDataRef has to provide that guarantee.
   However, that's usually pretty easily done by just retaining the
   SyntaxArena of the tree's root node.
 - The parent of a SyntaxDataRef must outlive the child node. This is
   the more tricky constraint, but if a tree is just walked top to
   bottom with nodes stored on the stack, this is given by the way the
   stack is being unrolled.
2021-03-05 16:59:54 +01:00
Alex Hoppen
bde8224f73 Merge pull request #36230 from ahoppen/pr/ref-count-syntaxdata
[libSyntax] Reference count SyntaxData
2021-03-05 14:31:49 +01:00
Alex Hoppen
3fa3e6cf82 [libSyntax] Make copyToArenaIfNecessary a method on SyntaxArena 2021-03-05 14:26:33 +01:00
Alex Hoppen
95acf4d959 [libSyntax] Inline commonly called methods in RawSyntax and AbsoluteRawSyntax
These methods are super small and setting up the stack frame etc. takes
up the majority (or at least a significant amount) of their execution
time. So let's inline them.
2021-03-04 10:48:41 +01:00
Alex Hoppen
73cece2803 [libSyntax] Make SyntaxFactory methods instance methods
This saves us from passing in the SyntaxArena every time a new node is
created.
2021-03-03 11:02:55 +01:00
Alex Hoppen
56a923475f [libSyntax] Reference count SyntaxData
Instead of having a heap-allocated RefCountedBox to store a SyntaxData's
parent, reference-count SyntaxData itself. This has a couple of
advantages:
 - When passing SyntaxData around, only a pointer needs to be passed
   instead of the entire struct contents. This is faster.
 - We can later introduce a SyntaxDataRef, which behaves similar to
   SyntaxData, but delegates the responsibility that the parent stays
   alive to the user. While sacrificing guaranteed memory safety, this
   means that SyntaxData can then be stack-allocated without any
   ref-counting overhead.
2021-03-03 08:48:24 +01:00
Alex Hoppen
68877f987f Merge pull request #36229 from ahoppen/pr/opaque-const-pointer-and-delete-discarednode
[libSyntax] Miscellaneous minor improvements
2021-03-03 08:36:47 +01:00
Alex Hoppen
4bda6c1311 [libSyntax] Assert there are no reference-cycles in the SyntaxArenas 2021-03-02 22:42:45 +01:00
Alex Hoppen
0deff394bd [libSyntax] Fix 80+ columns issues in SyntaxCollection.h 2021-03-02 20:07:26 +01:00
Alex Hoppen
28f5f79bb7 [libSyntax] Don't reference count RawSyntax
Instead, only reference count the SyntaxArena that the RawSyntax nodes
live in. The user of RawSyntax nodes must guarantee that the SyntaxArena
stays alive as long as the RawSyntax nodes are being accessed.

During parse time, the SyntaxTreeCreator holds on to the SyntaxArena
in which it creates RawSyntax nodes. When inspecting a syntax tree,
the root SyntaxData node keeps the SyntaxArena alive. The change should
be mostly invisible to the users of the public libSyntax API.

This change significantly decreases the overall reference-counting
overhead. Since we were not able to free individual RawSyntax nodes
anyway, performing the reference-counting on the level of the
SyntaxArena feels natural.
2021-03-01 09:43:54 +01:00
Alex Hoppen
c1d65de89c [libSyntax] Optimise layout of RawSyntax to be more space efficient
This decreases the size of RawSyntax nodes from 88 to 64 bytes by
- Avoiding some padding by moving RefCount further up
- Limiting the length of tokens and their trivia to 32 bits. We would
  hit this limit with files >4GB but we also hit this limit at other
  places like the TextLength property in the Common bits.
2021-02-10 09:50:12 +01:00
Alex Hoppen
e43bad2c71 [libSyntax] Store the token's text in the SyntaxArena
Do the same thing that we are already doing for trivia: Since RawSyntax
nodes always live inside a SyntaxArena, we don't need to tail-allocate
an OwnedString to store the token's text. Instead we can just copy it
to the SyntaxArena. If we copy the entire source buffer to the syntax
arena at the start of parsing, this means that no more copies are
required later on. Plus we also avoid ref-counting the OwnedString which
should also increase performance.
2021-02-10 09:50:12 +01:00
Alex Hoppen
b6746f008f [libSyntax] Improve performance for commonly requested SyntaxArena.containsPointer calls
In practice SyntaxArena.containsPointer is almost always called with a
pointer from the SyntaxArena's source buffer. To avoid walking through
all of the bump allocator's slabs until we find the one containing the
source buffer, add a hot use memory region (which lives inside the bump
allocator) that is checked first before consulting the bump allocator.
2021-02-05 08:15:54 +01:00
Alex Hoppen
5637c25168 [libSyntax] Always copy leading and trailing trivia strings into a SyntaxArena buffer
Referencing a string in arbitrary memory is not safe since the source
buffer to which it points may have been freed. Instead copy all strings
into the SyntaxArena. Since RawSyntax nodes retain their arena, they can
be sure that the string won't disappear if it lives in their arena.

To avoid lots of small copies, we copy the entire source buffer once
into the syntax arena and make StringRefs point into that buffer.
2021-02-05 08:15:54 +01:00
Alex Hoppen
5e1ba8b16e [libSyntax] Store raw trivia inside RawSyntax and only lex into pieces when requested 2021-02-05 08:15:54 +01:00
Alex Hoppen
803499e165 [libSyntax] Require RawSyntax to always live inside a SyntaxArena
This way, we will later be able to store additional information about
the node inside the same arena with a guarantee that they will always be
alive as long as the node is alive.

These additional information will include
a) the token's text (which can be a StringRef into a copy of the source
   code that lives inside the SyntaxArena)
b) the token's unparsed trivia, which can be decomposed into pieces when
   needed.
2021-02-01 10:34:44 +01:00
Alex Hoppen
8a6b8b8195 [libSyntax] Fix test failures in SwiftSyntaxTests 2021-01-29 13:08:12 +01:00
Alex Hoppen
65f3d47c2d [libSyntax] Pass RC<RawSyntax> by reference whenever possible 2021-01-29 13:08:12 +01:00
Alex Hoppen
a356c89e92 [libSyntax] Don't reference count SyntaxData
Instead, reference count the SyntaxData's parent. This has a couple of
advantages:
1. We eliminate a const_cast that was potentially unsafe
2. It more closely resembles the architecture on the Swift side
3. It has the potential to be optimised further if the parent can be
   accessed in an unsafe, non-reference-counted way
2021-01-29 13:08:12 +01:00
Alex Hoppen
8bb1167e21 [libSyntax] Restructure RawSyntax to more closely resemble the SwiftSyntax implementation 2021-01-29 13:08:12 +01:00
Alex Hoppen
d36fae5840 Merge pull request #35446 from ahoppen/delete-syntaxastmap
Remove SyntaxASTMap
2021-01-18 10:33:24 +01:00
Alex Hoppen
5bf93d860b Remove SyntaxASTMap
As far as I can tell, this hasn't ever been really used, it's certainly
not used anymore.
2021-01-15 11:08:59 +01:00
Alex Hoppen
bfe0a00551 Merge pull request #35429 from ahoppen/remove-bytetree
Remove ByteTree serialization format
2021-01-15 09:28:19 +01:00
Alex Hoppen
8ec8516893 Remove ByteTree serialization format
It was originally designed for faster trasmission of syntax trees from
C++ to SwiftSyntax, but superceded by the CLibParseActions. There's no
deserializer for it anymore, so let's just remove it.
2021-01-14 20:37:49 +01:00
Robert Widmann
1119dfbcda Merge pull request #34622 from gevorgyana/main
Make the compiler understand what StringRef is.
2021-01-13 21:56:58 -08:00
Alex Hoppen
ce95a722e5 [syntax-parse] Make the child getters of SyntaxNodes const 2020-12-16 12:33:53 +01:00
Artyom Gevorgyan
03a6bc7bed Prepare the header for stand-alone usage. 2020-11-13 17:17:57 +02:00
Varun Gandhi
ea92df04e1 [NFC] Remove redundant includes for <vector>. 2020-05-31 13:05:02 -07:00
Dan Zheng
849bd62a26 [AutoDiff upstream] Add SIL differentiability witnesses. (#29623)
SIL differentiability witnesses are a new top-level SIL construct mapping
"original" SIL functions to derivative SIL functions.

SIL differentiability witnesses have the following components:
- "Original" `SILFunction`.
- SIL linkage.
- Differentiability parameter indices (`IndexSubset`).
- Differentiability result indices (`IndexSubset`).
- Derivative `GenericSignature` representing differentiability generic
  requirements (optional).
- JVP derivative `SILFunction` (optional).
- VJP derivative `SILFunction` (optional).
- "Is serialized?" bit.

This patch adds the `SILDifferentiabilityWitness` data structure, with
documentation, parsing, and printing.

Resolves TF-911.

Todos:
- TF-1136: upstream `SILDifferentiabilityWitness` serialization.
- TF-1137: upstream `SILDifferentiabilityWitness` verification.
- TF-1138: upstream `SILDifferentiabilityWitness` SILGen from
  `@differentiable` and `@derivative` attributes.
- TF-20: robust mangling for `SILDifferentiabilityWitness` names.
2020-02-04 12:53:27 -08:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Rintaro Ishizaki
2ec16f0600 [Syntax] Make getRaw() return const reference
This saves reference-counting operations.
2019-10-14 15:37:43 -07:00
Rintaro Ishizaki
570ed9361f Revert "Merge pull request #26883 from rintaro/revert-revert-26478-gsoc-2019-parser-types"
This reverts commit faaa3a859d, reversing
changes made to 62f947d6ba.
2019-10-14 15:18:05 -07:00
Rintaro Ishizaki
601c90d3cf Revert "Merge pull request #27024 from rintaro/syntaxparse-endloc-composition"
This reverts commit 99c65211e6, reversing
changes made to 3ddfcae24b.
2019-10-14 13:43:22 -07:00
Rintaro Ishizaki
8768832f24 Revert "Merge pull request #27281 from rintaro/reapply-syntaxparse-genericparam"
This reverts commit 5d3e8d6c83, reversing
changes made to 27e881d97e.
2019-10-14 12:46:31 -07:00
Rintaro Ishizaki
0165b4f472 Revert "Merge pull request #27291 from rintaro/syntaxparses-astgen-signature"
This reverts commit e315b6ff6a, reversing
changes made to 079a1a4048.
2019-10-14 12:28:30 -07:00
Rintaro Ishizaki
bf5aa0a5a1 Revert "Merge pull request #27325 from rintaro/syntaxparse-cctype"
This reverts commit 439b9111b7, reversing
changes made to 4e476ff243.
2019-10-14 12:20:57 -07:00