Commit Graph

56 Commits

Author SHA1 Message Date
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
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
Erik Eckstein
7709a9a043 don't verify the SIL module during de-serialization
The module doesn't necessarily need to be in a valid state when de-serializing a function
2023-05-11 08:03:19 +02:00
Michael Gottesman
c54acc83e2 [move-only] Add the ability to specify a deinit at the SIL level for a move only type.
Specifically, we get an additional table like thing called sil_moveonlydeinit. It looks as follows:

sil_moveonlydeinit TYPE {
  @FUNC_NAME
}

It always has a single entry.
2022-09-20 15:19:31 -07:00
Erik Eckstein
6a020f8f15 Stabilize and simplify SIL linkage and serialization
The main point of this change is to make sure that a shared function always has a body: both, in the optimizer pipeline and in the swiftmodule file.
This is important because the compiler always needs to emit code for a shared function. Shared functions cannot be referenced from outside the module.
In several corner cases we missed to maintain this invariant which resulted in unresolved-symbol linker errors.

As side-effect of this change we can drop the shared_external SIL linkage and the IsSerializable flag, which simplifies the serialization and linkage concept.
2022-03-09 15:28:05 +01:00
Michael Gottesman
02784b3cb2 Merge pull request #36025 from gottesmm/pr-36fe6ec4772e3d5e09f5031a505217e7d7aba8dc
[ownership] On non-Darwin platforms, serialize the stdlib in OSSA form.
2021-02-17 19:48:25 -08:00
Richard Wei
e494df2ee6 [AutoDiff] Add differentiability kind to differentiability witnesses and mangle them.
Differentiability witnesses are now keyed by the original function name, the differentiability kind, and the autodiff config.

Updated SIL syntax:
```
differentiability-kind ::= 'forward' | 'reverse' | 'normal' | 'linear'
sil-differentiability-witness ::=
    'sil_differentiability_witness'
    sil-linkage?
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
    sil-differentiability-witness-body?
sil-instruction ::=
    'differentiability_witness_function'
    '[' sil-differentiability-witness-function-kind ']'
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
```
```console
sil_differentiability_witness [reverse] [parameters 0 1] [results 0] <T where T: Differentiable> @foo : <T> $(T) -> T
differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] <T where T: Differentiable> @foo : $(T) -> T
```

New mangling:
```swift
  global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness
```
```console
$s13test_mangling3fooyS2f_S2ftFWJrSpSr ---> reverse differentiability witness for test_mangling.foo(Swift.Float, Swift.Float, Swift.Float) -> Swift.Float with respect to parameters {0} and results {0}
```

Resolves rdar://74380324.
2021-02-17 18:27:42 -05:00
Michael Gottesman
891bd00f66 [sil-deserialization] Implement cache invalidation for all serialized SIL entities and use it to properly invalidate global variables/witness tables when we delete them.
Otherwise, one runs into memory corruption. I ran into this while enabling ossa
on the stdlib for non-Darwin platforms.

Hopefully we do not regress on this again when someone adds more optzns that
eliminate these since I added a big NOTE to warn people to do it and implemented
support even for the entities we do not support deleting at the SIL
level... yet.
2021-02-17 13:15:57 -08:00
Hamish Knight
b3dcef9796 Avoid unnecessary linking in SIL tools
Previously we were linking in all SIL entities
if the input was a serialized non-SIB AST, and
`-disable-sil-linking` wasn't specified. However
none of the tests appear to want this behaviour.

Stop calling `SerializedSILLoader::getAll`, and
remove the `-disable-sil-linking` option, as this
is now the default behaviour.
2020-07-01 23:14:01 -07:00
Robert Widmann
057097a6c3 [NFC] Hide LoadedModules From Clients of ASTContext
There's no reason clients need to be able to access this data directly.
It obscures where module loading is actually happening, and makes it too
easy to accidentally register a module with the wrong identifier in the
context.

