[llvm] [PGO] Add ProfileInjector and ProfileVerifier passes (PR #147388)
Snehasish Kumar via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 22 10:28:31 PDT 2025
================
@@ -0,0 +1,116 @@
+//===- ProfileVerify.cpp - Verify profile info for testing ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Utils/ProfileVerify.h"
+#include "llvm/ADT/DynamicAPInt.h"
+#include "llvm/ADT/PostOrderIterator.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Analysis/BranchProbabilityInfo.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/IR/Analysis.h"
+#include "llvm/IR/Dominators.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/ProfDataUtils.h"
+#include "llvm/Support/BranchProbability.h"
+
+using namespace llvm;
+namespace {
+class ProfileInjector {
+ Function &F;
+ FunctionAnalysisManager &FAM;
+
+public:
+ static const Instruction *
+ getTerminatorBenefitingFromMDProf(const BasicBlock &BB) {
+ if (succ_size(&BB) < 2)
+ return nullptr;
+ auto *Term = BB.getTerminator();
+ return (isa<BranchInst>(Term) || isa<SwitchInst>(Term) ||
----------------
snehasish wrote:
I added this legality check in IR/MetaData.cpp some time ago: https://github.com/llvm/llvm-project/blob/b3e720b4deb481df11cb6be09e5a2ad7a4d4a7eb/llvm/lib/IR/Metadata.cpp#L1227-L1241
Can we refactor it out into a helper exposed by MetaData.h and reuse it here? Also struck me that the check there for `Instruction::Br` should include IsConditional.
https://github.com/llvm/llvm-project/pull/147388
More information about the llvm-commits
mailing list