Skip to content

Commit

Permalink
CodeGen: Remove MachineFunctionAnalysis => Enable (Machine)ModulePasses
Browse files Browse the repository at this point in the history
Re-apply this commit with the deletion of a MachineFunction delegated to
a separate pass to avoid use after free when doing this directly in
AsmPrinter.

This patch removes the MachineFunctionAnalysis. Instead we keep a
map from IR Function to MachineFunction in the MachineModuleInfo.

This allows the insertion of ModulePasses into the codegen pipeline
without breaking it because the MachineFunctionAnalysis gets dropped
before a module pass.

Peak memory should stay unchanged without a ModulePass in the codegen
pipeline: Previously the MachineFunction was freed at the end of a codegen
function pipeline because the MachineFunctionAnalysis was dropped; With
this patch the MachineFunction is freed after the AsmPrinter has
finished.

Differential Revision: http://reviews.llvm.org/D23736

llvm-svn: 279564
  • Loading branch information
MatzeB committed Aug 23, 2016
1 parent 54690dc commit 4c1f1f1
Show file tree
Hide file tree
Showing 25 changed files with 115 additions and 171 deletions.
55 changes: 0 additions & 55 deletions llvm/include/llvm/CodeGen/MachineFunctionAnalysis.h

This file was deleted.

