Skip to content

Commit bdfd220

Browse files
committed
[Coverage] Mark symtab entries as guaranteed after intrinsic lowering
SIL optimizations may rewrite profiling intrinsics in a way that IRGen can't lower (r://39146527). Don't claim that a coverage mapping has a guaranteed associated symbol table entry when this happens. I have not added a test, as this is a defensive workaround until we can land add a SIL verifier check that prevents profiling intrinsics from being rewritten. rdar://40133800
1 parent 75450e5 commit bdfd220

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

include/swift/SIL/SILProfiler.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ class SILProfiler : public SILAllocated<SILProfiler> {
9393
return RegionCounterMap;
9494
}
9595

96-
/// Increment the number of counter updates associated with this profiler.
97-
void recordCounterUpdate();
98-
9996
private:
10097
/// Map counters to ASTNodes and set them up for profiling the function.
10198
void assignRegionCounters();

lib/IRGen/GenBuiltin.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
206206
if (IID == llvm::Intrinsic::instrprof_increment) {
207207
// If we import profiling intrinsics from a swift module but profiling is
208208
// not enabled, ignore the increment.
209-
if (!IGF.getSILModule().getOptions().GenerateProfile) {
209+
SILModule &SILMod = IGF.getSILModule();
210+
const auto &Opts = SILMod.getOptions();
211+
if (!Opts.GenerateProfile) {
210212
(void)args.claimAll();
211213
return;
212214
}
@@ -246,6 +248,15 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
246248
replacement.add(NameGEP);
247249
replacement.add(args.claimAll());
248250
args = std::move(replacement);
251+
252+
if (Opts.EmitProfileCoverageMapping) {
253+
// Update the associated coverage mapping: it's now safe to emit, because
254+
// a symtab entry for this function is guaranteed (r://39146527).
255+
auto &coverageMaps = SILMod.getCoverageMaps();
256+
auto CovMapIt = coverageMaps.find(PGOFuncName);
257+
if (CovMapIt != coverageMaps.end())
258+
CovMapIt->second->setSymtabEntryGuaranteed();
259+
}
249260
}
250261

251262
if (IID != llvm::Intrinsic::not_intrinsic) {

lib/IRGen/GenDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,6 @@ void IRGenerator::emitGlobalTopLevel(bool emitForParallelEmission) {
10651065
CurrentIGMPtr IGM = getGenModule(decl ? decl->getDeclContext() : nullptr);
10661066
IGM->emitSILGlobalVariable(&v);
10671067
}
1068-
PrimaryIGM->emitCoverageMapping();
10691068

10701069
// Emit SIL functions.
10711070
for (SILFunction &f : PrimaryIGM->getSILModule()) {
@@ -1099,6 +1098,9 @@ void IRGenerator::emitGlobalTopLevel(bool emitForParallelEmission) {
10991098
IGM->emitSILProperty(&prop);
11001099
}
11011100

1101+
// Emit code coverage mapping data.
1102+
PrimaryIGM->emitCoverageMapping();
1103+
11021104
for (auto Iter : *this) {
11031105
IRGenModule *IGM = Iter.second;
11041106
IGM->finishEmitAfterTopLevel();

lib/SIL/SILProfiler.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,3 @@ Optional<ASTNode> SILProfiler::getPGOParent(ASTNode Node) {
10811081
}
10821082
return it->getSecond();
10831083
}
1084-
1085-
void SILProfiler::recordCounterUpdate() {
1086-
// If a counter update is recorded, the profile symbol table is guaranteed
1087-
// to have name data needed by the coverage mapping.
1088-
if (CovMap)
1089-
CovMap->setSymtabEntryGuaranteed();
1090-
}

lib/SILGen/SILGenFunction.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ void SILGenFunction::emitProfilerIncrement(ASTNode N) {
658658
B.createIntegerLiteral(Loc, Int32Ty, CounterIt->second)};
659659
B.createBuiltin(Loc, C.getIdentifier("int_instrprof_increment"),
660660
SGM.Types.getEmptyTupleType(), {}, Args);
661-
SP->recordCounterUpdate();
662661
}
663662

664663
ProfileCounter SILGenFunction::loadProfilerCount(ASTNode Node) const {

0 commit comments

Comments
 (0)