llvm-project/llvm/lib/CodeGen/MachineFunctionAnalysis.cpp
Vikram Hegde 06528070fc
[CodeGen][NPM] Clear MachineFunctions without using PA (#148113)
same as https://github.com/llvm/llvm-project/pull/139517

This replaces the InvalidateAnalysisPass<MachineFunctionAnalysis> pass.

There are no cross-function analysis requirements right now, so clearing
all analyses works for the last pass in the pipeline.

Having the InvalidateAnalysisPass<MachineFunctionAnalysis>() is causing
a problem with ModuleToCGSCCPassAdaptor by deleting machine functions
for other functions and ending up with exactly one correctly compiled
MF, with the rest being vanished.

This is because ModuleToCGSCCPAdaptor propagates PassPA (received from
the CGSCCToFunctionPassAdaptor that runs the actual codegen pipeline on
MFs) to the next SCC. That causes MFA invalidation on functions in the
next SCC.

For us, PassPA happens to be returned from
invalidate<machine-function-analysis> which abandons the
MachineFunctionAnalysis. So while the first function runs through the
pipeline normally, invalidate also deletes the functions in the next SCC
before its pipeline is run. (this seems to be the intended mechanism of
the CG adaptor to allow cross-SCC invalidations.

Co-authored-by : Oke, Akshat
<[Akshat.Oke@amd.com](mailto:Akshat.Oke@amd.com)>
2025-07-18 11:58:01 +05:30

54 lines
2.0 KiB
C++

//===- MachineFunctionAnalysis.cpp ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains the definitions of the MachineFunctionAnalysis
// members.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
AnalysisKey MachineFunctionAnalysis::Key;
bool MachineFunctionAnalysis::Result::invalidate(
Function &, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Unless it is invalidated explicitly, it should remain preserved.
auto PAC = PA.getChecker<MachineFunctionAnalysis>();
return !PAC.preservedWhenStateless();
}
MachineFunctionAnalysis::Result
MachineFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
auto &Context = F.getContext();
const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(F);
auto &MMI = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
.getCachedResult<MachineModuleAnalysis>(*F.getParent())
->getMMI();
auto MF = std::make_unique<MachineFunction>(
F, *TM, STI, MMI.getContext(), Context.generateMachineFunctionNum(F));
MF->initTargetMachineFunctionInfo(STI);
// MRI callback for target specific initializations.
TM->registerMachineRegisterInfoCallback(*MF);
return Result(std::move(MF));
}
PreservedAnalyses FreeMachineFunctionPass::run(Function &F,
FunctionAnalysisManager &FAM) {
FAM.clearAnalysis<MachineFunctionAnalysis>(F);
return PreservedAnalyses::all();
}