Commit Graph

745 Commits

Author SHA1 Message Date
Adrian Prantl
b8f15cf0f5 Debug info: Fix a bug in the argument numbering for exploded arguments
that manifested itself as tuple arguments having multiple entries.

rdar://problem/20005023

Swift SVN r28841
2015-05-20 20:09:37 +00:00
Slava Pestov
8e179d646e IRGen: get unchecked_ref_cast working with class existential operand
There was some duplicated code and we weren't doing a claimAll() on the
input explosion in one of the duplicates.

This partially reverts r28686, but keeping the test and updating it.

Fixes <rdar://problem/21005110>.

Swift SVN r28840
2015-05-20 19:57:05 +00:00
Andrew Trick
1ba9b58668 Add SILFunction::hasSelfMetadataParam() and getSelfMetadataArgument().
I want IRGen and the SIL optimizer to use consistent logic.

Swift SVN r28785
2015-05-19 20:54:07 +00:00
Roman Levenstein
42fb70a413 [sil-verifier] Allow upcasts between optional types.
Allow for
 upcast %0 : $Optional<D> to $Optional<C>
as long as D is a subclass of C.

Swift SVN r28433
2015-05-11 19:34:22 +00:00
Adrian Prantl
f18d802c19 Debug Info: Emit the error result argument.
If a function has an error result slot, emit debug info for an artificial
function argument "$error" with indirect storage that holds a pointer
to the error object address.

<rdar://problem/20736999> lldb needs to be able to locate the error return location when stepping out of a function

Swift SVN r28229
2015-05-07 00:29:40 +00:00
Adrian Prantl
2aff7e78d7 Debug Info: Mangle the AST type instead of the SIL type for local variables
and function arguments.

<rdar://problem/20565180> Debug info should not expose ImplFunctionType

Swift SVN r28148
2015-05-05 03:50:28 +00:00
Erik Eckstein
94b3f8c545 IRGen: improve debug logging for multi-threaded compilation
Swift SVN r28115
2015-05-04 15:10:49 +00:00
Chris Lattner
7733b060c5 Enhance IRGen to know a simple heuristic in emitValueBitCast to avoid
going to the stack when two explosions will end up with the same elements.
This helps with optional to optional conversions.  We now compile:

public func opt_to_opt_trivial(x: Int?) -> Int! { return x }
public func opt_to_opt_reference(x : C!) -> C? { return x }

into:

define { i64, i1 } @_TF1x18opt_to_opt_trivialFGSqSi_GSQSi_(i64, i1) {
entry:
  %2 = insertvalue { i64, i1 } undef, i64 %0, 0
  %3 = insertvalue { i64, i1 } %2, i1 %1, 1
  ret { i64, i1 } %3
}

define i64 @_TF1x20opt_to_opt_referenceFGSQCS_1C_GSqS0__(i64) {
entry:
  ret i64 %0
}

instead of:

define { i64, i1 } @_TF1x18opt_to_opt_trivialFGSqSi_GSQSi_(i64, i1) {
entry:
  %bitcast = alloca %Sq, align 8
  %2 = bitcast %Sq* %bitcast to i64*
  store i64 %0, i64* %2, align 8
  %3 = getelementptr inbounds %Sq, %Sq* %bitcast, i32 0, i32 1
  %4 = bitcast [1 x i8]* %3 to i1*
  store i1 %1, i1* %4, align 8
  %5 = bitcast %Sq* %bitcast to %SQ*
  %6 = bitcast %SQ* %5 to i64*
  %7 = load i64, i64* %6, align 8
  %8 = getelementptr inbounds %SQ, %SQ* %5, i32 0, i32 1
  %9 = bitcast [1 x i8]* %8 to i1*
  %10 = load i1, i1* %9, align 8
  %11 = insertvalue { i64, i1 } undef, i64 %7, 0
  %12 = insertvalue { i64, i1 } %11, i1 %10, 1
  ret { i64, i1 } %12
}

define i64 @_TF1x20opt_to_opt_referenceFGSQCS_1C_GSqS0__(i64) {
entry:
  %bitcast = alloca %SQ.17, align 8
  %1 = bitcast %SQ.17* %bitcast to i64*
  store i64 %0, i64* %1, align 8
  %2 = bitcast %SQ.17* %bitcast to %Sq.16*
  %3 = bitcast %Sq.16* %2 to i64*
  %4 = load i64, i64* %3, align 8
  ret i64 %4
}




