Skip to content

Commit c5a6fde

Browse files
committed
Support for macro scopes
1 parent f68889e commit c5a6fde

File tree

17 files changed

+218
-951
lines changed

17 files changed

+218
-951
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,31 @@ class SILBuilder {
182182
/// location.
183183
///
184184
/// SILBuilderContext must outlive this SILBuilder instance.
185-
SILBuilder(SILInstruction *I, const SILDebugScope *DS, SILBuilderContext &C)
185+
SILBuilder(SILInstruction *I, SILBuilderContext &C,
186+
const SILDebugScope *DS = nullptr)
186187
: TempContext(C.getModule()), C(C), F(I->getFunction()) {
187-
assert(DS && "instruction has no debug scope");
188-
setCurrentDebugScope(DS);
189188
setInsertionPoint(I);
189+
if (DS)
190+
setCurrentDebugScope(DS);
190191
}
191192

192-
SILBuilder(SILBasicBlock *BB, const SILDebugScope *DS, SILBuilder &B)
193-
: SILBuilder(BB, DS, B.getBuilderContext()) {}
193+
SILBuilder(SILBasicBlock *BB, SILBuilder &B,
194+
const SILDebugScope *DS = nullptr)
195+
: SILBuilder(BB, B.getBuilderContext(), DS) {}
194196

195197
/// Build instructions before the given insertion point, inheriting the debug
196198
/// location.
197199
///
198200
/// SILBuilderContext must outlive this SILBuilder instance.
199-
SILBuilder(SILBasicBlock *BB, const SILDebugScope *DS, SILBuilderContext &C)
201+
SILBuilder(SILBasicBlock *BB, SILBuilderContext &C,
202+
const SILDebugScope *DS = nullptr)
200203
: TempContext(C.getModule()), C(C), F(BB->getParent()) {
201204
assert(DS && "block has no debug scope");
202-
setCurrentDebugScope(DS);
205+
if (DS)
206+
setCurrentDebugScope(DS);
203207
setInsertionPoint(BB);
204208
}
209+
205210
virtual ~SILBuilder() {}
206211

207212
// Allow a pass to override the current SIL module conventions. This should
@@ -415,7 +420,7 @@ class SILBuilder {
415420
SILBasicBlock *createFallthroughBlock(SILLocation loc,
416421
SILBasicBlock *targetBB) {
417422
auto *newBB = F->createBasicBlock();
418-
SILBuilder(newBB, this->getCurrentDebugScope(), this->getBuilderContext())
423+
SILBuilder(newBB, this->getBuilderContext(), this->getCurrentDebugScope())
419424
.createBranch(loc, targetBB);
420425
return newBB;
421426
}
@@ -2978,7 +2983,21 @@ class SILBuilder {
29782983
class SILBuilderWithScope : public SILBuilder {
29792984
void inheritScopeFrom(SILInstruction *I) {
29802985
assert(I->getDebugScope() && "instruction has no debug scope");
2981-
setCurrentDebugScope(I->getDebugScope());
2986+
SILBasicBlock::iterator II(*I);
2987+
auto End = I->getParent()->end();
2988+
const SILDebugScope *DS = II->getDebugScope();
2989+
assert(DS);
2990+
// Skip over meta instructions, since debug_values may originate from outer
2991+
// scopes. Don't do any of this after inlining.
2992+
while (!DS->InlinedCallSite && II != End && II->isMetaInstruction())
2993+
++II;
2994+
if (II != End) {
2995+
auto nextScope = II->getDebugScope();
2996+
if (!nextScope->InlinedCallSite)
2997+
DS = nextScope;
2998+
}
2999+
assert(DS);
3000+
setCurrentDebugScope(DS);
29823001
}
29833002

29843003
public:
@@ -2987,29 +3006,33 @@ class SILBuilderWithScope : public SILBuilder {
29873006
///
29883007
/// Clients should prefer this constructor.
29893008
SILBuilderWithScope(SILInstruction *I, SILBuilderContext &C)
2990-
: SILBuilder(I, I->getDebugScope(), C)
2991-
{}
3009+
: SILBuilder(I, C) {
3010+
inheritScopeFrom(I);
3011+
}
29923012

29933013
/// Build instructions before the given insertion point, inheriting the debug
29943014
/// location and using the context from the passed in builder.
29953015
///
29963016
/// Clients should prefer this constructor.
29973017
SILBuilderWithScope(SILInstruction *I, SILBuilder &B)
2998-
: SILBuilder(I, I->getDebugScope(), B.getBuilderContext()) {}
3018+
: SILBuilder(I, B.getBuilderContext()) {
3019+
inheritScopeFrom(I);
3020+
}
29993021

30003022
explicit SILBuilderWithScope(
30013023
SILInstruction *I,
30023024
SmallVectorImpl<SILInstruction *> *InsertedInstrs = nullptr)
30033025
: SILBuilder(I, InsertedInstrs) {
3004-
assert(I->getDebugScope() && "instruction has no debug scope");
3005-
setCurrentDebugScope(I->getDebugScope());
3026+
inheritScopeFrom(I);
30063027
}
30073028

30083029
explicit SILBuilderWithScope(SILBasicBlock::iterator I)
30093030
: SILBuilderWithScope(&*I) {}
30103031

30113032
explicit SILBuilderWithScope(SILBasicBlock::iterator I, SILBuilder &B)
3012-
: SILBuilder(&*I, &*I->getDebugScope(), B.getBuilderContext()) {}
3033+
: SILBuilder(&*I, B.getBuilderContext()) {
3034+
inheritScopeFrom(&*I);
3035+
}
30133036

30143037
explicit SILBuilderWithScope(SILInstruction *I,
30153038
SILInstruction *InheritScopeFrom)
@@ -3031,12 +3054,13 @@ class SILBuilderWithScope : public SILBuilder {
30313054

30323055
explicit SILBuilderWithScope(SILBasicBlock *BB, SILBuilder &B,
30333056
SILInstruction *InheritScopeFrom)
3034-
: SILBuilder(BB, InheritScopeFrom->getDebugScope(),
3035-
B.getBuilderContext()) {}
3057+
: SILBuilder(BB, B.getBuilderContext()) {
3058+
inheritScopeFrom(InheritScopeFrom);
3059+
}
30363060

30373061
explicit SILBuilderWithScope(SILBasicBlock *BB, SILBuilderContext &C,
30383062
const SILDebugScope *debugScope)
3039-
: SILBuilder(BB, debugScope, C) {}
3063+
: SILBuilder(BB, C, debugScope) {}
30403064

30413065
/// Creates a new SILBuilder with an insertion point at the
30423066
/// beginning of BB and the debug scope from the first

include/swift/SIL/SILLocation.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class SILLocation {
213213
llvm_unreachable("location type has no AST node");
214214
}
215215
llvm_unreachable("covered switch");
216-
}
216+
}
217217

218218
/// Returns true if the location has a separate AST node for debugging.
219219
/// See ExtendedASTNodeLoc.
@@ -343,6 +343,11 @@ class SILLocation {
343343
/// Marks the location as coming from auto-generated body.
344344
void markAutoGenerated() { kindAndFlags.fields.autoGenerated = true; }
345345

346+
/// Marks the location as not bein auto-generated.
347+
/// FIXME: This functionality is only used to work around bugs and should be
348+
/// removed.
349+
void markNonAutoGenerated() { kindAndFlags.fields.autoGenerated = false; }
350+
346351
/// Returns this location with the auto-generated flag set.
347352
SILLocation asAutoGenerated() const {
348353
SILLocation loc = *this;
@@ -477,6 +482,7 @@ class SILLocation {
477482
}
478483

479484
friend llvm::hash_code hash_value(const SILLocation &);
485+
friend class RegularLocation;
480486
};
481487

482488
inline llvm::hash_code hash_value(const SILLocation &R) {
@@ -495,6 +501,9 @@ class RegularLocation : public SILLocation {
495501
RegularLocation(Expr *E) : SILLocation(ASTNodeTy(E), RegularKind) {}
496502
RegularLocation(Decl *D) : SILLocation(ASTNodeTy(D), RegularKind) {}
497503
RegularLocation(Pattern *P) : SILLocation(ASTNodeTy(P), RegularKind) {}
504+
/// Used for bindings that nedd to be injected into a different scope than
505+
/// that of their VarDecls.
506+
RegularLocation(Decl *D, SILLocation LocForDebugging, SILModule &Module);
498507
RegularLocation(Stmt *S, Pattern *P, SILModule &Module);
499508
RegularLocation(SILLocation ForDebuggingOrDiagnosticsOnly, SILModule &Module,
500509
bool isForDebugOnly = true);

lib/SIL/IR/SILBuilder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ SILBuilder::SILBuilder(SILGlobalVariable *GlobVar,
3030
setInsertionPoint(&GlobVar->StaticInitializerBlock);
3131
}
3232

33-
void SILBuilder::setCurrentDebugScope(const SILDebugScope *DS) {
33+
void SILBuilder::setCurrentDebugScope(const SILDebugScope *NewDebugScope) {
3434
#ifndef NDEBUG
35-
if (EnableDIHoleVerification && DS != CurDebugScope) {
35+
if (EnableDIHoleVerification && NewDebugScope != CurDebugScope) {
3636
// Detect a situation where:
3737
// PrevDebugScope = sil_scope(parent: CurDebugScope)
3838
// CurDebugScope = sil_scope(parent:)
39-
// DS = PrevDebugScope
40-
if (DS && DS == PrevDebugScope) {
39+
// NewDebugScope = PrevDebugScope
40+
if (NewDebugScope && NewDebugScope == PrevDebugScope) {
4141
if (CurDebugScope->isAncestor(PrevDebugScope)) {
4242
SILPrintDebugInfo.setValue(true);
4343
BB->dump();
@@ -48,7 +48,7 @@ void SILBuilder::setCurrentDebugScope(const SILDebugScope *DS) {
4848
PrevDebugScope = CurDebugScope;
4949
}
5050
#endif
51-
CurDebugScope = DS;
51+
CurDebugScope = NewDebugScope;
5252
}
5353

5454
IntegerLiteralInst *SILBuilder::createIntegerLiteral(IntegerLiteralExpr *E) {

lib/SIL/IR/SILLocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ void SILLocation::print(raw_ostream &OS) const {
268268
}
269269
}
270270

271+
RegularLocation::RegularLocation(Decl *D, SILLocation LocForDebugging,
272+
SILModule &Module)
273+
: SILLocation(new(Module) ExtendedASTNodeLoc(
274+
{D, 0}, LocForDebugging.getPrimaryASTNode()),
275+
RegularKind) {}
271276
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module)
272277
: SILLocation(new(Module) ExtendedASTNodeLoc({S, 0}, {P, 0}), RegularKind) {}
273278

lib/SILGen/SILGenDecl.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,19 +832,19 @@ class LetValueInitialization : public Initialization {
832832
// an argument, for example.
833833
if (value->getType().isAddress())
834834
address = value;
835-
SILLocation PrologueLoc(loc);
836835

837-
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
838-
value = getValueForLexicalLifetimeBinding(SGF, PrologueLoc, value,
839-
wasPlusOne);
840-
}
836+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule()))
837+
value = getValueForLexicalLifetimeBinding(SGF, loc, value, wasPlusOne);
841838

842839
SGF.VarLocs[vd] = SILGenFunction::VarLoc::get(value);
843840

844841
// Emit a debug_value[_addr] instruction to record the start of this value's
845842
// lifetime, if permitted to do so.
846843
if (!EmitDebugValueOnInit)
847844
return;
845+
846+
// Use the scope from loc and diagnostic location from vd.
847+
RegularLocation PrologueLoc(vd, loc, SGF.getModule());
848848
PrologueLoc.markAsPrologue();
849849
SILDebugVariable DbgVar(vd->getName().str(), vd->isLet(), /*ArgNo=*/0);
850850
SGF.B.emitDebugDescription(PrologueLoc, value, DbgVar);
@@ -1603,9 +1603,6 @@ void SILGenFunction::visitVarDecl(VarDecl *D) {
16031603
}
16041604

16051605
void SILGenFunction::visitMacroExpansionDecl(MacroExpansionDecl *D) {
1606-
MacroScope scope(*this, CleanupLocation(D), D,
1607-
D->getMacroName().getBaseIdentifier().str(),
1608-
D->getMacroRef().getDecl());
16091606
D->forEachExpandedExprOrStmt([&](ASTNode node) {
16101607
if (auto *expr = node.dyn_cast<Expr *>())
16111608
emitIgnoredExpr(expr);

lib/SILGen/SILGenEpilog.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,13 @@ emitEpilog(SILLocation TopLevel, bool UsesCustomEpilog) {
317317
SILValue returnValue = *maybeReturnValue;
318318

319319
// Return () if no return value was given.
320-
if (!returnValue) {
321-
RegularLocation loc(returnLoc);
322-
loc.pointToEnd();
323-
returnValue = emitEmptyTuple(loc);
324-
}
320+
if (!returnValue)
321+
returnValue = emitEmptyTuple(CleanupLocation(TopLevel));
322+
//{
323+
// RegularLocation loc(returnLoc);
324+
// loc.pointToEnd();
325+
// returnValue = emitEmptyTuple(loc);
326+
//}
325327

326328
B.createReturn(returnLoc, returnValue);
327329
}

lib/SILGen/SILGenExpr.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6173,11 +6173,6 @@ RValue RValueEmitter::visitMoveExpr(MoveExpr *E, SGFContext C) {
61736173
RValue RValueEmitter::visitMacroExpansionExpr(MacroExpansionExpr *E,
61746174
SGFContext C) {
61756175
if (auto *rewritten = E->getRewritten()) {
6176-
Mangle::ASTMangler mangler;
6177-
auto name =
6178-
SGF.getASTContext().getIdentifier(mangler.mangleMacroExpansion(E));
6179-
MacroScope scope(SGF, CleanupLocation(rewritten), E, name.str(),
6180-
E->getMacroRef().getDecl());
61816176
return visit(rewritten, C);
61826177
} else {
61836178
assert(E->getSubstituteDecl());

0 commit comments

Comments
 (0)