Skip to content

Commit f68889e

Browse files
committed
Rebase SILScope generation on top of ASTScope
1 parent 760584c commit f68889e

20 files changed

+408
-148
lines changed

include/swift/AST/ASTScope.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class GenericContext;
7878
class DeclName;
7979
class StmtConditionElement;
8080

81+
namespace Lowering {
82+
class SILGenFunction;
83+
}
84+
8185
namespace ast_scope {
8286
class ASTScopeImpl;
8387
class GenericTypeOrExtensionScope;
@@ -129,6 +133,7 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
129133
friend class IterableTypeBodyPortion;
130134
friend class ScopeCreator;
131135
friend class ASTSourceFileScope;
136+
friend class Lowering::SILGenFunction;
132137

133138
#pragma mark - tree state
134139
protected:
@@ -273,6 +278,10 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
273278
static std::pair<CaseStmt *, CaseStmt *>
274279
lookupFallthroughSourceAndDest(SourceFile *sourceFile, SourceLoc loc);
275280

281+
/// Scopes that cannot bind variables may set this to true to create more
282+
/// compact scope tree in the debug info.
283+
virtual bool ignoreInDebugInfo() const { return false; }
284+
276285
#pragma mark - - lookup- starting point
277286
private:
278287
static const ASTScopeImpl *findStartingScopeForLookup(SourceFile *,
@@ -777,6 +786,7 @@ class ParameterListScope final : public ASTScopeImpl {
777786
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
778787

779788
NullablePtr<const void> addressForPrinting() const override { return params; }
789+
bool ignoreInDebugInfo() const override { return true; }
780790
};
781791

782792
/// Body of functions, methods, constructors, destructors and accessors.
@@ -799,6 +809,7 @@ class FunctionBodyScope : public ASTScopeImpl {
799809
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
800810
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
801811
Decl *getDecl() const { return decl; }
812+
bool ignoreInDebugInfo() const override { return true; }
802813

803814
protected:
804815
bool lookupLocalsOrMembers(DeclConsumer) const override;
@@ -825,6 +836,7 @@ class DefaultArgumentInitializerScope final : public ASTScopeImpl {
825836
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
826837
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
827838
Decl *getDecl() const { return decl; }
839+
bool ignoreInDebugInfo() const override { return true; }
828840
};
829841

830842
/// Consider:
@@ -858,7 +870,7 @@ class AttachedPropertyWrapperScope final : public ASTScopeImpl {
858870
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
859871
return attr;
860872
}
861-
873+
bool ignoreInDebugInfo() const override { return true; }
862874
private:
863875
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
864876
};
@@ -952,6 +964,7 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
952964
std::string getClassName() const override;
953965
SourceRange
954966
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
967+
bool ignoreInDebugInfo() const override { return true; }
955968

956969
protected:
957970
bool lookupLocalsOrMembers(DeclConsumer) const override;
@@ -1049,6 +1062,7 @@ class ClosureParametersScope final : public ASTScopeImpl {
10491062
}
10501063
NullablePtr<Expr> getExprIfAny() const override { return closureExpr; }
10511064
Expr *getExpr() const { return closureExpr; }
1065+
bool ignoreInDebugInfo() const override { return true; }
10521066

10531067
protected:
10541068
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

include/swift/SIL/SILBuilder.h

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef SWIFT_SIL_SILBUILDER_H
1414
#define SWIFT_SIL_SILBUILDER_H
1515

16+
#include "SILDebugVariable.h"
17+
#include "SILInstruction.h"
1618
#include "swift/Basic/ProfileCounter.h"
1719
#include "swift/SIL/SILArgument.h"
1820
#include "swift/SIL/SILDebugScope.h"
@@ -22,6 +24,7 @@
2224
#include "swift/SIL/SILUndef.h"
2325
#include "llvm/ADT/PointerUnion.h"
2426
#include "llvm/ADT/StringExtras.h"
27+
#include <type_traits>
2528

2629
namespace swift {
2730

@@ -199,6 +202,7 @@ class SILBuilder {
199202
setCurrentDebugScope(DS);
200203
setInsertionPoint(BB);
201204
}
205+
virtual ~SILBuilder() {}
202206

203207
// Allow a pass to override the current SIL module conventions. This should
204208
// only be done by a pass responsible for lowering SIL to a new stage
@@ -249,7 +253,7 @@ class SILBuilder {
249253
}
250254

251255
/// Convenience function for building a SILDebugLocation.
252-
SILDebugLocation getSILDebugLocation(SILLocation Loc) {
256+
virtual SILDebugLocation getSILDebugLocation(SILLocation Loc) {
253257
// FIXME: Audit all uses and enable this assertion.
254258
// assert(getCurrentDebugScope() && "no debug scope");
255259
auto Scope = getCurrentDebugScope();
@@ -259,6 +263,17 @@ class SILBuilder {
259263
return SILDebugLocation(overriddenLoc, Scope);
260264
}
261265

266+
/// When the frontend generates synthesized conformances it generates a
267+
/// fully-typechecked AST without source locations. This means that the
268+
/// ASTScope-based mechanism to generate SILDebugScopes doesn't work, which
269+
/// means we can't disambiguate local variables in different lexical
270+
/// scopes. To avoid a verification error later in the pipeline, drop all
271+
/// variables without a proper source location.
272+
bool shouldDropVariable(SILDebugVariable Var, SILLocation Loc) {
273+
return !Var.ArgNo && Loc.isSynthesizedAST();
274+
}
275+
276+
262277
/// If we have a SILFunction, return SILFunction::hasOwnership(). If we have a
263278
/// SILGlobalVariable, just return false.
264279
bool hasOwnership() const {
@@ -310,26 +325,38 @@ class SILBuilder {
310325
/// setInsertionPoint - Set the insertion point to insert before the specified
311326
/// instruction.
312327
void setInsertionPoint(SILInstruction *I) {
328+
#ifndef NDEBUG
329+
PrevDebugScope = nullptr;
330+
#endif
313331
assert(I && "can't set insertion point to a null instruction");
314332
setInsertionPoint(I->getParent(), I->getIterator());
315333
}
316334

317335
/// setInsertionPoint - Set the insertion point to insert before the specified
318336
/// instruction.
319337
void setInsertionPoint(SILBasicBlock::iterator IIIter) {
338+
#ifndef NDEBUG
339+
PrevDebugScope = nullptr;
340+
#endif
320341
setInsertionPoint(IIIter->getParent(), IIIter);
321342
}
322343

323344
/// setInsertionPoint - Set the insertion point to insert at the end of the
324345
/// specified block.
325346
void setInsertionPoint(SILBasicBlock *BB) {
347+
#ifndef NDEBUG
348+
PrevDebugScope = nullptr;
349+
#endif
326350
assert(BB && "can't set insertion point to a null basic block");
327351
setInsertionPoint(BB, BB->end());
328352
}
329353

330354
/// setInsertionPoint - Set the insertion point to insert at the end of the
331355
/// specified block.
332356
void setInsertionPoint(SILFunction::iterator BBIter) {
357+
#ifndef NDEBUG
358+
PrevDebugScope = nullptr;
359+
#endif
333360
setInsertionPoint(&*BBIter);
334361
}
335362

@@ -401,6 +428,8 @@ class SILBuilder {
401428
Optional<SILDebugVariable>
402429
substituteAnonymousArgs(llvm::SmallString<4> Name,
403430
Optional<SILDebugVariable> Var, SILLocation Loc) {
431+
if (Var && shouldDropVariable(*Var, Loc))
432+
return {};
404433
if (!Var || !Var->ArgNo || !Var->Name.empty())
405434
return Var;
406435

@@ -416,12 +445,19 @@ class SILBuilder {
416445
AllocStackInst *createAllocStack(SILLocation Loc, SILType elementType,
417446
Optional<SILDebugVariable> Var = None,
418447
bool hasDynamicLifetime = false,
419-
bool isLexical = false,
420-
bool wasMoved = false) {
448+
bool isLexical = false, bool wasMoved = false
449+
#ifndef NDEBUG
450+
,
451+
bool skipVarDeclAssert = false
452+
#endif
453+
) {
421454
llvm::SmallString<4> Name;
422455
Loc.markAsPrologue();
423-
assert((!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode<Decl>()) || Var) &&
424-
"location is a VarDecl, but SILDebugVariable is empty");
456+
#ifndef NDEBUG
457+
if (dyn_cast_or_null<VarDecl>(Loc.getAsASTNode<Decl>()))
458+
assert((skipVarDeclAssert || Loc.isSynthesizedAST() || Var) &&
459+
"location is a VarDecl, but SILDebugVariable is empty");
460+
#endif
425461
return insert(AllocStackInst::create(
426462
getSILDebugLocation(Loc), elementType, getFunction(),
427463
substituteAnonymousArgs(Name, Var, Loc), hasDynamicLifetime, isLexical,
@@ -474,10 +510,15 @@ class SILBuilder {
474510
Optional<SILDebugVariable> Var = None,
475511
bool hasDynamicLifetime = false,
476512
bool reflection = false,
477-
bool usesMoveableValueDebugInfo = false) {
513+
bool usesMoveableValueDebugInfo = false
514+
#ifndef NDEBUG
515+
, bool skipVarDeclAssert = false
516+
#endif
517+
) {
478518
llvm::SmallString<4> Name;
479519
Loc.markAsPrologue();
480-
assert((!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode<Decl>()) || Var) &&
520+
assert((skipVarDeclAssert ||
521+
!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode<Decl>()) || Var) &&
481522
"location is a VarDecl, but SILDebugVariable is empty");
482523
return insert(AllocBoxInst::create(getSILDebugLocation(Loc), BoxType, *F,
483524
substituteAnonymousArgs(Name, Var, Loc),

include/swift/SIL/SILCloner.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,12 @@ SILCloner<ImplClass>::visitAllocBoxInst(AllocBoxInst *Inst) {
962962
Inst,
963963
getBuilder().createAllocBox(
964964
Loc, this->getOpType(Inst->getType()).template castTo<SILBoxType>(),
965-
VarInfo));
965+
VarInfo, false, false, false
966+
#ifndef NDEBUG
967+
,
968+
true
969+
#endif
970+
));
966971
}
967972

968973
template<typename ImplClass>

include/swift/SIL/SILLocation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,12 @@ class SILLocation {
412412
/// Returns the source location for diagnoses.
413413
SourceLoc getSourceLoc() const;
414414

415+
/// Heuristic to detect the fake source locations generated for synthesized
416+
/// conformances.
417+
bool isSynthesizedAST() {
418+
return isASTNode() && getSourceLoc().isInvalid();
419+
}
420+
415421
/// Returns the source location for debugging.
416422
/// See ExtendedASTNodeLoc.
417423
SourceLoc getSourceLocForDebugging() const;

lib/AST/ASTScopeLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ ASTScopeImpl::findChildContaining(SourceLoc loc,
160160

161161
auto generatedInfo =
162162
sourceMgr.getGeneratedSourceInfo(*potentialLCA->getBufferID());
163-
expansionLoc = generatedInfo->originalSourceRange.getStart();
163+
if (generatedInfo)
164+
expansionLoc = generatedInfo->originalSourceRange.getStart();
164165
potentialLCA = potentialLCA->getEnclosingSourceFile();
165166
}
166167
}

lib/IRGen/IRGenSIL.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5745,8 +5745,6 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
57455745
if (!Decl)
57465746
return;
57475747

5748-
assert(i->getVarInfo() && "alloc_box without debug info");
5749-
57505748
// FIXME: This is a workaround to not produce local variables for
57515749
// capture list arguments like "[weak self]". The better solution
57525750
// would be to require all variables to be described with a
@@ -5765,7 +5763,8 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
57655763
DebugTypeInfo::getLocalVariable(Decl, RealType, type, IGM, false);
57665764

57675765
auto VarInfo = i->getVarInfo();
5768-
assert(VarInfo && "debug_value without debug info");
5766+
if (!VarInfo)
5767+
return;
57695768

57705769
auto Storage =
57715770
emitShadowCopyIfNeeded(boxWithAddr.getAddress(), i->getDebugScope(),

lib/SIL/IR/SILBuilder.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
using namespace swift;
1919

20+
extern llvm::cl::opt<bool> SILPrintDebugInfo;
21+
2022
//===----------------------------------------------------------------------===//
2123
// SILBuilder Implementation
2224
//===----------------------------------------------------------------------===//
@@ -35,9 +37,14 @@ void SILBuilder::setCurrentDebugScope(const SILDebugScope *DS) {
3537
// PrevDebugScope = sil_scope(parent: CurDebugScope)
3638
// CurDebugScope = sil_scope(parent:)
3739
// DS = PrevDebugScope
38-
if (DS && DS == PrevDebugScope)
40+
if (DS && DS == PrevDebugScope) {
41+
if (CurDebugScope->isAncestor(PrevDebugScope)) {
42+
SILPrintDebugInfo.setValue(true);
43+
BB->dump();
44+
}
3945
assert(!CurDebugScope->isAncestor(PrevDebugScope) &&
4046
"attempting to re-enter scope within same basic block");
47+
}
4148
PrevDebugScope = CurDebugScope;
4249
}
4350
#endif
@@ -630,6 +637,9 @@ DebugValueInst *SILBuilder::createDebugValue(SILLocation Loc, SILValue src,
630637
bool poisonRefs,
631638
bool operandWasMoved,
632639
bool trace) {
640+
if (shouldDropVariable(Var, Loc))
641+
return nullptr;
642+
633643
llvm::SmallString<4> Name;
634644

635645
// Debug location overrides cannot apply to debug value instructions.
@@ -643,6 +653,9 @@ DebugValueInst *SILBuilder::createDebugValue(SILLocation Loc, SILValue src,
643653
DebugValueInst *SILBuilder::createDebugValueAddr(SILLocation Loc, SILValue src,
644654
SILDebugVariable Var,
645655
bool wasMoved, bool trace) {
656+
if (shouldDropVariable(Var, Loc))
657+
return nullptr;
658+
646659
llvm::SmallString<4> Name;
647660

648661
// Debug location overrides cannot apply to debug addr instructions.

lib/SILGen/ResultPlan.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,9 @@ ResultPlanPtr ResultPlanBuilder::buildForScalar(Initialization *init,
11691169
if (SGF.silConv.isSILIndirect(result)) {
11701170
auto &resultTL = SGF.getTypeLowering(result.getReturnValueType(
11711171
SGF.SGM.M, calleeTy, SGF.getTypeExpansionContext()));
1172-
temporary = SGF.emitTemporary(loc, resultTL);
1172+
SILLocation tmpLoc(loc);
1173+
tmpLoc.markAutoGenerated();
1174+
temporary = SGF.emitTemporary(tmpLoc, resultTL);
11731175
}
11741176

11751177
return ResultPlanPtr(new ScalarResultPlan(

lib/SILGen/SILGenBuilder.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ SILGenBuilder::SILGenBuilder(SILGenFunction &SGF)
4646

4747
SILGenBuilder::SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
4848
SmallVectorImpl<SILInstruction *> *insertedInsts)
49-
: SILBuilder(insertBB, insertedInsts), SGF(SGF) {
49+
: SILBuilder(insertBB, insertedInsts), SGF(SGF) {}
5050
#ifndef NDEBUG
5151
EnableDIHoleVerification = true;
5252
#endif
53+
54+
auto FirstInsn = insertBB->begin();
55+
if (FirstInsn != insertBB->end())
56+
setCurrentDebugScope(FirstInsn->getDebugScope());
5357
}
5458

5559
SILGenBuilder::SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
@@ -58,6 +62,13 @@ SILGenBuilder::SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
5862
#ifndef NDEBUG
5963
EnableDIHoleVerification = true;
6064
#endif
65+
66+
if (insertInst != insertBB->end())
67+
setCurrentDebugScope(insertInst->getDebugScope());
68+
}
69+
70+
SILDebugLocation SILGenBuilder::getSILDebugLocation(SILLocation Loc) {
71+
return SGF.getSILDebugLocation(*this, Loc);
6172
}
6273

6374
//===----------------------------------------------------------------------===//

lib/SILGen/SILGenBuilder.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "swift/SIL/SILLocation.h"
3333

3434
namespace swift {
35+
namespace ast_scope {
36+
class ASTScopeImpl;
37+
}
38+
3539
namespace Lowering {
3640

3741
class SILGenFunction;
@@ -50,8 +54,8 @@ class SILGenBuilder : public SILBuilder {
5054
SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
5155
SILBasicBlock::iterator insertInst);
5256

53-
// Create a new builder, inheriting the given builder's context and debug
54-
// scope.
57+
/// Create a new builder, inheriting the given builder's context and debug
58+
/// scope.
5559
SILGenBuilder(SILGenBuilder &builder, SILBasicBlock *insertBB)
5660
: SILBuilder(insertBB, builder.getCurrentDebugScope(),
5761
builder.getBuilderContext()),
@@ -64,6 +68,8 @@ class SILGenBuilder : public SILBuilder {
6468
SILGenModule &getSILGenModule() const;
6569
SILGenFunction &getSILGenFunction() const { return SGF; }
6670

71+
SILDebugLocation getSILDebugLocation(SILLocation Loc) override;
72+
6773
using SILBuilder::createInitExistentialValue;
6874
ManagedValue
6975
createInitExistentialValue(SILLocation loc, SILType existentialType,

0 commit comments

Comments
 (0)