Swift SVN r28107
2015-05-04 03:50:48 +00:00
Andrew Trick
5bc23acf75 Add wrappers around Builtin.isUnique.
Currently they do nothing but allow stdlib code to use regular (Bool)
types.  However, soon the wrappers for the _native variants will
provide point-of-use sanity checking.

These need to be fully generic to support class protocols and
single-payload enums (not just for optional). It also avoids a massive
amount of overloading for all the reference type variations
(AnyObject, Native, Unknown, Bridge) x 2 for optional versions of
each.

Because the wrapper is generic, type checking had to be deferred until
IRGen. Generating code for the wrapper itself will result in an
IRGen-time type error. They need to be transparent anyway for proper
diagnostics, but also must be internal.

Note that the similar external API type checks ok because it
forces conformance to AnyObject.

The sanity checks are disabled because our current facilities for
unsafe type casting are incomplete and unsound. SILCombine can
remove UnsafeMutablePointer and RawPointer casts by assuming layout
compatibility. IRGen will later discover layout incompatibility and
generate a trap.

I'll send out a proposal for improving the casting situation so we can
get the sanity checks back.

Swift SVN r28057
2015-05-01 23:45:44 +00:00
Andrew Trick
ec31f6dbc9 Revert "Add wrappers around Builtin.isUnique."
This reverts commit 64e9f11211a19fa603f5bc2d2bea171a9b07d3fa.

I think this is breaking ExistentialCollection test in the
Release + stdlib asserts build.

Swift SVN r27947
2015-04-29 23:23:13 +00:00
Andrew Trick
09db0dda92 Add wrappers around Builtin.isUnique.
The wrappers for the _native variants provide point-of-use sanity checking.
They also allows stdlib code to use regular (Bool) types.

These need to be fully generic to support class protocols. It also
avoids a massive amount of overloading for all the reference type
variations (AnyObject, Native, Unknown, Bridge) x 2 for optional
versions of each.

Because the wrapper is generic, type checking had to be deferred until
IRGen. Generating code for the wrapper itself will result in an
IRGen-time type error. They need to be transparent anyway for proper
diagnostics, but also must be internal.

The external API passes type checks because it forces conformance to AnyObject.

Swift SVN r27930
2015-04-29 21:59:19 +00:00
Andrew Trick
a174aa4dfe Add AST and SILGen support for Builtin.isUnique.
Preparation to fix <rdar://problem/18151694> Add Builtin.checkUnique
to avoid lost Array copies.

This adds the following new builtins:

    isUnique : <T> (inout T[?]) -> Int1
    isUniqueOrPinned : <T> (inout T[?]) -> Int1

These builtins take an inout object reference and return a
boolean. Passing the reference inout forces the optimizer to preserve
a retain distinct from what’s required to maintain lifetime for any of
the reference's source-level copies, because the called function is
allowed to replace the reference, thereby releasing the referent.

Before this change, the API entry points for uniqueness checking
already took an inout reference. However, after full inlining, it was
possible for two source-level variables that reference the same object
to appear to be the same variable from the optimizer's perspective
because an address to the variable was longer taken at the point of
checking uniqueness. Consequently the optimizer could remove
"redundant" copies which were actually needed to implement
copy-on-write semantics. With a builtin, the variable whose reference
is being checked for uniqueness appears mutable at the level of an
individual SIL instruction.

The kind of reference count checking that Builtin.isUnique performs
depends on the argument type:

    - Native object types are directly checked by reading the
      strong reference count:
      (Builtin.NativeObject, known native class reference)

    - Objective-C object types require an additional check that the
      dynamic object type uses native swift reference counting:
      (Builtin.UnknownObject, unknown class reference, class existential)

    - Bridged object types allow the dymanic object type check to be
      bypassed based on the pointer encoding:
      (Builtin.BridgeObject)

Any of the above types may also be wrapped in an optional.  If the
static argument type is optional, then a null check is also performed.

Thus, isUnique only returns true for non-null, native swift object
references with a strong reference count of one.

