Files
swift-mirror/lib/IRGen/GenHeap.h
Nate Chandler 506473dfba [Async CC] Supported partial application.
The majority of support comes in the form of emitting partial
application forwarders for partial applications of async functions.
Such a partial application forwarder must take an async context which
has been partially populated at the apply site.  It is responsible for
populating it "the rest of the way".  To do so, like sync partial
application forwarders, it takes a second argument, its context, from
which it pulls the additional arguments which were capture at
partial_apply time.

The size of the async context that is passed to the forwarder, however,
can't be known at the apply site by simply looking at the signature of
the function to be applied (not even by looking at the size associated
with the function in the special async function pointer constant which
will soon be emitted).  The reason is that there are an unknown (at the
apply site) number of additional arguments which will be filled by the
partial apply forwarder (and in the case of repeated partial
applications, further filled in incrementally at each level).  To enable
this, there will always be a heap object for thick async functions.
These heap objects will always store the size of the async context to be
allocated as their first element.  (Note that it may be possible to
apply the same optimization that was applied for thick sync functions
where a single refcounted object could be used as the context; doing so,
however, must be made to interact properly with the async context size
stored in the heap object.)

To continue to allow promoting thin async functions to thick async
functions without incurring a thunk, at the apply site, a null-check
will be performed on the context pointer.  If it is null, then the async
context size will be determined based on the signature.  (When async
function pointers become pointers to a constant with a size i32 and a
relative address to the underlying function, the size will be read from
that constant.)  When it is not-null, the size will be pulled from the
first field of the context (which will in that case be cast to
<{%swift.refcounted, i32}>).

To facilitate sharing code and preserving the original structure of
emitPartialApplicationForwarder (which weighed in at roughly 700 lines
prior to this change), a new small class hierarchy, descending from
PartialApplicationForwarderEmission has been added, with subclasses for
the sync and async case.  The shuffling of arguments into and out of the
final explosion that was being performed in the synchronous case has
been preserved there, though the arguments are added and removed through
a number of methods on the superclass with more descriptive names.  That
was necessary to enable the async class to handle these different
flavors of parameters correctly.

To get some initial test coverage, the preexisting
IRGen/partial_apply.sil and IRGen/partial_apply_forwarder.sil tests have
been duplicated into the async folder.  Those tests cases within these
files which happened to have been crashing have each been extracted into
its own runnable test that both verifies that the compiler does not
crash and also that the partial application forwarder behaves correctly.
The FileChecks in these tests are extremely minimal, providing only
enough information to be sure that arguments are in fact squeezed into
an async context.
2020-10-22 12:21:56 -07:00

188 lines
6.8 KiB
C++

