Skip to content

Commit 4b42944

Browse files
authored
[profcheck] Don't verify generated global ctors/dtors (#170597)
Functions listed in `llvm.global_ctors` or `llvm.global_dtors` aren't too interesting for performance. Passes like LowerTypeTests synthesize some of these, and, while we could add a "0" entry count explicitly, we can also just not bother verifying in the first place.
1 parent 1295e87 commit 4b42944

File tree

8 files changed

+64
-13
lines changed

8 files changed

+64
-13
lines changed

llvm/include/llvm/Transforms/Utils/ProfileVerify.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_TRANSFORMS_UTILS_PROFILEVERIFY_H
1414
#define LLVM_TRANSFORMS_UTILS_PROFILEVERIFY_H
1515

16+
#include "llvm/ADT/DenseSet.h"
1617
#include "llvm/IR/Analysis.h"
1718
#include "llvm/IR/PassManager.h"
1819
#include "llvm/Support/Compiler.h"
@@ -29,8 +30,15 @@ class ProfileInjectorPass : public PassInfoMixin<ProfileInjectorPass> {
2930
/// in conjunction with the ProfileInjectorPass. MD_prof "unknown" is considered
3031
/// valid (i.e. !{!"unknown"})
3132
class ProfileVerifierPass : public PassInfoMixin<ProfileVerifierPass> {
33+
DenseSet<const Function *> IgnoreList;
34+
// This pass is mostly a function pass but we want to initialize the
35+
// IngoreList once, which is why we present it as a module-level pass. We make
36+
// the function-level run private to avoid accidentally hooking up the pass as
37+
// a function pass.
38+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
39+
3240
public:
33-
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
41+
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
3442
};
3543

3644
} // namespace llvm

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ MODULE_PASS("print<ir2vec>", IR2VecPrinterPass(errs()))
149149
MODULE_PASS("print<ir2vec-vocab>", IR2VecVocabPrinterPass(errs()))
150150
MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(errs()))
151151
MODULE_PASS("print<reg-usage>", PhysicalRegisterUsageInfoPrinterPass(errs()))
152+
MODULE_PASS("prof-verify", ProfileVerifierPass())
152153
MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))
153154
MODULE_PASS("pseudo-probe-update", PseudoProbeUpdatePass())
154155
MODULE_PASS("recompute-globalsaa", RecomputeGlobalsAAPass())
@@ -526,7 +527,6 @@ FUNCTION_PASS("print<scev-division>", SCEVDivisionPrinterPass(errs()))
526527
FUNCTION_PASS("print<stack-safety-local>", StackSafetyPrinterPass(errs()))
527528
FUNCTION_PASS("print<uniformity>", UniformityInfoPrinterPass(errs()))
528529
FUNCTION_PASS("prof-inject", ProfileInjectorPass())
529-
FUNCTION_PASS("prof-verify", ProfileVerifierPass())
530530
FUNCTION_PASS("reassociate", ReassociatePass())
531531
FUNCTION_PASS("redundant-dbg-inst-elim", RedundantDbgInstEliminationPass())
532532
FUNCTION_PASS("replace-with-veclib", ReplaceWithVeclib())

llvm/lib/Transforms/Utils/ProfileVerify.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111
#include "llvm/ADT/STLExtras.h"
1212
#include "llvm/Analysis/BranchProbabilityInfo.h"
1313
#include "llvm/IR/Analysis.h"
14+
#include "llvm/IR/Constants.h"
1415
#include "llvm/IR/Dominators.h"
1516
#include "llvm/IR/Function.h"
17+
#include "llvm/IR/GlobalValue.h"
18+
#include "llvm/IR/GlobalVariable.h"
1619
#include "llvm/IR/Instructions.h"
1720
#include "llvm/IR/LLVMContext.h"
1821
#include "llvm/IR/MDBuilder.h"
22+
#include "llvm/IR/Module.h"
23+
#include "llvm/IR/PassManager.h"
1924
#include "llvm/IR/ProfDataUtils.h"
2025
#include "llvm/Support/BranchProbability.h"
26+
#include "llvm/Support/Casting.h"
2127
#include "llvm/Support/CommandLine.h"
2228

2329
using namespace llvm;
@@ -189,11 +195,43 @@ PreservedAnalyses ProfileInjectorPass::run(Function &F,
189195
return PreservedAnalyses::none();
190196
}
191197

