Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions llvm/include/llvm/CodeGen/BasicBlockMatchingAndInference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef LLVM_CODEGEN_BASIC_BLOCK_AND_INFERENCE_H
#define LLVM_CODEGEN_BASIC_BLOCK_AND_INFERENCE_H

#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/Transforms/Utils/SampleProfileInference.h"

namespace llvm {

class BasicBlockMatchingAndInference : public MachineFunctionPass {
private:
using Edge = std::pair<const MachineBasicBlock *, const MachineBasicBlock *>;
using BlockWeightMap = DenseMap<const MachineBasicBlock *, uint64_t>;
using EdgeWeightMap = DenseMap<Edge, uint64_t>;
using BlockEdgeMap = DenseMap<const MachineBasicBlock *,
SmallVector<const MachineBasicBlock *, 8>>;

struct WeightInfo {
// Weight of basic blocks.
BlockWeightMap BlockWeights;
// Weight of edges.
EdgeWeightMap EdgeWeights;
};

public:
static char ID;
BasicBlockMatchingAndInference();

StringRef getPassName() const override {
return "Basic Block Matching and Inference";
}

void getAnalysisUsage(AnalysisUsage &AU) const override;

bool runOnMachineFunction(MachineFunction &F) override;

std::optional<WeightInfo> getWeightInfo(StringRef FuncName) const;

private:
StringMap<WeightInfo> ProgramWeightInfo;

WeightInfo initWeightInfoByMatching(MachineFunction &MF);

void generateWeightInfoByInference(MachineFunction &MF,
WeightInfo &MatchWeight);
};

} // end namespace llvm

#endif // LLVM_CODEGEN_BASIC_BLOCK_AND_INFERENCE_H
9 changes: 9 additions & 0 deletions llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct FunctionPathAndClusterInfo {
DenseMap<UniqueBBID, uint64_t> NodeCounts;
// Edge counts for each edge, stored as a nested map.
DenseMap<UniqueBBID, DenseMap<UniqueBBID, uint64_t>> EdgeCounts;
// Hash for each basic block.
DenseMap<unsigned, uint64_t> BBHashes;
};

