Merge pull request #65047 from adrian-prantl/107764966

Ignore profile counter instructions in the dihole verifier.
This commit is contained in:
swift-ci
2023-04-12 21:36:02 -07:00
committed by GitHub
7 changed files with 109 additions and 46 deletions

View File

@@ -176,7 +176,7 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
const SILDebugScope *Scope = B.getCurrentDebugScope();
if (!Scope)
Scope = F.getDebugScope();
if (auto *SILScope = getScopeOrNull(Loc)) {
if (auto *SILScope = getScopeOrNull(Loc, ForMetaInstruction)) {
Scope = SILScope;
// Metainstructions such as a debug_value may break the flow of scopes and
// should not change the state of the builder.
@@ -187,14 +187,16 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
return SILDebugLocation(overriddenLoc, Scope);
}
const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc) {
if (Loc.getKind() == SILLocation::CleanupKind ||
Loc.getKind() == SILLocation::ImplicitReturnKind ||
// The source locations produced by the ResultBuilder transformation are
// all over the place.
Loc.isImplicit() ||
Loc.isAutoGenerated())
return nullptr;
const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
bool ForMetaInstruction) {
if (!ForMetaInstruction) {
if (Loc.getKind() == SILLocation::CleanupKind ||
Loc.getKind() == SILLocation::ImplicitReturnKind ||
// The source locations produced by the ResultBuilder transformation are
// all over the place.
Loc.isImplicit() || Loc.isAutoGenerated())
return nullptr;
}
SourceLoc SLoc = Loc.getSourceLoc();
if (!SF || LastSourceLoc == SLoc)
@@ -218,35 +220,44 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
return Scope;
}
static std::pair<SILLocation, std::string>
getMacroName(GeneratedSourceInfo &Info) {
namespace {
struct MacroInfo {
MacroInfo(SourceLoc SLoc) : Loc(SLoc) {}
RegularLocation Loc;
std::string Name;
bool Freestanding = false;
};
}
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
SourceLoc MacroSLoc = Info.generatedSourceRange.getStart();
RegularLocation Loc(MacroSLoc);
Mangle::ASTMangler mangler;
std::string Name = "__unknown_macro__";
MacroInfo Result(MacroSLoc);
Result.Name = "__unknown_macro__";
if (!Info.astNode)
return {Loc, Name};
return Result;
// Keep this in sync with ASTMangler::appendMacroExpansionContext().
Mangle::ASTMangler mangler;
switch (Info.kind) {
case GeneratedSourceInfo::ExpressionMacroExpansion: {
auto parent = ASTNode::getFromOpaqueValue(Info.astNode);
if (auto expr =
cast_or_null<MacroExpansionExpr>(parent.dyn_cast<Expr *>())) {
Loc = RegularLocation(expr);
Name = mangler.mangleMacroExpansion(expr);
Result.Loc = RegularLocation(expr);
Result.Name = mangler.mangleMacroExpansion(expr);
} else {
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
Loc = RegularLocation(decl);
Name = mangler.mangleMacroExpansion(decl);
Result.Loc = RegularLocation(decl);
Result.Name = mangler.mangleMacroExpansion(decl);
}
break;
}
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
auto expansion = cast<MacroExpansionDecl>(
ASTNode::getFromOpaqueValue(Info.astNode).get<Decl *>());
Loc = RegularLocation(expansion);
Name = mangler.mangleMacroExpansion(expansion);
Result.Loc = RegularLocation(expansion);
Result.Name = mangler.mangleMacroExpansion(expansion);
Result.Freestanding = true;
break;
}
case GeneratedSourceInfo::AccessorMacroExpansion:
@@ -257,8 +268,9 @@ getMacroName(GeneratedSourceInfo &Info) {
auto decl = ASTNode::getFromOpaqueValue(Info.astNode).get<Decl *>();
auto attr = Info.attachedMacroCustomAttr;
if (auto *macroDecl = decl->getResolvedMacro(attr)) {
Loc = RegularLocation(macroDecl);
Name = macroDecl->getBaseName().userFacingName();
Result.Loc = RegularLocation(macroDecl);
Result.Name = macroDecl->getBaseName().userFacingName();
Result.Freestanding = true;
}
break;
}
@@ -266,7 +278,7 @@ getMacroName(GeneratedSourceInfo &Info) {
case GeneratedSourceInfo::ReplacedFunctionBody:
break;
}
return {Loc, Name};
return Result;
}
const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
@@ -276,6 +288,15 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
if (!GeneratedSourceInfo)
return nullptr;
// There is no good way to represent freestanding macros as inlined functions,
// because entire function would need to be "inlined" into a top-level
// declaration that isn't part of a real function. By not handling them here,
// source locations will still point into the macro expansion buffer, but
// debug info doesn't know what macro that buffer was expanded from.
auto Macro = getMacroInfo(*GeneratedSourceInfo);
if (Macro.Freestanding)
return nullptr;
SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange.getStart();
if (!OrigSLoc)
return nullptr;
@@ -296,22 +317,24 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
/*yields*/
{},
/*Results*/ {}, None, SubstitutionMap(), SubstitutionMap(), ASTContext);
auto LocName = getMacroName(*GeneratedSourceInfo);
RegularLocation MacroLoc(LocName.first);
StringRef MacroName = ASTContext.getIdentifier(LocName.second).str();
StringRef MacroName = ASTContext.getIdentifier(Macro.Name).str();
SILFunction *MacroFn = B.getOrCreateFunction(
MacroLoc, MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
Macro.Loc, MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
IsNotDistributed, IsNotRuntimeAccessible);
// At the end of the chain OrigSLoc should be a macro expansion node.
const SILDebugScope *InlinedAt = getOrCreateScope(OrigSLoc);
const SILDebugScope *InlinedAt = nullptr;
const SILDebugScope *OrigScope = getOrCreateScope(OrigSLoc);
RegularLocation OrigLoc(OrigSLoc);
// Inject an extra scope to hold the inlined call site.
if (InlinedAt)
InlinedAt =
new (SGM.M) SILDebugScope(RegularLocation(OrigSLoc), nullptr, InlinedAt,
InlinedAt->InlinedCallSite);
if (OrigScope)
InlinedAt = new (SGM.M)
SILDebugScope(Macro.Freestanding ? Macro.Loc : OrigLoc, nullptr,
OrigScope, OrigScope->InlinedCallSite);
const SILDebugScope *Scope =
new (SGM.M) SILDebugScope(MacroLoc, MacroFn, nullptr, InlinedAt);
new (SGM.M) SILDebugScope(Macro.Loc, MacroFn, nullptr, InlinedAt);
InlinedScopeMap.insert({BufferID, Scope});
return Scope;