@@ -162,6 +162,9 @@ namespace {
162162 using DebugScopeID = DeclID;
163163 using DebugScopeIDField = DeclIDField;
164164
165+ using LocationID = DeclID;
166+ using LocationIDField = DeclIDField;
167+
165168 Serializer &S;
166169
167170 llvm::BitstreamWriter &Out;
@@ -226,6 +229,7 @@ namespace {
226229
227230 llvm::DenseMap<PointerUnion<const SILDebugScope *, SILFunction *>, DeclID>
228231 DebugScopeMap;
232+ llvm::DenseMap<const void *, unsigned > SourceLocMap;
229233
230234 // / Give each SILBasicBlock a unique ID.
231235 llvm::DenseMap<const SILBasicBlock *, unsigned > BasicBlockMap;
@@ -299,6 +303,7 @@ namespace {
299303
300304 // / Serialize and write SILDebugScope graph in post order.
301305 void writeDebugScopes (const SILDebugScope *Scope, const SourceManager &SM);
306+ void writeSourceLoc (SILLocation SLoc, const SourceManager &SM);
302307
303308 void writeNoOperandLayout (const SILInstruction *I) {
304309 unsigned abbrCode = SILAbbrCodes[SILInstNoOperandLayout::Code];
@@ -600,6 +605,7 @@ void SILSerializer::writeSILFunction(const SILFunction &F, bool DeclOnly) {
600605 }
601606
602607 DebugScopeMap.clear ();
608+ SourceLocMap.clear ();
603609
604610 if (SerializeDebugInfoSIL)
605611 writeDebugScopes (F.getDebugScope (), F.getModule ().getSourceManager ());
@@ -697,6 +703,9 @@ void SILSerializer::writeSILBasicBlock(const SILBasicBlock &BB) {
697703 writeDebugScopes (Prev, SM);
698704 }
699705 }
706+ if (SerializeDebugInfoSIL) {
707+ writeSourceLoc (SI.getLoc (), SM);
708+ }
700709
701710 writeSILInstruction (SI);
702711 }
@@ -3074,6 +3083,51 @@ void SILSerializer::writeSILProperty(const SILProperty &prop) {
30743083 componentValues);
30753084}
30763085
3086+ void SILSerializer::writeSourceLoc (SILLocation Loc, const SourceManager &SM) {
3087+ auto SLoc = Loc.getSourceLoc ();
3088+ auto OpaquePtr = SLoc.getOpaquePointerValue ();
3089+ uint8_t LocationKind;
3090+ switch (Loc.getKind ()) {
3091+ case SILLocation::ReturnKind:
3092+ LocationKind = SILLocation::ReturnKind;
3093+ break ;
3094+ case SILLocation::ImplicitReturnKind:
3095+ LocationKind = SILLocation::ImplicitReturnKind;
3096+ break ;
3097+ case SILLocation::InlinedKind:
3098+ case SILLocation::MandatoryInlinedKind:
3099+ case SILLocation::CleanupKind:
3100+ case SILLocation::ArtificialUnreachableKind:
3101+ case SILLocation::RegularKind:
3102+ LocationKind = SILLocation::RegularKind;
3103+ break ;
3104+ }
3105+
3106+ if (SourceLocMap.find (OpaquePtr) != SourceLocMap.end ()) {
3107+ SourceLocRefLayout::emitRecord (Out, ScratchRecord,
3108+ SILAbbrCodes[SourceLocRefLayout::Code],
3109+ SourceLocMap[OpaquePtr], LocationKind, (unsigned )Loc.isImplicit ());
3110+ return ;
3111+ }
3112+
3113+ ValueID Row = 0 ;
3114+ ValueID Column = 0 ;
3115+ ValueID FNameID = 0 ;
3116+
3117+ if (!SLoc.isValid ()) {
3118+ // emit empty source loc
3119+ SourceLocRefLayout::emitRecord (Out, ScratchRecord, SILAbbrCodes[SourceLocRefLayout::Code], 0 , 0 , (unsigned )0 );
3120+ return ;
3121+ }
3122+
3123+ std::tie (Row, Column) = SM.getPresumedLineAndColumnForLoc (SLoc);
3124+ FNameID = S.addUniquedStringRef (SM.getDisplayNameForLoc (SLoc));
3125+ SourceLocMap.insert ({OpaquePtr, SourceLocMap.size () + 1 });
3126+ SourceLocLayout::emitRecord (Out, ScratchRecord,
3127+ SILAbbrCodes[SourceLocLayout::Code], Row, Column,
3128+ FNameID, LocationKind, (unsigned )Loc.isImplicit ());
3129+ }
3130+
30773131void SILSerializer::writeDebugScopes (const SILDebugScope *Scope,
30783132 const SourceManager &SM) {
30793133
@@ -3387,6 +3441,8 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
33873441 registerSILAbbr<DifferentiabilityWitnessLayout>();
33883442 registerSILAbbr<SILDebugScopeLayout>();
33893443 registerSILAbbr<SILDebugScopeRefLayout>();
3444+ registerSILAbbr<SourceLocLayout>();
3445+ registerSILAbbr<SourceLocRefLayout>();
33903446
33913447 // Write out VTables first because it may require serializations of
33923448 // non-transparent SILFunctions (body is not needed).
0 commit comments