25 changes: 23 additions & 2 deletions llvm/include/llvm/CodeGen/MachineModuleInfo.h
Expand Up @@ -54,6 +54,7 @@ class BlockAddress;
class MDNode;
class MMIAddrLabelMap;
class MachineBasicBlock;
class MachineFunctionInitializer;
class MachineFunction;
class Module;
class PointerType;
Expand Down Expand Up @@ -107,6 +108,8 @@ class MachineModuleInfoImpl {
/// schemes and reformated for specific use.
///
class MachineModuleInfo : public ImmutablePass {
const TargetMachine &TM;

/// Context - This is the MCContext used for the entire code generator.
MCContext Context;

Expand Down Expand Up @@ -184,6 +187,14 @@ class MachineModuleInfo : public ImmutablePass {

EHPersonality PersonalityTypeCache;

MachineFunctionInitializer *MFInitializer;
/// Maps IR Functions to their corresponding MachineFunctions.
DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
/// Next unique number available for a MachineFunction.
unsigned NextFnNum = 0;
const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.

public:
static char ID; // Pass identification, replacement for typeid

Expand All @@ -202,8 +213,9 @@ class MachineModuleInfo : public ImmutablePass {

MachineModuleInfo(); // DUMMY CONSTRUCTOR, DO NOT CALL.
// Real constructor.
MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
const MCObjectFileInfo *MOFI);
MachineModuleInfo(const TargetMachine &TM, const MCAsmInfo &MAI,
const MCRegisterInfo &MRI, const MCObjectFileInfo *MOFI,
MachineFunctionInitializer *MFInitializer = nullptr);
~MachineModuleInfo() override;

// Initialization and Finalization
Expand All @@ -220,6 +232,15 @@ class MachineModuleInfo : public ImmutablePass {
void setModule(const Module *M) { TheModule = M; }
const Module *getModule() const { return TheModule; }

/// Returns the MachineFunction constructed for the IR function \p F.
/// Creates a new MachineFunction and runs the MachineFunctionInitializer
/// if none exists yet.
MachineFunction &getMachineFunction(const Function &F);

/// \brief Delete the MachineFunction \p MF and reset the link in the IR
/// Function to Machine Function map.
void deleteMachineFunctionFor(Function &F);

/// getInfo - Keep track of various per-function pieces of information for
/// backends that would like to do so.
///
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/Passes.h
Expand Up @@ -377,6 +377,9 @@ namespace llvm {

/// This pass performs software pipelining on machine instructions.
extern char &MachinePipelinerID;

/// This pass frees the memory occupied by the MachineFunction.
FunctionPass *createFreeMachineFunctionPass();
} // End llvm namespace

/// Target machine pass initializer for passes with dependencies. Use with
Expand Down
7 changes: 2 additions & 5 deletions llvm/include/llvm/Target/TargetMachine.h
Expand Up @@ -314,11 +314,8 @@ class LLVMTargetMachine : public TargetMachine {
bool DisableVerify = true) override;

/// Add MachineModuleInfo pass to pass manager.
MachineModuleInfo &addMachineModuleInfo(PassManagerBase &PM) const;

/// Add MachineFunctionAnalysis pass to pass manager.
void addMachineFunctionAnalysis(PassManagerBase &PM,
MachineFunctionInitializer *MFInitializer) const;
MachineModuleInfo &addMachineModuleInfo(PassManagerBase &PM,
MachineFunctionInitializer *MFI = nullptr) const;
};

} // End llvm namespace
Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Expand Up @@ -2571,8 +2571,6 @@ isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
return true;
}



GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
if (!S.usesMetadata())
return nullptr;
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/CodeGen/CMakeLists.txt
Expand Up @@ -59,7 +59,6 @@ add_llvm_library(LLVMCodeGen
MachineCSE.cpp
MachineDominanceFrontier.cpp
MachineDominators.cpp
MachineFunctionAnalysis.cpp
MachineFunction.cpp
MachineFunctionPass.cpp
MachineFunctionPrinterPass.cpp
Expand Down
18 changes: 7 additions & 11 deletions llvm/lib/CodeGen/LLVMTargetMachine.cpp
Expand Up @@ -15,7 +15,6 @@
#include "llvm/Analysis/Passes.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/BasicTTIImpl.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
Expand Down Expand Up @@ -103,19 +102,15 @@ TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
}

MachineModuleInfo &
LLVMTargetMachine::addMachineModuleInfo(PassManagerBase &PM) const {
MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
LLVMTargetMachine::addMachineModuleInfo(PassManagerBase &PM,
MachineFunctionInitializer *MFI) const {
MachineModuleInfo *MMI = new MachineModuleInfo(*this, *getMCAsmInfo(),
*getMCRegisterInfo(),
getObjFileLowering());
getObjFileLowering(), MFI);
PM.add(MMI);
return *MMI;
}

void LLVMTargetMachine::addMachineFunctionAnalysis(PassManagerBase &PM,
MachineFunctionInitializer *MFInitializer) const {
PM.add(new MachineFunctionAnalysis(*this, MFInitializer));
}

/// addPassesToX helper drives creation and initialization of TargetPassConfig.
static MCContext *
addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
Expand Down Expand Up @@ -150,8 +145,7 @@ addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,

PassConfig->addISelPrepare();

MachineModuleInfo &MMI = TM->addMachineModuleInfo(PM);
TM->addMachineFunctionAnalysis(PM, MFInitializer);
MachineModuleInfo &MMI = TM->addMachineModuleInfo(PM, MFInitializer);

// Enable FastISel with -fast, but allow that to be overridden.
TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
Expand Down Expand Up @@ -273,6 +267,7 @@ bool LLVMTargetMachine::addPassesToEmitFile(
return true;

PM.add(Printer);
PM.add(createFreeMachineFunctionPass());

return false;
}
Expand Down Expand Up @@ -319,6 +314,7 @@ bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
return true;

PM.add(Printer);
PM.add(createFreeMachineFunctionPass());

return false; // success!
}
62 changes: 0 additions & 62 deletions llvm/lib/CodeGen/MachineFunctionAnalysis.cpp

This file was deleted.

10 changes: 6 additions & 4 deletions llvm/lib/CodeGen/MachineFunctionPass.cpp
Expand Up @@ -22,7 +22,7 @@
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/StackProtector.h"
#include "llvm/IR/Dominators.h"
Expand All @@ -41,7 +41,9 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
if (F.hasAvailableExternallyLinkage())
return false;

MachineFunction &MF = getAnalysis<MachineFunctionAnalysis>().getMF();
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
MachineFunction &MF = MMI.getMachineFunction(F);

MachineFunctionProperties &MFProps = MF.getProperties();

#ifndef NDEBUG
Expand All @@ -65,8 +67,8 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
}

void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<MachineFunctionAnalysis>();
AU.addPreserved<MachineFunctionAnalysis>();
AU.addRequired<MachineModuleInfo>();
AU.addPreserved<MachineModuleInfo>();

// MachineFunctionPass preserves all LLVM IR passes, but there's no
// high-level way to express this. Instead, just list a bunch of
Expand Down

0 comments on commit 4c1f1f1

Please sign in to comment.