Skip to content

Commit ca1348e

Browse files
authored
Merge pull request #27052 from eeckstein/fix-gsil
2 parents d1c5663 + f1ae1dc commit ca1348e

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class SILFunction
152152
/// The source location and scope of the function.
153153
const SILDebugScope *DebugScope;
154154

155+
/// The AST decl context of the function.
156+
DeclContext *DeclCtxt;
157+
155158
/// The profiler for instrumentation based profiling, or null if profiling is
156159
/// disabled.
157160
SILProfiler *Profiler = nullptr;
@@ -614,10 +617,8 @@ class SILFunction
614617
ExactSelfClass = t;
615618
}
616619

617-
/// Get the DeclContext of this function. (Debug info only).
618-
DeclContext *getDeclContext() const {
619-
return getLocation().getAsDeclContext();
620-
}
620+
/// Get the DeclContext of this function.
621+
DeclContext *getDeclContext() const { return DeclCtxt; }
621622

622623
/// \returns True if the function is marked with the @_semantics attribute
623624
/// and has special semantics that the optimizer can use to optimize the
@@ -704,8 +705,16 @@ class SILFunction
704705
return getDebugScope()->Loc;
705706
}
706707

707-
/// Initialize the debug scope of the function.
708-
void setDebugScope(const SILDebugScope *DS) { DebugScope = DS; }
708+
/// Initialize the debug scope of the function and also set the DeclCtxt.
709+
void setDebugScope(const SILDebugScope *DS) {
710+
DebugScope = DS;
711+
DeclCtxt = (DS ? DebugScope->Loc.getAsDeclContext() : nullptr);
712+
}
713+
714+
/// Initialize the debug scope for debug info on SIL level (-gsil).
715+
void setSILDebugScope(const SILDebugScope *DS) {
716+
DebugScope = DS;
717+
}
709718

710719
/// Get the source location of the function.
711720
const SILDebugScope *getDebugScope() const { return DebugScope; }

lib/SIL/SILFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ SILFunction::SILFunction(SILModule &Module, SILLinkage Linkage, StringRef Name,
9393
IsExactSelfClass_t isExactSelfClass)
9494
: Module(Module), Name(Name), LoweredType(LoweredType),
9595
GenericEnv(genericEnv), SpecializationInfo(nullptr),
96-
DebugScope(DebugScope), Bare(isBareSILFunction), Transparent(isTrans),
96+
Bare(isBareSILFunction), Transparent(isTrans),
9797
Serialized(isSerialized), Thunk(isThunk),
9898
ClassSubclassScope(unsigned(classSubclassScope)), GlobalInitFlag(false),
9999
InlineStrategy(inlineStrategy), Linkage(unsigned(Linkage)),
@@ -104,6 +104,7 @@ SILFunction::SILFunction(SILModule &Module, SILLinkage Linkage, StringRef Name,
104104
EffectsKindAttr(E), EntryCount(entryCount) {
105105
assert(!Transparent || !IsDynamicReplaceable);
106106
validateSubclassScope(classSubclassScope, isThunk, nullptr);
107+
setDebugScope(DebugScope);
107108

108109
if (InsertBefore)
109110
Module.functions.insert(SILModule::iterator(InsertBefore), this);

lib/SILOptimizer/UtilityPasses/SILDebugInfoGenerator.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class SILDebugInfoGenerator : public SILModuleTransform {
128128
SILLocation::DebugLoc DL(Ctx.LCS.LineNum, 1, FileNameBuf);
129129
RegularLocation Loc(DL);
130130
SILDebugScope *Scope = new (*M) SILDebugScope(Loc, F);
131-
F->setDebugScope(Scope);
131+
F->setSILDebugScope(Scope);
132132

133133
// Ensure that the function is visible for debugging.
134134
F->setBare(IsNotBare);
@@ -141,16 +141,24 @@ class SILDebugInfoGenerator : public SILModuleTransform {
141141
for (SILFunction *F : PrintedFuncs) {
142142
const SILDebugScope *Scope = F->getDebugScope();
143143
for (SILBasicBlock &BB : *F) {
144-
for (SILInstruction &I : BB) {
145-
SILLocation Loc = I.getLoc();
146-
SILLocation::DebugLoc DL(Ctx.LineNums[&I], 1, FileNameBuf);
144+
for (auto iter = BB.begin(), end = BB.end(); iter != end;) {
145+
SILInstruction *I = &*iter;
146+
++iter;
147+
if (isa<DebugValueInst>(I) || isa<DebugValueAddrInst>(I)) {
148+
// debug_value and debug_value_addr are not needed anymore.
149+
// Also, keeping them might trigger a verifier error.
150+
I->eraseFromParent();
151+
continue;
152+
}
153+
SILLocation Loc = I->getLoc();
154+
SILLocation::DebugLoc DL(Ctx.LineNums[I], 1, FileNameBuf);
147155
assert(DL.Line && "no line set for instruction");
148156
if (Loc.is<ReturnLocation>() || Loc.is<ImplicitReturnLocation>()) {
149157
Loc.setDebugInfoLoc(DL);
150-
I.setDebugLocation(SILDebugLocation(Loc, Scope));
158+
I->setDebugLocation(SILDebugLocation(Loc, Scope));
151159
} else {
152160
RegularLocation RLoc(DL);
153-
I.setDebugLocation(SILDebugLocation(RLoc, Scope));
161+
I->setDebugLocation(SILDebugLocation(RLoc, Scope));
154162
}
155163
}
156164
}

test/DebugInfo/Inputs/testclass.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
open class X {
3+
public var testvar: Int = 27
4+
}
5+

test/DebugInfo/gsil.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: %FileCheck %s < %t/out.ir
44
// RUN: %FileCheck %s --check-prefix=CHECK_OUT_SIL < %t/out.ir.gsil_0.sil
55

6+
// Second test: check that we don't crash with multi-threaded IRGen
7+
// RUN: %target-swift-frontend -c %s %S/Inputs/testclass.swift -wmo -O -num-threads 1 -gsil -o %t/gsil.o -o %t/testclass.o
8+
69
// CHECK: !DIFile(filename: "{{.+}}gsil.swift", directory: "{{.+}}")
710
// CHECK: [[F:![0-9]+]] = !DIFile(filename: "{{.+}}out.ir.gsil_0.sil",
811
// CHECK: !DISubprogram(linkageName: "$s3out6testityyF", scope: !{{[0-9]+}}, file: [[F]], line: {{[1-9][0-9]+}},

0 commit comments

Comments
 (0)