198+
PreservedAnalyses ProfileVerifierPass::run(Module &M,
199+
ModuleAnalysisManager &MAM) {
200+
auto PopulateIgnoreList = [&](StringRef GVName) {
201+
if (const auto *CT = M.getGlobalVariable(GVName))
202+
if (const auto *CA =
203+
dyn_cast_if_present<ConstantArray>(CT->getInitializer()))
204+
for (const auto &Elt : CA->operands())
205+
if (const auto *CS = dyn_cast<ConstantStruct>(Elt))
206+
if (CS->getNumOperands() >= 2 && CS->getOperand(1))
207+
if (const auto *F = dyn_cast<Function>(
208+
CS->getOperand(1)->stripPointerCasts()))
209+
IgnoreList.insert(F);
210+
};
211+
PopulateIgnoreList("llvm.global_ctors");
212+
PopulateIgnoreList("llvm.global_dtors");
213+
214+
// expose the function-level run as public through a wrapper, so we can use
215+
// pass manager mechanisms dealing with declarations and with composing the
216+
// returned PreservedAnalyses values.
217+
struct Wrapper : PassInfoMixin<Wrapper> {
218+
ProfileVerifierPass &PVP;
219+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
220+
return PVP.run(F, FAM);
221+
}
222+
explicit Wrapper(ProfileVerifierPass &PVP) : PVP(PVP) {}
223+
};
224+
225+
return createModuleToFunctionPassAdaptor(Wrapper(*this)).run(M, MAM);
226+
}
227+
192228
PreservedAnalyses ProfileVerifierPass::run(Function &F,
193229
FunctionAnalysisManager &FAM) {
194230
// skip purely asm functions
195231
if (isAsmOnly(F))
196232
return PreservedAnalyses::all();
233+
if (IgnoreList.contains(&F))
234+
return PreservedAnalyses::all();
197235

198236
const auto EntryCount = F.getEntryCount(/*AllowSynthetic=*/true);
199237
if (!EntryCount) {

llvm/test/Transforms/PGOProfile/prof-verify-known-cold.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; Test prof-verify for functions explicitly marked as cold
22

3-
; RUN: opt -passes=prof-inject,prof-verify %s -o - 2>&1 | FileCheck %s
3+
; RUN: opt -passes=function(prof-inject),module(prof-verify) %s -o - 2>&1 | FileCheck %s
44

55
define void @foo(i32 %i) !prof !0 {
66
%c = icmp eq i32 %i, 0

llvm/test/Transforms/PGOProfile/prof-verify.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
; RUN: opt -passes=prof-inject %s -S -o - | FileCheck %s --check-prefix=INJECT
88
; RUN: not opt -passes=prof-verify %s -S -o - 2>&1 | FileCheck %s --check-prefix=VERIFY
9-
; RUN: opt -passes=prof-inject,prof-verify %s --disable-output
9+
; RUN: opt -passes=function(prof-inject),module(prof-verify) %s --disable-output
1010
; RUN: opt -enable-profcheck %s -S -o - | FileCheck %s --check-prefix=INJECT
1111

1212
define void @foo(i32 %i) !prof !0 {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: opt -passes=prof-verify %s -o - 2>&1 | FileCheck %s
2+
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @ctor, ptr null }]
3+
@llvm.global_dtors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @dtor, ptr null }]
4+
5+
define internal void @ctor() {
6+
ret void
7+
}
8+
9+
define internal void @dtor() {
10+
ret void
11+
}
12+
13+
; CHECK-NOT: Profile verification failed: function entry count missing

llvm/tools/opt/NewPMDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ bool llvm::runPassPipeline(
520520
false, "", nullptr, DebugifyMode::OriginalDebugInfo,
521521
&DebugInfoBeforePass, VerifyDIPreserveExport));
522522
if (EnableProfcheck)
523-
MPM.addPass(createModuleToFunctionPassAdaptor(ProfileVerifierPass()));
523+
MPM.addPass(ProfileVerifierPass());
524524

525525
// Add any relevant output pass at the end of the pipeline.
526526
switch (OK) {

llvm/utils/profcheck-xfail.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ CodeGen/X86/masked_gather_scatter.ll
2020
DebugInfo/AArch64/ir-outliner.ll
2121
DebugInfo/assignment-tracking/X86/hotcoldsplit.ll
2222
DebugInfo/Generic/block-asan.ll
23-
DebugInfo/X86/asan_debug_info.ll
2423
LTO/X86/diagnostic-handler-remarks-with-hotness.ll
2524
Other/optimization-remarks-auto.ll
2625
Transforms/AtomicExpand/ARM/atomic-expansion-v7.ll
@@ -475,10 +474,6 @@ Transforms/LowerConstantIntrinsics/objectsize_basic.ll
475474
Transforms/LowerGlobalDestructors/lower-global-dtors-existing-dos_handle.ll
476475
Transforms/LowerGlobalDestructors/lower-global-dtors.ll
477476
Transforms/LowerGlobalDestructors/non-literal-type.ll
478-
Transforms/LowerIFunc/ifunc-alias.ll
479-
Transforms/LowerIFunc/ifunc-nonsense-resolvers.ll
480-
Transforms/LowerIFunc/ifunc-program-addrspace.ll
481-
Transforms/LowerIFunc/lower-ifunc.ll
482477
Transforms/LowerMatrixIntrinsics/data-layout-multiply-fused.ll
483478
Transforms/LowerMatrixIntrinsics/multiply-fused-dominance.ll
484479
Transforms/LowerMatrixIntrinsics/multiply-fused.ll
@@ -498,9 +493,6 @@ Transforms/LowerSwitch/do-not-handle-impossible-values.ll
498493
Transforms/LowerSwitch/feature.ll
499494
Transforms/LowerSwitch/fold-popular-case-to-unreachable-default.ll
500495
Transforms/LowerSwitch/pr59316.ll
501-
Transforms/LowerTypeTests/cfi-coff-comdat-rename.ll
502-
Transforms/LowerTypeTests/function.ll
503-
Transforms/LowerTypeTests/function-weak.ll
504496
Transforms/LowerTypeTests/import.ll
505497
Transforms/LowerTypeTests/simple.ll
506498
Transforms/MergeFunc/2011-02-08-RemoveEqual.ll

0 commit comments

Comments
 (0)