Skip to content

[CodeGen][NPM] Port LiveDebugValues to NPM #131563

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 4 commits into from
Mar 24, 2025
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
32 changes: 32 additions & 0 deletions llvm/include/llvm/CodeGen/LiveDebugValuesPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===- llvm/CodeGen/LiveDebugValuesPass.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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_LIVEDEBUGVALUESPASS_H
#define LLVM_CODEGEN_LIVEDEBUGVALUESPASS_H

#include "llvm/CodeGen/MachinePassManager.h"

namespace llvm {

class LiveDebugValuesPass : public PassInfoMixin<LiveDebugValuesPass> {
const bool ShouldEmitDebugEntryValues;

public:
LiveDebugValuesPass(bool ShouldEmitDebugEntryValues)
: ShouldEmitDebugEntryValues(ShouldEmitDebugEntryValues) {}

PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);

void printPipeline(raw_ostream &OS,
function_ref<StringRef(StringRef)> MapClassName2PassName);
};

} // namespace llvm

#endif // LLVM_CODEGEN_LIVEDEBUGVALUESPASS_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void initializeLegacyLICMPassPass(PassRegistry &);
void initializeLegalizerPass(PassRegistry &);
void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &);
void initializeGISelKnownBitsAnalysisPass(PassRegistry &);
void initializeLiveDebugValuesPass(PassRegistry &);
void initializeLiveDebugValuesLegacyPass(PassRegistry &);
void initializeLiveDebugVariablesWrapperLegacyPass(PassRegistry &);
void initializeLiveIntervalsWrapperPassPass(PassRegistry &);
void initializeLiveRangeShrinkPass(PassRegistry &);
Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/Passes/CodeGenPassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "llvm/CodeGen/InterleavedAccess.h"
#include "llvm/CodeGen/InterleavedLoadCombine.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
#include "llvm/CodeGen/LiveDebugValuesPass.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
#include "llvm/CodeGen/LowerEmuTLS.h"
Expand Down Expand Up @@ -1002,7 +1003,8 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
addPass(FuncletLayoutPass());

addPass(StackMapLivenessPass());
addPass(LiveDebugValuesPass());
addPass(LiveDebugValuesPass(
getTM<TargetMachine>().Options.ShouldEmitDebugEntryValues()));
addPass(MachineSanitizerBinaryMetadata());

if (TM.Options.EnableMachineOutliner &&
Expand Down
12 changes: 11 additions & 1 deletion llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ MACHINE_FUNCTION_PASS_WITH_PARAMS(
},
"enable-tail-merge")

MACHINE_FUNCTION_PASS_WITH_PARAMS(
"live-debug-values", "LiveDebugValuesPass",
[](bool ShouldEmitDebugEntryValues) {
return LiveDebugValuesPass(ShouldEmitDebugEntryValues);
},
[](StringRef Params) {
return parseSinglePassOption(Params, "emit-debug-entry-values",
"LiveDebugValuesPass");
},
"emit-debug-entry-values")

