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
|
//===--- Outlining.h - Value operation outlining ----------------*- 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 interfaces for outlined value witnesses.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_OUTLINING_H
#define SWIFT_IRGEN_OUTLINING_H
#include "IRGen.h"
#include "LocalTypeDataKind.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/TaggedUnion.h"
#include "swift/IRGen/GenericRequirement.h"
#include "swift/SIL/SILType.h"
#include "llvm/ADT/MapVector.h"
namespace llvm {
class Value;
class Type;
}
namespace swift {
class CanGenericSignature;
class CanType;
enum IsInitialization_t : bool;
enum IsTake_t : bool;
class SILType;
class NominalTypeDecl;
namespace irgen {
class Address;
class Explosion;
class IRGenFunction;
class IRGenModule;
class TypeInfo;
enum LayoutIsNeeded_t : bool {
LayoutIsNotNeeded = false,
LayoutIsNeeded = true
};
enum DeinitIsNeeded_t : bool {
DeinitIsNotNeeded = false,
DeinitIsNeeded = true
};
/// Emit outlined value operations.
///
/// The typical use-pattern is:
/// - construct it
/// OutliningMetadataCollector::OutliningMetadataCollector
/// - collect polymorphic values required for outlining
/// TypeInfo::collectMetadataForOutlining
/// - materialize the arguments for those values in the caller
/// OutliningMetadataCollector::materialize
/// - emit the call to the outlined value function
/// OutliningMetadataCollector::emitCallToOutlined(Copy|Destroy|Release)
///
/// For custom outlined functions (e.g. the outlined consume function for enums)
/// the use-pattern is:
/// - construct it
/// OutliningMetadataCollector::OutliningMetadataCollector
/// - collect polymorphic values required for outlining
/// TypeInfo::collectMetadataForOutlining
/// - materialize the arguments for those values in the caller
/// OutliningMetadataCollector::materialize
/// - add the polymorphic arguments to the list of arguments to be passed
/// OutliningMetadataCollector::addPolymorphicArguments
/// - when creating the outlined function, add the polymorphic parameters to its
/// signature
/// OutliningMetadataCollector::addPolymorphicParameterTypes
/// - when emitting the outlined function, after binding the custom parameters,
/// bind the polymorphic parameters
/// OutliningMetadataCollector::bindPolymorphicParameters
class OutliningMetadataCollector {
public:
SILType T;
IRGenFunction &IGF;
const unsigned needsLayout : 1;
const unsigned needsDeinit : 1;
OutliningMetadataCollector(SILType T, IRGenFunction &IGF,
LayoutIsNeeded_t needsLayout,
DeinitIsNeeded_t needsDeinitTypes);
unsigned size() const;
// If any local type data is needed for \p type, add it.
//
// NOTE: To be called from TypeData instances.
void collectTypeMetadata(SILType type);
void emitCallToOutlinedCopy(Address dest, Address src,
SILType T, const TypeInfo &ti,
IsInitialization_t isInit, IsTake_t isTake) const;
void emitCallToOutlinedDestroy(Address addr, SILType T,
const TypeInfo &ti) const;
void emitCallToOutlinedRelease(Address addr, SILType T, const TypeInfo &ti,
Atomicity atomicity) const;
void addPolymorphicArguments(SmallVectorImpl<llvm::Value *> &args) const;
void
addPolymorphicParameterTypes(SmallVectorImpl<llvm::Type *> ¶mTys) const;
void bindPolymorphicParameters(IRGenFunction &helperIGF,
Explosion ¶ms) const;
void materialize();
private:
void collectTypeMetadataForLayout(SILType ty);
void collectTypeMetadataForDeinit(SILType ty);
/// Emulates the following enum with associated values:
///
/// enum State {
/// case empty
/// enum Collecting {
/// enum Element {
/// case metadataForFormal(CanType)
/// case metadataForRepresentation(SILType)
/// }
/// case elements([Element])
/// case environment
/// }
/// case collecting(Collecting)
/// enum Collected {
/// case elements([LocalTypeDataKey : llvm::Value *])
/// case environment(SubstitutionMap [GenericRequirement : llvm::Value *])
/// }
/// case collected(Collected)
/// }
class State {
public:
enum class CollectionKind {
Elements,
Environment,
};
enum class ElementKind {
MetadataForFormal,
MetadataForRepresentation,
};
private:
struct Empty {};
class Collecting {
struct Elements {
class Element {
struct MetadataForFormal {
CanType ty;
};
struct MetadataForRepresentation {
SILType ty;
};
using Payload =
TaggedUnion<MetadataForFormal, MetadataForRepresentation>;
Payload payload;
Element(Payload payload) : payload(payload) {}
public:
using Kind = ElementKind;
operator Kind() {
if (payload.isa<MetadataForFormal>())
return Kind::MetadataForFormal;
return Kind::MetadataForRepresentation;
}
static Element metadataForFormal(CanType ty) {
return {MetadataForFormal{ty}};
}
static Element metadataForRepresentation(SILType ty) {
return {MetadataForRepresentation{ty}};
}
CanType getFormalType() {
return payload.get<MetadataForFormal>().ty;
}
SILType getRepresentationType() {
return payload.get<MetadataForRepresentation>().ty;
}
};
llvm::SmallVector<Element, 4> elements;
};
struct Environment {};
using Payload = TaggedUnion<Elements, Environment>;
Payload payload;
public:
Collecting() : payload(Elements{}) {}
using Kind = CollectionKind;
operator Kind() const {
if (payload.isa<Elements>()) {
return Kind::Elements;
}
return Kind::Environment;
}
void addRepresentationTypeMetadata(SILType ty) {
if (*this == Kind::Environment)
return;
payload.get<Elements>().elements.push_back(
Elements::Element::metadataForRepresentation(ty));
}
void addFormalTypeMetadata(CanType ty) {
if (*this == Kind::Environment)
return;
payload.get<Elements>().elements.push_back(
Elements::Element::metadataForFormal(ty));
}
void addValueTypeWithDeinit(SILType ty) {
if (*this == Kind::Environment)
return;
payload = {Environment{}};
}
Elements &getElements() { return payload.get<Elements>(); };
};
public:
class Collected {
public:
struct Elements {
llvm::MapVector<LocalTypeDataKey, llvm::Value *> Values;
};
struct Environment {
llvm::MapVector<GenericRequirement, llvm::Value *> Requirements;
SubstitutionMap Subs;
};
private:
using Payload = TaggedUnion<Elements, Environment>;
Payload payload;
Collected(Payload payload) : payload(payload) {}
public:
using Kind = CollectionKind;
operator Kind() const {
if (payload.isa<Elements>())
return Kind::Elements;
return Kind::Environment;
}
static Collected elements() { return {Elements{}}; };
static Collected environment(SubstitutionMap subs) {
return {Environment{{}, subs}};
}
Elements &getElements() { return payload.get<Elements>(); }
Elements const &getElements() const { return payload.get<Elements>(); }
Environment &getEnvironment() { return payload.get<Environment>(); }
Environment const &getEnvironment() const {
return payload.get<Environment>();
}
unsigned size() const {
switch (*this) {
case Kind::Elements:
return getElements().Values.size();
case Kind::Environment:
return getEnvironment().Requirements.size();
}
}
};
private:
using Payload = TaggedUnion<Empty, Collecting, Collected>;
Payload payload;
public:
State() : payload(Empty{}) {}
enum class Kind {
Empty,
Collecting,
Collected,
};
operator Kind() const {
if (payload.isa<Empty>())
return Kind::Empty;
if (payload.isa<Collecting>())
return Kind::Collecting;
return Kind::Collected;
}
Collecting &getCollecting() {
if (payload.isa<Empty>())
payload = Collecting{};
return payload.get<Collecting>();
}
Collected const &getCollected() const { return payload.get<Collected>(); }
Collected::Elements &setCollectedElements() {
assert(*this == Kind::Collecting);
payload = Collected::elements();
return payload.get<Collected>().getElements();
}
Collected::Environment &setCollectedEnvironment(SubstitutionMap subs) {
assert(*this == Kind::Collecting);
payload = Collected::environment(subs);
return payload.get<Collected>().getEnvironment();
}
};
State state;
bool hasFinished() const {
return state == State::Kind::Empty || state == State::Kind::Collected;
}
void materializeFormalTypeMetadata(CanType ty,
State::Collected::Elements &into);
void materializeRepresentationTypeMetadata(SILType ty,
State::Collected::Elements &into);
friend class IRGenModule;
};
inline unsigned OutliningMetadataCollector::size() const {
return state.getCollected().size();
}
std::pair<CanType, CanGenericSignature>
getTypeAndGenericSignatureForManglingOutlineFunction(SILType type);
}
}
#endif
|