@@ -55,10 +55,52 @@ SILGenFunction::SILGenFunction(SILGenModule &SGM, SILFunction &F,
5555 assert (DC && " creating SGF without a DeclContext?" );
5656 B.setInsertionPoint (createBasicBlock ());
5757 B.setCurrentDebugScope (F.getDebugScope ());
58+
59+ // Populate VarDeclScopeMap.
5860 SourceLoc SLoc = F.getLocation ().getSourceLoc ();
5961 if (SF && SLoc) {
6062 FnASTScope = ast_scope::ASTScopeImpl::findStartingScopeForLookup (SF, SLoc);
6163 ScopeMap.insert ({{FnASTScope, nullptr }, F.getDebugScope ()});
64+
65+ // Collect all variable declarations in this scope.
66+ struct Consumer : public namelookup ::AbstractASTScopeDeclConsumer {
67+ const ast_scope::ASTScopeImpl *ASTScope;
68+ VarDeclScopeMapTy &VarDeclScopeMap;
69+ Consumer (const ast_scope::ASTScopeImpl *ASTScope,
70+ VarDeclScopeMapTy &VarDeclScopeMap)
71+ : ASTScope(ASTScope), VarDeclScopeMap(VarDeclScopeMap) {}
72+
73+ bool consume (ArrayRef<ValueDecl *> values,
74+ NullablePtr<DeclContext> baseDC) override {
75+ LLVM_DEBUG (ASTScope->print (llvm::errs (), 0 , false , false ));
76+ for (auto &value : values) {
77+ LLVM_DEBUG ({
78+ if (value->hasName ())
79+ llvm::dbgs () << " + " << value->getBaseIdentifier () << " \n " ;
80+ });
81+
82+ // FIXME: ASTs coming out of the autodiff transformation trigger this.
83+ // assert((VarDeclScopeMap.count(value) == 0 ||
84+ // VarDeclScopeMap[value] == ASTScope) &&
85+ // "VarDecl appears twice");
86+ VarDeclScopeMap.insert ({value, ASTScope});
87+ }
88+ return false ;
89+ }
90+ bool lookInMembers (const DeclContext *) const override { return false ; }
91+ #ifndef NDEBUG
92+ void startingNextLookupStep () override {}
93+ void finishingLookup (std::string) const override {}
94+ bool isTargetLookup () const override { return false ; }
95+ #endif
96+ };
97+ const_cast <ast_scope::ASTScopeImpl *>(FnASTScope)
98+ ->preOrderChildrenDo ([&](ast_scope::ASTScopeImpl *ASTScope) {
99+ if (!ASTScope->ignoreInDebugInfo ()) {
100+ Consumer consumer (ASTScope, VarDeclScopeMap);
101+ ASTScope->lookupLocalsOrMembers (consumer);
102+ }
103+ });
62104 }
63105}
64106
@@ -202,8 +244,6 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
202244 SourceLoc SLoc = Loc.getSourceLoc ();
203245 if (!SF || LastSourceLoc == SLoc)
204246 return nullptr ;
205- // Prime VarDeclScopeMap.
206- auto Scope = getOrCreateScope (SLoc);
207247 if (ForMetaInstruction)
208248 if (ValueDecl *ValDecl = Loc.getAsASTNode <ValueDecl>()) {
209249 // The source location of a VarDecl isn't necessarily in the same scope
@@ -212,7 +252,7 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
212252 if (ValueScope != VarDeclScopeMap.end ())
213253 return getOrCreateScope (ValueScope->second , F.getDebugScope ());
214254 }
215- return Scope ;
255+ return getOrCreateScope (SLoc) ;
216256}
217257
218258const SILDebugScope *SILGenFunction::getOrCreateScope (SourceLoc SLoc) {
@@ -406,32 +446,6 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
406446 return ParentScope->InlinedCallSite != InlinedAt ? FnScope : ParentScope;
407447 }
408448
409- // Collect all variable declarations in this scope.
410- struct Consumer : public namelookup ::AbstractASTScopeDeclConsumer {
411- const ast_scope::ASTScopeImpl *ASTScope;
412- VarDeclScopeMapTy &VarDeclScopeMap;
413- Consumer (const ast_scope::ASTScopeImpl *ASTScope,
414- VarDeclScopeMapTy &VarDeclScopeMap)
415- : ASTScope(ASTScope), VarDeclScopeMap(VarDeclScopeMap) {}
416-
417- bool consume (ArrayRef<ValueDecl *> values,
418- NullablePtr<DeclContext> baseDC) override {
419- for (auto &value : values) {
420- assert (VarDeclScopeMap.count (value) == 0 && " VarDecl appears twice" );
421- VarDeclScopeMap.insert ({value, ASTScope});
422- }
423- return false ;
424- }
425- bool lookInMembers (const DeclContext *) const override { return false ; }
426- #ifndef NDEBUG
427- void startingNextLookupStep () override {}
428- void finishingLookup (std::string) const override {}
429- bool isTargetLookup () const override { return false ; }
430- #endif
431- };
432- Consumer consumer (ASTScope, VarDeclScopeMap);
433- ASTScope->lookupLocalsOrMembers (consumer);
434-
435449 // Collapse BraceStmtScopes whose parent is a .*BodyScope.
436450 if (auto Parent = ASTScope->getParent ().getPtrOrNull ())
437451 if (Parent->getSourceRangeOfThisASTNode () ==
0 commit comments