Commit Graph

619 Commits

Author SHA1 Message Date
Doug Gregor
d31ef61a28 Eliminate "sorting" of DeclContext-local protocols / conformances.
Sorting of DeclContext-local protocols and conformances shouldn't ever
be necessary, because the underlying data structures that produce
these lists should be deterministic. Sorting can hide any
non-determinism, so stop doing it and we can address the underlying
nondeterminism.
2019-02-15 14:16:48 -08:00
Slava Pestov
b64db715a8 IRGen: Remove -enable-class-resilience staging flag 2019-02-08 14:22:00 -05:00
Slava Pestov
c28848ac70 IRGen: Stop using 'completely fragile' mode when emitting class metadata
This was a stop-gap measure until the YAML legacy type layout infrastructure
was in place.
2019-02-08 14:22:00 -05:00
Slava Pestov
5680bfde55 IRGen: Always use YAML files for completely fragile class layout
The layouts of resilient value types shipped in the Swift 5 standard library
x and overlays will forever be frozen in time for backward deployment to old
Objective-C runtimes. This PR ensures that even if the layouts of these types
evolve in the future, binaries built to run on the old runtime will continue
to lay out class instances in a manner compatible with Swift 5.

Fixes <rdar://problem/45646886>.
2019-02-08 14:22:00 -05:00
Slava Pestov
bb4cae06d0 IRGen: Rename -read-type-info-path flag to -read-legacy-type-info-path 2019-02-08 14:22:00 -05:00
Ding Ye
56888e2395 [IRGen] Do not emit ObjC property ivar field for non-trivially-representable types. (#21976)
This patch prevents writing ivar field for ObjC property whose type
is not trivially representable in ObjC within its DeclContext.
For example, in the case of the property `@objc var foo: String`,
it is accessed as an NSString on the ObjC side. Without this patch,
this property is backed with an ivar, to which direct accesses would
fail.

Resolves: SR-9557.
2019-01-18 17:58:53 -08:00
Daniel Dunbar
8980213f7c Merge pull request #20687 from ddunbar/swasm-allow-wasm-object-format
[Swift+WASM] Allow Wasm object format.
2019-01-04 08:34:43 -08:00
Félix Cloutier
c43ee6b355 Do not write out ObjC property ivar field for static properties 2018-12-19 10:12:19 -08:00
Jordan Rose
4abb570025 [IRGen] Stop using "extended" type encodings where not supported
These are only used in two places in the Apple Objective-C runtime:

- A protocol's "extended method types" list
- Block type descriptors

We were using them when dynamically registering a protocol with the
Objective-C runtime, but that's just expecting "normal" types; the
"extended method types" list is never present in such a protocol.
2018-12-08 14:20:49 -08:00
Slava Pestov
ff09603f30 IRGen: @_fixed_layout classes still have resilient metadata 2018-11-29 23:20:02 -05:00
Slava Pestov
16a459d0f1 IRGen: Refactor ClassLayoutBuilder a bit to help distinguish resilient-metadata from resilient-storage
We want @_fixed_layout classes to have non-resilient storage, but still have
resilient metadata.
2018-11-29 23:20:02 -05:00
Daniel Dunbar
1efee0c27a [Swift+WASM] Allow Wasm object format.
- This currently does nothing more than adopt the ELF conventions, but for the
   Wasm object file format.
2018-11-20 15:22:26 -07:00
Saleem Abdulrasool
13d56a4f56 IRGen: force lazy metadata initialization for PE/COFF
The metadata reference to the pre-exsting VWT cannot be supported on PE/COFF
due to the direct reference to a value in an external module that is in
static data (the model requires indirecting through memory).  Always
force the lazy initialization for the metadata on such platforms.

This requires a secondary change - to initialize the VWT as well.  This
is ideally moved into the runtime where we can do this uniformly.
2018-11-15 11:05:47 -08:00
Slava Pestov
dbd116119e IRGen: Only mark field offset globals as constant if they won't be updated at runtime
Even if we have a constant value, we might be emitting a legacy layout
that can be updated in place by newer runtimes. In this case, clients
cannot assume the field offsets are constant, and the globals cannot
be constant either.

Part of <rdar://problem/17528739>.
2018-10-31 20:45:18 -04:00
Slava Pestov
4cecc268dc IRGen: Emit Objective-C metadata update callbacks 2018-10-26 16:54:23 -04:00
Slava Pestov
87ec607233 IRGen: Add a new 'Legacy' lowering mode that loads type info from a YAML file
The YAML format is the same one produced by the -dump-type-info
frontend mode.

For now this is only enabled if the -read-type-info-path frontend
flag is specified.

Progress on <rdar://problem/17528739>.
2018-10-04 23:33:17 -04:00
Slava Pestov
d9d64db53f IRGen: We no longer deallocate partially-initialized foreign classes
This was an old workaround for a bug that should no longer
happen now that convenience initializers only have allocating
entry points.
2018-10-04 20:01:23 -04:00
Slava Pestov
4da47823a5 Runtime/IRGen: Add new initialization pattern for classes with backward deployment layout
If a class has a backward deployment layout:

- We still want to emit it using the FixedClassMetadataBuilder.

- We still want it to appear in the objc_classes section, and get an
  OBJC_CLASS_$_ symbol if its @objc.

- However, we want to use the singleton metadata initialization pattern
  in the metadata accessor.

- We want to emit metadata for all field types, and call the
  swift_updateClassMetadata() function to initialize the class
  metadata.

For now, this function just performs the idempotent initialization of
invoking a static method on the class, causing it to be realized with
the Objective-C runtime.
2018-09-23 21:26:46 -07:00
Slava Pestov
a7f668c89c IRGen: Clean up class metadata emission 2018-09-23 21:26:46 -07:00
Slava Pestov
5e56e486b5 IRGen: Clean up ivar descriptor emission 2018-09-23 21:26:46 -07:00
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
Slava Pestov
719ba2fb27 IRGen: Fix case where we would go through the vtable entry and not the dispatch thunk
Make sure we check resilience of the class defining the method,
and not the class with the override we're calling.
2018-09-07 21:57:16 -07:00
Slava Pestov
3808ae0d58 IRGen: Use method lookup function for resilient super method calls
Fixes <https://bugs.swift.org/browse/SR-3928>, <rdar://problem/31411193>.
2018-09-07 21:57:16 -07:00
Slava Pestov
b3060d8836 IRGen: Emit dispatch thunks while emitting nominal type descriptor 2018-08-31 00:20:38 -06:00
Slava Pestov
98b5eafcca IRGen: Split up doesClassMetadataRequireDynamicInitialization() into two predicates
- doesClassMetadataRequireRelocation() -- returns true if we must
  allocate new metadata at runtime and fill it in, because the class
  has multiple instantiations (generic case) or because the total size
  of the metadata is not known at compile time (resilient ancestry).

- doesClassMetadataRequireInitialization() -- weaker condition than
  the above. It's true if the metadata must be relocated, but it is
  also true if the metadata has otherwise fixed size but must be
  filled in dynamically. This occurs if the class has generic
  ancestry but is itself not generic, or if the class has
  resiliently-sized fields, or missing members.

For now, we don't actually care about the distinciton anywhere,
because we cannot do in-place initialization of class metadata yet.
2018-08-20 14:24:19 -07:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Jordan Rose
f505c8b8a0 [IRGen] Add some PrettyStackTraces for metadata emission (#18727) 2018-08-15 12:51:20 -07:00
Doug Gregor
b08a6f56b4 [AST] Drop resilience expansion from TypeBase::getReferenceCounting().
We’re not using this parameter, and don’t expect to do so in the future,
so remove it. Also fold away TypeBase::usesNativeReferenceCounting()
and irgen::getReferenceCountingForType(), both of which are trivial.
2018-08-14 09:10:49 -07:00
Slava Pestov
e44721dd19 IRGen: Remove the 'access type'/'layout type' distinction from StructLayout
This was part of the old resilience workaround for classes and
is no longer used.
2018-08-10 00:42:34 -07:00
Slava Pestov
b26d8ca6ed IRGen: For classes with resiliently-sized fields, calculate both a fragile and resilient layout
Use the fragile layout to emit static metadata, and use the
resilient metadata for almost everything else.

Add some tests to ensure that we use runtime metadata, even
for classes with fully fragile layout.
2018-08-10 00:42:34 -07:00
Slava Pestov
9f5505d95c IRGen: Don't make values constant in getAddrOfSimpleVariable()
We also use this for field offset globals, which are not always
constant. I think in practice everything was getting set
correctly, but it was hard to follow the logic.
2018-08-10 00:42:34 -07:00
Slava Pestov
90f01deba2 IRGen: Remove tryEmitClassConstantFragileInstance{Size,AlignMask}() 2018-08-10 00:42:34 -07:00
Slava Pestov
b26f3efced IRGen: Remove ClassTypeInfo::getHeapAlignment() 2018-08-10 00:42:34 -07:00
Slava Pestov
69be7d93b2 IRGen: Split up ClassLayoutBuilder::ClassMetadataRequiresDynamicInitialization into fine-grained conditions 2018-08-10 00:42:34 -07:00
Slava Pestov
f594d5bfbf IRGen: Split off ClassLayout implementation into its own file 2018-08-07 05:34:29 -07:00
Slava Pestov
fc91a58d82 IRGen: [ClassLayout] Better fix for SR-4687
We cannot use field offset globals if *any* field of a generic class
with Objective-C ancestry is dependent.

This is because the Swift runtime first performs layout starting
from a static instance start offset, and then asks the Objective-C
runtime to slide the offsets based on the dynamic superclass size.

So if the class has a field of generic type, the alignment of that
type can change the offsets of fields *before* it as well as after.

So we cannot assuem that any fields in such a class have the same
offset across instantiations at all.

The previous fix captured the intent of the above, but it only
kicked in if the immediate superclass of the class was imported
from Objective-C. But really we need to do this for any class with
Objective-C ancestry.

While fixing this, re-organize the code in ClassLayoutBuilder a
little bit to untangle the stored property iteration from the
interesting FieldAccess adjustments that take place after.
2018-08-07 04:26:43 -07:00
Slava Pestov
d460d5def6 IRGen: Remove ASTTy field from StructLayout 2018-08-07 04:26:43 -07:00
Slava Pestov
92f1ddc981 IRGen: [ClassLayout] Don't compute FieldAccess for superclass fields
Previously we would recursively get the abstract layout for the fully
generic class type for every superclass. But really we only need to do
that for fields of the class itself, not any superclass fields, since
we throw out the superclass field information anyway.
2018-08-07 04:26:43 -07:00
Slava Pestov
bdb5eb3289 IRGen: [ClassLayout] Don't build a separate StructLayout for classes
The type info for a class described its layout using a combination of a
StructLayout and ClassLayout, with different information stored in both.

Since we never use a class as a struct, it's simpler to add the relevant
bits to ClassLayout, and not build a StructLayout at all.

Also, drop inherited properties from the ClassLayout -- they're no
longer needed.
2018-08-07 00:37:03 -07:00
Slava Pestov
7315893d3d IRGen: [ClassLayout] Only look for fields of the immediate class
When emitting fixed class metadata, we emit field offsets for all fields,
including those from superclasses, if any.

Get the ClassLayout for the correct class before looking up a field that
might potentially belong to a superclass. Soon, I'm going to slim down
ClassLayout instances to only store the fields belonging to the class
itself, removing the InheritedStoredProperites array altogether.
2018-08-07 00:34:55 -07:00
Slava Pestov
a4f06c40fc IRGen: [ClassLayout] Don't recursively compute layout of the same type
Even if two types are different, they might still have the same
type info, so don't call getLayout() without checking the type info
for identity first. This allows simplifying an early exit into an
assertion elsewhere.
2018-08-06 23:22:16 -07:00
Doug Gregor
4d39506c4a [IRGen] Use a more precise computation for the kind of reference count.
TypeBase::usesNativeReferenceCounting() was doing a lot of work to
find the class that a type refers to, then determine whether it
would use the native reference-counting scheme. Its primary caller
in IRGen would use an overly-conservative approximation to decide
between the “Objective-C” and “unknown” cases, which resulted in
uses of “unknown” reference counting for some obviously-ObjC cases
(e.g., values of “NSObject”).

Moreover, the approximation would try to call into the type checker
(because it relied unnecessarily on the superclass *type* of a class
declaration), causing an assertion.

Fixes rdar://problem/42828798.
2018-08-06 17:04:47 -07:00
Slava Pestov
bee6b09a02 IRGen: Remove emitClassFragileInstanceSizeAndAlignMask() 2018-08-02 19:41:59 -07:00
Slava Pestov
9e15390216 IRGen: Don't visit superclass in classHasIncompleteLayout() and make it static 2018-08-02 19:41:59 -07:00
Slava Pestov
864d9cdf58 IRGen: Add a new ClassLayout::HasFixedSize and use it
Asking isFixedLayout() on the class's StructLayout does not take
missing members or Objective-C sliding into account. Adding a new
bit that carries this information allows removing the hack where
across modules the size of a class was always loaded from metadata.

In practice we hope most classes will be resilient, but its
better to centralize the checking for what resilience means instead
of using different rules in different places.
2018-08-02 19:41:59 -07:00
Slava Pestov
7c0ee05a2f IRGen: Don't unconditionally use field offset globals for cross-module class field access
While most class field accesses go through accessors, a special
case is if you have a final field (or class) in a non-resilient
module. Then, we were allowed to directly access the field.

However, an earlier hack made it so that this access always went
through a field offset global, which is unnecessary in the case
where the class layout is fully known.

One consequence of this is that 'Array.count' would compile down
to a load from a global followed by an indirect load, instead of
a single load from a constant offset.
2018-08-02 02:01:01 -07:00
Slava Pestov
13eb3489e4 IRGen: Remove unused __getInstanceSizeAndAlignMask() hack 2018-08-01 00:22:29 -07:00
Doug Gregor
d07fa5ab69 Switch many callers of ClassDecl::getSuperclass() to ClassDecl::getSuperclassDecl().
ClassDecl::getSuperclass() produces a complete interface type describing the
superclass of a class, including any generic arguments (for a generic type).
Most callers only need the referenced ClassDecl, which is (now) cheaper
to compute: switch those callers over to ClassDecl::getSuperclassDecl().

Fixes an existing test for SR-5993.
2018-07-31 10:14:44 -07:00
Doug Gregor
2860557a77 [Name lookup] Use the declaration-based lookupQualified() where it’s easy.
Switch a number of callers of the Type-based lookupQualified() over to
the newer (and preferred) declaration-based lookupQualified(). These are
the easy ones; NFC.
2018-07-31 10:14:44 -07:00
John McCall
7a4aeed570 Implement generalized accessors using yield-once coroutines.
For now, the accessors have been underscored as `_read` and `_modify`.
I'll prepare an evolution proposal for this feature which should allow
us to remove the underscores or, y'know, rename them to `purple` and
`lettuce`.

`_read` accessors do not make any effort yet to avoid copying the
value being yielded.  I'll work on it in follow-up patches.

Opaque accesses to properties and subscripts defined with `_modify`
accessors will use an inefficient `materializeForSet` pattern that
materializes the value to a temporary instead of accessing it in-place.
That will be fixed by migrating to `modify` over `materializeForSet`,
which is next up after the `read` optimizations.

SIL ownership verification doesn't pass yet for the test cases here
because of a general fault in SILGen where borrows can outlive their
borrowed value due to being cleaned up on the general cleanup stack
when the borrowed value is cleaned up on the formal-access stack.
Michael, Andy, and I discussed various ways to fix this, but it seems
clear to me that it's not in any way specific to coroutine accesses.

rdar://35399664
2018-07-23 18:59:58 -04:00