-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathGenCall.h
311 lines (265 loc) · 12.4 KB
/
GenCall.h
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
//===--- GenCall.h - IR generation for calls and prologues ------*- 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 provides the private interface to the function call
// and prologue emission support code.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_GENCALL_H
#define SWIFT_IRGEN_GENCALL_H
#include <stdint.h>
#include "swift/AST/Types.h"
#include "swift/Basic/LLVM.h"
#include "swift/SIL/ApplySite.h"
#include "llvm/IR/CallingConv.h"
#include "Callee.h"
#include "GenHeap.h"
#include "IRGenModule.h"
namespace llvm {
class AttributeList;
class Constant;
class Twine;
class Type;
class Value;
}
namespace clang {
template <class> class CanQual;
class Type;
}
namespace swift {
enum class CoroAllocatorKind : uint8_t;
namespace irgen {
class Address;
class Alignment;
class Callee;
class CalleeInfo;
class Explosion;
class ExplosionSchema;
class ForeignFunctionInfo;
class IRGenFunction;
class IRGenModule;
class LoadableTypeInfo;
class NativeCCEntryPointArgumentEmission;
class Size;
class TypeInfo;
enum class TranslationDirection : bool {
ToForeign,
ToNative
};
inline TranslationDirection reverse(TranslationDirection direction) {
return TranslationDirection(!bool(direction));
}
// struct SwiftContext {
// SwiftContext * __ptrauth(...) callerContext;
// SwiftPartialFunction * __ptrauth(...) returnToCaller;
// SwiftActor * __ptrauth(...) callerActor;
// SwiftPartialFunction * __ptrauth(...) yieldToCaller?;
// };
struct AsyncContextLayout : StructLayout {
struct ArgumentInfo {
SILType type;
ParameterConvention convention;
};
struct TrailingWitnessInfo {};
private:
enum class FixedIndex : unsigned {
Parent = 0,
ResumeParent = 1,
Flags = 2,
};
enum class FixedCount : unsigned {
Parent = 1,
ResumeParent = 1,
};
CanSILFunctionType originalType;
CanSILFunctionType substitutedType;
SubstitutionMap substitutionMap;
unsigned getParentIndex() { return (unsigned)FixedIndex::Parent; }
unsigned getResumeParentIndex() {
return (unsigned)FixedIndex::ResumeParent;
}
unsigned getFlagsIndex() { return (unsigned)FixedIndex::Flags; }
public:
ElementLayout getParentLayout() { return getElement(getParentIndex()); }
ElementLayout getResumeParentLayout() {
return getElement(getResumeParentIndex());
}
ElementLayout getFlagsLayout() { return getElement(getFlagsIndex()); }
AsyncContextLayout(
IRGenModule &IGM, LayoutStrategy strategy, ArrayRef<SILType> fieldTypes,
ArrayRef<const TypeInfo *> fieldTypeInfos,
CanSILFunctionType originalType, CanSILFunctionType substitutedType,
SubstitutionMap substitutionMap);
};
AsyncContextLayout getAsyncContextLayout(IRGenModule &IGM,
SILFunction *function);
AsyncContextLayout getAsyncContextLayout(IRGenModule &IGM,
CanSILFunctionType originalType,
CanSILFunctionType substitutedType,
SubstitutionMap substitutionMap);
struct CombinedResultAndErrorType {
llvm::Type *combinedTy;
llvm::SmallVector<unsigned, 2> errorValueMapping;
};
CombinedResultAndErrorType
combineResultAndTypedErrorType(const IRGenModule &IGM,
const NativeConventionSchema &resultSchema,
const NativeConventionSchema &errorSchema);
/// Given an async function, get the pointer to the function to be called and
/// the size of the context to be allocated.
///
/// \param values Whether any code should be emitted to retrieve the function
/// pointer and the size, respectively. If false is passed, no
/// code will be emitted to generate that value and null will
/// be returned for it.
///
/// \return {function, size}
std::pair<llvm::Value *, llvm::Value *>
getAsyncFunctionAndSize(IRGenFunction &IGF, FunctionPointer functionPointer,
std::pair<bool, bool> values = {true, true});
std::pair<llvm::Value *, llvm::Value *>
getCoroFunctionAndSize(IRGenFunction &IGF, FunctionPointer functionPointer,
std::pair<bool, bool> values = {true, true});
llvm::CallingConv::ID
expandCallingConv(IRGenModule &IGM, SILFunctionTypeRepresentation convention,
bool isAsync, bool isCalleeAllocatedCoro);
Signature emitCastOfFunctionPointer(IRGenFunction &IGF, llvm::Value *&fnPtr,
CanSILFunctionType fnType,
bool forAsyncReturn = false);
/// Does the given function have a self parameter that should be given
/// the special treatment for self parameters?
bool hasSelfContextParameter(CanSILFunctionType fnType);
/// Add function attributes to an attribute set for a byval argument.
void addByvalArgumentAttributes(IRGenModule &IGM,
llvm::AttributeList &attrs,
unsigned argIndex,
Alignment align,
llvm::Type *storageType);
/// Can a series of values be simply pairwise coerced to (or from) an
/// explosion schema, or do they need to traffic through memory?
bool canCoerceToSchema(IRGenModule &IGM,
ArrayRef<llvm::Type*> types,
const ExplosionSchema &schema);
void emitForeignParameter(IRGenFunction &IGF, Explosion ¶ms,
ForeignFunctionInfo foreignInfo,
unsigned foreignParamIndex, SILType paramTy,
const LoadableTypeInfo ¶mTI,
Explosion ¶mExplosion, bool isOutlined);
void emitClangExpandedParameter(IRGenFunction &IGF,
Explosion &in, Explosion &out,
clang::CanQual<clang::Type> clangType,
SILType swiftType,
const LoadableTypeInfo &swiftTI);
bool addNativeArgument(IRGenFunction &IGF,
Explosion &in,
CanSILFunctionType fnTy,
SILParameterInfo origParamInfo, Explosion &args,
bool isOutlined);
/// Allocate a stack buffer of the appropriate size to bitwise-coerce a value
/// between two LLVM types.
std::pair<Address, Size>
allocateForCoercion(IRGenFunction &IGF,
llvm::Type *fromTy,
llvm::Type *toTy,
const llvm::Twine &basename);
void extractScalarResults(IRGenFunction &IGF, llvm::Type *bodyType,
llvm::Value *call, Explosion &out);
Callee getBlockPointerCallee(IRGenFunction &IGF, llvm::Value *blockPtr,
CalleeInfo &&info);
Callee getCFunctionPointerCallee(IRGenFunction &IGF, llvm::Value *fnPtr,
CalleeInfo &&info);
Callee getSwiftFunctionPointerCallee(IRGenFunction &IGF,
llvm::Value *fnPtr,
llvm::Value *contextPtr,
CalleeInfo &&info,
bool castOpaqueToRefcountedContext,
bool isClosure);
Address emitAllocYieldOnceCoroutineBuffer(IRGenFunction &IGF);
void emitDeallocYieldOnceCoroutineBuffer(IRGenFunction &IGF, Address buffer);
void
emitYieldOnceCoroutineEntry(IRGenFunction &IGF,
CanSILFunctionType coroutineType,
NativeCCEntryPointArgumentEmission &emission);
llvm::Value *
emitYieldOnce2CoroutineAllocator(IRGenFunction &IGF,
std::optional<CoroAllocatorKind> kind);
StackAddress emitAllocYieldOnce2CoroutineFrame(IRGenFunction &IGF,
llvm::Value *size);
void emitDeallocYieldOnce2CoroutineFrame(IRGenFunction &IGF,
StackAddress allocation);
void
emitYieldOnce2CoroutineEntry(IRGenFunction &IGF, LinkEntity coroFunction,
CanSILFunctionType coroutineType,
NativeCCEntryPointArgumentEmission &emission);
void emitYieldOnce2CoroutineEntry(IRGenFunction &IGF,
CanSILFunctionType fnType,
llvm::Value *buffer, llvm::Value *allocator,
llvm::GlobalVariable *cfp);
Address emitAllocYieldManyCoroutineBuffer(IRGenFunction &IGF);
void emitDeallocYieldManyCoroutineBuffer(IRGenFunction &IGF, Address buffer);
void
emitYieldManyCoroutineEntry(IRGenFunction &IGF,
CanSILFunctionType coroutineType,
NativeCCEntryPointArgumentEmission &emission);
/// Allocate task local storage for the provided dynamic size.
Address emitAllocAsyncContext(IRGenFunction &IGF, llvm::Value *sizeValue);
void emitDeallocAsyncContext(IRGenFunction &IGF, Address context);
Address emitStaticAllocAsyncContext(IRGenFunction &IGF, Size size);
void emitStaticDeallocAsyncContext(IRGenFunction &IGF, Address context,
Size size);
void emitAsyncFunctionEntry(IRGenFunction &IGF,
const AsyncContextLayout &layout,
LinkEntity asyncFunction,
unsigned asyncContextIndex);
StackAddress emitAllocCoroStaticFrame(IRGenFunction &IGF, llvm::Value *size);
void emitDeallocCoroStaticFrame(IRGenFunction &IGF, StackAddress frame);
/// Yield the given values from the current continuation.
///
/// \return an i1 indicating whether the caller wants to unwind this
/// coroutine instead of resuming it normally
llvm::Value *emitYield(IRGenFunction &IGF,
CanSILFunctionType coroutineType,
Explosion &yieldedValues);
enum class AsyncFunctionArgumentIndex : unsigned {
Context = 0,
};
void emitAsyncReturn(
IRGenFunction &IGF, AsyncContextLayout &layout, CanSILFunctionType fnType,
std::optional<ArrayRef<llvm::Value *>> nativeResultArgs = std::nullopt);
void emitAsyncReturn(IRGenFunction &IGF, AsyncContextLayout &layout,
SILType funcResultTypeInContext,
CanSILFunctionType fnType, Explosion &result,
Explosion &error);
void emitYieldOnceCoroutineResult(IRGenFunction &IGF, Explosion &result,
SILType funcResultType, SILType returnResultType);
Address emitAutoDiffCreateLinearMapContextWithType(
IRGenFunction &IGF, llvm::Value *topLevelSubcontextMetatype);
Address emitAutoDiffProjectTopLevelSubcontext(
IRGenFunction &IGF, Address context);
Address
emitAutoDiffAllocateSubcontextWithType(IRGenFunction &IGF, Address context,
llvm::Value *subcontextMetatype);
FunctionPointer getFunctionPointerForDispatchCall(IRGenModule &IGM,
const FunctionPointer &fn);
void forwardAsyncCallResult(IRGenFunction &IGF, CanSILFunctionType fnType,
AsyncContextLayout &layout, llvm::CallInst *call);
/// Converts a value for direct error return.
llvm::Value *convertForDirectError(IRGenFunction &IGF, llvm::Value *value,
llvm::Type *toTy, bool forExtraction);
void buildDirectError(IRGenFunction &IGF,
const CombinedResultAndErrorType &combined,
const NativeConventionSchema &errorSchema,
SILType silErrorTy, Explosion &errorResult,
bool forAsync, Explosion &out);
} // end namespace irgen
} // end namespace swift
#endif