isUniqueOrPinned has the same semantics as isUnique except that it
also returns true if the object is marked pinned regardless of the
reference count. This allows for simultaneous non-structural
modification of multiple subobjects.

In some cases, the standard library can dynamically determine that it
has a native reference even though the static type is a bridge or
unknown object. Unsafe variants of the builtin are available to allow
the additional pointer bit mask and dynamic class lookup to be
bypassed in these cases:

    isUnique_native : <T> (inout T[?]) -> Int1
    isUniqueOrPinned_native : <T> (inout T[?]) -> Int1

These builtins perform an implicit cast to NativeObject before
checking uniqueness. There’s no way at SIL level to cast the address
of a reference, so we need to encapsulate this operation as part of
the builtin.

Swift SVN r27887
2015-04-28 22:54:24 +00:00
Nadav Rotem
3e8c871c6a Emit calls to swift_willThrow as part of SILGen using a builtin.
Calls to willThrow are marked as read-none so that the optimizer can remove
them. The willThrow builtin is still generated for all throw/rethrow sites,
but I plan to look at this next.

rdar://20356658

Swift SVN r27877
2015-04-28 18:36:22 +00:00
Erik Eckstein
2a366df113 Remove SILType::isExistentialMetatypeType().
Suggested by John.



Swift SVN r27864
2015-04-28 08:43:15 +00:00
Erik Eckstein
519dfca347 IRGen: fix crash on x.dynamicType.dynamicType.
rdar://problem/19545563



Swift SVN r27862
2015-04-28 07:36:13 +00:00
Nadav Rotem
217d6ba5a6 Don't emit calls to swift_willThrow in optimized builds.
rdar://20356658.

Swift SVN r27764
2015-04-26 16:41:23 +00:00
Nadav Rotem
9b2b75b859 IRGen: Insert a call to swift_willThrow to allow lldb to place a breakpoint.
rdar://20356658.

Swift SVN r27710
2015-04-24 22:09:15 +00:00
Chris Lattner
42b4a966b0 Introduce a new null_class SIL instruction for forming a null pointer
reference to something of class type.  This is required to model
RebindSelfInConstructorExpr correctly to DI, since in the class case, 
self.init and super.init *take* a value out of class box so that it 
can pass the +1 value without performing an extra retain.  Nothing
else in the compiler uninitializes a DI-controlled memory object
like this, so nothing else needs this.  DI really doesn't like something
going from initialized to uninitialized.

Yes, I feel super-gross about this and am really unhappy about it.  I
may end up reverting this if I can find an alternate solution to this
problem.



Swift SVN r27525
2015-04-21 05:56:55 +00:00
Erik Eckstein
7dcbdb0c3f IRGen for bridge_object_to_ref: don't emit a check for a tagged pointer if not required.
Swift SVN r27122
2015-04-08 09:25:12 +00:00
Joe Groff
ad0d20c07a Fold "AbstractCC" into SILFunctionType::Representation.
These aren't really orthogonal concerns--you'll never have a @thick @cc(objc_method), or an @objc_block @cc(witness_method)--and we have gross decision trees all over the codebase that try to hopscotch between the subset of combinations that make sense. Stop the madness by eliminating AbstractCC and folding its states into SILFunctionTypeRepresentation. This cleans up a ton of code across the compiler.

I couldn't quite eliminate AbstractCC's information from AST function types, since SIL type lowering transiently created AnyFunctionTypes with AbstractCCs set, even though these never occur at the source level. To accommodate type lowering, allow AnyFunctionType::ExtInfo to carry a SILFunctionTypeRepresentation, and arrange for the overlapping representations to share raw values.

In order to avoid disturbing test output, AST and SILFunctionTypes are still printed and parsed using the existing @thin/@thick/@objc_block and @cc() attributes, which is kind of gross, but lets me stage in the real source-breaking change separately.

Swift SVN r27095
2015-04-07 21:59:39 +00:00
Joe Groff
4821f594bb SIL: Separate SILFunctionType::Representation and ExtInfo from AST FunctionTypes.
The set of attributes that make sense at the AST level is increasingly divergent from those at the SIL level, so it doesn't really make sense for these to be the same. It'll also help prevent us from accidental unwanted propagation of attributes from the AST to SIL, which has caused bugs in the past. For staging purposes, start off with SILFunctionType's versions exactly the same as the FunctionType versions, which necessitates some ugly glue code but minimizes the potential disruption.

