Commit Graph

1350 Commits

Author SHA1 Message Date
Joe Groff
24336465c0 Add debug output for vtable layout 2020-05-29 14:30:02 -07:00
Nate Chandler
bdef1cef23 [metadata prespecialization] Support for classes.
When generic metadata for a class is requested in the same module where
the class is defined, rather than a call to the generic metadata
accessor or to a variant of typeForMangledNode, a call to a new
accessor--a canonical specialized generic metadata accessor--is emitted.
The new function is defined schematically as follows:

    MetadataResponse `canonical specialized metadata accessor for C<K>`(MetadataRequest request) {
      (void)`canonical specialized metadata accessor for superclass(C<K>)`(::Complete)
      (void)`canonical specialized metadata accessor for generic_argument_class(C<K>, 1)`(::Complete)
      ...
      (void)`canonical specialized metadata accessor for generic_argument_class(C<K>, count)`(::Complete)
      auto *metadata = objc_opt_self(`canonical specialized metadata for C<K>`);
      return {metadata, MetadataState::Complete};
    }

where generic_argument_class(C<K>, N) denotes the Nth generic argument
which is both (1) itself a specialized generic type and is also (2) a
class.  These calls to the specialized metadata accessors for these
related types ensure that all generic class types are registered with
the Objective-C runtime.

To enable these new canonical specialized generic metadata accessors,
metadata for generic classes is prespecialized as needed. So are the
metaclasses and the corresponding rodata.

Previously, the lazy objc naming hook was registered during process
execution when the first generic class metadata was instantiated. Since
that instantiation may occur "before process launch" (i.e. if the
generic metadata is prespecialized), the lazy naming hook is now
installed at process launch.
2020-05-29 13:20:33 -07:00
Arnold Schwaighofer
20f4ef93de IRGen: Default to clang's frame pointer elimination settings
Clang provides options to override that default value.
These options are accessible via the -Xcc flag.

Some Swift functions explicitly disable the frame pointer.

The clang options will not override those.
2020-05-28 12:21:42 -07:00
Slava Pestov
8e35609df2 IRGen: Sign the class stub initialization callback pointer on arm64e
The Objective-C runtime expects a signed pointer here. The existing test
would have caught this, except it was always disabled because the
symbol name passed to the dlsym() check should not have had the leading
'_'.

Fixes <rdar://problem/57679510>.
2020-05-01 21:55:11 -04:00
nate-chandler
c9912c5964 Merge pull request #31056 from nate-chandler/generic-metadata-prespecialization-components/rdar61465515
[metadata prespecialization] Zero trailing flags field.
2020-04-23 20:39:42 -07:00
Nate Chandler
2b50150e61 [metadata prespecialization] Zero trailing flags.
Previously, the trailing flags field of runtime instantiated generic
metadata for types which had prespecialization enabled were not zeroed.
Consequently, the field always contained garbage.  Often, metadata was
instantiated on new (and so, zeroed) pages, so the garbage happened to
be zero as is appropriate.  However, when the metadata was instantiated
on pages which had previously been dirtied, the garbage value would
sometimes indicate that the metadata was canonical statically
specialized.  When that occurred, swift_checkMetadataState would
incorrectly return a metadata state of complete, despite the fact that
the metadata might not in fact be complete.  As a result, the runtime
was trafficking in incomplete metadata as if it were complete, resulting
in various crashes that would arise from for example missing a witness
table or a value witness table.

Here the problem is corrected.  The trailing flags field of structs and
enums that have the field is set to 0.

For structs, this is accomplished by modifying the extra data pattern to
exist not only when there is fixed type info for the type but also when
the type is being prespecialized.  Specifically, the extra data for a
struct is, rather than an i32 array of field offsets, a struct
consisting of none, one, or both of the following: (1) the array of
field offsets, (2) the trailing flags.

Similarly, enums now have an extra data pattern which consists of none,
one, or both of the following: (1) the payload size, (2) the trailing
flags.  Enum metadata extra data setting was previously achieved by
customizing the metadata allocation function; that customization is now
eliminated, being replaced with the shared runtime code for copying
extra data into place, a modest code size savings.

