Commit Graph

163 Commits

Author SHA1 Message Date
Doug Gregor
71c38934d3 Fix an incorrect assertion involving existential formation.
This assertion was tripping when the existential involved a marker
protocol, because it was checking for the number of protocols rather
than the number of protocols that require witness tables. Correct the
assertion and add a test.

Fixes rdar://83020734.
2021-09-24 17:05:56 -07:00
Andrew Trick
97ec127d25 Lower and emit debug_value [poison].
OwnershipEliminator lowers destroy_value [poison] to debug_value
[poison].

IRGen overwrites all references in shadow copies with a sentinel value
in place of debug_value [poison].

Part 2/2: rdar://75012368 (-Onone compiler support for early object
deinitialization with sentinel dead references)
2021-03-12 19:33:23 -08:00
Erik Eckstein
f5a8f600ea SIL: new instructions for copy-on-write support
* a new [immutable] attribute on ref_element_addr and ref_tail_addr
* new instructions: begin_cow_mutation and end_cow_mutation

These new instructions are intended to be used for the stdlib's COW containers, e.g. Array.
They allow more aggressive optimizations, especially for Array.
2020-05-14 08:39:54 +02:00
Joe Groff
950ee3b0de IRGen: Generalize the nullable optimization for single-payload enums.
Augment the `isSingleRetainablePointer` check that allows IRGen to avoid adding branching around retain/release operations on enums that use the null pointer extra inhabitant with a more general "can value witness extra inhabitants" method on TypeInfo, which says whether a type's retain/release operations are safe to invoke on some or all of its extra inhabitants. This lets us generalize the optimization to include things like `String?` or `ClassProtocol?` which are common types with a nullable pointer in them.
2020-03-03 15:00:27 -08:00
Arnold Schwaighofer
7b65768167 IRGen: Add code to compute type layouts
rdar://51988441
2020-02-19 07:12:55 -08:00
Slava Pestov
bc1aa97e4e IRGen: Clean up types of outlined existential buffer operations
The uniquing key for these was just the number of witness tables,
but the function itself referenced the specific existential type
it was instantiated with.

Everything still worked because getOrCreateHelperFunction() would
bitcast an existing function to the correct type, and in practice
the layout of an existential type only depends on the number of
witness tables.

However on master-next, other changes were made that stripped
off the bitcasts. This would result in assertions or LLVM
verifier failures when multiple existential types were used in
a single translation unit.

Fixes <rdar://problem/54780404>.
2019-09-10 14:00:06 -04:00
Slava Pestov
d434188157 SIL: Refactor TypeConverter to not require a SILModule
This mostly requires changing various entry points to pass around a
TypeConverter instead of a SILModule. I've left behind entry points
that take a SILModule for a few methods like SILType::subst() to
avoid creating even more churn.
2019-09-06 21:50:15 -04:00
Joe Groff
b9e6ae07e4 IRGen: Avoid unnecessary swift_getObjectType calls.
In too many places, we were calling into `emitDynamicTypeOfOpaqueHeapObject` even when we had
more specific type information about the heap object we were querying. Replace all calls with
`emitDynamicTypeOfHeapObject`, which uses the best available access path and completely avoids
runtime calls for pure Swift classes and heap objects. When targeting non-ObjC-interop platforms,
we also know we never need to call `swift_getObjectType`, so avoid doing so altogether.
2019-08-08 18:00:10 -07:00
David Zarzycki
eb1c203cbf [IRGen] Fix a missing case of the new bit pattern builder
This fixes a downstream regression introduced by:
bb2740e540

Nothing in Swift proper uses this scenario.
2019-08-08 10:38:57 +01:00
Michael Munday
bb2740e540 IRGen: fix enum bit packing on big-endian platforms.
This change modifies spare bit masks so that they are arranged in
the byte order of the target platform. It also modifies and
consolidates the code that gathers and scatters bits into enum
values.

