mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Similarly to how we've always handled parameter types, we now recursively expand tuples in result types and separately determine a result convention for each result. The most important code-generation change here is that indirect results are now returned separately from each other and from any direct results. It is generally far better, when receiving an indirect result, to receive it as an independent result; the caller is much more likely to be able to directly receive the result in the address they want to initialize, rather than having to receive it in temporary memory and then copy parts of it into the target. The most important conceptual change here that clients and producers of SIL must be aware of is the new distinction between a SILFunctionType's *parameters* and its *argument list*. The former is just the formal parameters, derived purely from the parameter types of the original function; indirect results are no longer in this list. The latter includes the indirect result arguments; as always, all the indirect results strictly precede the parameters. Apply instructions and entry block arguments follow the argument list, not the parameter list. A relatively minor change is that there can now be multiple direct results, each with its own result convention. This is a minor change because I've chosen to leave return instructions as taking a single operand and apply instructions as producing a single result; when the type describes multiple results, they are implicitly bound up in a tuple. It might make sense to split these up and allow e.g. return instructions to take a list of operands; however, it's not clear what to do on the caller side, and this would be a major change that can be separated out from this already over-large patch. Unsurprisingly, the most invasive changes here are in SILGen; this requires substantial reworking of both call emission and reabstraction. It also proved important to switch several SILGen operations over to work with RValue instead of ManagedValue, since otherwise they would be forced to spuriously "implode" buffers.
296 lines
11 KiB
C++
296 lines
11 KiB
C++
//===--- Initialization.h - Buffer initialization. --------------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 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 representing a buffer or collection of buffers to
|
|
// be initialized.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_LOWERING_INITIALIZATION_H
|
|
#define SWIFT_LOWERING_INITIALIZATION_H
|
|
|
|
#include "ManagedValue.h"
|
|
#include "llvm/ADT/TinyPtrVector.h"
|
|
#include <memory>
|
|
|
|
namespace swift {
|
|
namespace Lowering {
|
|
class SILGenFunction;
|
|
|
|
class Initialization;
|
|
using InitializationPtr = std::unique_ptr<Initialization>;
|
|
|
|
/// An abstract class for consuming a value. This is used for initializing
|
|
/// variables, although that is not the only way it is used.
|
|
///
|
|
/// Implementations of this interface deal with the details of managing
|
|
/// cleanups for the received value, as well as potentially managing partial
|
|
/// cleanups of components of the value during the operation.
|
|
///
|
|
/// For example, during the initialization of a boxed local variable, it
|
|
/// is invalid to release the box, because that will attempt to destroy
|
|
/// the uninitialized value. Instead, a cleanup to deallocate the box
|
|
/// (with `dealloc_ref`) must be active; once initialization is complete,
|
|
/// that cleanup (and any separate cleanup on the boxed value) must be
|
|
/// deactivated, and a cleanup to release the box can be enabled instead.
|
|
///
|
|
/// This interface supports four ways to receive the initializing value:
|
|
///
|
|
/// - If getAddressForInPlaceInitialization() returns non-null, the
|
|
/// initializing value may be created directly in that location.
|
|
/// It is legal to call getAddressForInPlaceInitialization()
|
|
/// multiple times.
|
|
///
|
|
/// - If canSplitIntoTupleElements() returns true, getTupleElements may
|
|
/// be called. It is not legal to call getTupleElements multiple times.
|
|
/// Once getTupleElements has been called, the returned initializations
|
|
/// must be completely initialized (including calling
|
|
/// finishInitialization) before finishInitialization is called on the
|
|
/// outer initialization.
|
|
///
|
|
/// - copyOrInitValueInto may be called.
|
|
///
|
|
/// In all of these cases, finishInitialization must be called after
|
|
/// initialization is complete.
|
|
///
|
|
/// Alternatively, some "initializers" may call finishUninitialized if there
|
|
/// was no immediate initializer. This is generally not possibly when the
|
|
/// Initialization is used merely as the destination for expression emission;
|
|
/// an Initialization subclass only need implement this when the subclass
|
|
/// might be used for an irrefutable pattern lacking an initializer.
|
|
///
|
|
/// FIXME: provide a reset() operation to support multiple
|
|
/// initialization paths.
|
|
class Initialization {
|
|
public:
|
|
Initialization() {}
|
|
virtual ~Initialization() {}
|
|
|
|
/// Return true if this initialization is a simple address in memory.
|
|
virtual bool isSingleBuffer() const {
|
|
return false;
|
|
}
|
|
|
|
/// If this initialization represents a single contiguous buffer, return the
|
|
/// SILValue of that buffer's address. If not, returns an invalid SILValue.
|
|
virtual SILValue getAddressOrNull() const = 0;
|
|
|
|
/// Returns the address of the single contiguous buffer represented by this
|
|
/// initialization. Once the address has been stored to,
|
|
/// finishInitialization must be called.
|
|
SILValue getAddress() const {
|
|
SILValue address = getAddressOrNull();
|
|
assert(address && "initialization does not represent a single buffer");
|
|
return address;
|
|
}
|
|
|
|
|
|
/// If this initialization has an address we can directly emit into, return
|
|
/// it. Otherwise, return a null SILValue.
|
|
virtual SILValue getAddressForInPlaceInitialization() const {
|
|
return SILValue();
|
|
}
|
|
|
|
/// Return true if we can get the addresses of elements with the
|
|
/// 'splitIntoTupleElements' method. Subclasses can override this to
|
|
/// enable this behavior.
|
|
virtual bool canSplitIntoTupleElements() const {
|
|
return false;
|
|
}
|
|
|
|
/// Break this initialization (which expects a value of tuple type)
|
|
/// into component sub-initializations for the elements. This
|
|
/// only destructures a single level of tuple.
|
|
///
|
|
/// Once this method is called, the caller must ensure the complete
|
|
/// initialization of the result initializations, including calling
|
|
/// finishInitialization on them. It is still necessary to call
|
|
/// finishInitialization on the tuple initialization after this is done.
|
|
///
|
|
/// \param buf - If new Initializations need to be created, their ownership
|
|
/// is given to this vector. The caller should not otherwise interact
|
|
/// with these initializations.
|
|
/// \param loc The location for any instructions required to split the
|
|
/// initialization.
|
|
virtual MutableArrayRef<InitializationPtr>
|
|
splitIntoTupleElements(SILGenFunction &gen, SILLocation loc, CanType type,
|
|
SmallVectorImpl<InitializationPtr> &buf) {
|
|
llvm_unreachable("Must implement if canSplitIntoTupleElements "
|
|
"returns true");
|
|
}
|
|
|
|
/// Initialize this with the given value. This should be an operation
|
|
/// of last resort: it is generally better to split tuples or evaluate
|
|
/// in-place when the initialization supports that.
|
|
///
|
|
/// If this is an *copy* of the rvalue into this initialization then isInit is
|
|
/// false. If it is an *initialization* of the memory in the initialization,
|
|
/// then isInit is true.
|
|
virtual void copyOrInitValueInto(SILGenFunction &gen, SILLocation loc,
|
|
ManagedValue explodedElement,
|
|
bool isInit) = 0;
|
|
|
|
/// Perform post-initialization bookkeeping for this initialization.
|
|
virtual void finishInitialization(SILGenFunction &gen) {}
|
|
|
|
/// Perform post-initialization bookkeeping for this initialization,
|
|
/// given that it wasn't actually initialized.
|
|
virtual void finishUninitialized(SILGenFunction &gen) {
|
|
llvm_unreachable("Initialization subclass does not support being left "
|
|
"uninitialized");
|
|
}
|
|
|
|
private:
|
|
Initialization(const Initialization &) = delete;
|
|
Initialization(Initialization &&) = delete;
|
|
|
|
virtual void _anchor();
|
|
};
|
|
|
|
/// Abstract base class for single-buffer initializations. These are
|
|
/// initializations that have an addressable memory object to be stored into.
|
|
class SingleBufferInitialization : public Initialization {
|
|
llvm::TinyPtrVector<CleanupHandle::AsPointer> SplitCleanups;
|
|
public:
|
|
SingleBufferInitialization() {}
|
|
|
|
bool isSingleBuffer() const override {
|
|
return true;
|
|
}
|
|
|
|
// SingleBufferInitializations always have an address.
|
|
SILValue getAddressForInPlaceInitialization() const override {
|
|
return getAddress();
|
|
}
|
|
|
|
bool canSplitIntoTupleElements() const override {
|
|
return true;
|
|
}
|
|
|
|
MutableArrayRef<InitializationPtr>
|
|
splitIntoTupleElements(SILGenFunction &gen, SILLocation loc, CanType type,
|
|
SmallVectorImpl<InitializationPtr> &buf) override;
|
|
|
|
void copyOrInitValueInto(SILGenFunction &gen, SILLocation loc,
|
|
ManagedValue value, bool isInit) override {
|
|
copyOrInitValueIntoSingleBuffer(gen, loc, value, isInit, getAddress());
|
|
}
|
|
|
|
/// Overriders must call this.
|
|
void finishInitialization(SILGenFunction &gen) override;
|
|
|
|
/// Emit the exploded element into a buffer at the specified address.
|
|
static void copyOrInitValueIntoSingleBuffer(SILGenFunction &gen,
|
|
SILLocation loc,
|
|
ManagedValue value,
|
|
bool isInit,
|
|
SILValue bufferAddress);
|
|
|
|
static MutableArrayRef<InitializationPtr>
|
|
splitSingleBufferIntoTupleElements(SILGenFunction &gen, SILLocation loc,
|
|
CanType type, SILValue bufferAddress,
|
|
SmallVectorImpl<InitializationPtr> &buf,
|
|
TinyPtrVector<CleanupHandle::AsPointer> &splitCleanups);
|
|
};
|
|
|
|
/// This is an initialization for a specific address in memory.
|
|
class KnownAddressInitialization : public SingleBufferInitialization {
|
|
/// The physical address of the global.
|
|
SILValue address;
|
|
|
|
virtual void anchor() const;
|
|
public:
|
|
KnownAddressInitialization(SILValue address) : address(address) {}
|
|
|
|
SILValue getAddressOrNull() const override {
|
|
return address;
|
|
}
|
|
|
|
void finishUninitialized(SILGenFunction &gen) override {}
|
|
};
|
|
|
|
/// Abstract base class for single-buffer initializations.
|
|
class TemporaryInitialization : public SingleBufferInitialization {
|
|
SILValue Addr;
|
|
CleanupHandle Cleanup;
|
|
public:
|
|
TemporaryInitialization(SILValue addr, CleanupHandle cleanup)
|
|
: Addr(addr), Cleanup(cleanup) {}
|
|
|
|
void finishInitialization(SILGenFunction &gen) override;
|
|
|
|
void finishUninitialized(SILGenFunction &gen) override {
|
|
TemporaryInitialization::finishInitialization(gen);
|
|
}
|
|
|
|
SILValue getAddressOrNull() const override {
|
|
return Addr;
|
|
}
|
|
|
|
SILValue getAddress() const {
|
|
return Addr;
|
|
}
|
|
|
|
/// Returns the cleanup corresponding to the value of the temporary.
|
|
CleanupHandle getInitializedCleanup() const { return Cleanup; }
|
|
|
|
ManagedValue getManagedAddress() const {
|
|
return ManagedValue(getAddress(), getInitializedCleanup());
|
|
}
|
|
};
|
|
|
|
/// An initialization which accumulates several other initializations
|
|
/// into a tuple.
|
|
class TupleInitialization : public Initialization {
|
|
public:
|
|
/// The sub-Initializations aggregated by this tuple initialization.
|
|
/// The TupleInitialization object takes ownership of Initializations pushed
|
|
/// here.
|
|
SmallVector<InitializationPtr, 4> SubInitializations;
|
|
|
|
TupleInitialization() {}
|
|
|
|
SILValue getAddressOrNull() const override {
|
|
if (SubInitializations.size() == 1)
|
|
return SubInitializations[0]->getAddressOrNull();
|
|
else
|
|
return SILValue();
|
|
}
|
|
|
|
bool canSplitIntoTupleElements() const override {
|
|
return true;
|
|
}
|
|
|
|
MutableArrayRef<InitializationPtr>
|
|
splitIntoTupleElements(SILGenFunction &gen, SILLocation loc, CanType type,
|
|
SmallVectorImpl<InitializationPtr> &buf) override {
|
|
return SubInitializations;
|
|
}
|
|
|
|
void copyOrInitValueInto(SILGenFunction &gen, SILLocation loc,
|
|
ManagedValue valueMV, bool isInit) override;
|
|
|
|
// We don't need to do anything in finishInitialization. There are two
|
|
// ways to initialize a TupleInitialization:
|
|
// - splitting the initialization, in which case the initializer is
|
|
// responsible for finishing the sub-initializations itself, or
|
|
// - calling copyOrInitValueInto, which immediately finishes all
|
|
// of the sub-initializations.
|
|
|
|
void finishUninitialized(SILGenFunction &gen) override;
|
|
};
|
|
|
|
} // end namespace Lowering
|
|
} // end namespace swift
|
|
|
|
#endif
|