rdar://problem/61465515
2020-04-23 18:18:40 -07:00
Robert Widmann
40d9cd8d3f [NFC] Give IRGenModule Exclusive Ownership of an LLVMContext Object 2020-04-16 11:57:44 -07:00
Nate Chandler
47943f8304 [Runtime] Fixed zeroing of extra data padding for values.
See also bcc309efa1 .
2020-04-15 17:22:26 -07:00
Artem Chikin
1b11e8798c [IRGen] Skip witness table query for protocols that do not require one
Some protocols, such as protocols marked with 'objc', do not have a Witness Table.
The code before this patch assumes all protocols do have a Witness Table when generating the layout of an OpaqueTypeDescriptor, causing an assert to be triggered for opaque return types that conform to an 'objc' protocol.

Fixes SR-12257 / rdar://problem/59740179
2020-03-31 10:56:20 -07:00
Dan Zheng
e5cb871428 [AutoDiff upstream] Add flag-gated AdditiveArithmetic derivation. (#30628)
Add `AdditiveArithmetic` derived conformances for structs, gated by the
`-enable-experimential-additive-arithmetic-derivation` flag.

Structs whose stored properties all conform to `AdditiveArithmetic` can derive
`AdditiveArithmetic`:
- `static var zero: Self`
- `static func +(lhs: Self, rhs: Self) -> Self`
- `static func -(lhs: Self, rhs: Self) -> Self`
- An "effective memberwise initializer":
  - Either a synthesized memberwise initializer or a user-defined initializer
    with the same type.

Effective memberwise initializers are used only by derived conformances for
`Self`-returning protocol requirements like `AdditiveArithmetic.+`, which
require memberwise initialization.

Resolves TF-844.
Unblocks TF-845: upstream `Differentiable` derived conformances.
2020-03-25 10:31:50 -07:00
marcrasi
025cb9a501 autodiff builtins (#30624)
Define type signatures and SILGen for the following builtins:

```
/// Applies the {jvp|vjp} of `f` to `arg1`, ..., `argN`.
func applyDerivative_arityN_{jvp|vjp}(f, arg1, ..., argN) -> jvp/vjp return type

/// Applies the transpose of `f` to `arg`.
func applyTranspose_arityN(f, arg) -> transpose return type

/// Makes a differentiable function from the given `original`, `jvp`, and
/// `vjp` functions.
func differentiableFunction_arityN(original, jvp, vjp)

/// Makes a linear function from the given `original` and `transpose` functions.
func linearFunction_arityN(original, transpose)
```

Add SILGen FileCheck tests for all builtins.
2020-03-25 02:36:42 -07:00
Fred Riss
259d78a350 Adapt to llvm.org StringRef API change 2020-03-13 19:08:22 +01:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Nate Chandler
949029a788 [metadata prespecialization] Create enum records.
Extracted implementation of SpecializedGenericStructMetadataBuilder into
SpecializedGenericNominalMetadataBuilderBase, a CRTP with a template
template argument for the CRTP superclass and a template argument for
the implementation.  That new type is now subclassed by
SpecializedGenericStructMetadataBuilder.  Additionally, this new type is
also subclassed by the newly added SpecializedGenericEnumMetadataBuilder
which is responsible for build the prespecialization of generic enum
metadata.

rdar://problem/56960887
2020-02-11 09:57:22 -08:00
Owen Voorhees
166555c34f [Diagnostics] Better diagnostic for integer used as a boolean condition 2020-02-03 21:20:41 -08:00
Dan Zheng
1486d6b346 NFC: Add GenericSignature::getCanonicalSignature. (#29105)
Motivation: `GenericSignatureImpl::getCanonicalSignature` crashes for
`GenericSignature` with underlying `nullptr`. This led to verbose workarounds
when computing `CanGenericSignature` from `GenericSignature`.

Solution: `GenericSignature::getCanonicalSignature` is a wrapper around
`GenericSignatureImpl::getCanonicalSignature` that returns the canonical
signature, or `nullptr` if the underlying pointer is `nullptr`.

Rewrite all verbose workarounds using `GenericSignature::getCanonicalSignature`.
2020-01-12 12:17:41 -08:00
Nate Chandler
d78dc038c4 [metadata prespecialization] Specialize VWTs.
Prespecialized metadata records must refer to value witness tables that
have correct values for size and stride.  In order to do that, a
prespecialized value witness table must emitted and referred to.  Here,
a fully specialized value witness table is emitted.

For the moment, the value witness table is fully specialized.  Having it
be specialized, though, will likely cause serious code size problems,
since it results in specialized value witness functions being generated.

rdar://problem/58088270
2020-01-09 17:25:34 -08:00
Nate Chandler
9e2e090623 [IRGen] Emit metadata accessors last.
Metadata accessors are dependent on prespecializations of the metadata
of generic, in-module types.  Those prespecializations are themselves
dependent on usages of the types in functions.  Consequently, the
accessors must be emitted after all the functions are emitted.
2020-01-09 17:25:34 -08:00
Nate Chandler
7068141770 [IRGen] Emit prespecialized metadata records.
Prespecialized records contain direct references to the generic
arguments and protocol witnesses with which it is specialized for now.

Both prespecialized records and the records that are specialized at
runtime gain a trailing pointer-sized flagset.  For now, the flags in it
include whether the record was prespecialized and whether it was known
to be canonical at compile time (which is true for prespecialized
records within the module which defines the type whose metadata is
specialized since in those cases the metadata accessor can be modified).

rdar://problem/56960307
2020-01-09 17:25:33 -08:00
Nate Chandler
d9205fafd3 [IRGen] Prepare to emit prespecializations. 2020-01-09 17:25:33 -08:00
Nate Chandler
525e25603f [IRGen] Pass argument/table to builders.
Previously, the various generic builders implemented the methods
addGenericArgument and addGenericWitnessTable without being provided
what argument or witness table they were to add (because it was not
previously needed).  Now, the GenericRequirement is passed along to both
methods.
2020-01-09 17:25:31 -08:00
Doug Gregor
d59c23fa2c Merge pull request #28869 from DougGregor/assoc-type-witness-canon
[IRGen] Canonicalize associated type witnesses.
2019-12-19 00:27:25 -08:00
Doug Gregor
4e71ce53a9 [IRGen] Canonicalize associated type witnesses.
Associated type witnesses were not getting canonicalized with respect to
their appropriate generic signatures, causing types to be emitted into
the metadata that could not be properly demangled. Be consistent about
providing a generic signature for canonicalization.

Fixes SR-11642 / rdar://problem/56466693.
2019-12-18 22:21:50 -08:00
Xi Ge
1ebc78b22b [RemoteAST] Using module names specified by @_originallyDefinedIn for top-level decls marked as so
This should allow runtime de-mangling of those moved symbols.
2019-12-16 17:57:32 -08:00
Saleem Abdulrasool
636564e2cd IRGen: collocate WASM/ELF handling
WASM currently is treated identically to the ELF paths.  Collocate the
types to make it easier to ensure that all the paths are correctly
handling the emission.  This adds the missed case for the module hash.
2019-12-09 09:21:04 -08:00
Varun Gandhi
affa1b49bf [Runtime] Memset Extradata fully before copy from pattern. (#28478)
The third argument of memset needs a size in *bytes* not words.
2019-12-04 08:48:01 -08:00
Arnold Schwaighofer
4cba76309f IRGen: Add TypeExpansionContext to IRGen 2019-11-11 14:21:52 -08:00
Dan Zheng
53e61a9587 [AutoDiff upstream] Add the _Differentiation module. (#27511)
The `_Differentiation` module is the experimental support library for
differentiable programming. It is built when the build-script flag
`--enable-experimental-differentiable-programming` is enabled.

The `Differentiable` protocol generalizes all types that work with
differentiation. It is a core piece of the differentiable programming
project. Other parts depending on the `Differentiable` protocol will
be upstreamed piece by piece.

The `Differentiable` protocol is compiler-known and will be used during
type-checking, SILGen, and the SIL differentiation transform.
2019-11-06 11:31:12 -08:00
Mishal Shah
2f86d67500 Merge pull request #27396 from shahmishal/master-rebranch
Update master to support apple/stable/20190619 branch for LLVM projects
2019-10-01 10:50:49 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Harlan Haskins
e349b7b123 Merge branch 'master' into master-rebranch 2019-09-26 20:16:05 -07:00
Jordan Rose
a6dd630ca3 Eliminate Builtin.UnknownObject as an AST type (#27378)
This removes it from the AST and largely replaces it with AnyObject
at the SIL and IRGen layers. Some notes:

- Reflection still uses the notion of "unknown object" to mean an
  object with unknown refcounting. There's no real reason to make
  this different from AnyObject (an existential containing a
  single object with unknown refcounting), but this way nothing
  changes for clients of Reflection, and it's consistent with how
  native objects are represented.

- The value witness table and reflection descriptor for AnyObject
  use the mangling "BO" instead of "yXl".

- The demangler and remangler continue to support "BO" because it's
  still in use as a type encoding, even if it's not an AST-level
  Type anymore.

- Type-based alias analysis for Builtin.UnknownObject was incorrect,
  so it's a good thing we weren't using it.

- Same with enum layout. (This one assumed UnknownObject never
  referred to an Objective-C tagged pointer. That certainly wasn't how
  we were using it!)
2019-09-26 17:48:04 -07:00
swift-ci
b62b6fbd91 Merge remote-tracking branch 'origin/master' into master-rebranch 2019-09-16 18:23:59 -07:00
Joe Groff
30f5269670 IRGen: Use correct generic sig when mangling opaque underlying type.
Caught while testing on Swift 5.1.
2019-09-16 15:27:07 -07:00
swift-ci
a6f4a4650b Merge remote-tracking branch 'origin/master' into master-rebranch 2019-09-04 12:43:45 -07:00
Joe Groff
0ae86c9c9d IRGen: Backward-deploy fix using open-coded accessors.
If we mangled an opaque associated type while targeting an older OS, we can use a \9
accessor reference string to instantiate the associated type.
2019-09-04 10:22:17 -07:00
swift-ci
2f37dd154c Merge remote-tracking branch 'origin/master' into master-rebranch 2019-08-09 15:04:20 -07:00
Joe Groff
b5f36f7bea IRGen: Use known value witnesses in generic metadata patterns.
We were unnecessarily conservative here; generic metadata patterns support indirectable references
to value witness tables exported by the standard library, so if we have a fixed-layout generic
type that matches a known value witness table layout, use that instead of generating a new
value witness table.
2019-08-09 12:52:22 -07:00
Brent Royal-Gordon
fb20b503ba Merge branch 'master' into master-rebranch
# Conflicts:
#	lib/ClangImporter/ClangImporter.cpp
#	test/IRGen/builtins.swift
#	test/IRGen/enum.sil
#	tools/driver/autolink_extract_main.cpp
#	utils/build-presets.ini
2019-08-08 17:07:59 -07:00
Joe Groff
3e2965f452 IRGen: Omit frame pointers from metadata accessors. 2019-08-07 14:16:41 -07:00
Joe Groff
f0e5e1911d IRGen: Access concrete type metadata by mangled name.
When we generate code that asks for complete metadata for a fully concrete specific type that
doesn't have trivial metadata access, like `(Int, String)` or `[String: [Any]]`,
generate a cache variable that points to a mangled name, and use a common accessor function
that turns that cache variable into a pointer to the instantiated metadata. This saves a bunch
of code size, and should have minimal runtime impact, since the demangling of any string only
has to happen once.

This mostly just works, though it exposed a couple of issues:

- Mangling a type ref including objc protocols didn't cause the objc protocol record to get
  instantiated. Fixed as part of this patch.
- The runtime type demangler doesn't correctly handle retroactive conformances. If there are
  multiple retroactive conformances in a process at runtime, then even though the mangled string
  refers to a specific conformance, the runtime still just picks one without listening to the
  mangler. This is left to fix later, rdar://problem/53828345.

There is some more follow-up work that we can do to further improve the gains:

- We could improve the runtime-provided entry points, adding versions that don't require size
  to be cached, and which can handle arbitrary metadata requests. This would allow for mangled
  names to also be used for incomplete metadata accesses and improve code size of some generic
  type accessors. However, we'd only be able to take advantage of the new entry points in
  OSes that ship a new runtime.
- We could choose to always symbolic reference all type references, which would generally reduce
  the size of mangled strings, as well as make runtime demangling more efficient, since it wouldn't
  need to hit the runtime caches. This would however require that we be able to handle symbolic
  references across files in the MetadataReader in order to avoid regressing remote mirror
  functionality.
2019-08-02 14:28:53 -07:00
Joe Groff
e12f935b9a IRGen: Use correct archetype conformance code path for opaque associated types.
The code here was not correct in a situation where an opaque type had constraints that were
refinements of the protocol requirements of an associated type, as in:

```
protocol ParentProtocol {}
protocol SubProtocol: ParentProtocol {}

protocol P {
  associatedtype A: ParentProtocol
  func foo() -> A
}

struct S: P {
  func foo() -> some SubProtocol
}
```

because it assumed that the conformance could be found directly on the opaque type instead of
potentially via an arbitrary MetadataPath. Falling through to the code that already correctly
handles archetype conformances right below the removed code does the right thing. Fixes
rdar://problem/53081207.
2019-07-19 21:30:45 -07:00
Slava Pestov
83c90b6b2a AST: Turn NominalTypeDecl::getStoredProperties() into a request
This improves on the previous situation:

- The request ensures that the backing storage for lazy properties
  and property wrappers gets synthesized first; previously it was
  only somewhat guaranteed by callers.

- Instead of returning a range this just returns an ArrayRef,
  which simplifies clients.

- Indexing into the ArrayRef is O(1), which addresses some FIXMEs
  in the SIL optimizer.
2019-07-16 16:38:38 -04:00
Jordan Rose
4abefdb326 [IRGen] Canonicalize symbolic ref types in the right generic context (#25955)
When referencing a superclass type from a subclass, for example, the
type uses the subclass's generic parameters, not the superclass's.
This can be important if a nested type constrains away some of its
parent type's generic parameters.

This doesn't solve all the problems around mis-referenced generic
parameters when some are constrained away, though. That might
require a runtime change. See the FIXME comments in the test cases.

rdar://problem/51627403
2019-07-08 14:40:26 -07:00
swift-ci
2d6266a631 Merge remote-tracking branch 'origin/master' into master-next 2019-06-01 22:09:21 -07:00
Saleem Abdulrasool
731c31f9a5 MSVC: litter the code with llvm_unreachable (NFC)
Add `llvm_unreachable` to mark covered switches which MSVC does not
analyze correctly and believes that there exists a path through the
function without a return value.
2019-06-01 19:02:46 -07:00
swift-ci
13c61ecb15 Merge remote-tracking branch 'origin/master' into master-next 2019-05-03 08:29:43 -07:00
Joe Groff
a6b5a41a32 IRGen: Generate anonymous contexts for properties with opaque return types.
They aren't normally decl contexts, but if one has an opaque type, we want to be able to record
the property as a context so that we can reconstruct it in RemoteAST.
2019-05-02 17:28:08 -07:00
swift-ci
9da051c4e6 Merge remote-tracking branch 'origin/master' into master-next 2019-04-24 08:09:16 -07:00
Arnold Schwaighofer
84c7b77d02 Make opaque type descriptors dynamically replaceable
This is to support dynamic function replacement of functions with opaque
result type.

This approach requires that all state is thrown away (that could contain the
old returned type for an opaque type) between replacements.

rdar://48887938
2019-04-22 07:31:07 -07:00