Commit Graph

114 Commits

Author SHA1 Message Date
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
454495f974 IRGen: Subclass existential scalar downcasts
This handles the case where the left hand side of the cast is known
to be class-like, and the right hand side is known at compile time
to be a protocol composition type.

Note that this change results in a small optimization -- a checked
cast of a metatype known to be a class metatype to a class-constrained
existential metatype no longer has to emit an explicit check that
the source is class-constrained.

Fully dynamic casts are coming up next.
2017-04-25 02:42:14 -07: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
Slava Pestov
d58f049608 AST: Introduce ASTContext::getAnyObjectType()
This replaces a number of usages of KnownProtocolKind::AnyObject,
which is soon going away.
2017-04-13 21:17:05 -07:00
Slava Pestov
83ca7a852f AST: Remove TypeBase::getClassBound() 2017-02-27 20:00:36 -08:00
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
Arnold Schwaighofer
1f914eb4c4 Silence warn_unused_result warnings 2017-01-18 15:20:27 -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
Joe Groff
b4041a5b57 SIL: Restrict partial_apply when -disable-sil-partial-apply is enabled.
Allow it only to have one context parameter, whose ownership convention matches the convention of the resulting thick function, effectively limiting it to binding a closure invocation function to its context.
2016-10-10 11:25:57 -07:00
John McCall
34fb15e375 Abstract the object type of an optional type according to the
abstraction pattern of the type rather than always using the
most-general pattern, and erase ImplicitlyUnwrappedOptional from
the SIL type system.
2016-09-08 23:26:19 -07:00
Jordan Rose
3b6e40c030 Use ClassDecl::ForeignKind to model Clang's objc_runtime_visible.
We're now correctly checking for inheritance, adding @objc methods,
and adding @objc protocols for both CF types and objc_runtime_visible
classes (those without visible symbols). The latter is used for some
of the types in Dispatch, which has exposed some of the classes that
were considered implementation details on past OSs.

We still don't properly implement using 'as?' to check conformance to
a Swift protocol for a CF or objc_runtime_visible type, but we can do
that later.

rdar://problem/26850367
2016-06-30 11:20:58 -07:00
John McCall
6c92c324f6 Rename IRGenModuleDispatcher to just IRGenerator and transfer
ownership of some of the basic structures to it.
2016-04-27 09:42:03 -07:00
practicalswift
212aba51c6 [gardening] Fix recently introduced typos: "’" → "'", "represention" → "representation", "arcitectures" → "architectures", "refernce" → "reference", "incovation" → "invocation" 2016-04-08 21:22:25 +02:00
John McCall
44f71b1f11 Handle all the cases of scalar dynamic casts allowed by SIL in IRGen.
In particular, reliably look through a single level of optional
types and handle metatype-to-class casts.

Fixes rdar://24924966.
2016-04-06 19:10:02 -07:00
John McCall
4f669fe563 Move some cast-emission code to GenCast.cpp. NFC. 2016-04-06 13:31:18 -07:00
John McCall
0ffb7278bc Only use metadata patterns for generic types; perform other
initialization in-place on demand.  Initialize parent metadata
references correctly on struct and enum metadata.

Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.

Includes a fix since the earlier commit to make enum metadata
writable if they have an unfilled payload size.  This didn't show
up on Darwin because "constant" is currently unenforced there in
global data containing relocations.

This patch requires an associated LLDB change which is being
submitted in parallel.
2016-03-24 15:10:31 -07:00
John McCall
abba7f0c8b Revert "Only use metadata patterns for generic types; perform other"
This reverts commit 41efb3d4d3.
LLDB has too many tendrils into our metadata.
2016-03-23 20:26:43 -07:00
John McCall
41efb3d4d3 Only use metadata patterns for generic types; perform other
initialization in-place on demand.  Initialize parent metadata
references correctly on struct and enum metadata.

Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.
2016-03-23 17:04:04 -07:00
Roman Levenstein
de3b850ce8 Use more descriptive names for calling conventions.
Rename RuntimeCC into DefaultCC
Rename RuntimeCC1 into RegisterPreservingCC
Remove RuntimeCC0 because it was identical to DefaultCC.
2016-02-25 05:31:00 -08:00
Roman Levenstein
ec04b22145 Properly propagate the calling convention into LLVM's call instructions.
Each runtime function definition in RuntimeFunctions.def states which calling convention
should be used for this runtime function.  But IRGen and LLVMPasses were not always
properly propagating this declared calling convention all the way down to llvm's Call instructions.
In many cases, the standard C convention was set for the call irrespective of the actual calling
convention defined for a given runtime function. As a result, incorrect code was generated.