Hide the registration operations behind opaque accessors.
2020-06-16 15:09:19 -07:00
Dan Zheng
96026575c4 [AutoDiff upstream] Upstream serialization changes. (#30720)
Upstream random serialization code from tensorflow branch:
- `SerializedSILLoader::getAllDifferentiabilityWitnesses()`
- Add diff. witness serialization for functions with `inout` parameters.
- `deserializeSILFunctionType`: fix assertion for differentiability kind.
2020-03-30 12:32:16 -07:00
Dan Zheng
7b064a6500 [AutoDiff upstream] Add SIL differentiability witness serialization. (#29642)
SIL differentiability witnesses are a new top-level SIL construct mapping
an "original" SIL function and derivative configuration to derivative SIL
functions.

This patch adds `SILDifferentiabilityWitness` serialization/deserialization.

Resolves TF-1136.
2020-02-07 09:35:16 -08:00
Erik Eckstein
8bc5daca77 SIL: always update the linkage of a function with the serialized linkage in the swiftmodule.
The cross-module-optimization can change the linkage of a function to public. Then the SILLinkage is "out of sync" with the linkage derived from the AST. We need to make sure to read the correct SILLinkage from the module file.
2019-12-04 09:16:28 +01:00
Jordan Rose
3c2d709996 [Serialization] Move a bunch of internal headers to lib/ (#27012)
This will discourage their use outside of Serialization, which is a
good thing for encapsulation.
2019-09-04 08:58:06 -07:00
Erik Eckstein
c957c50e28 Serialization: use the mangled class name for serializing vtables.
To distinguish between classes which have the same name (but are in different contexts).
Fixes a miscompile if classes with the same name are used from a different module.

SR-10634
rdar://problem/50538534
2019-05-11 08:30:15 -07:00
Michael Gottesman
872bf40e17 [sil-optimizer] Centralize how we send out serialization notifications.
Previously SILModule contained two different pathways for the deserializer to
send notifications that it had created functions:

1. A list of function pointers that were called when a function's body was
deserialized. This was added recently so that access enforcement elimination is
run on newly deserialized SIL code if we have already eliminated access
enforcement from the module.

2. SILModule::SerializationCallback. This is an implementation of the full
callback interface and is used by the SILModule to update linkage and other
sorts of book keeping.

To fix the pass manager notification infrastructure, I need to be able to send
notifications to a SILPassManager when deserializing. I also need to be able to
eliminate these callbacks when a SILPassManager is destroyed. These requirements
are incompatible with the current two implementations since: (2) is an
implementation detail of SILModule and (1) only notifies on function bodies
being deserialized instead of the creation of new declarations (what the caller
analysis wants).

Rather than adding a third group of callbacks, this commit refactors the
infrastructure in such a way that all of these use cases can use one
implementation. This is done by:

1. Lifting the interface of SerializedSILLoader::Callback into a base
notification protocol for deserialization called
DeserializationNotificationHandlerBase and its base no-op implementation into an
implementation of the aforementioned protocol:
DeserializationNotificationHandler.

2. Changing SILModule::SerializationCallback to implement
DeserializationNotificationHandler.

3. Creating a class called FunctionBodyDeserializationNotificationHandler that
takes in a function pointer and uses that to just override the
didDeserializeFunctionBody. This eliminates the need for the specific function
body deserialization list.

4. Replacing the state associated with the two other pathways with a single
DeserializationNotificationHandlerSet class that contains a set of
DeserializationNotificationHandler and chains notifications to them. This set
implements DeserializationNotificationHandlerBase so we know that its
implementation will always be in sync with DeserializationNotificationHandler.

rdar://42301529
2018-08-15 15:49:15 -07:00
Bob Wilson
8e330ee344 NFC: Fix indentation around the newly renamed LLVM_DEBUG macro.
Jordan used a sed command to rename DEBUG to LLVM_DEBUG. That caused some
lines to wrap and messed up indentiation for multi-line arguments.
2018-07-21 00:56:18 -07:00
Jordan Rose
cefb0b62ba Replace old DEBUG macro with new LLVM_DEBUG
...using a sed command provided by Vedant:

$ find . -name \*.cpp -print -exec sed -i "" -E "s/ DEBUG\(/ LLVM_DEBUG(/g" {} \;
2018-07-20 14:37:26 -07:00
Joe Groff
d365c153d4 SIL: Introduce sil_property declarations for property descriptors.
This provides SILGen a place to generate the key path component information for an exported property so that it can be linked to from other modules.
2018-02-23 14:57:45 -08:00
Roman Levenstein
fc7b9e1081 [sil-linker] Minor clean-ups of function lookup code in SILModule and SIL linker. NFC.
Stop using SILLinkage::Private as a flag for "linkage doesn't matter". Use Optional instead.
2017-02-14 08:15:51 -08:00
Jordan Rose
1c60910198 Revert "Merge pull request #6092 from swiftix/wip-generics-inlining-flag-4"
This reverts commit 1b3d29a163, reversing
changes made to b32424953e.

We're seeing a handful of issues from turning on inlining of generics,
so I'm reverting to unblock the bots.
2017-02-13 10:52:17 -08:00
Roman Levenstein
199e76d6d4 Fixes for a lookup of functions by name
Among other things it fixes a bug in the function signature optimization where it would use an already existing mangled name for a different function
2017-02-10 18:07:30 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Erik Eckstein
a01e9d4e41 Remove dead code in SIL linker
... which is not only dead but also broken.
NFC
2016-11-02 10:30:10 -07:00
Roman Levenstein
9234eb3ca7 [sil-linker] Improve the implementation of the hasSILFunction API
It it now possible to check if a function with a given name and a given linkage exists in one of the modules,
even if the current module contains a function with this name but a difference linkage.
This is useful e.g. for performing a lookup of pre-specializations.
2016-04-13 15:46:57 -07:00
Slava Pestov
116ac3d1f2 SIL Serialization: Serialize default witness tables
This will be used by SILGen to look up default witnesses for
requirements of protocols in other modules.
2016-03-17 10:39:34 -07:00
Roman Levenstein
94c3ae4c3c Add APIs to check if a function with a given name exists and to invalidate a SIL linker entry for a function.
These APIs are useful e.g. for quickly finding pre-specialisations by their names.
The existence check is very light-weight and does not try to deserialize bodies of SIL functions.
2016-02-26 22:10:59 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Roman Levenstein
f69c17568b Add the ability to lookup/link a SILFunction in the SILModule by its mangled name.
This feature is required for the implementation of pre-specialization, because one needs to check if a specialized SIL function with a given name exists in the standard library.

Swift SVN r30307
2015-07-17 06:52:04 +00:00
Luqman Aden
f7403e38c0 SILGen shouldn't know about ModuleFile, use FileUnit instead.
Swift SVN r26577
2015-03-26 03:33:41 +00:00
Luqman Aden
3f83973928 Handle multiple sib inputs.
Swift SVN r26572
2015-03-26 01:21:21 +00:00
Roman Levenstein
517992a51f [sil-linker] Use SILDeclRef instead of StringRef in SerializedSILLoader.
Addresses Michael's comments. And really uses the mangled name.

Swift SVN r26268
2015-03-18 17:45:06 +00:00
Roman Levenstein
1f875b9bcb Extend SILModule with an API for linking of SILFunctions by their SILDeclRef.
Before, providing a full SILFunction declaration object with a proper SILType was the only way to link a function. And constructing such a SILFunction declaration by hand using low-level SIL APIs is very annoying and requires a lot of code to be written. This new linkFunction API allows for a lookup using SILDeclRef and essentially performs linking of a SILFunction by its mangled name (assuming this name is unique), which is much easier to invoke. The new API is useful, e.g. when you need to link a well-known function from a standard library.

Swift SVN r26252
2015-03-18 06:11:22 +00:00
Luqman Aden
34b74cfa1c [driver] Add -emit-sib primary action.
Swift SVN r25783
2015-03-05 19:15:07 +00:00
Jordan Rose
dbd3b60f6b [Serialization] Move (Module)Status and validateSerializedAST into a namespace.
Also into a separate file.

Before (swift/Serialization/SerializedModuleLoader.h):
  ModuleStatus
  SerializedModuleLoader::ValidationInfo
  SerializedModuleLoader::ExtendedValidationInfo
  SerializedModuleLoader::isSerializedAST
  SerializedModuleLoader::validateSerializedAST

After (swift/Serialization/Validation.h):
  serialization::Status
  serialization::ValidationInfo
  serialization::ExtendedValidationInfo
  serialization::isSerializedAST
  serialization::validateSerializedAST

No functionality change, just a lot of renaming and a bit of reorganizing.

Swift SVN r25226
2015-02-12 05:32:25 +00:00
Erik Eckstein
daeed72153 Simplify and chose a better name for invalidating the Deserializer's function cache.
Swift SVN r22838
2014-10-20 12:51:56 +00:00
Jordan Rose
4eebcb9853 Change ASTContext's LoadedModules map to be keyed by Identifiers, not strings.
No intended functionality change, but there's no reason to be performing
string lookups here.

Swift SVN r20902
2014-08-01 18:03:47 +00:00
Manman Ren
b3e72be9d9 Remove unused deserialized SILFunctions.
The deserializer holds a reference to the deserialized SILFunction, which
prevents Dead Function Elimination from erasing them. 

We have a tradeoff on how often we should clean up the unused deserialized
SILFunctions. If we clean up at every optimization iteration, we may
end up deserializing the same SILFunction multiple times. For now, we clean
up only after we are done with the optimization iteration.

rdar://17046033


Swift SVN r18697
2014-06-04 00:30:34 +00:00
Manman Ren
8d7236f6a7 [SILSerializer] keep looking for a SILFunction when we find a declaration.
It is reasonable that one module has a declaration for a SILFunction while
another module has the definition. We return from lookupSILFunction when
we find a definition and keep looking when a declaration is found from
a module.
 
rdar://16815627


Swift SVN r17586
2014-05-06 23:47:20 +00:00
Michael Gottesman
f4bb049bfc [deserialization] Enable lazy deserialization of witness tables.
This patch adds in the necessary infrastructure for lazily deserializing
witness tables. This is done by following the same approach as the
deserialization/serialization of SILFunction.

Now if one calls SILModule::lookUpWitnessTable and the given witness table is a
definition, the SILModule will attempt to deserialize it from one of the other
modules.

Swift SVN r15403
2014-03-24 09:16:09 +00:00
Michael Gottesman
e3081a2027 [deserialization] Add debug logging to SerializedSILLoader so one can
easily tell the order that deserializers are being visited.

Swift SVN r15067
2014-03-14 20:27:20 +00:00
Dmitri Hrybenko
30ab76d3c3 Remove unused private member
Swift SVN r14207
2014-02-21 15:22:15 +00:00
Michael Gottesman
ec52d16f57 Teach SerializedSILLoader how to load all SILFunctions. Also add SerializedSILLoader::getAll().
getAll deserializes all SIL (except for globals). This enables us to iterate
over the SILModules once instead of once for first SILFunctions, then VTables,
then WitnessTables which is just inefficient.

Swift SVN r14176
2014-02-20 22:22:20 +00:00
Michael Gottesman
9311672c33 Move ModuleFile.h, ModuleFormat.h, and DeclTypeRecordNodes.def to include/swift/Serialization.
This allows for a secondary tool to access the deserialized Module inside the SerializedASTFile's ModuleFile.

Swift SVN r14173
2014-02-20 22:03:55 +00:00
Michael Gottesman
5fbb110de7 When ever we deserialize SIL, verify it.
Swift SVN r13292
2014-02-01 05:51:20 +00:00
Manman Ren
eaad8df51a SILSerializer: handle SILWitnessTable.
We add two records in sil_block to specify a witness table record and a method
entry record. Out of the four entry types, only "Method" is handled in this
commit.

Two records are also added to sil_index_block to search for a specific witness
table given a unique identifier. The interface lookupWitnessTable is not
implemented yet.

Right now, we serialize a witness table only when sil-serialize-all is on and
deserialize all witness tables in the module when sil-link-all is on.

rdar://15722175


Swift SVN r13000
2014-01-27 19:30:43 +00:00
John McCall
5da6defa1f Clean up the linkage model and the computation of linkage.
In general, this forces SILGen and IRGen code that's grabbing
a declaration to state whether it's doing so to define it.

Change SIL serialization to serialize the linkage of functions
and global variables, which means also serializing declarations.

Change the deserializer to use this stored linkage, even when
only deserializing a declaration, and to call a callback to
inform the client that it has deserialized a new entity.

Take advantage of that callback in the linking pass to alter
the deserialized linkage as appropriate for the fact that we
imported the declaration.  This computation should really take
advantage of the relationship between modules, but currently
it does not.

Swift SVN r12090
2014-01-09 08:58:07 +00:00