-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDeserializeSIL.h
310 lines (247 loc) · 11.4 KB
/
DeserializeSIL.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
//===--- DeserializeSIL.h - Read SIL ----------------------------*- 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
//
//===----------------------------------------------------------------------===//
#include "ModuleFile.h"
#include "SILFormat.h"
#include "swift/AST/Types.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILMoveOnlyDeinit.h"
#include "swift/Serialization/SerializedSILLoader.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/SaveAndRestore.h"
namespace llvm {
template <typename Info> class OnDiskIterableChainedHashTable;
}
namespace swift {
class SILDeserializer {
using TypeID = serialization::TypeID;
ModuleFile *MF;
SILModule &SILMod;
DeserializationNotificationHandlerSet *Callback;
/// The cursor used to lazily load SILFunctions.
llvm::BitstreamCursor SILCursor;
llvm::BitstreamCursor SILIndexCursor;
class FuncTableInfo;
using SerializedFuncTable =
llvm::OnDiskIterableChainedHashTable<FuncTableInfo>;
//-----
// Deserialization Caches
//
// NOTE: When adding more serialized tables to the deserializer,
// always add invalidate methods and make sure SILModule always
// invalidates the deserializer appropriately when it erases a
// value that we deserialized here. Otherwise, memory corruption
// may result.
std::unique_ptr<SerializedFuncTable> FuncTable;
MutableArrayRef<ModuleFile::PartiallySerialized<SILFunction*>> Funcs;
std::unique_ptr<SerializedFuncTable> GlobalVarList;
MutableArrayRef<ModuleFile::PartiallySerialized<SILGlobalVariable *>>
GlobalVars;
std::unique_ptr<SerializedFuncTable> VTableList;
MutableArrayRef<ModuleFile::PartiallySerialized<SILVTable *>> VTables;
std::unique_ptr<SerializedFuncTable> MoveOnlyDeinitList;
MutableArrayRef<ModuleFile::PartiallySerialized<SILMoveOnlyDeinit *>>
MoveOnlyDeinits;
std::unique_ptr<SerializedFuncTable> WitnessTableList;
MutableArrayRef<ModuleFile::PartiallySerialized<SILWitnessTable *>>
WitnessTables;
std::unique_ptr<SerializedFuncTable> DefaultWitnessTableList;
MutableArrayRef<ModuleFile::PartiallySerialized<SILDefaultWitnessTable *>>
DefaultWitnessTables;
MutableArrayRef<ModuleFile::PartiallySerialized<SILProperty *>>
Properties;
std::unique_ptr<SerializedFuncTable> DifferentiabilityWitnessList;
MutableArrayRef<
ModuleFile::PartiallySerialized<SILDifferentiabilityWitness *>>
DifferentiabilityWitnesses;
//-----
// End Deserialization Caches
//
// Before adding a new cache here, please read the comment at the
// beginning of the deserialization cache section.
/// A declaration will only
llvm::DenseMap<NormalProtocolConformance *, SILWitnessTable *>
ConformanceToWitnessTableMap;
/// Data structures used to perform name lookup for local values.
llvm::DenseMap<uint32_t, ValueBase*> LocalValues;
/// The first two local values are reserved for SILUndef.
serialization::ValueID LastValueID = 1;
/// Data structures used to perform lookup of basic blocks.
llvm::DenseMap<unsigned, SILBasicBlock*> BlocksByID;
llvm::DenseMap<SILBasicBlock*, unsigned> UndefinedBlocks;
unsigned BasicBlockID = 0;
/// Return the SILBasicBlock of a given ID.
SILBasicBlock *getBBForReference(SILFunction *Fn, unsigned ID);
SILBasicBlock *getBBForDefinition(SILFunction *Fn, SILBasicBlock *Prev,
unsigned ID);
/// Read a SIL function.
SILFunction *readSILFunction(serialization::DeclID, SILFunction *InFunc,
StringRef Name, bool declarationOnly,
bool errorIfEmptyBody = true);
/// Read a SIL function.
llvm::Expected<SILFunction *>
readSILFunctionChecked(serialization::DeclID, SILFunction *InFunc,
StringRef Name, bool declarationOnly,
bool errorIfEmptyBody = true);
/// Read a SIL basic block within a given SIL function.
SILBasicBlock *readSILBasicBlock(SILFunction *Fn,
SILBasicBlock *Prev,
SmallVectorImpl<uint64_t> &scratch);
/// Read a SIL instruction.
bool readSILInstruction(SILFunction *Fn,
SILBuilder &Builder,
unsigned RecordKind,
SmallVectorImpl<uint64_t> &scratch);
/// Read the SIL function table.
std::unique_ptr<SerializedFuncTable>
readFuncTable(ArrayRef<uint64_t> fields, StringRef blobData);
/// When an instruction or block argument is defined, this method is used to
/// register it and update our symbol table.
void setLocalValue(ValueBase *Value, serialization::ValueID Id);
/// Get a reference to a local value with the specified ID and type.
///
/// NOTE: \p inContext is expected to be nullptr if we are inserting into a
/// global variable initializer.
SILValue getLocalValue(SILFunction *inContext, serialization::ValueID Id,
SILType Type);
SILType getSILType(Type ty, SILValueCategory category,
SILFunction *inContext);
SILDifferentiabilityWitness *
getSILDifferentiabilityWitnessForReference(StringRef mangledKey);
SILFunction *getFuncForReference(StringRef Name, SILType Ty, TypeExpansionContext context);
SILFunction *getFuncForReference(StringRef Name);
SILVTable *readVTable(serialization::DeclID);
SILMoveOnlyDeinit *readMoveOnlyDeinit(serialization::DeclID);
SILGlobalVariable *getGlobalForReference(StringRef Name);
SILGlobalVariable *readGlobalVar(StringRef Name);
/// Read and return the witness table identified with \p WId.
SILWitnessTable *readWitnessTable(serialization::DeclID WId,
SILWitnessTable *existingWt);
/// Read the witness table identified with \p WId, return the table or
/// the first error if any.
llvm::Expected<SILWitnessTable *>
readWitnessTableChecked(serialization::DeclID WId,
SILWitnessTable *existingWt);
void readWitnessTableEntries(
llvm::BitstreamEntry &entry,
std::vector<SILWitnessTable::Entry> &witnessEntries,
std::vector<SILWitnessTable::ConditionalConformance>
&conditionalConformances);
SILProperty *readProperty(serialization::DeclID);
SILDefaultWitnessTable *
readDefaultWitnessTable(serialization::DeclID,
SILDefaultWitnessTable *existingWt);
SILDifferentiabilityWitness *
readDifferentiabilityWitness(serialization::DeclID);
std::optional<KeyPathPatternComponent>
readKeyPathComponent(ArrayRef<uint64_t> ListOfValues, unsigned &nextValue);
public:
Identifier getModuleIdentifier() const {
return MF->getAssociatedModule()->getName();
}
FileUnit *getFile() const {
return MF->getFile();
}
SILFunction *lookupSILFunction(SILFunction *InFunc, bool onlyUpdateLinkage);
SILFunction *lookupSILFunction(StringRef Name,
bool declarationOnly = false);
bool hasSILFunction(StringRef Name,
std::optional<SILLinkage> Linkage = std::nullopt);
SILVTable *lookupVTable(StringRef MangledClassName);
SILMoveOnlyDeinit *lookupMoveOnlyDeinit(StringRef mangledNominalTypeName);
SILWitnessTable *lookupWitnessTable(SILWitnessTable *wt);
SILDefaultWitnessTable *
lookupDefaultWitnessTable(SILDefaultWitnessTable *wt);
SILDifferentiabilityWitness *
lookupDifferentiabilityWitness(StringRef mangledDiffWitnessKey);
/// Invalidate all cached SILFunctions.
void invalidateFunctionCache();
/// Invalidate a specific cached SILFunction.
bool invalidateFunction(SILFunction *F);
/// Invalidate all cached SILGlobalVariable.
void invalidateGlobalVariableCache();
/// Invalidate a specific cached GlobalVariable.
bool invalidateGlobalVariable(SILGlobalVariable *gv);
/// Invalidate all cached SILVTable.
void invalidateVTableCache();
/// Invalidate a specific cached SILVTable.
bool invalidateVTable(SILVTable *v);
/// Invalidate all cached SILWitnessTable.
void invalidateWitnessTableCache();
/// Invalidate a specific cached SILWitnessTable.
bool invalidateWitnessTable(SILWitnessTable *v);
/// Invalidate all cached SILDefaultWitnessTable.
void invalidateDefaultWitnessTableCache();
/// Invalidate a specific cached SILDefaultWitnessTable.
bool invalidateDefaultWitnessTable(SILDefaultWitnessTable *v);
/// Invalidate all cached SILProperty.
void invalidatePropertyCache();
/// Invalidate a specific cached SILProperty.
bool invalidateProperty(SILProperty *v);
/// Invalidate all cached SILDifferentiabilityWitness.
void invalidateDifferentiabilityWitnessCache();
/// Invalidate a specific cached SILDifferentiabilityWitness.
bool invalidateDifferentiabilityWitness(SILDifferentiabilityWitness *v);
/// Invalidate all caches in this deserializer.
void invalidateAllCaches() {
invalidateFunctionCache();
invalidateGlobalVariableCache();
invalidateVTableCache();
invalidateWitnessTableCache();
invalidateDefaultWitnessTableCache();
invalidatePropertyCache();
invalidateDifferentiabilityWitnessCache();
}
/// Deserialize all SILFunctions, VTables, WitnessTables, and
/// DefaultWitnessTables inside the module, and add them to SILMod.
///
/// TODO: Globals.
void getAll(bool UseCallback = true) {
llvm::SaveAndRestore<DeserializationNotificationHandlerSet *> SaveCB(
Callback);
if (!UseCallback)
Callback = nullptr;
getAllSILFunctions();
getAllSILGlobalVariables();
getAllVTables();
getAllWitnessTables();
getAllDefaultWitnessTables();
getAllProperties();
getAllDifferentiabilityWitnesses();
getAllMoveOnlyDeinits();
}
/// Deserialize all SILFunctions inside the module and add them to SILMod.
void getAllSILFunctions();
/// Deserialize all SILGlobalVariables inside the module and add them to
/// SILMod.
void getAllSILGlobalVariables();
/// Deserialize all VTables inside the module and add them to SILMod.
void getAllVTables();
/// Deserialize all move only deinit tables inside the module and add them
/// to SILMod.
void getAllMoveOnlyDeinits();
/// Deserialize all WitnessTables inside the module and add them to SILMod.
void getAllWitnessTables();
/// Deserialize all DefaultWitnessTables inside the module and add them
/// to SILMod.
void getAllDefaultWitnessTables();
/// Deserialize all Property descriptors inside the module and add them
/// to SILMod.
void getAllProperties();
/// Deserialize all DifferentiabilityWitnesses inside the module and add
/// them to SILMod.
void getAllDifferentiabilityWitnesses();
SILDeserializer(ModuleFile *MF, SILModule &M,
DeserializationNotificationHandlerSet *callback);
// Out of line to avoid instantiation OnDiskChainedHashTable here.
~SILDeserializer();
};
} // end namespace swift