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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
//===--- GenType.h - Auxiliary Interface for Type IR Generation -*- 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 private interface used for turning AST types
// into LLVM IR types.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_GENTYPE_H
#define SWIFT_IRGEN_GENTYPE_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/ADT/StringMap.h"
#include "IRGenModule.h"
#include "IRGenFunction.h"
#include "LegacyLayoutFormat.h"
namespace llvm {
namespace vfs {
class FileSystem;
}
}
namespace swift {
class ArchetypeType;
class CanType;
class ClassDecl;
class AnyFunctionType;
class InOutType;
class MetatypeType;
class ModuleType;
class NominalTypeDecl;
class EnumDecl;
class ProtocolCompositionType;
class ProtocolDecl;
class ProtocolType;
class SILFunctionType;
class StructDecl;
class TupleType;
class TypeBase;
class Type;
class EnumDecl;
class UnownedStorageType;
class WeakStorageType;
enum IsTake_t : bool;
namespace irgen {
class Alignment;
class GenericContextScope;
class ProtocolInfo;
class Size;
class FixedTypeInfo;
class LoadableTypeInfo;
class TypeInfo;
/// The helper class for generating types.
class TypeConverter {
public:
enum class Mode : unsigned {
/// Normal type lowering mode where resilient types are opaque.
Normal,
/// Used for computing backward deployment class layouts, where we emit a
/// static class metadata layout using known sizes and alignments of any
/// resiliently-typed fields from a previous Swift version. On newer Swift
/// versions we use a runtime mechanism to re-initialize the class metadata
/// in-place with the current known layout.
Legacy,
/// A temporary hack for lldb where all resilient types are transparent and
/// treated like fixed-size (but still lowered in a way that matches the
/// runtime layout produced for resilient types, which is important for some
/// types like enums where enabling resilience changes the layout).
CompletelyFragile
/// When adding or removing fields, remember to update NumLoweringModes below.
};
static unsigned const NumLoweringModes = 3;
IRGenModule &IGM;
private:
// Set using the GenericContextScope RAII object.
friend GenericContextScope;
CanGenericSignature CurGenericSignature;
// Enter a generic context for lowering the parameters of a generic function
// type.
void setGenericContext(CanGenericSignature signature);
Mode LoweringMode = Mode::Normal;
llvm::DenseMap<ProtocolDecl*, std::unique_ptr<const ProtocolInfo>> Protocols;
const TypeInfo *FirstType;
const LoadableTypeInfo *NativeObjectTI = nullptr;
const LoadableTypeInfo *UnknownObjectTI = nullptr;
const LoadableTypeInfo *BridgeObjectTI = nullptr;
const LoadableTypeInfo *RawPointerTI = nullptr;
const LoadableTypeInfo *RawUnsafeContinuationTI = nullptr;
const LoadableTypeInfo *JobTI = nullptr;
const LoadableTypeInfo *ExecutorTI = nullptr;
const LoadableTypeInfo *WitnessTablePtrTI = nullptr;
const TypeInfo *TypeMetadataPtrTI = nullptr;
const TypeInfo *SwiftContextPtrTI = nullptr;
const TypeInfo *TaskContinuationFunctionPtrTI = nullptr;
const TypeInfo *ObjCClassPtrTI = nullptr;
const LoadableTypeInfo *EmptyTI = nullptr;
const LoadableTypeInfo *IntegerLiteralTI = nullptr;
const TypeInfo *ResilientStructTI[2][2] = {
{nullptr, nullptr},
{nullptr, nullptr},
};
const TypeInfo *DynamicTupleTI[2] = {nullptr, nullptr};
llvm::DenseMap<std::pair<unsigned, unsigned>, const LoadableTypeInfo *>
OpaqueStorageTypes;
const LoadableTypeInfo *NonFixedBoxTI = nullptr;
const LoadableTypeInfo *EmptyBoxTI = nullptr;
llvm::DenseMap<std::pair<unsigned, unsigned>, const LoadableTypeInfo *>
PODBoxTI;
const LoadableTypeInfo *SwiftRetainablePointerBoxTI = nullptr,
*UnknownObjectRetainablePointerBoxTI = nullptr;
llvm::StringMap<YAMLTypeInfoNode> LegacyTypeInfos;
llvm::DenseMap<NominalTypeDecl *, std::string> DeclMangledNames;
/// The key is the number of witness tables.
llvm::DenseMap<unsigned, llvm::StructType *> OpaqueExistentialTypes;
const LoadableTypeInfo *createPrimitive(llvm::Type *T,
Size size, Alignment align);
const FixedTypeInfo *createImmovable(llvm::Type *T,
Size size, Alignment align);
const TypeInfo *createOpaqueImmovable(llvm::Type *T, Alignment minAlign);
void addForwardDecl(TypeBase *key);
const TypeInfo *convertType(CanType T);
const TypeInfo *convertAnyNominalType(CanType T, NominalTypeDecl *D);
const TypeInfo *convertTupleType(TupleType *T);
const TypeInfo *convertClassType(CanType type, ClassDecl *D);
const TypeInfo *convertEnumType(TypeBase *key, CanType type, EnumDecl *D);
const TypeInfo *convertStructType(TypeBase *key, CanType type, StructDecl *D);
const TypeInfo *convertFunctionType(SILFunctionType *T);
const TypeInfo *convertNormalDifferentiableFunctionType(SILFunctionType *T);
const TypeInfo *convertLinearDifferentiableFunctionType(SILFunctionType *T);
const TypeInfo *convertBlockStorageType(SILBlockStorageType *T);
const TypeInfo *convertBoxType(SILBoxType *T);
const TypeInfo *convertArchetypeType(ArchetypeType *T);
const TypeInfo *convertInOutType(InOutType *T);
const TypeInfo *convertSILMoveOnlyWrappedType(SILMoveOnlyWrappedType *T) {
return convertType(T->getInnerType());
}
const TypeInfo *convertExistentialMetatypeType(ExistentialMetatypeType *T);
const TypeInfo *convertMetatypeType(MetatypeType *T);
const TypeInfo *convertModuleType(ModuleType *T);
const TypeInfo *convertProtocolType(ProtocolType *T);
const TypeInfo *convertProtocolCompositionType(ProtocolCompositionType *T);
const TypeInfo *convertParameterizedProtocolType(ParameterizedProtocolType *T);
const TypeInfo *convertExistentialType(ExistentialType *T);
const TypeInfo *convertPackType(SILPackType *T);
const LoadableTypeInfo *convertBuiltinNativeObject();
const LoadableTypeInfo *convertBuiltinUnknownObject();
const LoadableTypeInfo *convertBuiltinBridgeObject();
const TypeInfo *convertResilientStruct(IsCopyable_t copyable,
IsABIAccessible_t abiAccessible);
const TypeInfo *convertDynamicTupleType(IsCopyable_t copyable);
#define REF_STORAGE(Name, ...) \
const TypeInfo *convert##Name##StorageType(Name##StorageType *T);
#include "swift/AST/ReferenceStorage.def"
public:
TypeConverter(IRGenModule &IGM);
~TypeConverter();
Mode getLoweringMode() const {
return LoweringMode;
}
const TypeInfo *getTypeEntry(CanType type);
const TypeInfo &getCompleteTypeInfo(CanType type);
const TypeLayoutEntry
&getTypeLayoutEntry(SILType T, bool useStructLayouts);
const LoadableTypeInfo &getNativeObjectTypeInfo();
const LoadableTypeInfo &getUnknownObjectTypeInfo();
const LoadableTypeInfo &getBridgeObjectTypeInfo();
const LoadableTypeInfo &getRawPointerTypeInfo();
const LoadableTypeInfo &getRawUnsafeContinuationTypeInfo();
const LoadableTypeInfo &getJobTypeInfo();
const LoadableTypeInfo &getExecutorTypeInfo();
const TypeInfo &getTypeMetadataPtrTypeInfo();
const TypeInfo &getSwiftContextPtrTypeInfo();
const TypeInfo &getTaskContinuationFunctionPtrTypeInfo();
const TypeInfo &getObjCClassPtrTypeInfo();
const LoadableTypeInfo &getWitnessTablePtrTypeInfo();
const LoadableTypeInfo &getEmptyTypeInfo();
const LoadableTypeInfo &getIntegerLiteralTypeInfo();
const TypeInfo &getResilientStructTypeInfo(IsCopyable_t copyable,
IsABIAccessible_t abiAccessible);
const TypeInfo &getDynamicTupleTypeInfo(IsCopyable_t isCopyable);
const ProtocolInfo &getProtocolInfo(ProtocolDecl *P, ProtocolInfoKind kind);
const LoadableTypeInfo &getOpaqueStorageTypeInfo(Size storageSize,
Alignment storageAlign);
const TypeInfo &getMetatypeTypeInfo(MetatypeRepresentation representation);
#define REF_STORAGE(Name, ...) \
const TypeInfo *create##Name##StorageType(llvm::Type *valueType, \
ReferenceCounting style, \
bool isOptional);
#include "swift/AST/ReferenceStorage.def"
llvm::Type *getExistentialType(unsigned numWitnessTables);
/// Retrieve the generic signature for the current generic context, or null if no
/// generic environment is active.
CanGenericSignature getCurGenericContext() { return CurGenericSignature; }
/// Retrieve the generic environment for the current generic context.
///
/// Fails if there is no generic context.
GenericEnvironment *getGenericEnvironment();
private:
friend class LoweringModeScope;
void setLoweringMode(Mode mode) {
LoweringMode = mode;
}
/// Read a YAML legacy type layout dump. Returns false on success, true on
/// error.
bool readLegacyTypeInfo(llvm::vfs::FileSystem &fs, StringRef path);
std::optional<YAMLTypeInfoNode>
getLegacyTypeInfo(NominalTypeDecl *decl) const;
// Debugging aids.
#ifndef NDEBUG
bool isExemplarArchetype(ArchetypeType *arch) const;
#endif
ArchetypeType *getExemplarArchetype(ArchetypeType *t);
CanType getExemplarType(CanType t);
class Types_t {
llvm::DenseMap<TypeBase *, const TypeInfo *> IndependentCache[NumLoweringModes];
llvm::DenseMap<TypeBase *, const TypeInfo *> DependentCache[NumLoweringModes];
llvm::DenseMap<TypeBase *, const TypeLayoutEntry *>
IndependentTypeLayoutCache[NumLoweringModes];
llvm::DenseMap<TypeBase *, const TypeLayoutEntry *>
DependentTypeLayoutCache[NumLoweringModes];
public:
llvm::DenseMap<TypeBase *, const TypeInfo *> &getCacheFor(bool isDependent,
Mode mode);
llvm::DenseMap<TypeBase *, const TypeLayoutEntry *> &
getTypeLayoutCacheFor(bool isDependent, Mode mode);
};
Types_t Types;
};
/// An RAII interface for entering a generic context for type conversion in
/// a scope.
class GenericContextScope {
TypeConverter &TC;
CanGenericSignature newSig, oldSig;
public:
GenericContextScope(TypeConverter &TC, CanGenericSignature sig)
: TC(TC), newSig(sig), oldSig(TC.CurGenericSignature)
{
TC.setGenericContext(newSig);
}
GenericContextScope(IRGenModule &IGM, CanGenericSignature sig)
: GenericContextScope(IGM.Types, sig)
{}
~GenericContextScope() {
assert(TC.CurGenericSignature == newSig);
TC.setGenericContext(oldSig);
}
};
/// An RAII interface for forcing types to be lowered bypassing resilience.
class LoweringModeScope {
TypeConverter::Mode OldLoweringMode;
TypeConverter &TC;
public:
LoweringModeScope(TypeConverter &TC, TypeConverter::Mode LoweringMode)
: TC(TC) {
OldLoweringMode = TC.getLoweringMode();
TC.setLoweringMode(LoweringMode);
}
LoweringModeScope(IRGenModule &IGM, TypeConverter::Mode LoweringMode)
: LoweringModeScope(IGM.Types, LoweringMode) {}
~LoweringModeScope() {
TC.setLoweringMode(OldLoweringMode);
}
};
/// If a type is visibly a singleton aggregate (a tuple with one element, a
/// struct with one field, or an enum with a single payload case), return the
/// type of its field, which it is guaranteed to have identical layout to.
///
/// This can use more concrete type layout information than
/// SILType::getSingletonAggregateFieldType, because we have full access to the
/// LLVM-level layout of types in IRGen.
SILType getSingletonAggregateFieldType(IRGenModule &IGM,
SILType t,
ResilienceExpansion expansion);
/// An IRGenFunction interface for generating type layout verifiers.
class IRGenTypeVerifierFunction : public IRGenFunction {
private:
FunctionPointer VerifierFn;
struct VerifierArgumentBuffers {
Address runtimeBuf, staticBuf;
};
llvm::DenseMap<llvm::Type *, VerifierArgumentBuffers> VerifierArgBufs;
public:
IRGenTypeVerifierFunction(IRGenModule &IGM, llvm::Function *f);
void emit(ArrayRef<CanType> typesToVerify);
/// Call a runtime function that verifies that the two LLVM values are
/// equivalent, logging a detailed error if they differ.
void verifyValues(llvm::Value *typeMetadata,
llvm::Value *runtimeValue,
llvm::Value *compilerValue,
const llvm::Twine &description);
/// Call a runtime function that verifies that the contents of the two
/// memory buffers are equivalent, logging a detailed error if they differ.
void verifyBuffers(llvm::Value *typeMetadata,
Address runtimeValue,
Address compilerValue,
Size size,
const llvm::Twine &description);
};
template <class FixedTypeInfoType>
TypeLayoutEntry *buildTypeLayoutEntryForFields(IRGenModule &IGM, SILType T,
const FixedTypeInfoType &TI) {
std::vector<TypeLayoutEntry *> fields;
auto minimumAlignment = TI.getFixedAlignment().getValue();
Alignment::int_type minFieldAlignment = 1;
for (auto &field : TI.getFields()) {
auto fieldTy = field.getType(IGM, T);
auto fieldAlignment = cast<FixedTypeInfo>(field.getTypeInfo())
.getFixedAlignment()
.getValue();
if (minFieldAlignment < fieldAlignment)
minFieldAlignment = fieldAlignment;
fields.push_back(field.getTypeInfo().buildTypeLayoutEntry(IGM, fieldTy));
}
if (fields.empty() && minFieldAlignment >= minimumAlignment) {
return IGM.typeLayoutCache.getEmptyEntry();
}
// if (fields.size() == 1 && minFieldAlignment >= minimumAlignment) {
// return fields[0];
// }
if (minimumAlignment < minFieldAlignment)
minimumAlignment = minFieldAlignment;
return IGM.typeLayoutCache.getOrCreateAlignedGroupEntry(
fields, minimumAlignment, true);
}
/// Emit a call to the deinit for T to destroy the value at the given address,
/// if a deinit is available.
///
/// Returns true if the deinit call was emitted, or false if there is no deinit.
/// No code emission occurs if the function returns false.
bool tryEmitDestroyUsingDeinit(IRGenFunction &IGF,
Address address,
SILType T);
/// Emit a call to the deinit for T to destroy the value in the given explosion,
/// if a deinit is available.
///
/// Returns true if the deinit call was emitted, or false if there is no deinit.
/// No code emission occurs if the function returns false.
bool tryEmitConsumeUsingDeinit(IRGenFunction &IGF,
Explosion &explosion,
SILType T);
} // end namespace irgen
} // end namespace swift
#endif
|