MACHINE_FUNCTION_PASS_WITH_PARAMS(
"machine-sink", "MachineSinkingPass",
[](bool EnableSinkAndFold) {
Expand Down Expand Up @@ -278,7 +289,6 @@ DUMMY_MACHINE_FUNCTION_PASS("instruction-select", InstructionSelectPass)
DUMMY_MACHINE_FUNCTION_PASS("irtranslator", IRTranslatorPass)
DUMMY_MACHINE_FUNCTION_PASS("kcfi", MachineKCFIPass)
DUMMY_MACHINE_FUNCTION_PASS("legalizer", LegalizerPass)
DUMMY_MACHINE_FUNCTION_PASS("livedebugvalues", LiveDebugValuesPass)
DUMMY_MACHINE_FUNCTION_PASS("lrshrink", LiveRangeShrinkPass)
DUMMY_MACHINE_FUNCTION_PASS("machine-combiner", MachineCombinerPass)
DUMMY_MACHINE_FUNCTION_PASS("static-data-splitter", StaticDataSplitter)
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 @@ -58,7 +58,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeInterleavedLoadCombinePass(Registry);
initializeInterleavedAccessPass(Registry);
initializeJMCInstrumenterPass(Registry);
initializeLiveDebugValuesPass(Registry);
initializeLiveDebugValuesLegacyPass(Registry);
initializeLiveDebugVariablesWrapperLegacyPass(Registry);
initializeLiveIntervalsWrapperPassPass(Registry);
initializeLiveRangeShrinkPass(Registry);
Expand Down
63 changes: 47 additions & 16 deletions llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "LiveDebugValues.h"

#include "llvm/CodeGen/LiveDebugValuesPass.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
Expand Down Expand Up @@ -63,50 +64,82 @@ namespace {
/// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
/// InstrRefBasedLDV to perform location propagation, via the LDVImpl
/// base class.
class LiveDebugValues : public MachineFunctionPass {
class LiveDebugValuesLegacy : public MachineFunctionPass {
public:
static char ID;

LiveDebugValues();
~LiveDebugValues() = default;
LiveDebugValuesLegacy();
~LiveDebugValuesLegacy() = default;

/// Calculate the liveness information for the given machine function.
bool runOnMachineFunction(MachineFunction &MF) override;

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<TargetPassConfig>();
MachineFunctionPass::getAnalysisUsage(AU);
}
};

struct LiveDebugValues {
LiveDebugValues();
~LiveDebugValues() = default;
bool run(MachineFunction &MF, bool ShouldEmitDebugEntryValues);

private:
std::unique_ptr<LDVImpl> InstrRefImpl;
std::unique_ptr<LDVImpl> VarLocImpl;
TargetPassConfig *TPC = nullptr;
MachineDominatorTree MDT;
};
} // namespace

char LiveDebugValues::ID = 0;
char LiveDebugValuesLegacy::ID = 0;

char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
char &llvm::LiveDebugValuesID = LiveDebugValuesLegacy::ID;

INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", false,
false)
INITIALIZE_PASS(LiveDebugValuesLegacy, DEBUG_TYPE, "Live DEBUG_VALUE analysis",
false, false)

/// Default construct and initialize the pass.
LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
LiveDebugValuesLegacy::LiveDebugValuesLegacy() : MachineFunctionPass(ID) {
initializeLiveDebugValuesLegacyPass(*PassRegistry::getPassRegistry());
}

LiveDebugValues::LiveDebugValues() {
InstrRefImpl =
std::unique_ptr<LDVImpl>(llvm::makeInstrRefBasedLiveDebugValues());
VarLocImpl = std::unique_ptr<LDVImpl>(llvm::makeVarLocBasedLiveDebugValues());
}

bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
PreservedAnalyses
LiveDebugValuesPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
if (!LiveDebugValues().run(MF, ShouldEmitDebugEntryValues))
return PreservedAnalyses::all();
auto PA = getMachineFunctionPassPreservedAnalyses();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would hope this preserves everything

Copy link
Contributor

Choose a reason for hiding this comment

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

But this just copies what the old PM does

Copy link
Member

Choose a reason for hiding this comment

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

As far as I'm aware this should preserve all information in analyses -- LDV will insert new DBG_VALUE instructions and should do absolutely nothing else.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, should fix both PMs in a follow up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, will do

PA.preserveSet<CFGAnalyses>();
return PA;
}

void LiveDebugValuesPass::printPipeline(
raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
OS << MapClassName2PassName(name());
if (ShouldEmitDebugEntryValues)
OS << "<emit-debug-entry-values>";
}

bool LiveDebugValuesLegacy::runOnMachineFunction(MachineFunction &MF) {
auto *TPC = &getAnalysis<TargetPassConfig>();
return LiveDebugValues().run(
MF, TPC->getTM<TargetMachine>().Options.ShouldEmitDebugEntryValues());
}

bool LiveDebugValues::run(MachineFunction &MF,
bool ShouldEmitDebugEntryValues) {
bool InstrRefBased = MF.useDebugInstrRef();
// Allow the user to force selection of InstrRef LDV.
InstrRefBased |= ForceInstrRefLDV;

TPC = getAnalysisIfAvailable<TargetPassConfig>();
LDVImpl *TheImpl = &*VarLocImpl;

MachineDominatorTree *DomTree = nullptr;
Expand All @@ -116,10 +149,8 @@ bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
TheImpl = &*InstrRefImpl;
}

return TheImpl->ExtendRanges(
MF, DomTree,
TPC->getTM<TargetMachine>().Options.ShouldEmitDebugEntryValues(),
InputBBLimit, InputDbgValueLimit);
return TheImpl->ExtendRanges(MF, DomTree, ShouldEmitDebugEntryValues,
InputBBLimit, InputDbgValueLimit);
}

bool llvm::debuginfoShouldUseDebugInstrRef(const Triple &T) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#include "llvm/CodeGen/InterleavedAccess.h"
#include "llvm/CodeGen/InterleavedLoadCombine.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
#include "llvm/CodeGen/LiveDebugValuesPass.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Dead include?

Copy link
Contributor Author

@optimisan optimisan Mar 17, 2025

Choose a reason for hiding this comment

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

whoops forgot to add the new header file again. (updated now)

#include "llvm/CodeGen/LiveDebugVariables.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/LiveRegMatrix.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/ARM/dbg-range-extension.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: llc -mtriple=arm-eabi -run-pass=livedebugvalues %s -o - | FileCheck %s
# RUN: llc -mtriple=arm-eabi -passes=live-debug-values %s -o - | FileCheck %s
#
# Check that the debug information for variables are propagated into the correct blocks.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# RUN: llc -o - %s -O0 -regalloc=fast -run-pass=livedebugvalues | \
# RUN: FileCheck %s -implicit-check-not=DBG_VALUE

# RUN: llc -o - %s -O0 -regalloc=fast -passes=live-debug-values | \
# RUN: FileCheck %s -implicit-check-not=DBG_VALUE
--- |
target triple = "arm64-apple-ios12.1.0"

Expand Down