llvm-project
88 строк · 3.2 Кб
1//==-SummaryBasedOptimizations.cpp - Optimizations based on ThinLTO summary-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements optimizations that are based on the module summaries.
10// These optimizations are performed during the thinlink phase of the
11// compilation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/LTO/SummaryBasedOptimizations.h"16#include "llvm/Analysis/SyntheticCountsUtils.h"17#include "llvm/IR/ModuleSummaryIndex.h"18#include "llvm/Support/CommandLine.h"19
20using namespace llvm;21
22static cl::opt<bool> ThinLTOSynthesizeEntryCounts(23"thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,24cl::desc("Synthesize entry counts based on the summary"));25
26namespace llvm {27extern cl::opt<int> InitialSyntheticCount;28}
29
30static void initializeCounts(ModuleSummaryIndex &Index) {31auto Root = Index.calculateCallGraphRoot();32// Root is a fake node. All its successors are the actual roots of the33// callgraph.34// FIXME: This initializes the entry counts of only the root nodes. This makes35// sense when compiling a binary with ThinLTO, but for libraries any of the36// non-root nodes could be called from outside.37for (auto &C : Root.calls()) {38auto &V = C.first;39for (auto &GVS : V.getSummaryList()) {40auto S = GVS.get()->getBaseObject();41auto *F = cast<FunctionSummary>(S);42F->setEntryCount(InitialSyntheticCount);43}44}45}
46
47void llvm::computeSyntheticCounts(ModuleSummaryIndex &Index) {48if (!ThinLTOSynthesizeEntryCounts)49return;50
51using Scaled64 = ScaledNumber<uint64_t>;52initializeCounts(Index);53auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {54return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);55};56auto GetEntryCount = [](ValueInfo V) {57if (V.getSummaryList().size()) {58auto S = V.getSummaryList().front()->getBaseObject();59auto *F = cast<FunctionSummary>(S);60return F->entryCount();61} else {62return UINT64_C(0);63}64};65auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {66if (!V.getSummaryList().size())67return;68for (auto &GVS : V.getSummaryList()) {69auto S = GVS.get()->getBaseObject();70auto *F = cast<FunctionSummary>(S);71F->setEntryCount(72SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));73}74};75
76auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {77auto RelFreq = GetCallSiteRelFreq(Edge);78Scaled64 EC(GetEntryCount(V), 0);79return RelFreq * EC;80};81// After initializing the counts in initializeCounts above, the counts have to82// be propagated across the combined callgraph.83// SyntheticCountsUtils::propagate takes care of this propagation on any84// callgraph that specialized GraphTraits.85SyntheticCountsUtils<ModuleSummaryIndex *>::propagate(&Index, GetProfileCount,86AddToEntryCount);87Index.setHasSyntheticEntryCounts();88}
89