@@ -193,6 +193,7 @@ SILDeserializer::SILDeserializer(
193193 kind == sil_index_block::SIL_GLOBALVAR_NAMES ||
194194 kind == sil_index_block::SIL_WITNESS_TABLE_NAMES ||
195195 kind == sil_index_block::SIL_DEFAULT_WITNESS_TABLE_NAMES ||
196+ kind == sil_index_block::SIL_DEFAULT_OVERRIDE_TABLE_NAMES ||
196197 kind == sil_index_block::SIL_PROPERTY_OFFSETS ||
197198 kind == sil_index_block::SIL_DIFFERENTIABILITY_WITNESS_NAMES)) &&
198199 " Expect SIL_FUNC_NAMES, SIL_VTABLE_NAMES, SIL_GLOBALVAR_NAMES, \
@@ -212,6 +213,8 @@ SILDeserializer::SILDeserializer(
212213 WitnessTableList = readFuncTable (scratch, blobData);
213214 else if (kind == sil_index_block::SIL_DEFAULT_WITNESS_TABLE_NAMES)
214215 DefaultWitnessTableList = readFuncTable (scratch, blobData);
216+ else if (kind == sil_index_block::SIL_DEFAULT_OVERRIDE_TABLE_NAMES)
217+ DefaultOverrideTableList = readFuncTable (scratch, blobData);
215218 else if (kind == sil_index_block::SIL_DIFFERENTIABILITY_WITNESS_NAMES)
216219 DifferentiabilityWitnessList = readFuncTable (scratch, blobData);
217220 else if (kind == sil_index_block::SIL_PROPERTY_OFFSETS) {
@@ -256,6 +259,11 @@ SILDeserializer::SILDeserializer(
256259 offKind == sil_index_block::SIL_DEFAULT_WITNESS_TABLE_OFFSETS) &&
257260 " Expect a SIL_DEFAULT_WITNESS_TABLE_OFFSETS record." );
258261 MF->allocateBuffer (DefaultWitnessTables, scratch);
262+ } else if (kind == sil_index_block::SIL_DEFAULT_OVERRIDE_TABLE_NAMES) {
263+ assert ((next.Kind == llvm::BitstreamEntry::Record &&
264+ offKind == sil_index_block::SIL_DEFAULT_OVERRIDE_TABLE_OFFSETS) &&
265+ " Expect a SIL_DEFAULT_OVERRIDE_TABLE_OFFSETS record." );
266+ MF->allocateBuffer (DefaultOverrideTables, scratch);
259267 } else if (kind == sil_index_block::SIL_DIFFERENTIABILITY_WITNESS_NAMES) {
260268 assert ((next.Kind == llvm::BitstreamEntry::Record &&
261269 offKind ==
@@ -1121,6 +1129,7 @@ llvm::Expected<SILFunction *> SILDeserializer::readSILFunctionChecked(
11211129 Builder.setCurrentDebugScope (fn->getDebugScope ());
11221130 while (kind != SIL_FUNCTION && kind != SIL_VTABLE && kind != SIL_GLOBALVAR &&
11231131 kind != SIL_MOVEONLY_DEINIT && kind != SIL_WITNESS_TABLE &&
1132+ kind != SIL_DEFAULT_OVERRIDE_TABLE &&
11241133 kind != SIL_DIFFERENTIABILITY_WITNESS) {
11251134 if (kind == SIL_BASIC_BLOCK)
11261135 // Handle a SILBasicBlock record.
@@ -4091,6 +4100,7 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
40914100
40924101 while (kind != SIL_FUNCTION && kind != SIL_VTABLE && kind != SIL_GLOBALVAR &&
40934102 kind != SIL_MOVEONLY_DEINIT && kind != SIL_WITNESS_TABLE &&
4103+ kind != SIL_DEFAULT_OVERRIDE_TABLE &&
40944104 kind != SIL_DIFFERENTIABILITY_WITNESS) {
40954105 if (readSILInstruction (nullptr , Builder, kind, scratch))
40964106 MF->fatal (" readSILInstruction returns error" );
@@ -4219,7 +4229,8 @@ SILVTable *SILDeserializer::readVTable(DeclID VId) {
42194229 std::vector<SILVTable::Entry> vtableEntries;
42204230 // Another SIL_VTABLE record means the end of this VTable.
42214231 while (kind != SIL_VTABLE && kind != SIL_WITNESS_TABLE &&
4222- kind != SIL_DEFAULT_WITNESS_TABLE && kind != SIL_FUNCTION &&
4232+ kind != SIL_DEFAULT_WITNESS_TABLE &&
4233+ kind != SIL_DEFAULT_OVERRIDE_TABLE && kind != SIL_FUNCTION &&
42234234 kind != SIL_PROPERTY && kind != SIL_MOVEONLY_DEINIT) {
42244235 assert (kind == SIL_VTABLE_ENTRY &&
42254236 " Content of Vtable should be in SIL_VTABLE_ENTRY." );
@@ -4434,10 +4445,9 @@ void SILDeserializer::readWitnessTableEntries(
44344445 unsigned kind = maybeKind.get ();
44354446
44364447 // Another record means the end of this WitnessTable.
4437- while (kind != SIL_WITNESS_TABLE &&
4438- kind != SIL_DEFAULT_WITNESS_TABLE &&
4439- kind != SIL_DIFFERENTIABILITY_WITNESS &&
4440- kind != SIL_FUNCTION) {
4448+ while (kind != SIL_WITNESS_TABLE && kind != SIL_DEFAULT_WITNESS_TABLE &&
4449+ kind != SIL_DEFAULT_OVERRIDE_TABLE &&
4450+ kind != SIL_DIFFERENTIABILITY_WITNESS && kind != SIL_FUNCTION) {
44414451 if (kind == SIL_DEFAULT_WITNESS_TABLE_NO_ENTRY) {
44424452 witnessEntries.push_back (SILDefaultWitnessTable::Entry ());
44434453 } else if (kind == SIL_WITNESS_BASE_ENTRY) {
@@ -4825,6 +4835,181 @@ SILDeserializer::lookupDefaultWitnessTable(SILDefaultWitnessTable *existingWt) {
48254835 return Wt;
48264836}
48274837
4838+ void SILDeserializer::readDefaultOverrideTableEntries (
4839+ llvm::BitstreamEntry &entry,
4840+ std::vector<SILDefaultOverrideTable::Entry> &entries) {
4841+ SmallVector<uint64_t , 64 > scratch;
4842+ llvm::Expected<unsigned > maybeKind = SILCursor.readRecord (entry.ID , scratch);
4843+ if (!maybeKind)
4844+ MF->fatal (maybeKind.takeError ());
4845+ unsigned kind = maybeKind.get ();
4846+
4847+ // Another record means the end of this DefaultOverrideTable.
4848+ while (kind == SIL_DEFAULT_OVERRIDE_TABLE_ENTRY) {
4849+ ArrayRef<uint64_t > ListOfValues;
4850+ DeclID NameID;
4851+ DefaultOverrideTableEntryLayout::readRecord (scratch, NameID, ListOfValues);
4852+ SILFunction *impl = nullptr ;
4853+ if (NameID != 0 ) {
4854+ impl = getFuncForReference (MF->getIdentifierText (NameID));
4855+ }
4856+ if (impl || NameID == 0 ) {
4857+ unsigned NextValueIndex = 0 ;
4858+ auto method = getSILDeclRef (MF, ListOfValues, NextValueIndex);
4859+ auto original = getSILDeclRef (MF, ListOfValues, NextValueIndex);
4860+ entries.push_back (SILDefaultOverrideTable::Entry{method, original, impl});
4861+ }
4862+
4863+ // Fetch the next record.
4864+ scratch.clear ();
4865+ llvm::Expected<llvm::BitstreamEntry> maybeEntry =
4866+ SILCursor.advance (AF_DontPopBlockAtEnd);
4867+ if (!maybeEntry)
4868+ MF->fatal (maybeEntry.takeError ());
4869+ entry = maybeEntry.get ();
4870+ if (entry.Kind == llvm::BitstreamEntry::EndBlock)
4871+ // EndBlock means the end of this DefaultOverrideTable.
4872+ break ;
4873+ maybeKind = SILCursor.readRecord (entry.ID , scratch);
4874+ if (!maybeKind)
4875+ MF->fatal (maybeKind.takeError ());
4876+ kind = maybeKind.get ();
4877+ }
4878+ }
4879+
4880+ SILDefaultOverrideTable *
4881+ SILDeserializer::readDefaultOverrideTable (DeclID tableID,
4882+ SILDefaultOverrideTable *existingOt) {
4883+ if (tableID == 0 )
4884+ return nullptr ;
4885+ assert (tableID <= DefaultOverrideTables.size () &&
4886+ " invalid DefaultOverrideTable ID" );
4887+
4888+ auto &oTableOrOffset = DefaultOverrideTables[tableID - 1 ];
4889+
4890+ if (oTableOrOffset.isFullyDeserialized ())
4891+ return oTableOrOffset.get ();
4892+
4893+ BCOffsetRAII restoreOffset (SILCursor);
4894+ if (llvm::Error Err = SILCursor.JumpToBit (oTableOrOffset.getOffset ()))
4895+ MF->fatal (std::move (Err));
4896+ llvm::Expected<llvm::BitstreamEntry> maybeEntry =
4897+ SILCursor.advance (AF_DontPopBlockAtEnd);
4898+ if (!maybeEntry)
4899+ MF->fatal (maybeEntry.takeError ());
4900+ llvm::BitstreamEntry entry = maybeEntry.get ();
4901+ if (entry.Kind == llvm::BitstreamEntry::Error) {
4902+ LLVM_DEBUG (llvm::dbgs () << " Cursor advance error in "
4903+ " readDefaultOverrideTable.\n " );
4904+ return nullptr ;
4905+ }
4906+
4907+ SmallVector<uint64_t , 64 > scratch;
4908+ StringRef blobData;
4909+ llvm::Expected<unsigned > maybeKind =
4910+ SILCursor.readRecord (entry.ID , scratch, &blobData);
4911+ if (!maybeKind)
4912+ MF->fatal (maybeKind.takeError ());
4913+ unsigned kind = maybeKind.get ();
4914+ assert (kind == SIL_DEFAULT_OVERRIDE_TABLE &&
4915+ " expect a sil default override table" );
4916+ (void )kind;
4917+
4918+ unsigned rawLinkage;
4919+ DeclID classID;
4920+ DefaultOverrideTableLayout::readRecord (scratch, classID, rawLinkage);
4921+
4922+ auto Linkage = fromStableSILLinkage (rawLinkage);
4923+ if (!Linkage) {
4924+ LLVM_DEBUG (llvm::dbgs () << " invalid linkage code " << rawLinkage
4925+ << " for SILFunction\n " );
4926+ MF->fatal (" invalid linkage code" );
4927+ }
4928+
4929+ ClassDecl *decl = cast<ClassDecl>(MF->getDecl (classID));
4930+ if (decl == nullptr ) {
4931+ LLVM_DEBUG (llvm::dbgs () << " invalid class code " << classID << " \n " );
4932+ MF->fatal (" invalid class code" );
4933+ }
4934+
4935+ PrettyStackTraceDecl trace (" deserializing default override table for" , decl);
4936+
4937+ if (!existingOt)
4938+ existingOt =
4939+ SILMod.lookUpDefaultOverrideTable (decl, /* deserializeLazily=*/ false );
4940+ auto oT = existingOt;
4941+
4942+ // If we have an existing default override table, verify that the class
4943+ // matches up.
4944+ if (oT) {
4945+ if (oT->getClass () != decl) {
4946+ MF->fatal (" Protocol mismatch" );
4947+ }
4948+
4949+ // Don't override the linkage of a default override table with an existing
4950+ // declaration.
4951+
4952+ } else {
4953+ // Otherwise, create a new override table declaration.
4954+ oT = SILDefaultOverrideTable::declare (SILMod, *Linkage, decl);
4955+ if (Callback)
4956+ Callback->didDeserialize (MF->getAssociatedModule (), oT);
4957+ }
4958+
4959+ // Fetch the next record.
4960+ scratch.clear ();
4961+ maybeEntry = SILCursor.advance (AF_DontPopBlockAtEnd);
4962+ if (!maybeEntry)
4963+ MF->fatal (maybeEntry.takeError ());
4964+ entry = maybeEntry.get ();
4965+ if (entry.Kind == llvm::BitstreamEntry::EndBlock)
4966+ return nullptr ;
4967+
4968+ std::vector<SILDefaultOverrideTable::Entry> entries;
4969+ readDefaultOverrideTableEntries (entry, entries);
4970+
4971+ oT->define (entries);
4972+ oTableOrOffset.set (oT, /* fully deserialized*/ true );
4973+ if (Callback)
4974+ Callback->didDeserializeDefaultOverrideTableEntries (
4975+ MF->getAssociatedModule (), oT);
4976+ return oT;
4977+ }
4978+
4979+ SILDefaultOverrideTable *SILDeserializer::lookupDefaultOverrideTable (
4980+ SILDefaultOverrideTable *existingOt) {
4981+ assert (existingOt &&
4982+ " Cannot deserialize a null default override table declaration." );
4983+ assert (existingOt->isDeclaration () &&
4984+ " Cannot deserialize a default override table "
4985+ " definition." );
4986+
4987+ // If we don't have a default override table list, we can't look anything up.
4988+ if (!DefaultOverrideTableList)
4989+ return nullptr ;
4990+
4991+ // Use the mangled name of the class to lookup the partially deserialized
4992+ // value from the default override table list.
4993+ auto iter = DefaultOverrideTableList->find (existingOt->getUniqueName ());
4994+ if (iter == DefaultOverrideTableList->end ())
4995+ return nullptr ;
4996+
4997+ // Attempt to read the default override table.
4998+ auto Ot = readDefaultOverrideTable (*iter, existingOt);
4999+ if (Ot)
5000+ LLVM_DEBUG (llvm::dbgs () << " Deserialize SIL:\n " ; Ot->dump ());
5001+
5002+ return Ot;
5003+ }
5004+
5005+ void SILDeserializer::getAllDefaultOverrideTables () {
5006+ if (!DefaultOverrideTableList)
5007+ return ;
5008+ for (unsigned index = 0 , size = DefaultOverrideTables.size (); index < size;
5009+ ++index)
5010+ readDefaultOverrideTable (index + 1 , nullptr );
5011+ }
5012+
48285013SILDifferentiabilityWitness *
48295014SILDeserializer::readDifferentiabilityWitness (DeclID DId) {
48305015 if (DId == 0 )
0 commit comments