Swift SVN r27022
2015-04-05 17:04:55 +00:00
John McCall
dab7302d7c Finish wiring up try_apply in IRGen.
Swift SVN r26821
2015-04-01 21:08:25 +00:00
John McCall
6d8fff9c06 Parsing and basic structure of try_apply. Not yet properly
threaded into IRGen; tests to follow when that's done.

I made a preliminary effort to make the inliner do the
right thing with try_apply, but otherwise tried to avoid
touching the optimizer any more than was required by the
removal of ApplyInstBase.

Swift SVN r26747
2015-03-31 02:41:03 +00:00
John McCall
1ffb87bb1f Implement a 'throw' instruction in SIL.
Swift SVN r26668
2015-03-28 02:00:20 +00:00
John McCall
dc5a03a7bc Add IRGen support for error results from functions.
As part of this, re-arrange the argument order so that
generic arguments come before the context, which comes
before the error result.  Be more consistent about always
adding a context parameter on thick functions, even
when it's unused.  Pull out the witness-method Self
argument so that it appears last after the error
argument.

Swift SVN r26667
2015-03-28 02:00:17 +00:00
Joe Groff
1c18a71ab7 IRGen: Implement boxed existential instructions.
Provide a special single-ObjC-refcounted type info for error existentials, and lower the existential box instructions to their corresponding runtime calls.

Swift SVN r26469
2015-03-24 01:10:31 +00:00
Chris Lattner
01f3e81aa5 Work on VarDecl:
- Rename getParentPattern() -> getParentPatternBinding(), since
   it returns the pattern binding, not the pattern.
 - Introduce new getParentPattern()/getParentInitializer() methods,
   covering the most common uses of getParentPatternBinding().

NFC.



Swift SVN r26175
2015-03-16 01:54:20 +00:00
Joe Groff
fdde2a8e99 SIL: Add instructions for boxed existential operations.
Parsing and serialization for {Alloc,Open,Dealloc}ExistentialBox instructions to represent operations on ErrorType boxes.

Swift SVN r26145
2015-03-15 03:32:37 +00:00
Chris Lattner
4f708c049b fix const correctness and standardize on names for the successor list of
TerminatorInsts.  Now you can walk over the successor list of a terminator
and actually modify the SILSuccessor directly, allowing better CFG
transformations.  NFC.




Swift SVN r26140
2015-03-14 17:52:27 +00:00
Chris Lattner
c1bc639d7b add an IRGen peephole for lowering certain switch_enum's that commonly
come up when pattern matching on optional.  Instead of expanding to a
switch with a default and one case, expand to an icmp and condbr.



Swift SVN r26090
2015-03-13 05:14:49 +00:00
Erik Eckstein
22991b1088 Multi-threaded llvm code generation (experimental).
It can be enabled with the -num-threads <n> option.
Without this option there should be NFC.

When enabled, the LLVM IR is split into multiple modules: one module for each input file.
And for each module an output file is generated. All output files must be specified with -o options:
for each input file in the command line there must be an -o <outputfile> option.

LLVM compilation is performed on each module separately.
This means that the generated code is different than with regular -wmo.
But performance and code size should be approximately the same because important inter-file
optimizations are already done at SIL level (e.g. inlining, specialization).

There is still no support in the driver for this feature.



Swift SVN r25930
2015-03-10 16:52:14 +00:00
Joe Groff
962a87f444 SIL: Rename address-only existential instructions to '{init,deinit,open}_existential_addr'.
For better consistency with other address-only instruction variants, and to open the door to new exciting existential representations (such as a refcounted boxed representation for ErrorType).

Swift SVN r25902
2015-03-09 23:55:31 +00:00
Joe Groff
aed4aa6289 IRGen: Order block context parameters correctly during parameter lowering.
Insert a block's context argument into the normal parameter set before lowering external arguments, instead of adding it later in a post-pass, so that we assign argument attributes to the correct indices. Fix signature expansion to put the block context parameter in the right place relative to struct returns. Fixes rdar://problem/20035272.