//===--- GenHeap.h - Heap-object layout and management ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines some routines that are useful for emitting
// operations on heap objects and their metadata.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_GENHEAP_H
#define SWIFT_IRGEN_GENHEAP_H
#include "NecessaryBindings.h"
#include "StructLayout.h"
namespace llvm {
class Constant;
template <class T> class SmallVectorImpl;
}
namespace swift {
namespace irgen {
class Address;
class OwnedAddress;
enum class IsaEncoding : unsigned char;
/// A heap layout is the result of laying out a complete structure for
/// heap-allocation.
class HeapLayout : public StructLayout {
SmallVector<SILType, 8> ElementTypes;
NecessaryBindings Bindings;
unsigned BindingsIndex;
mutable llvm::Constant *privateMetadata = nullptr;
public:
HeapLayout(IRGenModule &IGM, LayoutStrategy strategy,
ArrayRef<SILType> elementTypes,
ArrayRef<const TypeInfo *> elementTypeInfos,
llvm::StructType *typeToFill = 0,
NecessaryBindings &&bindings = {}, unsigned bindingsIndex = 0);
/// True if the heap object carries type bindings.
///
/// If true, the first element of the heap layout will be the type metadata
/// buffer.
bool hasBindings() const {
return !Bindings.empty();
}
const NecessaryBindings &getBindings() const {
return Bindings;
}
unsigned getBindingsIndex() const { return BindingsIndex; }
unsigned getIndexAfterBindings() const {
return BindingsIndex + (hasBindings() ? 1 : 0);
}
/// Get the types of the elements.
ArrayRef<SILType> getElementTypes() const {
return ElementTypes;
}
/// Build a size function for this layout.
llvm::Constant *createSizeFn(IRGenModule &IGM) const;
/// As a convenience, build a metadata object with internal linkage
/// consisting solely of the standard heap metadata.
llvm::Constant *getPrivateMetadata(IRGenModule &IGM,
llvm::Constant *captureDescriptor) const;
};
class HeapNonFixedOffsets : public NonFixedOffsetsImpl {
SmallVector<llvm::Value *, 1> Offsets;
llvm::Value *TotalSize;
llvm::Value *TotalAlignMask;
public:
HeapNonFixedOffsets(IRGenFunction &IGF, const HeapLayout &layout);
llvm::Value *getOffsetForIndex(IRGenFunction &IGF, unsigned index) override {
auto result = Offsets[index];
assert(result != nullptr
&& "fixed-layout field doesn't need NonFixedOffsets");
return result;
}
// The total size of the heap object.
llvm::Value *getSize() const {
return TotalSize;
}
// The total alignment of the heap object.
llvm::Value *getAlignMask() const {
return TotalAlignMask;
}
};
/// Emit a heap object deallocation.
void emitDeallocateHeapObject(IRGenFunction &IGF,
llvm::Value *object,
llvm::Value *size,
llvm::Value *alignMask);
/// Emit a class instance deallocation.
void emitDeallocateClassInstance(IRGenFunction &IGF,
llvm::Value *object,
llvm::Value *size,
llvm::Value *alignMask);
/// Emit a partial class instance deallocation from a failing constructor.
void emitDeallocatePartialClassInstance(IRGenFunction &IGF,
llvm::Value *object,
llvm::Value *metadata,
llvm::Value *size,
llvm::Value *alignMask);
/// Allocate a boxed value.
///
/// The interface type is required for emitting reflection metadata.
OwnedAddress
emitAllocateBox(IRGenFunction &IGF,
CanSILBoxType boxType,
GenericEnvironment *env,
const llvm::Twine &name);
/// Deallocate a box whose value is uninitialized.
void emitDeallocateBox(IRGenFunction &IGF, llvm::Value *box,
CanSILBoxType boxType);
/// Project the address of the value inside a box.
Address emitProjectBox(IRGenFunction &IGF, llvm::Value *box,
CanSILBoxType boxType);
/// Allocate a boxed value based on the boxed type. Returns the address of the
/// storage for the value.
Address
emitAllocateExistentialBoxInBuffer(IRGenFunction &IGF, SILType boxedType,
Address destBuffer, GenericEnvironment *env,
const llvm::Twine &name, bool isOutlined);
/// Given a heap-object instance, with some heap-object type,
/// produce a reference to its type metadata.
llvm::Value *emitDynamicTypeOfHeapObject(IRGenFunction &IGF,
llvm::Value *object,
MetatypeRepresentation rep,
SILType objectType,
bool allowArtificialSubclasses = false);
/// Given a non-tagged object pointer, load a pointer to its class object.
llvm::Value *emitLoadOfObjCHeapMetadataRef(IRGenFunction &IGF,
llvm::Value *object);
/// Given a heap-object instance, with some heap-object type, produce a
/// reference to its heap metadata by dynamically asking the runtime for it.
llvm::Value *emitHeapMetadataRefForUnknownHeapObject(IRGenFunction &IGF,
llvm::Value *object);
/// Given a heap-object instance, with some heap-object type,
/// produce a reference to its heap metadata.
llvm::Value *emitHeapMetadataRefForHeapObject(IRGenFunction &IGF,
llvm::Value *object,
CanType objectType,
bool suppressCast = false);
/// Given a heap-object instance, with some heap-object type,
/// produce a reference to its heap metadata.
llvm::Value *emitHeapMetadataRefForHeapObject(IRGenFunction &IGF,
llvm::Value *object,
SILType objectType,
bool suppressCast = false);
/// What isa-encoding mechanism does a type use?
IsaEncoding getIsaEncodingForType(IRGenModule &IGM, CanType type);
} // end namespace irgen
} // end namespace swift
#endif