This commit tries to fix all those places, where such a mismatch was found. In many cases this is
achieved by defining a helper function CreateCall in such a way that makes sure that the call instruction
gets the same calling convention as the one used by its callee operand.
2016-02-25 05:30:59 -08:00
Adrian Prantl
64a516743b Emit debug info for the compiler-generated cast helper functions.
Failing to do so will lead to hard-to-debug verifier errors in LLVM
when these functions get inlined.
2016-02-04 10:11:12 -08:00
Slava Pestov
1eb1a8508e IRGen: StorageType doesn't need to be public, we already have an accessor for it, NFC 2016-01-07 14:33:15 -08:00
John McCall
9cd96ce600 Refine the use and behavior of ConditionalDominanceScope.
Instead of categorically forbidding caching within a conditional
scope, permit it but remember how to remove the cache entries.
This means that ad-hoc emission code can now get exactly the
right caching behavior if they use this properly.  In keeping
with that, adjust a bunch of code to properly nest scopes
according to the conditional paths they enter.
2016-01-06 00:24:22 -08:00
John McCall
7dd9f5f037 Improve IRGen's infrastructure for caching local type data.
There are several interesting new features here.

The first is that, when emitting a SILFunction, we're now able to
cache type data according to the full dominance structure of the
original function.  For example, if we ask for type metadata, and
we've already computed it in a dominating position, we're now able
to re-use that value; previously, we were limited to only doing this
if the value was from the entry block or the LLVM basic block
matched exactly.  Since this tracks the SIL dominance relationship,
things in IRGen which add their own control flow must be careful
to suppress caching within blocks that may not dominate the
fallthrough; this mechanism is currently very crude, but could be
made to allow a limited amount of caching within the
conditionally-executed blocks.

This query is done using a proper dominator tree analysis, even at -O0.
I do not expect that we will frequently need to actually build the
tree, and I expect that the code-size benefits of doing a real
analysis will be significant, especially as we move towards making
more metadata lazily computed.

The second feature is that this adds support for "abstract"
cache entries, which indicate that we know how to derive the metadata
but haven't actually done so.  This code isn't yet tested, but
it's going to be the basis of making a lot of things much lazier.
2016-01-05 17:55:51 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
John McCall
ba3959681d Rename tryEmitConstantHeapMetadataRef for consistency. NFC. 2015-12-16 18:20:23 -08:00
Slava Pestov
ed93dbe1a6 IRGen: Fix LLVM verifier failure with class casts when the source is an optional
We can't bitcast an i64 into an i8*, we have to do an int to pointer
cast instead.

This exposes a new issue, where dynamic casts do not support casting
from Optional<A> to A -- tracked in <rdar://problem/23122310>.

Fixes <rdar://problem/23055035>.

Swift SVN r32704
2015-10-15 05:43:33 +00:00
Joe Groff
dd97410f94 IRGen: Check class constraint when casting metatypes to AnyObject.Type.
Fixes rdar://problem/22643781.

Swift SVN r31971
2015-09-15 21:40:15 +00:00
Nadav Rotem
16bd644825 [IRGen] Remove an extra null comparison in 'exact' checked_cast_br.
rdar://22666588

This change removes a comparison and a branch on every virtual call. Before this
change we were generating code for comparing the metadata to figure out if the
incoming instance is an 'exact' cast, and then we checked if the result of the
cast was zero. This is unnecessary because we can simply reuse the result of the
exact metadata comparison. Moreover, we know that the metadata instance can't be
zero because we've emitted a load to that address that did not trap.

  %1 = getelementptr inbounds %C4main1X, %C4main1X* %0 ...
  %.metadata = load %swift.type*, %swift.type** %1  // Loading %0
  %2 = icmp eq %swift.type* %.metadata, bitcast (...)
  %3 = icmp ne %C4main1X* %0, null   ; <-----------  %0 can't be null.
  %4 = and i1 %3, %2
  br i1 %4, label %5, label %7

Swift SVN r31920
2015-09-12 05:10:53 +00:00
Michael Gottesman
0606d5843a Remove calls to raw_svector_ostream::flush()
This function is deleted since LLVM r244928.

Swift SVN r31798
2015-09-09 04:37:13 +00:00
Jordan Rose
50c0cf5319 [IRGen] Casts to an ObjC-class-bound archetype are not casts to the bound.
And they're not even guaranteed to be casts to ObjC classes. They might be
Swift subclasses of ObjC classes.

This fixes a type-safety hole where, e.g. a generic cast from NSPredicate
to NSDate was allowed because we were checking against NSObject.

rdar://problem/22242369

Swift SVN r31153
2015-08-12 01:07:31 +00:00
Dmitri Hrybenko
2fc1cbe8c1 Adjust to the new IRBuilder APIs
Swift SVN r29692
2015-06-25 22:01:24 +00:00
Slava Pestov
e6f8195d9c IRGen: Get scalar casts with existential result type working
emitScalarExistentialDowncast() would return an explosion consisting
of the original input value, followed by witness tables returned by
calling emitExistentialScalarCastFn().

The result of this explosion was then tested by comparing the first
element against NULL, which is wrong, since the first element was
set to the input value unconditionally.