class BasicBlockSectionsProfileReader {
Expand Down Expand Up @@ -86,6 +88,10 @@ class BasicBlockSectionsProfileReader {
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
const UniqueBBID &SinkBBID) const;

// Return the complete function path and cluster info for the given function.
std::pair<bool, FunctionPathAndClusterInfo>
getFunctionPathAndClusterInfo(StringRef FuncName) const;

private:
StringRef getAliasName(StringRef FuncName) const {
auto R = FuncAliasMap.find(FuncName);
Expand Down Expand Up @@ -195,6 +201,9 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
const UniqueBBID &DestBBID) const;

std::pair<bool, FunctionPathAndClusterInfo>
getFunctionPathAndClusterInfo(StringRef FuncName) const;

// Initializes the FunctionNameToDIFilename map for the current module and
// then reads the profile for the matching functions.
bool doInitialization(Module &M) override;
Expand Down
106 changes: 106 additions & 0 deletions llvm/include/llvm/CodeGen/MachineBlockHashInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#ifndef LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
#define LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H

#include "llvm/CodeGen/MachineFunctionPass.h"

namespace llvm {

/// An object wrapping several components of a basic block hash. The combined
/// (blended) hash is represented and stored as one uint64_t, while individual
/// components are of smaller size (e.g., uint16_t or uint8_t).
struct BlendedBlockHash {
private:
static uint64_t combineHashes(uint16_t Hash1, uint16_t Hash2, uint16_t Hash3,
uint16_t Hash4) {
uint64_t Hash = 0;

Hash |= uint64_t(Hash4);
Hash <<= 16;

Hash |= uint64_t(Hash3);
Hash <<= 16;

Hash |= uint64_t(Hash2);
Hash <<= 16;

Hash |= uint64_t(Hash1);

return Hash;
}

static void parseHashes(uint64_t Hash, uint16_t &Hash1, uint16_t &Hash2,
uint16_t &Hash3, uint16_t &Hash4) {
Hash1 = Hash & 0xffff;
Hash >>= 16;

Hash2 = Hash & 0xffff;
Hash >>= 16;

Hash3 = Hash & 0xffff;
Hash >>= 16;

Hash4 = Hash & 0xffff;
Hash >>= 16;
}

public:
explicit BlendedBlockHash() {}

explicit BlendedBlockHash(uint64_t CombinedHash) {
parseHashes(CombinedHash, Offset, OpcodeHash, InstrHash, NeighborHash);
}

/// Combine the blended hash into uint64_t.
uint64_t combine() const {
return combineHashes(Offset, OpcodeHash, InstrHash, NeighborHash);
}

/// Compute a distance between two given blended hashes. The smaller the
/// distance, the more similar two blocks are. For identical basic blocks,
/// the distance is zero.
uint64_t distance(const BlendedBlockHash &BBH) const {
assert(OpcodeHash == BBH.OpcodeHash &&
"incorrect blended hash distance computation");
uint64_t Dist = 0;
// Account for NeighborHash
Dist += NeighborHash == BBH.NeighborHash ? 0 : 1;
Dist <<= 16;
// Account for InstrHash
Dist += InstrHash == BBH.InstrHash ? 0 : 1;
Dist <<= 16;
// Account for Offset
Dist += (Offset >= BBH.Offset ? Offset - BBH.Offset : BBH.Offset - Offset);
return Dist;
}

/// The offset of the basic block from the function start.
uint16_t Offset{0};
/// (Loose) Hash of the basic block instructions, excluding operands.
uint16_t OpcodeHash{0};
/// (Strong) Hash of the basic block instructions, including opcodes and
/// operands.
uint16_t InstrHash{0};
/// Hash of the (loose) basic block together with (loose) hashes of its
/// successors and predecessors.
uint16_t NeighborHash{0};
};

class MachineBlockHashInfo : public MachineFunctionPass {
DenseMap<unsigned, uint64_t> MBBHashInfo;

public:
static char ID;
MachineBlockHashInfo();

StringRef getPassName() const override { return "Basic Block Hash Compute"; }

void getAnalysisUsage(AnalysisUsage &AU) const override;

bool runOnMachineFunction(MachineFunction &F) override;

uint64_t getMBBHash(const MachineBasicBlock &MBB);
};

} // end namespace llvm

#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
7 changes: 7 additions & 0 deletions llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ LLVM_ABI MachineFunctionPass *createBasicBlockSectionsPass();

LLVM_ABI MachineFunctionPass *createBasicBlockPathCloningPass();

/// createBasicBlockMatchingAndInferencePass - This pass enables matching
/// and inference when using propeller.
MachineFunctionPass *createBasicBlockMatchingAndInferencePass();

/// createMachineBlockHashInfoPass - This pass computes basic block hashes.
MachineFunctionPass *createMachineBlockHashInfoPass();

/// createMachineFunctionSplitterPass - This pass splits machine functions
/// using profile information.
LLVM_ABI MachineFunctionPass *createMachineFunctionSplitterPass();
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ LLVM_ABI void initializeAlwaysInlinerLegacyPassPass(PassRegistry &);
LLVM_ABI void initializeAssignmentTrackingAnalysisPass(PassRegistry &);
LLVM_ABI void initializeAssumptionCacheTrackerPass(PassRegistry &);
LLVM_ABI void initializeAtomicExpandLegacyPass(PassRegistry &);
LLVM_ABI void initializeBasicBlockMatchingAndInferencePass(PassRegistry &);
LLVM_ABI void initializeBasicBlockPathCloningPass(PassRegistry &);
LLVM_ABI void
initializeBasicBlockSectionsProfileReaderWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeBasicBlockSectionsPass(PassRegistry &);
LLVM_ABI void initializeBarrierNoopPass(PassRegistry &);
LLVM_ABI void initializeBasicAAWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeBlockFrequencyInfoWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeMachineBlockHashInfoPass(PassRegistry&);
LLVM_ABI void initializeBranchFolderLegacyPass(PassRegistry &);
LLVM_ABI void initializeBranchProbabilityInfoWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeBranchRelaxationLegacyPass(PassRegistry &);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class MCContext {
unsigned GetInstance(unsigned LocalLabelVal);

/// SHT_LLVM_BB_ADDR_MAP version to emit.
uint8_t BBAddrMapVersion = 3;
uint8_t BBAddrMapVersion = 4;

/// The file name of the log file from the environment variable
/// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
Expand Down
16 changes: 16 additions & 0 deletions llvm/include/llvm/Transforms/Utils/SampleProfileInference.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ template <typename FT> class SampleProfileInference {
SampleProfileInference(FunctionT &F, BlockEdgeMap &Successors,
BlockWeightMap &SampleBlockWeights)
: F(F), Successors(Successors), SampleBlockWeights(SampleBlockWeights) {}
SampleProfileInference(FunctionT &F, BlockEdgeMap &Successors,
BlockWeightMap &SampleBlockWeights,
EdgeWeightMap &SampleEdgeWeights)
: F(F), Successors(Successors), SampleBlockWeights(SampleBlockWeights),
SampleEdgeWeights(SampleEdgeWeights) {}

/// Apply the profile inference algorithm for a given function
void apply(BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights);
Expand Down Expand Up @@ -157,6 +162,9 @@ template <typename FT> class SampleProfileInference {

/// Map basic blocks to their sampled weights.
BlockWeightMap &SampleBlockWeights;

/// Map edges to their sampled weights.
EdgeWeightMap SampleEdgeWeights;
};

template <typename BT>
Expand Down Expand Up @@ -266,6 +274,14 @@ FlowFunction SampleProfileInference<BT>::createFlowFunction(
FlowJump Jump;
Jump.Source = BlockIndex[BB];
Jump.Target = BlockIndex[Succ];
auto It = SampleEdgeWeights.find(std::make_pair(BB, Succ));
if (It != SampleEdgeWeights.end()) {
Jump.HasUnknownWeight = false;
Jump.Weight = It->second;
} else {
Jump.HasUnknownWeight = true;
Jump.Weight = 0;
}
Func.Jumps.push_back(Jump);
}
}
Expand Down
14 changes: 13 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "llvm/CodeGen/GCMetadataPrinter.h"
#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineBlockHashInfo.h"
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineDominators.h"
Expand Down Expand Up @@ -182,6 +183,8 @@ static cl::opt<bool> PrintLatency(
cl::desc("Print instruction latencies as verbose asm comments"), cl::Hidden,
cl::init(false));

extern cl::opt<bool> EmitBBHash;

STATISTIC(EmittedInsts, "Number of machine instrs printed");

char AsmPrinter::ID = 0;
Expand Down Expand Up @@ -461,6 +464,8 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<GCModuleInfo>();
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
if (EmitBBHash)
AU.addRequired<MachineBlockHashInfo>();
}

bool AsmPrinter::doInitialization(Module &M) {
Expand Down Expand Up @@ -1427,7 +1432,8 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges,
BrProbEnabled,
MF.hasBBSections() && NumMBBSectionRanges > 1,
static_cast<bool>(BBAddrMapSkipEmitBBEntries),
HasCalls};
HasCalls,
static_cast<bool>(EmitBBHash)};
}

void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
Expand Down Expand Up @@ -1486,6 +1492,8 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
PrevMBBEndSymbol = MBBSymbol;
}

auto MBHI = Features.BBHash ? &getAnalysis<MachineBlockHashInfo>() : nullptr;

if (!Features.OmitBBEntries) {
OutStreamer->AddComment("BB id");
// Emit the BB ID for this basic block.
Expand Down Expand Up @@ -1513,6 +1521,10 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), CurrentLabel);
// Emit the Metadata.
OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
// Emit the Hash.
if (MBHI) {
OutStreamer->emitULEB128IntValue(MBHI->getMBBHash(MBB));
}
}
PrevMBBEndSymbol = MBB.getEndSymbol();
}
Expand Down
Loading