Skip to content

Commit 7032225

Browse files
authored
Merge pull request #65047 from adrian-prantl/107764966
Ignore profile counter instructions in the dihole verifier.
2 parents a636f76 + 8d6c55e commit 7032225

File tree

7 files changed

+109
-46
lines changed

7 files changed

+109
-46
lines changed

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6265,6 +6265,12 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
62656265
for (SILInstruction &SI : *BB) {
62666266
if (SI.isMetaInstruction())
62676267
continue;
6268+
// FIXME: Profile counters for loop bodies may be emitted before the
6269+
// instructions for the loop variable, but in a deeper scope.
6270+
if (isa<IncrementProfilerCounterInst>(SI))
6271+
continue;
6272+
if (!SI.getLoc().hasValidLineNumber())
6273+
continue;
62686274
if (SI.getLoc().getKind() == SILLocation::CleanupKind)
62696275
continue;
62706276
// FIXME: These still leave holes in the scopes. We should make them

lib/SILGen/SILGenFunction.cpp

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
176176
const SILDebugScope *Scope = B.getCurrentDebugScope();
177177
if (!Scope)
178178
Scope = F.getDebugScope();
179-
if (auto *SILScope = getScopeOrNull(Loc)) {
179+
if (auto *SILScope = getScopeOrNull(Loc, ForMetaInstruction)) {
180180
Scope = SILScope;
181181
// Metainstructions such as a debug_value may break the flow of scopes and
182182
// should not change the state of the builder.
@@ -187,14 +187,16 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
187187
return SILDebugLocation(overriddenLoc, Scope);
188188
}
189189

190-
const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc) {
191-
if (Loc.getKind() == SILLocation::CleanupKind ||
192-
Loc.getKind() == SILLocation::ImplicitReturnKind ||
193-
// The source locations produced by the ResultBuilder transformation are
194-
// all over the place.
195-
Loc.isImplicit() ||
196-
Loc.isAutoGenerated())
197-
return nullptr;
190+
const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
191+
bool ForMetaInstruction) {
192+
if (!ForMetaInstruction) {
193+
if (Loc.getKind() == SILLocation::CleanupKind ||
194+
Loc.getKind() == SILLocation::ImplicitReturnKind ||
195+
// The source locations produced by the ResultBuilder transformation are
196+
// all over the place.
197+
Loc.isImplicit() || Loc.isAutoGenerated())
198+
return nullptr;
199+
}
198200

199201
SourceLoc SLoc = Loc.getSourceLoc();
200202
if (!SF || LastSourceLoc == SLoc)
@@ -218,35 +220,44 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
218220
return Scope;
219221
}
220222

221-
static std::pair<SILLocation, std::string>
222-
getMacroName(GeneratedSourceInfo &Info) {
223+
namespace {
224+
struct MacroInfo {
225+
MacroInfo(SourceLoc SLoc) : Loc(SLoc) {}
226+
RegularLocation Loc;
227+
std::string Name;
228+
bool Freestanding = false;
229+
};
230+
}
231+
232+
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
223233
SourceLoc MacroSLoc = Info.generatedSourceRange.getStart();
224-
RegularLocation Loc(MacroSLoc);
225-
Mangle::ASTMangler mangler;
226-
std::string Name = "__unknown_macro__";
234+
MacroInfo Result(MacroSLoc);
235+
Result.Name = "__unknown_macro__";
227236
if (!Info.astNode)
228-
return {Loc, Name};
237+
return Result;
229238

230239
// Keep this in sync with ASTMangler::appendMacroExpansionContext().
240+
Mangle::ASTMangler mangler;
231241
switch (Info.kind) {
232242
case GeneratedSourceInfo::ExpressionMacroExpansion: {
233243
auto parent = ASTNode::getFromOpaqueValue(Info.astNode);
234244
if (auto expr =
235245
cast_or_null<MacroExpansionExpr>(parent.dyn_cast<Expr *>())) {
236-
Loc = RegularLocation(expr);
237-
Name = mangler.mangleMacroExpansion(expr);
246+
Result.Loc = RegularLocation(expr);
247+
Result.Name = mangler.mangleMacroExpansion(expr);
238248
} else {
239249
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
240-
Loc = RegularLocation(decl);
241-
Name = mangler.mangleMacroExpansion(decl);
250+
Result.Loc = RegularLocation(decl);
251+
Result.Name = mangler.mangleMacroExpansion(decl);
242252
}
243253
break;
244254
}
245255
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
246256
auto expansion = cast<MacroExpansionDecl>(
247257
ASTNode::getFromOpaqueValue(Info.astNode).get<Decl *>());
248-
Loc = RegularLocation(expansion);
249-
Name = mangler.mangleMacroExpansion(expansion);
258+
Result.Loc = RegularLocation(expansion);
259+
Result.Name = mangler.mangleMacroExpansion(expansion);
260+
Result.Freestanding = true;
250261
break;
251262
}
252263
case GeneratedSourceInfo::AccessorMacroExpansion:
@@ -257,16 +268,17 @@ getMacroName(GeneratedSourceInfo &Info) {
257268
auto decl = ASTNode::getFromOpaqueValue(Info.astNode).get<Decl *>();
258269
auto attr = Info.attachedMacroCustomAttr;
259270
if (auto *macroDecl = decl->getResolvedMacro(attr)) {
260-
Loc = RegularLocation(macroDecl);
261-
Name = macroDecl->getBaseName().userFacingName();
271+
Result.Loc = RegularLocation(macroDecl);
272+
Result.Name = macroDecl->getBaseName().userFacingName();
273+
Result.Freestanding = true;
262274
}
263275
break;
264276
}
265277
case GeneratedSourceInfo::PrettyPrinted:
266278
case GeneratedSourceInfo::ReplacedFunctionBody:
267279
break;
268280
}
269-
return {Loc, Name};
281+
return Result;
270282
}
271283