All enum-related validation tests are now passing on IBM Z (s390x)
which is a big-endian platform.
2019-08-07 03:54:16 -04:00
Saleem Abdulrasool
5f881decb9 IRGen: eliminate some nondeterminism due to UB
The order of evaluation of arguments for parameters is unspecified by
the language specification.  Windows evaluates the arguments in a
different order.  Hoist the function calls for parameters to enforce
ordering.  This reduces the nondeterminism in IRGen on Windows.
2019-02-20 16:19:57 -08:00
Saleem Abdulrasool
9f113d06ce IRGen: ensure ordering of instructions (NFC)
Split out the subconstructors to ensure guaranteed IR ordering irrespective of
order of evaluation of parameters which is not specified.  This is needed for
identical IRGen on Windows.  NFC.
2019-02-01 11:10:46 -08:00
Joe Groff
89979137fc Push ArchetypeType's API down to subclasses.
And clean up code that conditionally works only with certain kinds of archetype along the way.
2018-12-12 19:45:40 -08:00
Joe Groff
fbd21e1bd4 Merge pull request #19296 from jckarter/opaque-existential-extra-inhabitants
Give opaque existential containers extra inhabitants.
2018-09-17 10:48:20 -07:00
Joe Groff
b4abe8503a Give opaque existential containers extra inhabitants.
We can use the extra inhabitants of the type metadata field as extra inhabitants of the entire
existential container, allowing `Any?` and similar types to be the same size as non-optional
existentials.
2018-09-14 12:07:58 -07:00
Saleem Abdulrasool
12cc10131e IRGen: use the llvm::TrailingObjects base class explicitly
Visual Studio is unable to follow the inheritance to find the method and objects
with:

  lib\IRGen\GenExistential.cpp(153): error C2903: 'totalSizeToAlloc': symbol is neither a class template nor a function template
  lib\IRGen\GenExistential.cpp(153): note: This diagnostic occurred in the compiler generated function 'const Derived *`anonymous-namespace'::ExistentialTypeInfoBase<Derived,Base>::create(llvm::ArrayRef<T>,As &&...)'
        with
        [
            T=const swift::ProtocolDecl *
        ]
  lib\IRGen\GenExistential.cpp(202): note: see reference to class template instantiation '`anonymous-namespace'::ExistentialTypeInfoBase<Derived,Base>' being compiled
  lib\IRGen\GenExistential.cpp(154): error C2760: syntax error: unexpected token 'const', expected 'expression'
  lib\IRGen\GenExistential.cpp(154): note: This diagnostic occurred in the compiler generated function 'const Derived *`anonymous-namespace'::ExistentialTypeInfoBase<Derived,Base>::create(llvm::ArrayRef<T>,As &&...)'
        with
        [
            T=const swift::ProtocolDecl *
        ]
2018-09-13 15:59:09 -07:00
Joe Groff
9f02ecd1a5 IRGen: Use any field of structs for extra inhabitants.
This allows us to layout-optimize Optional<T> when T is a struct with an
extra-inhabitant-bearing field anywhere in its definition, not only at
the beginning. rdar://problem/43019427
2018-08-14 12:53:06 -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
Jordan Rose
9b9ef88dbd [IRGen] Don't compute witness table layout just to form existentials
We weren't using this information, and trying to access it runs into
trouble when the protocol is in another file.
2018-08-10 17:06:59 -07:00
Jordan Rose
3d34b9c156 [IRGen] Remove unused ExistentialTypeInfoBase::findWitnessTable
Turns out we never ask this of TypeInfo anymore.
2018-08-10 15:37:31 -07:00
David Zarzycki
a5050f5ce6 [IRGen] Make address-only existential reference storage types model optionality 2018-07-28 09:43:22 -04:00
John McCall
34b0cbc11d Merge pull request #16237 from davezarzycki/metaprogram_ref_storage_types
[AST] NFC: Enable reference storage type meta-programming
2018-07-05 14:45:38 -04:00
Slava Pestov
45fb11ce3c AST: Add ExistentialLayout::getSuperclass(), rename superclass to explicitSuperclass
More groundwork for protocols with superclass constraints.
In several places we need to distinguish between existential
types that have a superclass term (MyClass & Proto) and
existential types containing a protocol with a superclass
constraint.

This is similar to how I can write 'AnyObject & Proto', or
write 'Proto1 & Proto2' where Proto1 has an ': AnyObject'
in its inheritance clause.

