[clang] [PGO] Add ability to mark cold functions as optsize/minsize/optnone (PR #69030)

David Li via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 21:19:51 PDT 2023


================
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/Instrumentation/MarkColdFunctions.h"
+#include "llvm/Analysis/BlockFrequencyInfo.h"
+#include "llvm/Analysis/ProfileSummaryInfo.h"
+#include "llvm/IR/PassManager.h"
+
+using namespace llvm;
+
+PreservedAnalyses MarkColdFunctionsPass::run(Module &M,
+                                             ModuleAnalysisManager &AM) {
+  if (ColdType == PGOOptions::ColdFuncAttr::None)
+    return PreservedAnalyses::all();
+  ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
+  if (!PSI.hasProfileSummary())
+    return PreservedAnalyses::all();
+  FunctionAnalysisManager &FAM =
+      AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+  bool MadeChange = false;
+  for (Function &F : M) {
+    if (F.isDeclaration())
+      continue;
+    BlockFrequencyInfo &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
+    if (!PSI.isFunctionColdInCallGraph(&F, BFI))
----------------
david-xl wrote:

The same optimization strategy should also be applied to function already marked with cold attribute (either by user or previous passes).

https://github.com/llvm/llvm-project/pull/69030


More information about the cfe-commits mailing list