Skip to content

[CodeGen][NewPM] Port LiveDebugVariables to NPM #115468

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
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
68 changes: 62 additions & 6 deletions llvm/include/llvm/CodeGen/LiveDebugVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@
#define LLVM_CODEGEN_LIVEDEBUGVARIABLES_H

#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>

namespace llvm {

template <typename T> class ArrayRef;
class LiveIntervals;
class VirtRegMap;

class LiveDebugVariables : public MachineFunctionPass {
void *pImpl = nullptr;
class LiveDebugVariables {

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

class LDVImpl;
LiveDebugVariables();
~LiveDebugVariables() override;
~LiveDebugVariables();
LiveDebugVariables(LiveDebugVariables &&);

void analyze(MachineFunction &MF, LiveIntervals *LIS);
/// splitRegister - Move any user variables in OldReg to the live ranges in
/// NewRegs where they are live. Mark the values as unavailable where no new
/// register is live.
Expand All @@ -49,12 +52,39 @@ class LiveDebugVariables : public MachineFunctionPass {
/// @param VRM Rename virtual registers according to map.
void emitDebugValues(VirtRegMap *VRM);

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
/// dump - Print data structures to dbgs().
void dump() const;
#endif

void print(raw_ostream &OS) const;

void releaseMemory();

bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv);

private:
std::unique_ptr<LDVImpl> PImpl;
};

class LiveDebugVariablesWrapperLegacy : public MachineFunctionPass {
std::unique_ptr<LiveDebugVariables> Impl;

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

LiveDebugVariablesWrapperLegacy();

bool runOnMachineFunction(MachineFunction &) override;
void releaseMemory() override;

LiveDebugVariables &getLDV() { return *Impl; }
const LiveDebugVariables &getLDV() const { return *Impl; }

void releaseMemory() override {
if (Impl)
Impl->releaseMemory();
}
void getAnalysisUsage(AnalysisUsage &) const override;

MachineFunctionProperties getSetProperties() const override {
Expand All @@ -63,6 +93,32 @@ class LiveDebugVariables : public MachineFunctionPass {
}
};

class LiveDebugVariablesAnalysis
: public AnalysisInfoMixin<LiveDebugVariablesAnalysis> {
friend AnalysisInfoMixin<LiveDebugVariablesAnalysis>;
static AnalysisKey Key;

public:
using Result = LiveDebugVariables;

MachineFunctionProperties getSetProperties() const {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::TracksDebugUserValues);
}

Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add LiveDebugVariablesPrinterPass, LDVImpl has print method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print methods are hidden behind NDEBUG, and are invoked in runOnMachineFunction.

Is NPM moving away from printing debug info with DEBUG_TYPE?

Copy link
Contributor

@paperchalice paperchalice Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see it. DEBUG_TYPE and LLVM_DEBUG work in debug build and -debug-only options, a printer pass should work anyway, we could keep the LLVM_DEBUG in runOnMachineFunction as is and expose the hidden print method, if it doesn't call any API which is only available in debug mode.

class LiveDebugVariablesPrinterPass
: public PassInfoMixin<LiveDebugVariablesPrinterPass> {
raw_ostream &OS;

public:
LiveDebugVariablesPrinterPass(raw_ostream &OS) : OS(OS) {}

PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
};
} // end namespace llvm

#endif // LLVM_CODEGEN_LIVEDEBUGVARIABLES_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void initializeLegalizerPass(PassRegistry &);
void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &);
void initializeGISelKnownBitsAnalysisPass(PassRegistry &);
void initializeLiveDebugValuesPass(PassRegistry &);
void initializeLiveDebugVariablesPass(PassRegistry &);
void initializeLiveDebugVariablesWrapperLegacyPass(PassRegistry &);
void initializeLiveIntervalsWrapperPassPass(PassRegistry &);
void initializeLiveRangeShrinkPass(PassRegistry &);
void initializeLiveRegMatrixWrapperLegacyPass(PassRegistry &);
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass())
// computed. (We still either need to regenerate kill flags after regalloc, or
// preferably fix the scavenger to not depend on them).
MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
MACHINE_FUNCTION_ANALYSIS("livedebugvars", LiveDebugVariablesAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
Expand Down Expand Up @@ -146,6 +147,7 @@ MACHINE_FUNCTION_PASS("opt-phis", OptimizePHIsPass())
MACHINE_FUNCTION_PASS("peephole-opt", PeepholeOptimizerPass())
MACHINE_FUNCTION_PASS("phi-node-elimination", PHIEliminationPass())
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
MACHINE_FUNCTION_PASS("print<livedebugvars>", LiveDebugVariablesPrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<live-intervals>", LiveIntervalsPrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<live-vars>", LiveVariablesPrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<machine-block-freq>",
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeInterleavedAccessPass(Registry);
initializeJMCInstrumenterPass(Registry);
initializeLiveDebugValuesPass(Registry);
initializeLiveDebugVariablesPass(Registry);
initializeLiveDebugVariablesWrapperLegacyPass(Registry);
initializeLiveIntervalsWrapperPassPass(Registry);
initializeLiveRangeShrinkPass(Registry);
initializeLiveStacksPass(Registry);
Expand Down
Loading
Loading