diff --git a/bolt/lib/Profile/CMakeLists.txt b/bolt/lib/Profile/CMakeLists.txt --- a/bolt/lib/Profile/CMakeLists.txt +++ b/bolt/lib/Profile/CMakeLists.txt @@ -12,7 +12,6 @@ LINK_COMPONENTS Support - TransformUtils ) target_link_libraries(LLVMBOLTProfile diff --git a/bolt/lib/Profile/StaleProfileMatching.cpp b/bolt/lib/Profile/StaleProfileMatching.cpp --- a/bolt/lib/Profile/StaleProfileMatching.cpp +++ b/bolt/lib/Profile/StaleProfileMatching.cpp @@ -29,7 +29,7 @@ #include "bolt/Profile/YAMLProfileReader.h" #include "llvm/ADT/Hashing.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Transforms/Utils/SampleProfileInference.h" +#include "llvm/Transforms/Utils/SampleProfileInferenceBase.h" #include diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h --- a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h @@ -17,6 +17,7 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/Transforms/Utils/SampleProfileInferenceBase.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Instruction.h" @@ -42,101 +43,6 @@ } // end namespace afdo_detail -struct FlowJump; - -/// A wrapper of a binary basic block. -struct FlowBlock { - uint64_t Index; - uint64_t Weight{0}; - bool HasUnknownWeight{true}; - bool IsUnlikely{false}; - uint64_t Flow{0}; - std::vector SuccJumps; - std::vector PredJumps; - - /// Check if it is the entry block in the function. - bool isEntry() const { return PredJumps.empty(); } - - /// Check if it is an exit block in the function. - bool isExit() const { return SuccJumps.empty(); } -}; - -/// A wrapper of a jump between two basic blocks. -struct FlowJump { - uint64_t Source; - uint64_t Target; - uint64_t Weight{0}; - bool HasUnknownWeight{true}; - bool IsUnlikely{false}; - uint64_t Flow{0}; -}; - -/// A wrapper of binary function with basic blocks and jumps. -struct FlowFunction { - /// Basic blocks in the function. - std::vector Blocks; - /// Jumps between the basic blocks. - std::vector Jumps; - /// The index of the entry block. - uint64_t Entry{0}; -}; - -/// Various thresholds and options controlling the behavior of the profile -/// inference algorithm. Default values are tuned for several large-scale -/// applications, and can be modified via corresponding command-line flags. -struct ProfiParams { - /// Evenly distribute flow when there are multiple equally likely options. - bool EvenFlowDistribution{false}; - - /// Evenly re-distribute flow among unknown subgraphs. - bool RebalanceUnknown{false}; - - /// Join isolated components having positive flow. - bool JoinIslands{false}; - - /// The cost of increasing a block's count by one. - unsigned CostBlockInc{0}; - - /// The cost of decreasing a block's count by one. - unsigned CostBlockDec{0}; - - /// The cost of increasing a count of zero-weight block by one. - unsigned CostBlockZeroInc{0}; - - /// The cost of increasing the entry block's count by one. - unsigned CostBlockEntryInc{0}; - - /// The cost of decreasing the entry block's count by one. - unsigned CostBlockEntryDec{0}; - - /// The cost of increasing an unknown block's count by one. - unsigned CostBlockUnknownInc{0}; - - /// The cost of increasing a jump's count by one. - unsigned CostJumpInc{0}; - - /// The cost of increasing a fall-through jump's count by one. - unsigned CostJumpFTInc{0}; - - /// The cost of decreasing a jump's count by one. - unsigned CostJumpDec{0}; - - /// The cost of decreasing a fall-through jump's count by one. - unsigned CostJumpFTDec{0}; - - /// The cost of increasing an unknown jump's count by one. - unsigned CostJumpUnknownInc{0}; - - /// The cost of increasing an unknown fall-through jump's count by one. - unsigned CostJumpUnknownFTInc{0}; - - /// The cost of taking an unlikely block/jump. - const int64_t CostUnlikely = ((int64_t)1) << 30; -}; - -void applyFlowInference(const ProfiParams &Params, FlowFunction &Func); -void applyFlowInference(FlowFunction &Func); - /// Sample profile inference pass. template class SampleProfileInference { public: diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileInferenceBase.h b/llvm/include/llvm/Transforms/Utils/SampleProfileInferenceBase.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileInferenceBase.h @@ -0,0 +1,119 @@ +//===- Transforms/Utils/SampleProfileInferenceTypes.h ----------*- 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 +// +//===----------------------------------------------------------------------===// +// +/// \file +/// This file provides base types and interfaces for the profile inference +/// algorithm, profi. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCEBASE_H +#define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCEBASE_H + +#include +#include + +namespace llvm { + +struct FlowJump; + +/// A wrapper of a binary basic block. +struct FlowBlock { + uint64_t Index; + uint64_t Weight{0}; + bool HasUnknownWeight{true}; + bool IsUnlikely{false}; + uint64_t Flow{0}; + std::vector SuccJumps; + std::vector PredJumps; + + /// Check if it is the entry block in the function. + bool isEntry() const { return PredJumps.empty(); } + + /// Check if it is an exit block in the function. + bool isExit() const { return SuccJumps.empty(); } +}; + +/// A wrapper of a jump between two basic blocks. +struct FlowJump { + uint64_t Source; + uint64_t Target; + uint64_t Weight{0}; + bool HasUnknownWeight{true}; + bool IsUnlikely{false}; + uint64_t Flow{0}; +}; + +/// A wrapper of binary function with basic blocks and jumps. +struct FlowFunction { + /// Basic blocks in the function. + std::vector Blocks; + /// Jumps between the basic blocks. + std::vector Jumps; + /// The index of the entry block. + uint64_t Entry{0}; +}; + +/// Various thresholds and options controlling the behavior of the profile +/// inference algorithm. Default values are tuned for several large-scale +/// applications, and can be modified via corresponding command-line flags. +struct ProfiParams { + /// Evenly distribute flow when there are multiple equally likely options. + bool EvenFlowDistribution{false}; + + /// Evenly re-distribute flow among unknown subgraphs. + bool RebalanceUnknown{false}; + + /// Join isolated components having positive flow. + bool JoinIslands{false}; + + /// The cost of increasing a block's count by one. + unsigned CostBlockInc{0}; + + /// The cost of decreasing a block's count by one. + unsigned CostBlockDec{0}; + + /// The cost of increasing a count of zero-weight block by one. + unsigned CostBlockZeroInc{0}; + + /// The cost of increasing the entry block's count by one. + unsigned CostBlockEntryInc{0}; + + /// The cost of decreasing the entry block's count by one. + unsigned CostBlockEntryDec{0}; + + /// The cost of increasing an unknown block's count by one. + unsigned CostBlockUnknownInc{0}; + + /// The cost of increasing a jump's count by one. + unsigned CostJumpInc{0}; + + /// The cost of increasing a fall-through jump's count by one. + unsigned CostJumpFTInc{0}; + + /// The cost of decreasing a jump's count by one. + unsigned CostJumpDec{0}; + + /// The cost of decreasing a fall-through jump's count by one. + unsigned CostJumpFTDec{0}; + + /// The cost of increasing an unknown jump's count by one. + unsigned CostJumpUnknownInc{0}; + + /// The cost of increasing an unknown fall-through jump's count by one. + unsigned CostJumpUnknownFTInc{0}; + + /// The cost of taking an unlikely block/jump. + const int64_t CostUnlikely = ((int64_t)1) << 30; +}; + +void applyFlowInference(const ProfiParams &Params, FlowFunction &Func); +void applyFlowInference(FlowFunction &Func); + +} // namespace llvm +#endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCEBASE_H