Skip to content

Commit f25f024

Browse files
committed
Serialize and deserialize implicit flag in SourceLocs. Also, skip
emitting unreachable block diagnostic for deserialized instructions as it was suppressed already in the past as instructions were deserialized as implicit by default
1 parent b3e80f7 commit f25f024

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ class SILLocation {
263263
assert(filePos && !filePos->filename.empty());
264264
}
265265

266+
SILLocation(FilenameAndLocation *filePos, LocationKind K, bool Implicit)
267+
: storage(filePos), kindAndFlags(K, FilenameAndLocationKind) {
268+
assert(filePos && !filePos->filename.empty());
269+
kindAndFlags.fields.implicit = Implicit;
270+
}
271+
266272
// It is okay to pass a nullptr, but preferably, a null-location should be
267273
// created with `invalid()`.
268274
SILLocation(ASTNodeTy Node, LocationKind K)
@@ -552,6 +558,8 @@ class RegularLocation : public SILLocation {
552558
: SILLocation(L, RegularKind, Implicit) {}
553559
RegularLocation(FilenameAndLocation *filePos)
554560
: SILLocation(filePos, RegularKind) {}
561+
RegularLocation(FilenameAndLocation *filePos, bool Implicit)
562+
: SILLocation(filePos, RegularKind, Implicit) {}
555563

556564
/// Convert \p loc to a RegularLocation.
557565
explicit RegularLocation(SILLocation loc) : SILLocation(loc, RegularKind) {}
@@ -622,6 +630,9 @@ class ReturnLocation : public SILLocation {
622630
ReturnLocation(FilenameAndLocation *filePos)
623631
: SILLocation(filePos, ReturnKind) {}
624632

633+
ReturnLocation(FilenameAndLocation *filePos, bool Implicit)
634+
: SILLocation(filePos, ReturnKind, Implicit) {}
635+
625636
static bool isKind(const SILLocation& L) {
626637
return L.getKind() == ReturnKind;
627638
}
@@ -643,6 +654,9 @@ class ImplicitReturnLocation : public SILLocation {
643654
ImplicitReturnLocation(FilenameAndLocation *filePos)
644655
: SILLocation(filePos, ImplicitReturnKind) {}
645656

657+
ImplicitReturnLocation(FilenameAndLocation *filePos, bool Implicit)
658+
: SILLocation(filePos, ImplicitReturnKind, Implicit) {}
659+
646660
/// Convert \p loc to an ImplicitReturnLocation.
647661
explicit ImplicitReturnLocation(SILLocation Loc);
648662

lib/SILOptimizer/Mandatory/DiagnoseUnreachable.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,15 @@ static bool diagnoseUnreachableBlock(
994994
if (Loc.is<ImplicitReturnLocation>() || Loc.isAutoGenerated())
995995
return false;
996996

997+
// Without debug info serialization, source locations are deserialized as
998+
// implicit by default and as a result this diagnostic is never produced on
999+
// deserialized code. With support for serializing debug info/source
1000+
// locations, the implicit flag is deserialized properly . However, this
1001+
// creates new diagnostic warnings in many tests. Until these tests are
1002+
// fixed, we suppress these warnings to match existing behavior.
1003+
if (Loc.isFilenameAndLocation())
1004+
continue;
1005+
9971006
// Check if the instruction corresponds to user-written code, also make
9981007
// sure we don't report an error twice for the same instruction.
9991008
if (isUserCode(&*I) && !State->BlocksWithErrors.contains(&B)) {

lib/Serialization/DeserializeSIL.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,32 +396,36 @@ SILDeserializer::readNextRecord(SmallVectorImpl<uint64_t> &scratch) {
396396

397397
std::optional<SILLocation> SILDeserializer::readLoc(unsigned kind,
398398
SmallVectorImpl<uint64_t> &scratch) {
399-
uint8_t LocationKind = 0;
399+
unsigned LocationKind = 0, Implicit;
400400
SILLocation::FilenameAndLocation *FNameLoc = nullptr;
401401
if (kind == SIL_SOURCE_LOC_REF) {
402402
ValueID LocID;
403-
SourceLocRefLayout::readRecord(scratch, LocID, LocationKind);
403+
SourceLocRefLayout::readRecord(scratch, LocID, LocationKind, Implicit);
404404
if (LocID == 0)
405405
return std::optional<SILLocation>();
406-
FNameLoc = ParsedLocs[LocID];
406+
FNameLoc = ParsedLocs[LocID - 1];
407407
} else {
408408
ValueID Row = 0, Col = 0, FNameID = 0;
409-
SourceLocLayout::readRecord(scratch, Row, Col, FNameID, LocationKind);
409+
SourceLocLayout::readRecord(scratch, Row, Col, FNameID, LocationKind, Implicit);
410410

411411

412412
FNameLoc = SILLocation::FilenameAndLocation::alloc(
413413
Row, Col, MF->getIdentifierText(FNameID), SILMod);
414414

415-
ParsedLocs.insert({ParsedLocs.size() + 1, FNameLoc});
415+
ParsedLocs.push_back(FNameLoc);
416416
}
417417

418+
// FIXME: Instructions are being deserialized as implicit as existing code in
419+
// readSILInstruction also deserializes instructions as implicit by default.
420+
// This suppresses some diagnostics which otherwise break tests. This should
421+
// be fixed in both places
418422
switch(LocationKind) {
419423
case SILLocation::ReturnKind:
420-
return ReturnLocation(FNameLoc);
424+
return ReturnLocation(FNameLoc, Implicit);
421425
case SILLocation::ImplicitReturnKind:
422-
return ImplicitReturnLocation(FNameLoc);
426+
return ImplicitReturnLocation(FNameLoc, Implicit);
423427
default:
424-
return RegularLocation(FNameLoc);
428+
return RegularLocation(FNameLoc, Implicit);
425429
}
426430
}
427431

lib/Serialization/DeserializeSIL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace swift {
153153
std::optional<SILLocation> readLoc(unsigned kind, SmallVectorImpl<uint64_t> &scratch);
154154

155155
llvm::DenseMap<unsigned, const SILDebugScope *> ParsedScopes;
156-
llvm::DenseMap<unsigned, SILLocation::FilenameAndLocation *> ParsedLocs;
156+
llvm::SmallVector<SILLocation::FilenameAndLocation *> ParsedLocs;
157157

158158
SILFunction *getFuncForReference(StringRef Name, SILType Ty, TypeExpansionContext context);
159159
SILFunction *getFuncForReference(StringRef Name, bool forDebugScope = false);

lib/Serialization/SILFormat.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,15 @@ namespace sil_block {
317317
ValueIDField,
318318
ValueIDField,
319319
ValueIDField,
320-
BCFixed<8> /// LocationKind
320+
BCFixed<3>, /// LocationKind
321+
BCFixed<1> /// Implicit
321322
>;
322323

323324
using SourceLocRefLayout = BCRecordLayout<
324325
SIL_SOURCE_LOC_REF,
325326
ValueIDField,
326-
BCFixed<8>
327+
BCFixed<3>,
328+
BCFixed<1> /// Implicit
327329
>;
328330

329331
using SILFunctionLayout =

lib/Serialization/SerializeSIL.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,7 +3102,7 @@ void SILSerializer::writeSourceLoc(SILLocation Loc, const SourceManager &SM) {
31023102
if (SourceLocMap.find(OpaquePtr) != SourceLocMap.end()) {
31033103
SourceLocRefLayout::emitRecord(Out, ScratchRecord,
31043104
SILAbbrCodes[SourceLocRefLayout::Code],
3105-
SourceLocMap[OpaquePtr], LocationKind);
3105+
SourceLocMap[OpaquePtr], LocationKind, (unsigned)Loc.isImplicit());
31063106
return;
31073107
}
31083108

@@ -3112,7 +3112,7 @@ void SILSerializer::writeSourceLoc(SILLocation Loc, const SourceManager &SM) {
31123112

31133113
if (!SLoc.isValid()) {
31143114
//emit empty source loc
3115-
SourceLocRefLayout::emitRecord(Out, ScratchRecord, SILAbbrCodes[SourceLocRefLayout::Code], 0, 0);
3115+
SourceLocRefLayout::emitRecord(Out, ScratchRecord, SILAbbrCodes[SourceLocRefLayout::Code], 0, 0, (unsigned)0);
31163116
return;
31173117
}
31183118

@@ -3121,7 +3121,7 @@ void SILSerializer::writeSourceLoc(SILLocation Loc, const SourceManager &SM) {
31213121
SourceLocMap.insert({OpaquePtr, SourceLocMap.size() + 1});
31223122
SourceLocLayout::emitRecord(Out, ScratchRecord,
31233123
SILAbbrCodes[SourceLocLayout::Code], Row, Column,
3124-
FNameID, LocationKind);
3124+
FNameID, LocationKind, (unsigned)Loc.isImplicit());
31253125
}
31263126

31273127
void SILSerializer::writeDebugScopes(const SILDebugScope *Scope,

0 commit comments

Comments
 (0)