13
13
#ifndef SWIFT_SIL_SILBUILDER_H
14
14
#define SWIFT_SIL_SILBUILDER_H
15
15
16
+ #include " SILDebugVariable.h"
17
+ #include " SILInstruction.h"
16
18
#include " swift/Basic/ProfileCounter.h"
17
19
#include " swift/SIL/SILArgument.h"
18
20
#include " swift/SIL/SILDebugScope.h"
22
24
#include " swift/SIL/SILUndef.h"
23
25
#include " llvm/ADT/PointerUnion.h"
24
26
#include " llvm/ADT/StringExtras.h"
27
+ #include < type_traits>
25
28
26
29
namespace swift {
27
30
@@ -179,27 +182,33 @@ class SILBuilder {
179
182
// / location.
180
183
// /
181
184
// / SILBuilderContext must outlive this SILBuilder instance.
182
- SILBuilder (SILInstruction *I, const SILDebugScope *DS, SILBuilderContext &C)
185
+ SILBuilder (SILInstruction *I, SILBuilderContext &C,
186
+ const SILDebugScope *DS = nullptr )
183
187
: TempContext(C.getModule()), C(C), F(I->getFunction ()) {
184
- assert (DS && " instruction has no debug scope" );
185
- setCurrentDebugScope (DS);
186
188
setInsertionPoint (I);
189
+ if (DS)
190
+ setCurrentDebugScope (DS);
187
191
}
188
192
189
- SILBuilder (SILBasicBlock *BB, const SILDebugScope *DS, SILBuilder &B)
190
- : SILBuilder(BB, DS, B.getBuilderContext()) {}
193
+ SILBuilder (SILBasicBlock *BB, SILBuilder &B,
194
+ const SILDebugScope *DS = nullptr )
195
+ : SILBuilder(BB, B.getBuilderContext(), DS) {}
191
196
192
197
// / Build instructions before the given insertion point, inheriting the debug
193
198
// / location.
194
199
// /
195
200
// / SILBuilderContext must outlive this SILBuilder instance.
196
- SILBuilder (SILBasicBlock *BB, const SILDebugScope *DS, SILBuilderContext &C)
201
+ SILBuilder (SILBasicBlock *BB, SILBuilderContext &C,
202
+ const SILDebugScope *DS = nullptr )
197
203
: TempContext(C.getModule()), C(C), F(BB->getParent ()) {
198
204
assert (DS && " block has no debug scope" );
199
- setCurrentDebugScope (DS);
200
205
setInsertionPoint (BB);
206
+ if (DS)
207
+ setCurrentDebugScope (DS);
201
208
}
202
209
210
+ virtual ~SILBuilder () {}
211
+
203
212
// Allow a pass to override the current SIL module conventions. This should
204
213
// only be done by a pass responsible for lowering SIL to a new stage
205
214
// (e.g. AddressLowering).
@@ -249,7 +258,8 @@ class SILBuilder {
249
258
}
250
259
251
260
// / Convenience function for building a SILDebugLocation.
252
- SILDebugLocation getSILDebugLocation (SILLocation Loc) {
261
+ virtual SILDebugLocation
262
+ getSILDebugLocation (SILLocation Loc, bool ForMetaInstruction = false ) {
253
263
// FIXME: Audit all uses and enable this assertion.
254
264
// assert(getCurrentDebugScope() && "no debug scope");
255
265
auto Scope = getCurrentDebugScope ();
@@ -259,6 +269,17 @@ class SILBuilder {
259
269
return SILDebugLocation (overriddenLoc, Scope);
260
270
}
261
271
272
+ // / When the frontend generates synthesized conformances it generates a
273
+ // / fully-typechecked AST without source locations. This means that the
274
+ // / ASTScope-based mechanism to generate SILDebugScopes doesn't work, which
275
+ // / means we can't disambiguate local variables in different lexical
276
+ // / scopes. To avoid a verification error later in the pipeline, drop all
277
+ // / variables without a proper source location.
278
+ bool shouldDropVariable (SILDebugVariable Var, SILLocation Loc) {
279
+ return !Var.ArgNo && Loc.isSynthesizedAST ();
280
+ }
281
+
282
+
262
283
// / If we have a SILFunction, return SILFunction::hasOwnership(). If we have a
263
284
// / SILGlobalVariable, just return false.
264
285
bool hasOwnership () const {
@@ -310,26 +331,38 @@ class SILBuilder {
310
331
// / setInsertionPoint - Set the insertion point to insert before the specified
311
332
// / instruction.
312
333
void setInsertionPoint (SILInstruction *I) {
334
+ #ifndef NDEBUG
335
+ PrevDebugScope = nullptr ;
336
+ #endif
313
337
assert (I && " can't set insertion point to a null instruction" );
314
338
setInsertionPoint (I->getParent (), I->getIterator ());
315
339
}
316
340
317
341
// / setInsertionPoint - Set the insertion point to insert before the specified
318
342
// / instruction.
319
343
void setInsertionPoint (SILBasicBlock::iterator IIIter) {
344
+ #ifndef NDEBUG
345
+ PrevDebugScope = nullptr ;
346
+ #endif
320
347
setInsertionPoint (IIIter->getParent (), IIIter);
321
348
}
322
349
323
350
// / setInsertionPoint - Set the insertion point to insert at the end of the
324
351
// / specified block.
325
352
void setInsertionPoint (SILBasicBlock *BB) {
353
+ #ifndef NDEBUG
354
+ PrevDebugScope = nullptr ;
355
+ #endif
326
356
assert (BB && " can't set insertion point to a null basic block" );
327
357
setInsertionPoint (BB, BB->end ());
328
358
}
329
359
330
360
// / setInsertionPoint - Set the insertion point to insert at the end of the
331
361
// / specified block.
332
362
void setInsertionPoint (SILFunction::iterator BBIter) {
363
+ #ifndef NDEBUG
364
+ PrevDebugScope = nullptr ;
365
+ #endif
333
366
setInsertionPoint (&*BBIter);
334
367
}
335
368
@@ -388,7 +421,7 @@ class SILBuilder {
388
421
SILBasicBlock *createFallthroughBlock (SILLocation loc,
389
422
SILBasicBlock *targetBB) {
390
423
auto *newBB = F->createBasicBlock ();
391
- SILBuilder (newBB, this ->getCurrentDebugScope (), this ->getBuilderContext ())
424
+ SILBuilder (newBB, this ->getBuilderContext (), this ->getCurrentDebugScope ())
392
425
.createBranch (loc, targetBB);
393
426
return newBB;
394
427
}
@@ -401,6 +434,8 @@ class SILBuilder {
401
434
Optional<SILDebugVariable>
402
435
substituteAnonymousArgs (llvm::SmallString<4 > Name,
403
436
Optional<SILDebugVariable> Var, SILLocation Loc) {
437
+ if (Var && shouldDropVariable (*Var, Loc))
438
+ return {};
404
439
if (!Var || !Var->ArgNo || !Var->Name .empty ())
405
440
return Var;
406
441
@@ -416,14 +451,21 @@ class SILBuilder {
416
451
AllocStackInst *createAllocStack (SILLocation Loc, SILType elementType,
417
452
Optional<SILDebugVariable> Var = None,
418
453
bool hasDynamicLifetime = false ,
419
- bool isLexical = false ,
420
- bool wasMoved = false ) {
454
+ bool isLexical = false , bool wasMoved = false
455
+ #ifndef NDEBUG
456
+ ,
457
+ bool skipVarDeclAssert = false
458
+ #endif
459
+ ) {
421
460
llvm::SmallString<4 > Name;
422
461
Loc.markAsPrologue ();
423
- assert ((!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode <Decl>()) || Var) &&
424
- " location is a VarDecl, but SILDebugVariable is empty" );
462
+ #ifndef NDEBUG
463
+ if (dyn_cast_or_null<VarDecl>(Loc.getAsASTNode <Decl>()))
464
+ assert ((skipVarDeclAssert || Loc.isSynthesizedAST () || Var) &&
465
+ " location is a VarDecl, but SILDebugVariable is empty" );
466
+ #endif
425
467
return insert (AllocStackInst::create (
426
- getSILDebugLocation (Loc), elementType, getFunction (),
468
+ getSILDebugLocation (Loc, true ), elementType, getFunction (),
427
469
substituteAnonymousArgs (Name, Var, Loc), hasDynamicLifetime, isLexical,
428
470
wasMoved));
429
471
}
@@ -474,15 +516,20 @@ class SILBuilder {
474
516
Optional<SILDebugVariable> Var = None,
475
517
bool hasDynamicLifetime = false ,
476
518
bool reflection = false ,
477
- bool usesMoveableValueDebugInfo = false ) {
519
+ bool usesMoveableValueDebugInfo = false
520
+ #ifndef NDEBUG
521
+ , bool skipVarDeclAssert = false
522
+ #endif
523
+ ) {
478
524
llvm::SmallString<4 > Name;
479
525
Loc.markAsPrologue ();
480
- assert ((!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode <Decl>()) || Var) &&
526
+ assert ((skipVarDeclAssert ||
527
+ !dyn_cast_or_null<VarDecl>(Loc.getAsASTNode <Decl>()) || Var) &&
481
528
" location is a VarDecl, but SILDebugVariable is empty" );
482
- return insert (AllocBoxInst::create (getSILDebugLocation (Loc), BoxType, *F,
483
- substituteAnonymousArgs (Name, Var, Loc) ,
484
- hasDynamicLifetime, reflection,
485
- usesMoveableValueDebugInfo));
529
+ return insert (AllocBoxInst::create (
530
+ getSILDebugLocation (Loc, true ), BoxType, *F ,
531
+ substituteAnonymousArgs (Name, Var, Loc), hasDynamicLifetime, reflection,
532
+ usesMoveableValueDebugInfo));
486
533
}
487
534
488
535
AllocExistentialBoxInst *
@@ -2937,7 +2984,21 @@ class SILBuilder {
2937
2984
class SILBuilderWithScope : public SILBuilder {
2938
2985
void inheritScopeFrom (SILInstruction *I) {
2939
2986
assert (I->getDebugScope () && " instruction has no debug scope" );
2940
- setCurrentDebugScope (I->getDebugScope ());
2987
+ SILBasicBlock::iterator II (*I);
2988
+ auto End = I->getParent ()->end ();
2989
+ const SILDebugScope *DS = II->getDebugScope ();
2990
+ assert (DS);
2991
+ // Skip over meta instructions, since debug_values may originate from outer
2992
+ // scopes. Don't do any of this after inlining.
2993
+ while (!DS->InlinedCallSite && II != End && II->isMetaInstruction ())
2994
+ ++II;
2995
+ if (II != End) {
2996
+ auto nextScope = II->getDebugScope ();
2997
+ if (!nextScope->InlinedCallSite )
2998
+ DS = nextScope;
2999
+ }
3000
+ assert (DS);
3001
+ setCurrentDebugScope (DS);
2941
3002
}
2942
3003
2943
3004
public:
@@ -2946,29 +3007,33 @@ class SILBuilderWithScope : public SILBuilder {
2946
3007
// /
2947
3008
// / Clients should prefer this constructor.
2948
3009
SILBuilderWithScope (SILInstruction *I, SILBuilderContext &C)
2949
- : SILBuilder(I, I->getDebugScope (), C)
2950
- {}
3010
+ : SILBuilder(I, C) {
3011
+ inheritScopeFrom (I);
3012
+ }
2951
3013
2952
3014
// / Build instructions before the given insertion point, inheriting the debug
2953
3015
// / location and using the context from the passed in builder.
2954
3016
// /
2955
3017
// / Clients should prefer this constructor.
2956
3018
SILBuilderWithScope (SILInstruction *I, SILBuilder &B)
2957
- : SILBuilder(I, I->getDebugScope (), B.getBuilderContext()) {}
3019
+ : SILBuilder(I, B.getBuilderContext()) {
3020
+ inheritScopeFrom (I);
3021
+ }
2958
3022
2959
3023
explicit SILBuilderWithScope (
2960
3024
SILInstruction *I,
2961
3025
SmallVectorImpl<SILInstruction *> *InsertedInstrs = nullptr )
2962
3026
: SILBuilder(I, InsertedInstrs) {
2963
- assert (I->getDebugScope () && " instruction has no debug scope" );
2964
- setCurrentDebugScope (I->getDebugScope ());
3027
+ inheritScopeFrom (I);
2965
3028
}
2966
3029
2967
3030
explicit SILBuilderWithScope (SILBasicBlock::iterator I)
2968
3031
: SILBuilderWithScope(&*I) {}
2969
3032
2970
3033
explicit SILBuilderWithScope (SILBasicBlock::iterator I, SILBuilder &B)
2971
- : SILBuilder(&*I, &*I->getDebugScope (), B.getBuilderContext()) {}
3034
+ : SILBuilder(&*I, B.getBuilderContext()) {
3035
+ inheritScopeFrom (&*I);
3036
+ }
2972
3037
2973
3038
explicit SILBuilderWithScope (SILInstruction *I,
2974
3039
SILInstruction *InheritScopeFrom)
@@ -2990,12 +3055,13 @@ class SILBuilderWithScope : public SILBuilder {
2990
3055
2991
3056
explicit SILBuilderWithScope (SILBasicBlock *BB, SILBuilder &B,
2992
3057
SILInstruction *InheritScopeFrom)
2993
- : SILBuilder(BB, InheritScopeFrom->getDebugScope (),
2994
- B.getBuilderContext()) {}
3058
+ : SILBuilder(BB, B.getBuilderContext()) {
3059
+ inheritScopeFrom (InheritScopeFrom);
3060
+ }
2995
3061
2996
3062
explicit SILBuilderWithScope (SILBasicBlock *BB, SILBuilderContext &C,
2997
3063
const SILDebugScope *debugScope)
2998
- : SILBuilder(BB, debugScope, C ) {}
3064
+ : SILBuilder(BB, C, debugScope ) {}
2999
3065
3000
3066
// / Creates a new SILBuilder with an insertion point at the
3001
3067
// / beginning of BB and the debug scope from the first
0 commit comments