Note that some of the usages will be revisited later as
I do more refactoring and testing. This is just a first pass.
2018-07-02 22:06:33 -07:00
David Zarzycki
057bbb366a [IRGen] Adopt reference storage type meta-programming macros
This commit also fixes reference storage extra inhabitant bugs.
2018-06-30 11:48:47 -04:00
Arnold Schwaighofer
98506e03cf IRGen: Opaque existentials are now bitwise takable
Now that the existential inline buffer is bitwise takable so are opaque
existentials.
2018-05-23 11:17:54 -07:00
Arnold Schwaighofer
6267860a7e IRGen: Remove initializeBufferWithTakeOfBuffer in favor of memcpy
And update the existential container's initializeWithTake implementation
in the runtime. After only allowing bitwise takable values in the
inline buffer we can use memcpy to move existential container values.

rdar://31414907
SR-343
2018-05-22 13:05:00 -07:00
David Zarzycki
8c0c55539f [SIL] NFC: Rename misleading getSwiftRValueType() to getASTType()
Reference storage types are not RValues. Also, use more SILType helper
methods to avoid line wrap.
2018-05-04 08:14:38 -04:00
Huon Wilson
d0e5b17e4b [IRGen] std::function -> llvm::function_ref for some non-escaping params. 2018-05-01 08:29:07 +10:00
Arnold Schwaighofer
49b6196666 IRGen: Use the payload type when unowned taking/loading
SR-7369
rdar://39239510
2018-04-10 08:38:32 -07:00
Joe Groff
a4aa054838 IRGen: Make type(of:) behavior consistent in ObjC bridged contexts.
When we use type(of: x) on a class in an ObjC bridged context, the optimizer turns this into a SIL `value_metatype @objc` operation, which is supposed to get the dynamic type of the object as an ObjC class. This was previously lowered by IRGen into a `object_getClass` call, which extracts the isa pointer from the object, but is inconsistent with the `-class` method in ObjC or with the Swift-native behavior, which both look through artificial subclasses, proxies, and so on. This inconsistency led to observably different behavior between debug and release builds and between ObjC-bridged and native entry points, so provide an alternative runtime entry point that replicates the behavior of getting a native Swift class. Fixes SR-7258.
2018-04-06 15:17:04 -07:00
John McCall
976401157f Bind layout type metadata correctly in outlined helper functions.
Fixes a regression in the source compatibility suite which I had a
lot of trouble extracting into a separate test case.

Most of this patch is just moving the outlining code into a separate
file and organizing it into a helper class instead of copy/pasting
so much code.  The main functional change is implicit in the difference
between collecting formal metadata and collecting it for layout, which
then is exploited in bindMetadataParameters.

As a secondary change, stop collecting metadata for class-bounded
archetypes; we don't actually need it to do value operations.
2018-03-27 15:14:12 -04:00
John McCall
a906f43329 Allow type metadata to be incomplete.
Most of the work of this patch is just propagating metadata states
throughout the system, especially local-type-data caching and
metadata-path resolution.  It took a few design revisions to get both
DynamicMetadataRequest and MetadataResponse to a shape that felt
right and seemed to make everything easier.

The design is laid out pretty clearly (I hope) in the comments on
DynamicMetadataRequest and MetadataResponse, so I'm not going to
belabor it again here.  Instead, I'll list out the work that's still
outstanding:

- I'm sure there are places we're asking for complete metadata where
  we could be asking for something weaker.

- I need to actually test the runtime behavior to verify that it's
  breaking the cycles it's supposed to, instead of just not regressing
  anything else.

- I need to add something to the runtime to actually force all the
  generic arguments of a generic type to be complete before reporting
  completion.  I think we can get away with this for now because all
  existing types construct themselves completely on the first request,
  but there might be a race condition there if another asks for the
  type argument, gets an abstract metadata, and constructs a type with
  it without ever needing it to be completed.

- Non-generic resilient types need to be switched over to an IRGen
  pattern that supports initialization suspension.

- We should probably space out the MetadataStates so that there's some
  space between Abstract and Complete.

- The runtime just calmly sits there, never making progress and
  permanently blocking any waiting threads, if you actually form an
  unresolvable metadata dependency cycle.  It is possible to set up such
  a thing in a way that Sema can't diagnose, and we should detect it at
  runtime.  I've set up some infrastructure so that it should be
  straightforward to diagnose this, but I haven't actually implemented
  the diagnostic yet.