272284
const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
@@ -276,6 +288,15 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
276288
if (!GeneratedSourceInfo)
277289
return nullptr;
278290

291+
// There is no good way to represent freestanding macros as inlined functions,
292+
// because entire function would need to be "inlined" into a top-level
293+
// declaration that isn't part of a real function. By not handling them here,
294+
// source locations will still point into the macro expansion buffer, but
295+
// debug info doesn't know what macro that buffer was expanded from.
296+
auto Macro = getMacroInfo(*GeneratedSourceInfo);
297+
if (Macro.Freestanding)
298+
return nullptr;
299+
279300
SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange.getStart();
280301
if (!OrigSLoc)
281302
return nullptr;
@@ -296,22 +317,24 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
296317
/*yields*/
297318
{},
298319
/*Results*/ {}, None, SubstitutionMap(), SubstitutionMap(), ASTContext);
299-
auto LocName = getMacroName(*GeneratedSourceInfo);
300-
RegularLocation MacroLoc(LocName.first);
301-
StringRef MacroName = ASTContext.getIdentifier(LocName.second).str();
320+
StringRef MacroName = ASTContext.getIdentifier(Macro.Name).str();
321+
302322
SILFunction *MacroFn = B.getOrCreateFunction(
303-
MacroLoc, MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
323+
Macro.Loc, MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
304324
IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
305325
IsNotDistributed, IsNotRuntimeAccessible);
306326
// At the end of the chain OrigSLoc should be a macro expansion node.
307-
const SILDebugScope *InlinedAt = getOrCreateScope(OrigSLoc);
327+
const SILDebugScope *InlinedAt = nullptr;
328+
const SILDebugScope *OrigScope = getOrCreateScope(OrigSLoc);
329+
RegularLocation OrigLoc(OrigSLoc);
308330
// Inject an extra scope to hold the inlined call site.
309-
if (InlinedAt)
310-
InlinedAt =
311-
new (SGM.M) SILDebugScope(RegularLocation(OrigSLoc), nullptr, InlinedAt,
312-
InlinedAt->InlinedCallSite);
331+
if (OrigScope)
332+
InlinedAt = new (SGM.M)
333+
SILDebugScope(Macro.Freestanding ? Macro.Loc : OrigLoc, nullptr,
334+
OrigScope, OrigScope->InlinedCallSite);
335+
313336
const SILDebugScope *Scope =
314-
new (SGM.M) SILDebugScope(MacroLoc, MacroFn, nullptr, InlinedAt);
337+
new (SGM.M) SILDebugScope(Macro.Loc, MacroFn, nullptr, InlinedAt);
315338

316339
InlinedScopeMap.insert({BufferID, Scope});
317340
return Scope;

lib/SILGen/SILGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
713713
Optional<SILLocation> CurDebugLocOverride,
714714
bool ForMetaInstruction);
715715

716-
const SILDebugScope *getScopeOrNull(SILLocation Loc);
716+
const SILDebugScope *getScopeOrNull(SILLocation Loc,
717+
bool ForMetaInstruction = false);
717718

718719
private:
719720
const SILDebugScope *getOrCreateScope(SourceLoc SLoc);

test/DebugInfo/case-scope.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: %target-swift-frontend -module-name a -parse-as-library -emit-sil -g %s | %FileCheck %s
22

