mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
the call instead of during the formal evaluation of the argument.
This is the last major chunk of the semantic changes proposed
in the accessors document. It has two purposes, both related
to the fact that it shortens the duration of the formal access.
First, the change isolates later evaluations (as long as they
precede the call) from the formal access, preventing them from
spuriously seeing unspecified behavior. For example::
foo(&array[0], bar(array))
Here the value passed to bar is a proper copy of 'array',
and if bar() decides to stash it aside, any modifications
to 'array[0]' made by foo() will not spontaneously appear
in the copy. (In contrast, if something caused a copy of
'array' during foo()'s execution, that copy would violate
our formal access rules and would therefore be allowed to
have an arbitrary value at index 0.)
Second, when a mutating access uses a pinning addressor, the
change limits the amount of arbitrary code that falls between
the pin and unpin. For example::
array[0] += countNodes(subtree)
Previously, we would begin the access to array[0] before the
call to countNodes(). To eliminate the pin and unpin, the
optimizer would have needed to prove that countNodes didn't
access the same array. With this change, the call is evaluated
first, and the access instead begins immediately before the call
to +=. Since that operator is easily inlined, it becomes
straightforward to eliminate the pin/unpin.
A number of other changes got bundled up with this in ways that
are hard to tease apart. In particular:
- RValueSource is now ArgumentSource and can now store LValues.
- It is now illegal to use emitRValue to emit an l-value.
- Call argument emission is now smart enough to emit tuple
shuffles itself, applying abstraction patterns in reverse
through the shuffle. It also evaluates varargs elements
directly into the array.
- AllowPlusZero has been split in two. AllowImmediatePlusZero
is useful when you are going to immediately consume the value;
this is good enough to avoid copies/retains when reading a 'var'.
AllowGuaranteedPlusZero is useful when you need a stronger
guarantee, e.g. when arbitrary code might intervene between
evaluation and use; it's still good enough to avoid copies
from a 'let'. The upshot is that we're now a lot smarter
about generally avoiding retains on lets, but we've also
gotten properly paranoid about calling non-mutating methods
on vars.
(Note that you can't necessarily avoid a copy when passing
something in a var to an @in_guaranteed parameter! You
first have to prove that nothing can assign to the var during
the call. That should be easy as long as the var hasn't
escaped, but that does need to be proven first, so we can't
do it in SILGen.)
Swift SVN r24709
74 lines
2.2 KiB
C++
74 lines
2.2 KiB
C++
//===--- Varargs.h - SIL generation for (native) Swift varargs --*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// A storage structure for holding a destructured rvalue with an optional
|
|
// cleanup(s).
|
|
// Ownership of the rvalue can be "forwarded" to disable the associated
|
|
// cleanup(s).
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_LOWERING_VARARGS_H
|
|
#define SWIFT_LOWERING_VARARGS_H
|
|
|
|
#include "ManagedValue.h"
|
|
#include "swift/SIL/AbstractionPattern.h"
|
|
|
|
namespace swift {
|
|
namespace Lowering {
|
|
class SILGenFunction;
|
|
class TypeLowering;
|
|
|
|
/// Information about a varargs emission.
|
|
class VarargsInfo {
|
|
ManagedValue Array;
|
|
SILValue BaseAddress;
|
|
AbstractionPattern BasePattern;
|
|
const TypeLowering &BaseTL;
|
|
public:
|
|
VarargsInfo(ManagedValue array, SILValue baseAddress,
|
|
const TypeLowering &baseTL, AbstractionPattern basePattern)
|
|
: Array(array), BaseAddress(baseAddress), BasePattern(basePattern),
|
|
BaseTL(baseTL) {}
|
|
|
|
/// Return the array value. emitEndVarargs() is really the only
|
|
/// function that should be accessing this directly.
|
|
ManagedValue getArray() const {
|
|
return Array;
|
|
}
|
|
|
|
/// An address of the lowered type.
|
|
SILValue getBaseAddress() const { return BaseAddress; }
|
|
|
|
AbstractionPattern getBaseAbstractionPattern() const {
|
|
return BasePattern;
|
|
}
|
|
|
|
const TypeLowering &getBaseTypeLowering() const {
|
|
return BaseTL;
|
|
}
|
|
};
|
|
|
|
/// Begin a varargs emission sequence.
|
|
VarargsInfo emitBeginVarargs(SILGenFunction &gen, SILLocation loc,
|
|
CanType baseTy, CanType arrayTy,
|
|
unsigned numElements);
|
|
|
|
/// Successfully end a varargs emission sequence.
|
|
ManagedValue emitEndVarargs(SILGenFunction &gen, SILLocation loc,
|
|
VarargsInfo &&varargs);
|
|
|
|
} // end namespace Lowering
|
|
} // end namespace swift
|
|
|
|
#endif
|