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
Port CodeGenPrepare to new pass manager (and BasicBlockSectionsProfil… #75380
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
I am new to llvm-project and would appreciate any suggestions, especially on what is needed to port
Currently, I am reading the docs at the bottom of Contributing to understand how the |
530b51e
to
2b4e41c
Compare
@llvm/pr-subscribers-backend-amdgpu @llvm/pr-subscribers-backend-aarch64 Author: Nick Anderson (nickleus27) ChangesPort CodeGenPrepare to new pass manager and dependency BasicBlockSectionsProfileReader Patch is 100.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/75380.diff 112 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
index dfb8d5d9f2f5d3..665cf9c7d6e225 100644
--- a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
+++ b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
@@ -21,6 +21,7 @@
#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"
@@ -72,25 +73,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());
- };
-
- BasicBlockSectionsProfileReader() : ImmutablePass(ID) {
- initializeBasicBlockSectionsProfileReaderPass(
- *PassRegistry::getPassRegistry());
- }
+ : MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
- StringRef getPassName() const override {
- return "Basic Block Sections Profile Reader";
- }
+ BasicBlockSectionsProfileReader(){};
// Returns true if basic block sections profile exist for function \p
// FuncName.
@@ -109,10 +98,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);
@@ -170,7 +155,58 @@ 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> {
+ friend AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis>;
+ static AnalysisKey Key;
+
+public:
+ typedef BasicBlockSectionsProfileReader Result;
+
+ Result *run(const MemoryBuffer *Buf);
+};
+
+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
diff --git a/llvm/include/llvm/CodeGen/CodeGenPrepare.h b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
new file mode 100644
index 00000000000000..7ce2b0eae7cd60
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
@@ -0,0 +1,36 @@
+//===- 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
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index ca9fbb1def7624..bbfb8a0dbe26a4 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -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
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 46b1e95c3c15f3..3db639a6872407 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -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&);
@@ -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 &);
diff --git a/llvm/include/llvm/LinkAllPasses.h b/llvm/include/llvm/LinkAllPasses.h
index 7a21876e565a7c..fe7fedad18bc0e 100644
--- a/llvm/include/llvm/LinkAllPasses.h
+++ b/llvm/include/llvm/LinkAllPasses.h
@@ -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();
diff --git a/llvm/lib/CodeGen/BasicBlockPathCloning.cpp b/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
index 5d5f3c3da48160..901542e8507bdf 100644
--- a/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
+++ b/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
@@ -196,7 +196,7 @@ class BasicBlockPathCloning : public MachineFunctionPass {
public:
static char ID;
- BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+ BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;
BasicBlockPathCloning() : MachineFunctionPass(ID) {
initializeBasicBlockPathCloningPass(*PassRegistry::getPassRegistry());
@@ -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,
@@ -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);
}
diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 42997d2287d61d..94b5a503fbd0f3 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -103,7 +103,7 @@ class BasicBlockSections : public MachineFunctionPass {
public:
static char ID;
- BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+ BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;
BasicBlockSections() : MachineFunctionPass(ID) {
initializeBasicBlockSectionsPass(*PassRegistry::getPassRegistry());
@@ -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.",
@@ -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;
@@ -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);
}
diff --git a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
index 15b6f63e86327a..09f28bde48cc88 100644
--- a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
@@ -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)
@@ -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())
@@ -411,15 +412,45 @@ 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) {
+AnalysisKey BasicBlockSectionsProfileReaderAnalysis::Key;
+
+BasicBlockSectionsProfileReader *
+BasicBlockSectionsProfileReaderAnalysis::run(const MemoryBuffer *Buf) {
return new BasicBlockSectionsProfileReader(Buf);
}
+
+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);
+}
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 7b73a7b11ddf1c..418066452c1724 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -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);
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index f9e791c7334838..66bd0ac1898a88 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/CodeGenPrepare.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
@@ -301,7 +302,8 @@ using ValueToSExts = MapVector<Value *, SExts>;
class TypePromotionTransaction;
-class CodeGenPrepare : public FunctionPass {
+class CodeGenPrepare {
+ friend class CodeGenPrepareLegacyPass;
const TargetMachine *TM = nullptr;
const TargetSubtargetInfo *SubtargetInfo = nullptr;
const TargetLowering *TLI = nullptr;
@@ -365,6 +367,8 @@ class CodeGenPrepare : public FunctionPass {
std::unique_ptr<DominatorTree> DT;
public:
+ CodeGenPrepare(){};
+ CodeGenPrepare(const TargetMachine *TM) : TM(TM){};
/// If encounter huge function, we need to limit the build time.
bool IsHugeFunc = false;
@@ -374,15 +378,7 @@ class CodeGenPrepare : public FunctionPass {
/// to insert such BB into FreshBBs for huge function.
SmallSet<BasicBlock *, 32> FreshBBs;
- static char ID; // Pass identification, replacement for typeid
-
- CodeGenPrepare() : FunctionPass(ID) {
- initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
- }
-
- bool runOnFunction(Function &F) override;
-
- void releaseMemory() override {
+ void releaseMemory() {
// Clear per function information.
InsertedInsts.clear();
PromotedInsts.clear();
@@ -391,17 +387,7 @@ class CodeGenPrepare : public FunctionPass {
BFI.reset();
}
- StringRef getPassName() const override { return "CodeGen Prepare"; }
-
- void getAnalysisUsage(AnalysisUsage &AU) const override {
- // FIXME: When we can selectively preserve passes, preserve the domtree.
- AU.addRequired<ProfileSummaryInfoWrapperPass>();
- AU.addRequired<TargetLibraryInfoWrapperPass>();
- AU.addRequired<TargetPassConfig>();
- AU.addRequired<TargetTransformInfoWrapperPass>();
- AU.addRequired<LoopInfoWrapperPass>();
- AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>();
- }
+ bool run(Function &F, FunctionAnalysisManager &AM);
private:
template <typename F>
@@ -488,45 +474,107 @@ class CodeGenPrepare : public FunctionPass {
bool combineToUSubWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
void verifyBFIUpdates(Function &F);
+ bool _run(Function &F);
+};
+
+class CodeGenPrepareLegacyPass : public FunctionPass {
+public:
+ static char ID; // Pass identification, replacement for typeid
+
+ CodeGenPrepareLegacyPass() : FunctionPass(ID) {
+ initializeCodeGenPrepareLegacyPassPass(*PassRegistry::getPassRegistry());
+ }
+
+ bool runOnFunction(Function &F) override;
+
+ StringRef getPassName() const override { return "CodeGen Prepare"; }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ // FIXME: When we can selectively preserve passes, preserve the domtree.
+ AU.addRequired<ProfileSummaryInfoWrapperPass>();
+ AU.addRequired<TargetLibraryInfoWrapperPass>();
+ AU.addRequired<TargetPassConfig>();
+ AU.addRequired<TargetTransformInfoWrapperPass>();
+ AU.addRequired<LoopInfoWrapperPass>();
+ AU.addUsedIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
+ }
};
} // end anonymous namespace
-char CodeGenPrepare::ID = 0;
+char CodeGenPrepareLegacyPass::ID = 0;
-INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE,
+bool CodeGenPrepareLegacyPass::runOnFunction(Function &F) {
+ if (skipFunction(F))
+ return false;
+ auto TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
+ CodeGenPrepare CGP(TM);
+ CGP.DL = &F.getParent()->getDataLayout();
+ CGP.SubtargetInfo = TM->getSubtargetImpl(F);
+ CGP.TLI = CGP.SubtargetInfo->getTargetLowering();
+ CGP.TRI = CGP.SubtargetInfo->getRegisterInfo();
+ CGP.TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
+ CGP.TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
+ CGP.LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+ CGP.BPI.reset(new BranchProbabilityInfo(F, *CGP.LI));
+ CGP.BFI.reset(new BlockFrequencyInfo(F, *CGP.BPI, *CGP.LI));
+ CGP.PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+ auto BBSPRWP =
+ getAnalysisIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
+ CGP.BBSectionsProfileReader = BBSPRWP ? &BBSPRWP->getBBSPR() : nullptr;
+
+ return CGP._run(F);
+}
+
+INITIALIZE_PASS_BEGIN(CodeGenPrepareLegacyPass, DEBUG_TYPE,
"Optimize for code generation", false, false)
-INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProf...
[truncated]
|
@llvm/pr-subscribers-backend-x86 Author: Nick Anderson (nickleus27) ChangesPort CodeGenPrepare to new pass manager and dependency BasicBlockSectionsProfileReader Patch is 100.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/75380.diff 112 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
index dfb8d5d9f2f5d3..665cf9c7d6e225 100644
--- a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
+++ b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
@@ -21,6 +21,7 @@
#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"
@@ -72,25 +73,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());
- };
-
- BasicBlockSectionsProfileReader() : ImmutablePass(ID) {
- initializeBasicBlockSectionsProfileReaderPass(
- *PassRegistry::getPassRegistry());
- }
+ : MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
- StringRef getPassName() const override {
- return "Basic Block Sections Profile Reader";
- }
+ BasicBlockSectionsProfileReader(){};
// Returns true if basic block sections profile exist for function \p
// FuncName.
@@ -109,10 +98,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);
@@ -170,7 +155,58 @@ 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> {
+ friend AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis>;
+ static AnalysisKey Key;
+
+public:
+ typedef BasicBlockSectionsProfileReader Result;
+
+ Result *run(const MemoryBuffer *Buf);
+};
+
+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
diff --git a/llvm/include/llvm/CodeGen/CodeGenPrepare.h b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
new file mode 100644
index 00000000000000..7ce2b0eae7cd60
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
@@ -0,0 +1,36 @@
+//===- 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
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index ca9fbb1def7624..bbfb8a0dbe26a4 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -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
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 46b1e95c3c15f3..3db639a6872407 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -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&);
@@ -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 &);
diff --git a/llvm/include/llvm/LinkAllPasses.h b/llvm/include/llvm/LinkAllPasses.h
index 7a21876e565a7c..fe7fedad18bc0e 100644
--- a/llvm/include/llvm/LinkAllPasses.h
+++ b/llvm/include/llvm/LinkAllPasses.h
@@ -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();
diff --git a/llvm/lib/CodeGen/BasicBlockPathCloning.cpp b/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
index 5d5f3c3da48160..901542e8507bdf 100644
--- a/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
+++ b/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
@@ -196,7 +196,7 @@ class BasicBlockPathCloning : public MachineFunctionPass {
public:
static char ID;
- BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+ BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;
BasicBlockPathCloning() : MachineFunctionPass(ID) {
initializeBasicBlockPathCloningPass(*PassRegistry::getPassRegistry());
@@ -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,
@@ -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);
}
diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 42997d2287d61d..94b5a503fbd0f3 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -103,7 +103,7 @@ class BasicBlockSections : public MachineFunctionPass {
public:
static char ID;
- BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+ BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;
BasicBlockSections() : MachineFunctionPass(ID) {
initializeBasicBlockSectionsPass(*PassRegistry::getPassRegistry());
@@ -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.",
@@ -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;
@@ -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);
}
diff --git a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
index 15b6f63e86327a..09f28bde48cc88 100644
--- a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
@@ -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)
@@ -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())
@@ -411,15 +412,45 @@ 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) {
+AnalysisKey BasicBlockSectionsProfileReaderAnalysis::Key;
+
+BasicBlockSectionsProfileReader *
+BasicBlockSectionsProfileReaderAnalysis::run(const MemoryBuffer *Buf) {
return new BasicBlockSectionsProfileReader(Buf);
}
+
+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);
+}
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 7b73a7b11ddf1c..418066452c1724 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -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);
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index f9e791c7334838..66bd0ac1898a88 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/CodeGenPrepare.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
@@ -301,7 +302,8 @@ using ValueToSExts = MapVector<Value *, SExts>;
class TypePromotionTransaction;
-class CodeGenPrepare : public FunctionPass {
+class CodeGenPrepare {
+ friend class CodeGenPrepareLegacyPass;
const TargetMachine *TM = nullptr;
const TargetSubtargetInfo *SubtargetInfo = nullptr;
const TargetLowering *TLI = nullptr;
@@ -365,6 +367,8 @@ class CodeGenPrepare : public FunctionPass {
std::unique_ptr<DominatorTree> DT;
public:
+ CodeGenPrepare(){};
+ CodeGenPrepare(const TargetMachine *TM) : TM(TM){};
/// If encounter huge function, we need to limit the build time.
bool IsHugeFunc = false;
@@ -374,15 +378,7 @@ class CodeGenPrepare : public FunctionPass {
/// to insert such BB into FreshBBs for huge function.
SmallSet<BasicBlock *, 32> FreshBBs;
- static char ID; // Pass identification, replacement for typeid
-
- CodeGenPrepare() : FunctionPass(ID) {
- initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
- }
-
- bool runOnFunction(Function &F) override;
-
- void releaseMemory() override {
+ void releaseMemory() {
// Clear per function information.
InsertedInsts.clear();
PromotedInsts.clear();
@@ -391,17 +387,7 @@ class CodeGenPrepare : public FunctionPass {
BFI.reset();
}
- StringRef getPassName() const override { return "CodeGen Prepare"; }
-
- void getAnalysisUsage(AnalysisUsage &AU) const override {
- // FIXME: When we can selectively preserve passes, preserve the domtree.
- AU.addRequired<ProfileSummaryInfoWrapperPass>();
- AU.addRequired<TargetLibraryInfoWrapperPass>();
- AU.addRequired<TargetPassConfig>();
- AU.addRequired<TargetTransformInfoWrapperPass>();
- AU.addRequired<LoopInfoWrapperPass>();
- AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>();
- }
+ bool run(Function &F, FunctionAnalysisManager &AM);
private:
template <typename F>
@@ -488,45 +474,107 @@ class CodeGenPrepare : public FunctionPass {
bool combineToUSubWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
void verifyBFIUpdates(Function &F);
+ bool _run(Function &F);
+};
+
+class CodeGenPrepareLegacyPass : public FunctionPass {
+public:
+ static char ID; // Pass identification, replacement for typeid
+
+ CodeGenPrepareLegacyPass() : FunctionPass(ID) {
+ initializeCodeGenPrepareLegacyPassPass(*PassRegistry::getPassRegistry());
+ }
+
+ bool runOnFunction(Function &F) override;
+
+ StringRef getPassName() const override { return "CodeGen Prepare"; }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ // FIXME: When we can selectively preserve passes, preserve the domtree.
+ AU.addRequired<ProfileSummaryInfoWrapperPass>();
+ AU.addRequired<TargetLibraryInfoWrapperPass>();
+ AU.addRequired<TargetPassConfig>();
+ AU.addRequired<TargetTransformInfoWrapperPass>();
+ AU.addRequired<LoopInfoWrapperPass>();
+ AU.addUsedIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
+ }
};
} // end anonymous namespace
-char CodeGenPrepare::ID = 0;
+char CodeGenPrepareLegacyPass::ID = 0;
-INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE,
+bool CodeGenPrepareLegacyPass::runOnFunction(Function &F) {
+ if (skipFunction(F))
+ return false;
+ auto TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
+ CodeGenPrepare CGP(TM);
+ CGP.DL = &F.getParent()->getDataLayout();
+ CGP.SubtargetInfo = TM->getSubtargetImpl(F);
+ CGP.TLI = CGP.SubtargetInfo->getTargetLowering();
+ CGP.TRI = CGP.SubtargetInfo->getRegisterInfo();
+ CGP.TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
+ CGP.TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
+ CGP.LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+ CGP.BPI.reset(new BranchProbabilityInfo(F, *CGP.LI));
+ CGP.BFI.reset(new BlockFrequencyInfo(F, *CGP.BPI, *CGP.LI));
+ CGP.PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+ auto BBSPRWP =
+ getAnalysisIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
+ CGP.BBSectionsProfileReader = BBSPRWP ? &BBSPRWP->getBBSPR() : nullptr;
+
+ return CGP._run(F);
+}
+
+INITIALIZE_PASS_BEGIN(CodeGenPrepareLegacyPass, DEBUG_TYPE,
"Optimize for code generation", false, false)
-INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProf...
[truncated]
|
@llvm/pr-subscribers-backend-arm Author: Nick Anderson (nickleus27) ChangesPort CodeGenPrepare to new pass manager and dependency BasicBlockSectionsProfileReader Patch is 100.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/75380.diff 112 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
index dfb8d5d9f2f5d3..665cf9c7d6e225 100644
--- a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
+++ b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
@@ -21,6 +21,7 @@
#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"
@@ -72,25 +73,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());
- };
-
- BasicBlockSectionsProfileReader() : ImmutablePass(ID) {
- initializeBasicBlockSectionsProfileReaderPass(
- *PassRegistry::getPassRegistry());
- }
+ : MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
- StringRef getPassName() const override {
- return "Basic Block Sections Profile Reader";
- }
+ BasicBlockSectionsProfileReader(){};
// Returns true if basic block sections profile exist for function \p
// FuncName.
@@ -109,10 +98,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);
@@ -170,7 +155,58 @@ 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> {
+ friend AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis>;
+ static AnalysisKey Key;
+
+public:
+ typedef BasicBlockSectionsProfileReader Result;
+
+ Result *run(const MemoryBuffer *Buf);
+};
+
+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
diff --git a/llvm/include/llvm/CodeGen/CodeGenPrepare.h b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
new file mode 100644
index 00000000000000..7ce2b0eae7cd60
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
@@ -0,0 +1,36 @@
+//===- 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
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index ca9fbb1def7624..bbfb8a0dbe26a4 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -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
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 46b1e95c3c15f3..3db639a6872407 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -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&);
@@ -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 &);
diff --git a/llvm/include/llvm/LinkAllPasses.h b/llvm/include/llvm/LinkAllPasses.h
index 7a21876e565a7c..fe7fedad18bc0e 100644
--- a/llvm/include/llvm/LinkAllPasses.h
+++ b/llvm/include/llvm/LinkAllPasses.h
@@ -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();
diff --git a/llvm/lib/CodeGen/BasicBlockPathCloning.cpp b/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
index 5d5f3c3da48160..901542e8507bdf 100644
--- a/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
+++ b/llvm/lib/CodeGen/BasicBlockPathCloning.cpp
@@ -196,7 +196,7 @@ class BasicBlockPathCloning : public MachineFunctionPass {
public:
static char ID;
- BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+ BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;
BasicBlockPathCloning() : MachineFunctionPass(ID) {
initializeBasicBlockPathCloningPass(*PassRegistry::getPassRegistry());
@@ -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,
@@ -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);
}
diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 42997d2287d61d..94b5a503fbd0f3 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -103,7 +103,7 @@ class BasicBlockSections : public MachineFunctionPass {
public:
static char ID;
- BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+ BasicBlockSectionsProfileReaderWrapperPass *BBSectionsProfileReader = nullptr;
BasicBlockSections() : MachineFunctionPass(ID) {
initializeBasicBlockSectionsPass(*PassRegistry::getPassRegistry());
@@ -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.",
@@ -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;
@@ -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);
}
diff --git a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
index 15b6f63e86327a..09f28bde48cc88 100644
--- a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
@@ -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)
@@ -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())
@@ -411,15 +412,45 @@ 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) {
+AnalysisKey BasicBlockSectionsProfileReaderAnalysis::Key;
+
+BasicBlockSectionsProfileReader *
+BasicBlockSectionsProfileReaderAnalysis::run(const MemoryBuffer *Buf) {
return new BasicBlockSectionsProfileReader(Buf);
}
+
+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);
+}
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 7b73a7b11ddf1c..418066452c1724 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -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);
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index f9e791c7334838..66bd0ac1898a88 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/CodeGenPrepare.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
@@ -301,7 +302,8 @@ using ValueToSExts = MapVector<Value *, SExts>;
class TypePromotionTransaction;
-class CodeGenPrepare : public FunctionPass {
+class CodeGenPrepare {
+ friend class CodeGenPrepareLegacyPass;
const TargetMachine *TM = nullptr;
const TargetSubtargetInfo *SubtargetInfo = nullptr;
const TargetLowering *TLI = nullptr;
@@ -365,6 +367,8 @@ class CodeGenPrepare : public FunctionPass {
std::unique_ptr<DominatorTree> DT;
public:
+ CodeGenPrepare(){};
+ CodeGenPrepare(const TargetMachine *TM) : TM(TM){};
/// If encounter huge function, we need to limit the build time.
bool IsHugeFunc = false;
@@ -374,15 +378,7 @@ class CodeGenPrepare : public FunctionPass {
/// to insert such BB into FreshBBs for huge function.
SmallSet<BasicBlock *, 32> FreshBBs;
- static char ID; // Pass identification, replacement for typeid
-
- CodeGenPrepare() : FunctionPass(ID) {
- initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
- }
-
- bool runOnFunction(Function &F) override;
-
- void releaseMemory() override {
+ void releaseMemory() {
// Clear per function information.
InsertedInsts.clear();
PromotedInsts.clear();
@@ -391,17 +387,7 @@ class CodeGenPrepare : public FunctionPass {
BFI.reset();
}
- StringRef getPassName() const override { return "CodeGen Prepare"; }
-
- void getAnalysisUsage(AnalysisUsage &AU) const override {
- // FIXME: When we can selectively preserve passes, preserve the domtree.
- AU.addRequired<ProfileSummaryInfoWrapperPass>();
- AU.addRequired<TargetLibraryInfoWrapperPass>();
- AU.addRequired<TargetPassConfig>();
- AU.addRequired<TargetTransformInfoWrapperPass>();
- AU.addRequired<LoopInfoWrapperPass>();
- AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>();
- }
+ bool run(Function &F, FunctionAnalysisManager &AM);
private:
template <typename F>
@@ -488,45 +474,107 @@ class CodeGenPrepare : public FunctionPass {
bool combineToUSubWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
void verifyBFIUpdates(Function &F);
+ bool _run(Function &F);
+};
+
+class CodeGenPrepareLegacyPass : public FunctionPass {
+public:
+ static char ID; // Pass identification, replacement for typeid
+
+ CodeGenPrepareLegacyPass() : FunctionPass(ID) {
+ initializeCodeGenPrepareLegacyPassPass(*PassRegistry::getPassRegistry());
+ }
+
+ bool runOnFunction(Function &F) override;
+
+ StringRef getPassName() const override { return "CodeGen Prepare"; }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ // FIXME: When we can selectively preserve passes, preserve the domtree.
+ AU.addRequired<ProfileSummaryInfoWrapperPass>();
+ AU.addRequired<TargetLibraryInfoWrapperPass>();
+ AU.addRequired<TargetPassConfig>();
+ AU.addRequired<TargetTransformInfoWrapperPass>();
+ AU.addRequired<LoopInfoWrapperPass>();
+ AU.addUsedIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
+ }
};
} // end anonymous namespace
-char CodeGenPrepare::ID = 0;
+char CodeGenPrepareLegacyPass::ID = 0;
-INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE,
+bool CodeGenPrepareLegacyPass::runOnFunction(Function &F) {
+ if (skipFunction(F))
+ return false;
+ auto TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
+ CodeGenPrepare CGP(TM);
+ CGP.DL = &F.getParent()->getDataLayout();
+ CGP.SubtargetInfo = TM->getSubtargetImpl(F);
+ CGP.TLI = CGP.SubtargetInfo->getTargetLowering();
+ CGP.TRI = CGP.SubtargetInfo->getRegisterInfo();
+ CGP.TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
+ CGP.TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
+ CGP.LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+ CGP.BPI.reset(new BranchProbabilityInfo(F, *CGP.LI));
+ CGP.BFI.reset(new BlockFrequencyInfo(F, *CGP.BPI, *CGP.LI));
+ CGP.PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+ auto BBSPRWP =
+ getAnalysisIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
+ CGP.BBSectionsProfileReader = BBSPRWP ? &BBSPRWP->getBBSPR() : nullptr;
+
+ return CGP._run(F);
+}
+
+INITIALIZE_PASS_BEGIN(CodeGenPrepareLegacyPass, DEBUG_TYPE,
"Optimize for code generation", false, false)
-INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProf...
[truncated]
|
2b4e41c
to
a981874
Compare
@arsenm After debugging, it looks like I am failing some tests because |
I am thinking what I need is to require ProfileSummaryAnalysis like this: |
Ping |
a981874
to
5ebf174
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
5ebf174
to
29143d1
Compare
getCachedResult is for optional analyses; if it's a hard requirement you should just use getResult instead to ensure it's available |
llvm/lib/Passes/PassBuilder.cpp
Outdated
if (FirstName == "codegenprepare") { | ||
MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should avoid pre-adding dependent passes based on the name, does getResult work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, getResult doesn't work. If I understand correctly we cannot request a result from an outer-scope analysis. PSI needs to be pre-cached using RequireAnalysisPass<ProfileSummaryAnalysis, Module>()
in the pipeline. I was having a hard time finding the right place to conditionally require ProfileSummaryAnalysis in the pipeline, so I just used this hack here. Any suggestions would be appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vaguely recall seeing this hack in another patch by @paperchalice. For now I would drop the FirstName check and just unconditionally add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this analysis in default codegen pipeline in #74920 and use option -passes='require<profile-summary>,function(select-optimize)'
for testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the advice, the suggestions really helped.
@@ -0,0 +1,36 @@ | |||
//===- CodeGenPrepare.h ------------------------------------------*- C++ | |||
//-*-===// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad line break for comment
llvm/lib/Passes/PassBuilder.cpp
Outdated
if (FirstName == "codegenprepare") { | ||
MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vaguely recall seeing this hack in another patch by @paperchalice. For now I would drop the FirstName check and just unconditionally add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's progress although the ProfileSummaryInfo hack is unfortunate
Result run(Function &F, FunctionAnalysisManager &AM); | ||
|
||
private: | ||
const llvm::TargetMachine *TM; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need llvm::
I was able to remove the ProfileSmmaryInfo hack in the last commit, and instead require ProfileSummary in the .ll regression tests via paperchalice's example. |
Requiring manual specification of it isn't great, it just moves the workaround out of the sources and into the tests |
@arsenm Should I squash these fixup commits? Or leave for now? |
Squash and merge is the only enabled option for the project |
I guess if you aren't a project member you may not see it? I still find it odd a committer still needs to hit the button after approval |
I agree, and thank you. |
@paperchalice @nickleus27 I've reverted this (and #77054) as they were breaking expensive checks builds: https://lab.llvm.org/buildbot/#/builders/104 - please can you take a look? |
Thank you, I will work on fixing this. |
I cannot reproduce the failed tests on my local machine (MacOS 14.2.1 x86_64). I noticed the failed tests from my commit https://lab.llvm.org/buildbot/#/builders/16/builds/59614 and from https://lab.llvm.org/buildbot/#/builders/104/builds/14911 are both Linux (Debian/Ubuntu). Could I reproduce and test a fix in a Debian/Ubuntu Virtual Machine locally? Any suggestions on how to reproduce the failed tests would be appreciated. |
It's an expensive checks failure, so you'll need to enable expensive checks in order to reproduce it. |
Awesome, thank you! |
…57b334e67 Local branch amd-gfx 6b757b3 Merged main:d8db2733c87ef2ee54c322cbee76711147a94948 into amd-gfx:967ab3262a05 Remote branch main e0c554a Port CodeGenPrepare to new pass manager (and BasicBlockSectionsProfil… (llvm#75380)
llvm#77182) Port CodeGenPrepare to new pass manager and dependency BasicBlockSectionsProfileReader Fixes: llvm#75380 Co-authored-by: Krishna-13-cyber <84722531+Krishna-13-cyber@users.noreply.github.com>
Port CodeGenPrepare to new pass manager and dependency BasicBlockSectionsProfileReader
Fixes: #64560