- It's not clear to me that swift_checkMetadataState is really cheap
  enough that it doesn't make sense to use a cache for type-fulfilled
  metadata in associated type access functions.  Fortunately this is not
  ABI-affecting, so we can evaluate it anytime.

- Type layout really seems like a lot of code now that we sometimes
  need to call swift_checkMetadataState for generic arguments.  Maybe
  we can have the runtime do this by marking low bits or something, so
  that a TypeLayoutRef is actually either (1) a TypeLayout, (2) a known
  layout-complete metadata, or (3) a metadata of unknown state.  We could
  do that later with a flag, but we'll need to at least future-proof by
  allowing the runtime functions to return a MetadataDependency.
2018-03-26 12:18:04 -04:00
John McCall
aaa40ee82b Move metadata-accessing IRGen out of GenMeta. NFC.
Abstract type/heap metadata access goes into MetadataRequest.
Metadata access starting from a heap object goes into GenHeap.
Accessing various components of class metadata goes into GenClass
or MetadataLayout.
2018-03-18 23:53:11 -04:00
Joe Shajrawi
d8289aa3ec Code size: destroy_addr outline 2017-11-17 16:10:27 -08:00
Joe Shajrawi
62d823c56d Code size: Do not use a global state for isOutlined 2017-11-15 15:28:27 -08:00
Greg Parker
9a38e609af [IRGen][runtime] Reduce object header to 8 bytes on 32-bit platforms. (#12790)
SR-4353, rdar://29765800
2017-11-08 02:50:11 -08:00
Joe Shajrawi
f4db36426c Code Size: Outline copy addr instruction 2017-10-31 17:03:48 -07:00
Arnold Schwaighofer
ca63326e1b Delete unused existential value witnesses from the old existential
implementation

And remove the SWIFT_RUNTIME_ENABLE_COW_EXISTENTIALS flag.
2017-06-02 14:34:41 -07:00
Slava Pestov
4fd870b81d AST: Change ExistentialLayout::requiresClass from a field to a method
We need to compute it lazily to avoid calling
ProtocolDecl::requiresClass() before the protocol's
inheritance clause has been type checked.
2017-05-01 15:49:03 -07:00
Slava Pestov
2ec32c2d77 IRGen: Allow pure-ObjC refcounting class existentials
Class-constrained existentials usually have unknown
reference counting in Swift, because they can contain
either Objective-C or native Swift classes.

But if the class existential has a superclass bound
which is known to be an imported Objective-C class,
we can use Objective-C reference counting instead of
unknown reference counting.

This is tested with the next commit that adds
ClangImporter support.
2017-04-18 21:19:45 -07:00
practicalswift
7eb7d5b109 [gardening] Fix 100 typos. 2017-04-18 17:01:42 +02:00
Slava Pestov
497336c4bc IRGen: Use the right reference counting for subclass existentials 2017-04-17 17:22:28 -06:00
Slava Pestov
7bf2f7d6fc IRGen: Start using ExistentialLayout
Replace a few usages of TypeBase::getExistentialTypeProtocols(),
which I'm going to remove.

NFC for now, since subclass existentials are still not fully
plumbed through here.
2017-04-13 21:29:57 -07:00
Huon Wilson
31b92b1b46 Merge pull request #8473 from huonw/symbol-list-2
Remove dependency on IRGenModule of some linkage computations
2017-04-05 16:39:04 -07:00
Huon Wilson
3105c6ed00 [IRGen] Expose LinkInfo publicly, like LinkEntity. 2017-04-05 09:54:19 -07:00
Slava Pestov
37491e63ac AST: Refactor existential type accessors on TypeBase and CanType 2017-04-03 23:14:25 -07:00
Arnold Schwaighofer
cc3818cac6 IRGen: Delete dead functions 2017-03-30 15:17:01 -07:00
Arnold Schwaighofer
47fbdfdbfc Merge pull request #8391 from aschwaighofer/wip_enable_cow_existentials
Fix test cases and enable copy-on-write existentials
2017-03-29 09:59:29 -07:00
Arnold Schwaighofer
aa61ae8318 IRGen: Delete dead code 2017-03-29 06:34:32 -07:00
Arnold Schwaighofer
c066e42bf2 IRGen: Use a plain slowDealloc call for deallocateBoxedExistential box
The object has a refcount of one
2017-03-28 09:22:05 -07:00