Address this by changing the dynamic cast functions to take the value
as the first argument, and return it as the first element of the
return tuple. The value is not used directly, only set to NULL if the
cast fails. This makes the NULL check in visitCheckedCastBranchInst()
work as intended.

Note that now the result of the cast becomes a different LLVM value
than the input. With the dynamic cast function inlined, this should
not be an issue, since this is already the case for dynamic class cast.

There are also perhaps too many bitcast instructions generated now.
This could be cleaned up.

Fixes <rdar://problem/20920874>.

Swift SVN r28712
2015-05-18 18:25:31 +00:00
Slava Pestov
f7635e4be4 IRGen: Small cleanups in GenCast.cpp
This lays the groundwork for changing the scalar dynamic cast functions
to take an extra parameter.

NFC

Swift SVN r28711
2015-05-18 18:25:30 +00:00
Joe Groff
0f79c71e1d IRGen: Fix incorrect result type when doing trivial "checked" cast to AnyObject.Type.
Fixes rdar://problem/20860954.

Swift SVN r28403
2015-05-10 21:07:29 +00:00
Manman Ren
e94aae06da [Function Attribute] add target-cpu and target-features sets if they're non-null.
All llvm::Functions created during IRGen will have target-cpu and target-features
attributes if they are non-null.

Update testing cases to expect the attribute in function definition.
Add testing case function-target-features.swift to verify target-cpu and
target-features.

rdar://20772331


Swift SVN r28186
2015-05-05 23:19:48 +00:00
Slava Pestov
6b67758372 IRGen: remove requiresProtocolWitnessTable()
We can call TypeConverter::protocolRequiresWitnessTable() instead. NFC

Swift SVN r27986
2015-04-30 18:41:06 +00:00
Joe Groff
86157d533e Runtime: Reenable metatype casting code when ObjC interop is disabled.
Fixes rdar://problem/20583365, and incidentally gets test/Interpreter/layout_reabstraction.swift to work without ObjC interop as well.

Swift SVN r27557
2015-04-22 03:49:12 +00:00
Joe Groff
3d1af5cdd3 IRGen: Remove erroneous assertion.
Thick metatype casts don't require ObjC interop.

Swift SVN r27413
2015-04-17 05:30:31 +00:00
Joe Groff
e29a5e4b54 IRGen: Share requiresProtocolWitnessTable implementation between SIL and IRGen.
No reason to duplicate this. NFC.

Swift SVN r27328
2015-04-15 21:16:42 +00:00
Andrew Trick
fde264aaa1 IRGen: handle checked_cast_br of a metatype.
This is needed to support inline caching of self.dynamicType.foo().

Fixes <rdar://problem/19888836> Swift compiler segmentation fault during static analysis

Swift SVN r25686
2015-03-02 18:10:36 +00:00
Joe Groff
7e78ff68d1 IRGen/Runtime: Make dynamic metatype-to-AnyObject casts work.
Bonus fix for rdar://problem/19624697.

Swift SVN r24808
2015-01-28 23:06:45 +00:00
Joe Groff
bf9a17bef3 IRGen: Don't emit a check for class-to-AnyObject "checked" casts.
They don't need one, and nobody really conforms to the AnyObject sham protocol at runtime. Fixes rdar://problem/19624697, though there's going to be a similiar problem with metatype-to-AnyObject casts that needs fixing as well.

Swift SVN r24802
2015-01-28 19:26:17 +00:00
Joe Groff
2c257fa78a IRGen: Fix assertion failure when doing a checked cast to AnyObject.
The optimizer could eliminate these but isn't there yet. These instructions are valid nonetheless and should be emittable. Fixes rdar://problem/18934125.

Swift SVN r23269
2014-11-12 17:11:30 +00:00
Joe Groff
7ce4ea860c IRGen/Runtime: Handle non-class-metatype to ObjC existential casts.
These always fail, and it doesn't make sense to inline this check into the cast site, so provide additional runtime functions for metatype-to-objc-existential casts.

Swift SVN r23237
2014-11-11 18:51:09 +00:00
Joe Groff
5ed6e2731b IRGen: Code gen for casts to existential metatype.
The code path here is mostly the same as the class cast case--we have to test the ObjC class (if it is a class) against any ObjC protocols, then look up conformances for the native protocols.

Swift SVN r23184
2014-11-09 02:40:25 +00:00
Joe Groff
f229840161 IRGen: Code generation for general class existential casts.
Emit a helper that invokes conformsToProtocol the appropriate number of times for the destination protocol type.

Swift SVN r23176
2014-11-08 04:53:46 +00:00
Joe Groff
2047cec286 IRGen: Remove more obsolete casting entry points.
Swift SVN r23137
2014-11-06 22:09:48 +00:00
Joe Groff
64a2838ff9 Remove dead function.
Swift SVN r23136
2014-11-06 21:50:06 +00:00