Swift SVN r25763
2015-03-04 21:06:11 +00:00
Adrian Prantl
55e76bf15a Debug Info: Be sure to reset the debug location before entering a new basic
block in IRGenSIL.

Fixes <rdar://problem/19675260> Step over function to debug Swift code doesn't work correctly.

Swift SVN r25287
2015-02-13 23:01:43 +00:00
Adrian Prantl
788d9f5777 Simplify expression.
Swift SVN r25286
2015-02-13 23:01:35 +00:00
Erik Eckstein
4aac127226 Don't inline into thunks, except very small functions.
rdar://problem/19701613

Code size reductions (negative means less code size):
bin/PerfTests_O:  -3.7%
bin/PerfTests_Ounchecked:  -1.9%
bin/PerfTests_Onone:  +0.2%
stdlib/core/macosx/Swift.o:  -2.2%

The -2.2% in Swift.o constitutes of about +5% in specializations and -11% in protocoll witnesses in the dylib.
(-> still room for improvement regarding specializations)

Note that completely disabling inlining into thunks (even small functions) would increase the code size.

There is litte change in performance, a few + and - within 10%.
Beyond this there is (+ means faster):
Phonebook@O: +26%
ImageProc@Ounchecked: +14%
StringWalk@Ounchecked: -16%




Swift SVN r25001
2015-02-05 17:39:39 +00:00
John McCall
5f8a81ad8f Reapply r24949. My search for uses of ABIArgInfo::Expand
was incomplete; we do use it on i386 for arbitrary small
aggregates, and unfortunately this means we have to learn
how to expand quite a lot of types.

Tested by the i386-simulator build.  I still think
we can completely remove this code relatively soon.

Swift SVN r24981
2015-02-05 00:00:31 +00:00
Joe Groff
d2a17b3b1b IRGen: Handle all class-to-class casts, not just super-to-sub casts.
Specialization can introduce 'A as! B' casts between unrelated types A and B, and we're not always smart enough to fold them away to a failure. We shouldn't crash in IRGen when we see them. Fixes rdar://problem/19544667.

Swift SVN r24971
2015-02-04 19:50:13 +00:00
Dmitri Hrybenko
a979680e1d Revert "Implement clang::ABIArgInfo::Expand correctly when matching"
This reverts commit r24949.  It broke the standard library build for the
32-bit iphonesimulator.

Swift SVN r24966
2015-02-04 19:29:16 +00:00
John McCall
319b5f24fc Implement clang::ABIArgInfo::Expand correctly when matching
the C calling convention.

You might notice the absence of tests.  Older versions of Clang
used Expand for ARM64 homogeneous aggregates.  Trunk Clang no
longer does this, and indeed, on Apple platforms we no longer
use Expand to pass any kind of argument at all.  The only target
in trunk that uses it is the Windows target's vectorcall
alternative CC, which I have no way to convince Swift to
generate a call against.  Therefore this code is completely
dead in trunk, and thus untestable.

Removing Expand completely from Clang would be the ideal solution,
but that's politically tricky because this is a part of Clang
that's frequently edited by people supporting out-of-tree backends.
I've started the process, but until then, if we're going to have
dead code, it ought to at least be correct dead code.

This commit fixes rdar://19199427, or at least it will
when it's merged to branches that actually use Expand.

Swift SVN r24949
2015-02-04 08:08:39 +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
Arnold Schwaighofer
2e3c0b69f7 IRGen: Emit failure blocks individually
Reapply commit r24722.

The original commit did not break the build. There appears to be an issue in
CoreAutomation (an iOS test failed with empty output). Follow up builds with
the original commit in succeeded.

Also apply Dimitri's patch to make the test condfail.sil portable.

Original message:

This significantly improves debugging experience as failures such as overflow
can be mapped back to a source line.

Code size impacts I measured on PerfTestSuite and libswift*.dylib was
neglectable.

__text section size increase:

  0.24% bin/PerfTest_O
  0.01% bin/PerfTest_Onone
  0.20% libswift*.dylib

rdar://19118593

Swift SVN r24725
2015-01-26 00:35:53 +00:00
Arnold Schwaighofer
15b7a9347d Revert "IRGen: Emit failure blocks individually"
This reverts commit r24722.

It has broken a build bot.

