## Purpose This patch is one in a series of code-mods that annotate LLVM’s public interface for export. This patch annotates the `llvm/Transforms` library. These annotations currently have no meaningful impact on the LLVM build; however, they are a prerequisite to support an LLVM Windows DLL (shared library) build. ## Background This effort is tracked in #109483. Additional context is provided in [this discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307), and documentation for `LLVM_ABI` and related annotations is found in the LLVM repo [here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst). The bulk of these changes were generated automatically using the [Interface Definition Scanner (IDS)](https://github.com/compnerd/ids) tool, followed formatting with `git clang-format`. The following manual adjustments were also applied after running IDS on Linux: - Removed a redundant `operator<<` from Attributor.h. IDS only auto-annotates the 1st declaration, and the 2nd declaration being un-annotated resulted in an "inconsistent linkage" error on Windows when building LLVM as a DLL. - `#include` the `VirtualFileSystem.h` in PGOInstrumentation.h and remove the local declaration of the `vfs::FileSystem` class. This is required because exporting the `PGOInstrumentationUse` constructor requires the class be fully defined because it is used by an argument. - Add #include "llvm/Support/Compiler.h" to files where it was not auto-added by IDS due to no pre-existing block of include statements. - Add `LLVM_TEMPLATE_ABI` and `LLVM_EXPORT_TEMPLATE` to exported instantiated templates. ## Validation Local builds and tests to validate cross-platform compatibility. This included llvm, clang, and lldb on the following configurations: - Windows with MSVC - Windows with Clang - Linux with GCC - Linux with Clang - Darwin with Clang
109 lines
4.2 KiB
C++
109 lines
4.2 KiB
C++
//===- llvm/Transforms/Utils/SizeOpts.h - size optimization -----*- C++ -*-===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains some shared code size optimization related code.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
|
|
#define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
|
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
namespace llvm {
|
|
LLVM_ABI extern cl::opt<bool> EnablePGSO;
|
|
LLVM_ABI extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
|
|
LLVM_ABI extern cl::opt<bool> PGSOColdCodeOnly;
|
|
LLVM_ABI extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
|
|
LLVM_ABI extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
|
|
LLVM_ABI extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
|
|
LLVM_ABI extern cl::opt<bool> ForcePGSO;
|
|
LLVM_ABI extern cl::opt<int> PgsoCutoffInstrProf;
|
|
LLVM_ABI extern cl::opt<int> PgsoCutoffSampleProf;
|
|
|
|
class BasicBlock;
|
|
class BlockFrequencyInfo;
|
|
class Function;
|
|
|
|
enum class PGSOQueryType {
|
|
IRPass, // A query call from an IR-level transform pass.
|
|
Test, // A query call from a unit test.
|
|
Other, // Others.
|
|
};
|
|
|
|
static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
|
|
return PGSOColdCodeOnly ||
|
|
(PSI->hasInstrumentationProfile() && PGSOColdCodeOnlyForInstrPGO) ||
|
|
(PSI->hasSampleProfile() &&
|
|
((!PSI->hasPartialSampleProfile() && PGSOColdCodeOnlyForSamplePGO) ||
|
|
(PSI->hasPartialSampleProfile() &&
|
|
PGSOColdCodeOnlyForPartialSamplePGO))) ||
|
|
(PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
|
|
}
|
|
|
|
template <typename FuncT, typename BFIT>
|
|
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
|
|
BFIT *BFI, PGSOQueryType QueryType) {
|
|
assert(F);
|
|
if (!PSI || !BFI || !PSI->hasProfileSummary())
|
|
return false;
|
|
if (ForcePGSO)
|
|
return true;
|
|
if (!EnablePGSO)
|
|
return false;
|
|
if (isPGSOColdCodeOnly(PSI))
|
|
return PSI->isFunctionColdInCallGraph(F, *BFI);
|
|
if (PSI->hasSampleProfile())
|
|
// The "isCold" check seems to work better for Sample PGO as it could have
|
|
// many profile-unannotated functions.
|
|
return PSI->isFunctionColdInCallGraphNthPercentile(PgsoCutoffSampleProf, F,
|
|
*BFI);
|
|
return !PSI->isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf, F,
|
|
*BFI);
|
|
}
|
|
|
|
template <typename BlockTOrBlockFreq, typename BFIT>
|
|
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
|
|
ProfileSummaryInfo *PSI, BFIT *BFI,
|
|
PGSOQueryType QueryType) {
|
|
if (!PSI || !BFI || !PSI->hasProfileSummary())
|
|
return false;
|
|
if (ForcePGSO)
|
|
return true;
|
|
if (!EnablePGSO)
|
|
return false;
|
|
if (isPGSOColdCodeOnly(PSI))
|
|
return PSI->isColdBlock(BBOrBlockFreq, BFI);
|
|
if (PSI->hasSampleProfile())
|
|
// The "isCold" check seems to work better for Sample PGO as it could have
|
|
// many profile-unannotated functions.
|
|
return PSI->isColdBlockNthPercentile(PgsoCutoffSampleProf, BBOrBlockFreq,
|
|
BFI);
|
|
return !PSI->isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, BFI);
|
|
}
|
|
|
|
/// Returns true if function \p F is suggested to be size-optimized based on the
|
|
/// profile.
|
|
LLVM_ABI bool
|
|
shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
|
|
BlockFrequencyInfo *BFI,
|
|
PGSOQueryType QueryType = PGSOQueryType::Other);
|
|
|
|
/// Returns true if basic block \p BB is suggested to be size-optimized based on
|
|
/// the profile.
|
|
LLVM_ABI bool
|
|
shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
|
|
BlockFrequencyInfo *BFI,
|
|
PGSOQueryType QueryType = PGSOQueryType::Other);
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
|