1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
//===--- Signature.h - An IR function signature -----------------*- 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 the Signature type, which encapsulates all the
// information necessary to call a function value correctly.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_SIGNATURE_H
#define SWIFT_IRGEN_SIGNATURE_H
#include "MetadataSource.h"
#include "swift/AST/Types.h"
#include "swift/Basic/ExternalUnion.h"
#include "swift/IRGen/GenericRequirement.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/CallingConv.h"
namespace llvm {
class FunctionType;
}
namespace clang {
class CXXConstructorDecl;
namespace CodeGen {
class CGFunctionInfo;
}
}
namespace swift {
class Identifier;
enum class SILFunctionTypeRepresentation : uint8_t;
class SILType;
namespace irgen {
class FunctionPointerKind;
class IRGenModule;
class TypeInfo;
/// An encapsulation of different foreign calling-convention lowering
/// information we might have. Should be interpreted according to the
/// abstract CC of the formal function type.
class ForeignFunctionInfo {
public:
const clang::CodeGen::CGFunctionInfo *ClangInfo = nullptr;
/// True if the foreign function can throw an Objective-C / C++ exception.
bool canThrow = false;
};
/// An encapsulation of the extra lowering information we might want to
/// store about a coroutine.
///
/// The ABI for yields breaks the yielded values down into a scalar type
/// sequence, much like argument lowering does: indirect yields (either
/// due to abstraction or size) become pointers and direct yields undergo
/// the general expansion algorithm. We then find the longest prefix of
/// the resulting sequence for which the concatenation
/// (continuation function pointer) + prefix + (optional remainder pointer)
/// is a legal return type according to the swiftcall ABI. The remainder
/// pointer must be included whenever the prefix is strict; it points to
/// a structure containing the remainder of the type sequence, with each
/// element naturally aligned.
///
/// For example, suppose the yields are
/// @in Any, @inout Double, @in Int64, Float, Int8, Int16, Int32
/// This is expanded to the legal type sequence:
/// Any*, double*, i64*, float, i8, i16, i32
/// To the beginning is always appended a continuation function pointer:
/// void (...)*, Any*, double*, i64*, float, i8, i16, i32
/// Suppose that the current target can support at most 4 scalar results.
/// Then the final sequence will be:
/// void (...)*, Any*, double*, { i64*, float, i8, i16, i32 }*
///
/// This final sequence becomes the result type of the coroutine's ramp
/// function and (if a yield_many coroutine) its continuation functions.
class CoroutineInfo {
public:
/// The number of yield components that are returned directly in the
/// coroutine return value.
unsigned NumDirectYieldComponents = 0;
llvm::StructType *indirectResultsType = nullptr;
};
namespace {
class SignatureExpansion;
}
class AsyncInfo {
public:
uint32_t AsyncContextIdx = 0;
uint32_t AsyncResumeFunctionSwiftSelfIdx = 0;
};
/// Represents the source of the corresponding type pointer computed
/// during the expansion of the polymorphic signature.
///
/// The source is either a \c GenericRequirement, or a \c MetadataSource.
class PolymorphicSignatureExpandedTypeSource {
public:
inline PolymorphicSignatureExpandedTypeSource(
const GenericRequirement &requirement)
: requirement(requirement){};
inline PolymorphicSignatureExpandedTypeSource(
const MetadataSource &metadataSource)
: metadataSource(metadataSource) {}
inline void
visit(llvm::function_ref<void(const GenericRequirement &)> requirementVisitor,
llvm::function_ref<void(const MetadataSource &)> metadataSourceVisitor)
const {
if (requirement)
return requirementVisitor(*requirement);
return metadataSourceVisitor(*metadataSource);
}
private:
std::optional<GenericRequirement> requirement;
std::optional<MetadataSource> metadataSource;
};
/// Recorded information about the specific ABI details.
class SignatureExpansionABIDetails {
public:
/// Recorded information about the direct result type convention.
struct DirectResult {
std::reference_wrapper<const irgen::TypeInfo> typeInfo;
inline DirectResult(const irgen::TypeInfo &typeInfo) : typeInfo(typeInfo) {}
};
/// The direct result, or \c None if direct result is void.
std::optional<DirectResult> directResult;
/// Recorded information about the indirect result parameters convention.
struct IndirectResult {
/// Does this indirect result parameter have the `sret` attribute?
bool hasSRet;
};
/// The indirect results passed as parameters to the call.
llvm::SmallVector<IndirectResult, 1> indirectResults;
/// Recorded information about the parameter convention.
struct Parameter {
std::reference_wrapper<const irgen::TypeInfo> typeInfo;
ParameterConvention convention;
bool isSelf;
inline Parameter(const irgen::TypeInfo &typeInfo,
ParameterConvention convention)
: typeInfo(typeInfo), convention(convention), isSelf(false) {}
};
/// The parameters passed to the call.
llvm::SmallVector<Parameter, 8> parameters;
/// Type sources added to the signature during expansion.
llvm::SmallVector<PolymorphicSignatureExpandedTypeSource, 2>
polymorphicSignatureExpandedTypeSources;
/// True if a trailing self parameter is passed to the call.
bool hasTrailingSelfParam = false;
/// True if a context parameter passed to the call.
bool hasContextParam = false;
/// True if an error result value indirect parameter is passed to the call.
bool hasErrorResult = false;
/// The number of LLVM IR parameters in the LLVM IR function signature.
size_t numParamIRTypesInSignature = 0;
};
/// A signature represents something which can actually be called.
class Signature {
using ExtraData =
SimpleExternalUnion<void, ForeignFunctionInfo, CoroutineInfo, AsyncInfo>;
llvm::FunctionType *Type;
llvm::AttributeList Attributes;
llvm::CallingConv::ID CallingConv;
ExtraData::Kind ExtraDataKind; // packed with above
ExtraData ExtraDataStorage;
std::optional<SignatureExpansionABIDetails> ABIDetails;
static_assert(ExtraData::union_is_trivially_copyable,
"not trivially copyable");
friend class irgen::SignatureExpansion;
public:
Signature() : Type(nullptr) {}
Signature(llvm::FunctionType *fnType, llvm::AttributeList attrs,
llvm::CallingConv::ID callingConv)
: Type(fnType), Attributes(attrs), CallingConv(callingConv),
ExtraDataKind(ExtraData::kindForMember<void>()) {}
bool isValid() const {
return Type != nullptr;
}
/// Compute the signature of the given type.
///
/// This is a private detail of the implementation of
/// IRGenModule::getSignature(CanSILFunctionType), which is what
/// clients should generally be using.
static Signature
getUncached(IRGenModule &IGM, CanSILFunctionType formalType,
FunctionPointerKind kind, bool forStaticCall = false,
const clang::CXXConstructorDecl *cxxCtorDecl = nullptr);
static SignatureExpansionABIDetails
getUncachedABIDetails(IRGenModule &IGM, CanSILFunctionType formalType,
FunctionPointerKind kind);
/// Compute the signature of a coroutine's continuation function.
static Signature forCoroutineContinuation(IRGenModule &IGM,
CanSILFunctionType coroType);
static Signature forAsyncReturn(IRGenModule &IGM,
CanSILFunctionType asyncType);
static Signature forAsyncAwait(IRGenModule &IGM, CanSILFunctionType asyncType,
FunctionPointerKind kind);
static Signature forAsyncEntry(IRGenModule &IGM, CanSILFunctionType asyncType,
FunctionPointerKind kind);
llvm::FunctionType *getType() const {
assert(isValid());
return Type;
}
llvm::CallingConv::ID getCallingConv() const {
assert(isValid());
return CallingConv;
}
llvm::AttributeList getAttributes() const {
assert(isValid());
return Attributes;
}
ForeignFunctionInfo getForeignInfo() const {
assert(isValid());
if (auto info =
ExtraDataStorage.dyn_cast<ForeignFunctionInfo>(ExtraDataKind))
return *info;
return ForeignFunctionInfo();
}
CoroutineInfo getCoroutineInfo() const {
assert(isValid());
if (auto info = ExtraDataStorage.dyn_cast<CoroutineInfo>(ExtraDataKind))
return *info;
return CoroutineInfo();
}
AsyncInfo getAsyncInfo() const {
assert(isValid());
if (auto info = ExtraDataStorage.dyn_cast<AsyncInfo>(ExtraDataKind))
return *info;
return AsyncInfo();
}
uint32_t getAsyncContextIndex() const {
return getAsyncInfo().AsyncContextIdx;
}
uint32_t getAsyncResumeFunctionSwiftSelfIndex() const {
return getAsyncInfo().AsyncResumeFunctionSwiftSelfIdx;
}
// The mutators below should generally only be used when building up
// a callee.
void setType(llvm::FunctionType *type) {
Type = type;
}
llvm::AttributeList &getMutableAttributes() & {
assert(isValid());
return Attributes;
}
const SignatureExpansionABIDetails &getABIDetails() {
assert(ABIDetails.has_value());
return *ABIDetails;
}
};
} // end namespace irgen
} // end namespace swift
#endif
|