Swift SVN r24724
2015-01-25 23:38:13 +00:00
Arnold Schwaighofer
de50dcae24 IRGen: Emit failure blocks individually
This significantly improves debugging experience as failures such as overflow
can be mapped back to a source line.

Code size impacts I measured on PerfTestSuite and libswift*.dylib was
neglectable.

__text section size increase:

  0.24% bin/PerfTest_O
  0.01% bin/PerfTest_Onone
  0.20% libswift*.dylib

rdar://19118593

Swift SVN r24722
2015-01-25 20:55:59 +00:00
Joe Groff
1a845bfbd2 IRGen: Make partial application forwarders honor the convention of the applied parameters.
Until now, we treated all value parameters as +1, and all indirect parameters as +0 by reference, which is totally bogus. Now that we'll be partially applying guaranteed parameters, for instance to implement SomeType.foo curried method references, we need to get this right.

This breaks curries that capture dependent-layout parameters by value, but they were already broken due to us treating them as 'inout' captures. I'll fix this by adding NecessaryBindings and NonFixedOffsets to HeapLayout next.

Swift SVN r24475
2015-01-16 16:01:59 +00:00
Adrian Prantl
897e6eaace Debug info: No longer emit debug info for variables defined in implicit
patterns.

<rdar://problem/19184859> Swift methods defining closures that capture self in the "in" clause end up with two "self" variables in the debug info

Swift SVN r24461
2015-01-16 00:30:14 +00:00
Chris Lattner
6e9737ae51 Enhance IRGen of select_enum[_addr] to use the new
emitValueCaseTest/emitIndirectCaseTest implementations when testing for
one case.  This leads to dramatically better IRGen for optional (among other
things).  For example, for:

class C{}
var x : C?
if let a = x {
}

we used to produce:

  %15 = load i64* %14, align 8
  switch i64 %15, label %16 [
    i64 0, label %17
  ]

; <label>:16                                      ; preds = %entry
  br label %select_enum

; <label>:17                                      ; preds = %entry
  br label %select_enum

select_enum:                                      ; preds = %17, %16
  %18 = phi i1 [ true, %16 ], [ false, %17 ]
  br i1 %18, label %22, label %19

now we produce:

  %15 = load i64* %14, align 8
  %16 = icmp eq i64 %15, 0
  %17 = xor i1 %16, true
  br i1 %17, label %21, label %18

Hopefully this makes a measurable improvement in the -O0 performance tests, but
I didn't check.

This resolves:
<rdar://problem/19404937> select_enum/select_enum_addr should generate select in obvious cases [-O0 performance]



Swift SVN r24366
2015-01-11 23:00:00 +00:00
John McCall
275ef489d4 Switch IRGen to use ClusteredBitVector.
IRGen uses a typedef, SpareBitVector, for its principal
purpose of tracking spare bits.  Other uses should not
use this typedef, and I've tried to follow that, but I
did this rewrite mostly with sed and may have missed
some fixups.

This should be almost completely NFC.  There may be
some subtle changes in spare bits for witness tables
and other off-beat pointer types.  I also fixed a bug
where IRGen thought that thin functions were two
pointers wide, but this wouldn't have affected anything
because we never store thin functions anyway, since
they're not a valid AST type.

This commit repplies r24305 with two fixes:

  - It fixes the computation of spare bits for unusual
    integer types to use the already-agreed-upon type
    size instead of recomputing it.  This fixes the
    i386 stdlib build.  Joe and I agreed that we should
    also change the size to use the LLVM alloc size
    instead of the next power of 2, but this patch
    does not do that yet.

  - It changes the spare bits in function types back
    to the empty set.  I'll be changing this in a
    follow-up, but it needs to be tied to runtime
    changes.  This fixes the regression test failures.

Swift SVN r24324
2015-01-09 21:06:37 +00:00
Adrian Prantl
a513e03680 Debug info: Push the non-handling of indirect pieces down to
IRGenDebugInfo.

NFC.

rdar://problem/19417227.

Swift SVN r24319
2015-01-09 18:38:32 +00:00
Dmitri Hrybenko
4caca5d098 Revert "Switch IRGen to use ClusteredBitVector."
This reverts commit r24305.  It broke the standard library build.

Swift SVN r24318
2015-01-09 18:25:21 +00:00