3-
43
public enum E<T> {
54
case A(T)
65
case B(T)
@@ -13,15 +12,15 @@ func sink<T>(_ t: T) {}
1312
public func f<T>(_ e: E<T>) -> [T] {
1413
switch e {
1514
case .A(let a), .B(let a): return [a]
16-
case .D(let a, _, let c): return [a, c]
17-
default:
18-
return []
15+
case .D(let a, _, let c): return [a, c]
16+
default: return []
1917
}
2018
}
21-
// CHECK: sil_scope [[F:[0-9]+]] { loc "{{.*}}":13:13 parent @$s1a1fySayxGAA1EOyxGlF
22-
// CHECK: sil_scope [[S0:[0-9]+]] { loc "{{.*}}":14:3 parent [[F]] }
23-
// CHECK: sil_scope [[A0:[0-9]+]] { loc "{{.*}}":15:3 parent [[S0]] }
24-
// CHECK: sil_scope [[A1:[0-9]+]] { loc "{{.*}}":16:3 parent [[S0]] }
25-
// CHECK: alloc_stack {{.*}} $T, let, name "a", {{.*}}:15:15, scope [[A0]]
26-
// CHECK: alloc_stack {{.*}} $T, let, name "a", {{.*}}:15:26, scope [[A0]]
27-
// CHECK: alloc_stack {{.*}} $T, let, name "a", {{.*}}:16:15, scope [[A1]]
19+
20+
// CHECK: sil_scope [[F:[0-9]+]] { loc "{{.*}}":12:13 parent @$s1a1fySayxGAA1EOyxGlF
21+
// CHECK: sil_scope [[S0:[0-9]+]] { loc "{{.*}}":13:3 parent [[F]] }
22+
// CHECK: sil_scope [[A0:[0-9]+]] { loc "{{.*}}":14:3 parent [[S0]] }
23+
// CHECK: sil_scope [[A1:[0-9]+]] { loc "{{.*}}":15:3 parent [[S0]] }
24+
// CHECK: alloc_stack {{.*}} $T, let, name "a", {{.*}}:14:15, scope [[A0]]
25+
// CHECK: alloc_stack {{.*}} $T, let, name "a", {{.*}}:14:26, scope [[A0]]
26+
// CHECK: alloc_stack {{.*}} $T, let, name "a", {{.*}}:15:15, scope [[A1]]

test/DebugInfo/case-scope2.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -module-name a -parse-as-library -emit-sil -g %s | %FileCheck %s
2+
func consume<T>(_ t: T) {}
3+
4+
// CHECK: sil_scope [[F:[0-9]+]] { loc "{{.*}}":9:13 parent @$s1a1fyyShySiGSg_ADtF
5+
// CHECK: sil_scope [[S0:[0-9]+]] { loc "{{.*}}":10:3 parent [[F]] }
6+
// CHECK: sil_scope [[S1:[0-9]+]] { loc "{{.*}}":11:3 parent [[S0]] }
7+
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":13:5 parent [[S1]] }
8+
// CHECK: sil_scope [[S3:[0-9]+]] { loc "{{.*}}":14:3 parent [[S0]] }
9+
public func f(_ s1: Set<Int>?, _ s2: Set<Int>?) {
10+
switch (s1, s2) {
11+
case (nil, let a), (let a, nil):
12+
// CHECK: debug_value {{.*}} $Optional<Set<Int>>, let, name "a", {{.*}}:[[@LINE-1]]:18, scope [[S1]]
13+
consume(a)
14+
case (let a?, _):
15+
// CHECK: debug_value {{.*}} $Set<Int>, let, name "a", {{.*}}:[[@LINE-1]]:13, scope [[S3]]
16+
consume((a))
17+
}
18+
}

test/DebugInfo/profile_counter.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend %s -g -emit-sil -profile-generate -profile-coverage-mapping -parse-as-library -o - | %FileCheck %s
2+
3+
func consume<T>(_ t: T) {}
4+
5+
public func f<T>(collection : [T]) {
6+
for element in collection {
7+
// CHECK: increment_profiler_counter {{.*}}:[[@LINE-1]]:29, scope [[SCOPE:[0-9]+]]
8+
// FIXME: Ideally, these would share the same scope, or the increment should come below the variable initialization code.
9+
// CHECK: unchecked_take_enum_data_addr {{.*}}:[[@LINE-3]]:3, scope
10+
// CHECK: copy_addr {{.*}}:[[@LINE-4]]:29, scope [[SCOPE]]
11+
consume(element)
12+
}
13+
}

test/Macros/macro_expand.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ func testNestedDeclInExpr() {
253253
@freestanding(declaration, names: named(A), named(B), named(foo), named(addOne))
254254
macro defineDeclsWithKnownNames() = #externalMacro(module: "MacroDefinition", type: "DefineDeclsWithKnownNamesMacro")
255255

256+
// Freestanding macros are not in inlined scopes.
257+
// CHECK-SIL: sil_scope {{.*}} { loc "@__swiftmacro_9MacroUser016testFreestandingA9ExpansionyyF4Foo2L_V25defineDeclsWithKnownNamesfMf0_.swift":9:14 {{.*}} -> Int }
258+
256259
// FIXME: Macros producing arbitrary names are not supported yet
257260
#if false
258261
#bitwidthNumberedStructs("MyIntGlobal")

0 commit comments

Comments
 (0)