Skip to content

Port CodeGenPrepare to new pass manager (and BasicBlockSectionsProfil… #75380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
83 changes: 62 additions & 21 deletions llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Target/TargetMachine.h"

using namespace llvm;

namespace llvm {
Expand Down Expand Up @@ -72,25 +75,13 @@ template <> struct DenseMapInfo<UniqueBBID> {
}
};

class BasicBlockSectionsProfileReader : public ImmutablePass {
class BasicBlockSectionsProfileReader {
public:
static char ID;

friend class BasicBlockSectionsProfileReaderWrapperPass;
BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
: ImmutablePass(ID), MBuf(Buf),
LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#') {
initializeBasicBlockSectionsProfileReaderPass(
*PassRegistry::getPassRegistry());
};
: MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};

BasicBlockSectionsProfileReader() : ImmutablePass(ID) {
initializeBasicBlockSectionsProfileReaderPass(
*PassRegistry::getPassRegistry());
}

StringRef getPassName() const override {
return "Basic Block Sections Profile Reader";
}
BasicBlockSectionsProfileReader(){};

// Returns true if basic block sections profile exist for function \p
// FuncName.
Expand All @@ -109,10 +100,6 @@ class BasicBlockSectionsProfileReader : public ImmutablePass {
SmallVector<SmallVector<unsigned>>
getClonePathsForFunction(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;

private:
StringRef getAliasName(StringRef FuncName) const {
auto R = FuncAliasMap.find(FuncName);
Expand Down Expand Up @@ -170,7 +157,61 @@ class BasicBlockSectionsProfileReader : public ImmutablePass {
// sections profile. \p Buf is a memory buffer that contains the list of
// functions and basic block ids to selectively enable basic block sections.
ImmutablePass *
createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf);
createBasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf);

/// Analysis pass providing the \c BasicBlockSectionsProfileReader.
///
/// Note that this pass's result cannot be invalidated, it is immutable for the
/// life of the module.
class BasicBlockSectionsProfileReaderAnalysis
: public AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis> {

public:
static AnalysisKey Key;
typedef BasicBlockSectionsProfileReader Result;
BasicBlockSectionsProfileReaderAnalysis(const TargetMachine *TM) : TM(TM) {}

Result run(Function &F, FunctionAnalysisManager &AM);

private:
const TargetMachine *TM;
};

class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
public:
static char ID;
BasicBlockSectionsProfileReader BBSPR;

BasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf)
: ImmutablePass(ID), BBSPR(BasicBlockSectionsProfileReader(Buf)) {
initializeBasicBlockSectionsProfileReaderWrapperPassPass(
*PassRegistry::getPassRegistry());
};

BasicBlockSectionsProfileReaderWrapperPass()
: ImmutablePass(ID), BBSPR(BasicBlockSectionsProfileReader()) {
initializeBasicBlockSectionsProfileReaderWrapperPassPass(
*PassRegistry::getPassRegistry());
}

StringRef getPassName() const override {
return "Basic Block Sections Profile Reader";
}

bool isFunctionHot(StringRef FuncName) const;

std::pair<bool, SmallVector<BBClusterInfo>>
getClusterInfoForFunction(StringRef FuncName) const;

SmallVector<SmallVector<unsigned>>
getClonePathsForFunction(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;

BasicBlockSectionsProfileReader &getBBSPR();
};

} // namespace llvm
#endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
35 changes: 35 additions & 0 deletions llvm/include/llvm/CodeGen/CodeGenPrepare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===- CodeGenPrepare.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
///
/// Defines an IR pass for CodeGen Prepare.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_PREPARE_H
#define LLVM_CODEGEN_PREPARE_H

#include "llvm/IR/PassManager.h"

namespace llvm {

class Function;
class TargetMachine;

class CodeGenPreparePass : public PassInfoMixin<CodeGenPreparePass> {
private:
const TargetMachine *TM;

public:
CodeGenPreparePass(const TargetMachine *TM) : TM(TM) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};

} // end namespace llvm

#endif // LLVM_CODEGEN_PREPARE_H
4 changes: 2 additions & 2 deletions llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ namespace llvm {
MachineFunctionPass *createResetMachineFunctionPass(bool EmitFallbackDiag,
bool AbortOnFailedISel);

/// createCodeGenPreparePass - Transform the code to expose more pattern
/// createCodeGenPrepareLegacyPass - Transform the code to expose more pattern
/// matching during instruction selection.
FunctionPass *createCodeGenPreparePass();
FunctionPass *createCodeGenPrepareLegacyPass();

/// This pass implements generation of target-specific intrinsics to support
/// handling of complex number arithmetic
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void initializeAssignmentTrackingAnalysisPass(PassRegistry &);
void initializeAssumptionCacheTrackerPass(PassRegistry&);
void initializeAtomicExpandPass(PassRegistry&);
void initializeBasicBlockPathCloningPass(PassRegistry &);
void initializeBasicBlockSectionsProfileReaderPass(PassRegistry &);
void initializeBasicBlockSectionsProfileReaderWrapperPassPass(PassRegistry &);
void initializeBasicBlockSectionsPass(PassRegistry &);
void initializeBarrierNoopPass(PassRegistry&);
void initializeBasicAAWrapperPassPass(PassRegistry&);
Expand All @@ -75,7 +75,7 @@ void initializeCallGraphDOTPrinterPass(PassRegistry&);
void initializeCallGraphViewerPass(PassRegistry&);
void initializeCallGraphWrapperPassPass(PassRegistry&);
void initializeCheckDebugMachineModulePass(PassRegistry &);
void initializeCodeGenPreparePass(PassRegistry&);
void initializeCodeGenPrepareLegacyPassPass(PassRegistry &);
void initializeComplexDeinterleavingLegacyPassPass(PassRegistry&);
void initializeConstantHoistingLegacyPassPass(PassRegistry&);
void initializeCycleInfoWrapperPassPass(PassRegistry &);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/LinkAllPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace {
(void) llvm::createTailCallEliminationPass();
(void)llvm::createTLSVariableHoistPass();
(void) llvm::createConstantHoistingPass();
(void) llvm::createCodeGenPreparePass();
(void)llvm::createCodeGenPrepareLegacyPass();
(void) llvm::createEarlyCSEPass();
(void) llvm::createGVNPass();
(void) llvm::createPostDomTree();
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/CodeGen/BasicBlockPathCloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BasicBlockPathCloning : public MachineFunctionPass {
public:
static char ID;

BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;

BasicBlockPathCloning() : MachineFunctionPass(ID) {
initializeBasicBlockPathCloningPass(*PassRegistry::getPassRegistry());
Expand All @@ -218,7 +218,7 @@ INITIALIZE_PASS_BEGIN(
BasicBlockPathCloning, "bb-path-cloning",
"Applies path clonings for the -basic-block-sections=list option", false,
false)
INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReader)
INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass)
INITIALIZE_PASS_END(
BasicBlockPathCloning, "bb-path-cloning",
"Applies path clonings for the -basic-block-sections=list option", false,
Expand All @@ -230,13 +230,14 @@ bool BasicBlockPathCloning::runOnMachineFunction(MachineFunction &MF) {
if (hasInstrProfHashMismatch(MF))
return false;

return ApplyCloning(MF, getAnalysis<BasicBlockSectionsProfileReader>()
.getClonePathsForFunction(MF.getName()));
return ApplyCloning(MF,
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
.getClonePathsForFunction(MF.getName()));
}

void BasicBlockPathCloning::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<BasicBlockSectionsProfileReader>();
AU.addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/CodeGen/BasicBlockSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BasicBlockSections : public MachineFunctionPass {
public:
static char ID;

BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;

BasicBlockSections() : MachineFunctionPass(ID) {
initializeBasicBlockSectionsPass(*PassRegistry::getPassRegistry());
Expand All @@ -128,7 +128,7 @@ INITIALIZE_PASS_BEGIN(
"Prepares for basic block sections, by splitting functions "
"into clusters of basic blocks.",
false, false)
INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReader)
INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass)
INITIALIZE_PASS_END(BasicBlockSections, "bbsections-prepare",
"Prepares for basic block sections, by splitting functions "
"into clusters of basic blocks.",
Expand Down Expand Up @@ -306,7 +306,7 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
DenseMap<UniqueBBID, BBClusterInfo> FuncClusterInfo;
if (BBSectionsType == BasicBlockSection::List) {
auto [HasProfile, ClusterInfo] =
getAnalysis<BasicBlockSectionsProfileReader>()
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
.getClusterInfoForFunction(MF.getName());
if (!HasProfile)
return false;
Expand Down Expand Up @@ -362,7 +362,7 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {

void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<BasicBlockSectionsProfileReader>();
AU.addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}

Expand Down
52 changes: 42 additions & 10 deletions llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

using namespace llvm;

char BasicBlockSectionsProfileReader::ID = 0;
INITIALIZE_PASS(BasicBlockSectionsProfileReader, "bbsections-profile-reader",
char BasicBlockSectionsProfileReaderWrapperPass::ID = 0;
INITIALIZE_PASS(BasicBlockSectionsProfileReaderWrapperPass,
"bbsections-profile-reader",
"Reads and parses a basic block sections profile.", false,
false)

Expand Down Expand Up @@ -395,11 +396,11 @@ Error BasicBlockSectionsProfileReader::ReadProfile() {
}
}

bool BasicBlockSectionsProfileReader::doInitialization(Module &M) {
if (!MBuf)
bool BasicBlockSectionsProfileReaderWrapperPass::doInitialization(Module &M) {
if (!BBSPR.MBuf)
return false;
// Get the function name to debug info filename mapping.
FunctionNameToDIFilename.clear();
BBSPR.FunctionNameToDIFilename.clear();
for (const Function &F : M) {
SmallString<128> DIFilename;
if (F.isDeclaration())
Expand All @@ -411,15 +412,46 @@ bool BasicBlockSectionsProfileReader::doInitialization(Module &M) {
DIFilename = sys::path::remove_leading_dotslash(CU->getFilename());
}
[[maybe_unused]] bool inserted =
FunctionNameToDIFilename.try_emplace(F.getName(), DIFilename).second;
BBSPR.FunctionNameToDIFilename.try_emplace(F.getName(), DIFilename)
.second;
assert(inserted);
}
if (auto Err = ReadProfile())
if (auto Err = BBSPR.ReadProfile())
report_fatal_error(std::move(Err));
return false;
}

ImmutablePass *
llvm::createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf) {
return new BasicBlockSectionsProfileReader(Buf);
AnalysisKey BasicBlockSectionsProfileReaderAnalysis::Key;

BasicBlockSectionsProfileReader
BasicBlockSectionsProfileReaderAnalysis::run(Function &F,
FunctionAnalysisManager &AM) {
return BasicBlockSectionsProfileReader(TM->getBBSectionsFuncListBuf());
}

bool BasicBlockSectionsProfileReaderWrapperPass::isFunctionHot(
StringRef FuncName) const {
return BBSPR.isFunctionHot(FuncName);
}

std::pair<bool, SmallVector<BBClusterInfo>>
BasicBlockSectionsProfileReaderWrapperPass::getClusterInfoForFunction(
StringRef FuncName) const {
return BBSPR.getClusterInfoForFunction(FuncName);
}

SmallVector<SmallVector<unsigned>>
BasicBlockSectionsProfileReaderWrapperPass::getClonePathsForFunction(
StringRef FuncName) const {
return BBSPR.getClonePathsForFunction(FuncName);
}

BasicBlockSectionsProfileReader &
BasicBlockSectionsProfileReaderWrapperPass::getBBSPR() {
return BBSPR;
}

ImmutablePass *llvm::createBasicBlockSectionsProfileReaderWrapperPass(
const MemoryBuffer *Buf) {
return new BasicBlockSectionsProfileReaderWrapperPass(Buf);
}
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeCFIFixupPass(Registry);
initializeCFIInstrInserterPass(Registry);
initializeCheckDebugMachineModulePass(Registry);
initializeCodeGenPreparePass(Registry);
initializeCodeGenPrepareLegacyPassPass(Registry);
initializeDeadMachineInstructionElimPass(Registry);
initializeDebugifyMachineModulePass(Registry);
initializeDetectDeadLanesPass(Registry);
Expand Down
Loading