Skip to content

[CodeGen][NewPM] Port "RemoveRedundantDebugValues" to NPM #129005

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 2 commits into from
Mar 3, 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
25 changes: 25 additions & 0 deletions llvm/include/llvm/CodeGen/RemoveRedundantDebugValues.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===- llvm/CodeGen/RemoveRedundantDebugValues.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_REMOVEREDUNDANTDEBUGVALUES_H
#define LLVM_CODEGEN_REMOVEREDUNDANTDEBUGVALUES_H

#include "llvm/CodeGen/MachinePassManager.h"

namespace llvm {

class RemoveRedundantDebugValuesPass
: public PassInfoMixin<RemoveRedundantDebugValuesPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
};

} // namespace llvm

#endif // LLVM_CODEGEN_REMOVEREDUNDANTDEBUGVALUES_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void initializeRegionPrinterPass(PassRegistry &);
void initializeRegionViewerPass(PassRegistry &);
void initializeRegisterCoalescerLegacyPass(PassRegistry &);
void initializeRemoveLoadsIntoFakeUsesPass(PassRegistry &);
void initializeRemoveRedundantDebugValuesPass(PassRegistry &);
void initializeRemoveRedundantDebugValuesLegacyPass(PassRegistry &);
void initializeRenameIndependentSubregsLegacyPass(PassRegistry &);
void initializeReplaceWithVeclibLegacyPass(PassRegistry &);
void initializeResetMachineFunctionPass(PassRegistry &);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/Passes/CodeGenPassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "llvm/CodeGen/RegUsageInfoPropagate.h"
#include "llvm/CodeGen/RegisterCoalescerPass.h"
#include "llvm/CodeGen/RegisterUsageInfo.h"
#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
#include "llvm/CodeGen/RenameIndependentSubregs.h"
#include "llvm/CodeGen/ReplaceWithVeclib.h"
#include "llvm/CodeGen/SafeStack.h"
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass())
MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass())
MACHINE_FUNCTION_PASS("register-coalescer", RegisterCoalescerPass())
MACHINE_FUNCTION_PASS("rename-independent-subregs", RenameIndependentSubregsPass())
MACHINE_FUNCTION_PASS("remove-redundant-debug-values", RemoveRedundantDebugValuesPass())
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
RequireAllMachineFunctionPropertiesPass())
MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
Expand Down Expand Up @@ -266,7 +267,6 @@ DUMMY_MACHINE_FUNCTION_PASS("regalloc", RegAllocPass)
DUMMY_MACHINE_FUNCTION_PASS("regallocscoringpass", RegAllocScoringPass)
DUMMY_MACHINE_FUNCTION_PASS("regbankselect", RegBankSelectPass)
DUMMY_MACHINE_FUNCTION_PASS("remove-loads-into-fake-uses", RemoveLoadsIntoFakeUsesPass)
DUMMY_MACHINE_FUNCTION_PASS("removeredundantdebugvalues", RemoveRedundantDebugValuesPass)
DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass)
DUMMY_MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass)
DUMMY_MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass)
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 @@ -118,7 +118,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeRegUsageInfoPropagationLegacyPass(Registry);
initializeRegisterCoalescerLegacyPass(Registry);
initializeRemoveLoadsIntoFakeUsesPass(Registry);
initializeRemoveRedundantDebugValuesPass(Registry);
initializeRemoveRedundantDebugValuesLegacyPass(Registry);
initializeRenameIndependentSubregsLegacyPass(Registry);
initializeSafeStackLegacyPassPass(Registry);
initializeSelectOptimizePass(Registry);
Expand Down
59 changes: 39 additions & 20 deletions llvm/lib/CodeGen/RemoveRedundantDebugValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -33,14 +34,15 @@ STATISTIC(NumRemovedForward, "Number of DBG_VALUEs removed (forward scan)");

namespace {

class RemoveRedundantDebugValues : public MachineFunctionPass {
struct RemoveRedundantDebugValuesImpl {
bool reduceDbgValues(MachineFunction &MF);
};

class RemoveRedundantDebugValuesLegacy : public MachineFunctionPass {
public:
static char ID;

RemoveRedundantDebugValues();

bool reduceDbgValues(MachineFunction &MF);

RemoveRedundantDebugValuesLegacy();
/// Remove redundant debug value MIs for the given machine function.
bool runOnMachineFunction(MachineFunction &MF) override;

Expand All @@ -56,17 +58,18 @@ class RemoveRedundantDebugValues : public MachineFunctionPass {
// Implementation
//===----------------------------------------------------------------------===//

char RemoveRedundantDebugValues::ID = 0;
char RemoveRedundantDebugValuesLegacy::ID = 0;

char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValues::ID;
char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValuesLegacy::ID;

INITIALIZE_PASS(RemoveRedundantDebugValues, DEBUG_TYPE,
INITIALIZE_PASS(RemoveRedundantDebugValuesLegacy, DEBUG_TYPE,
"Remove Redundant DEBUG_VALUE analysis", false, false)

/// Default construct and initialize the pass.
RemoveRedundantDebugValues::RemoveRedundantDebugValues()
RemoveRedundantDebugValuesLegacy::RemoveRedundantDebugValuesLegacy()
: MachineFunctionPass(ID) {
initializeRemoveRedundantDebugValuesPass(*PassRegistry::getPassRegistry());
initializeRemoveRedundantDebugValuesLegacyPass(
*PassRegistry::getPassRegistry());
}

// This analysis aims to remove redundant DBG_VALUEs by going forward
Expand Down Expand Up @@ -199,7 +202,7 @@ static bool reduceDbgValsBackwardScan(MachineBasicBlock &MBB) {
return !DbgValsToBeRemoved.empty();
}

bool RemoveRedundantDebugValues::reduceDbgValues(MachineFunction &MF) {
bool RemoveRedundantDebugValuesImpl::reduceDbgValues(MachineFunction &MF) {
LLVM_DEBUG(dbgs() << "\nDebug Value Reduction\n");

bool Changed = false;
Expand All @@ -212,16 +215,32 @@ bool RemoveRedundantDebugValues::reduceDbgValues(MachineFunction &MF) {
return Changed;
}

bool RemoveRedundantDebugValues::runOnMachineFunction(MachineFunction &MF) {
// Skip functions without debugging information.
if (!MF.getFunction().getSubprogram())
bool RemoveRedundantDebugValuesLegacy::runOnMachineFunction(
MachineFunction &MF) {
// Skip functions without debugging information or functions from NoDebug
// compilation units.
if (!MF.getFunction().getSubprogram() ||
(MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
DICompileUnit::NoDebug))
return false;

// Skip functions from NoDebug compilation units.
if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
DICompileUnit::NoDebug)
return false;
return RemoveRedundantDebugValuesImpl().reduceDbgValues(MF);
}

bool Changed = reduceDbgValues(MF);
return Changed;
PreservedAnalyses
RemoveRedundantDebugValuesPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
// Skip functions without debugging information or functions from NoDebug
// compilation units.
if (!MF.getFunction().getSubprogram() ||
(MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
DICompileUnit::NoDebug))
return PreservedAnalyses::all();

if (!RemoveRedundantDebugValuesImpl().reduceDbgValues(MF))
return PreservedAnalyses::all();

auto PA = getMachineFunctionPassPreservedAnalyses();
PA.preserveSet<CFGAnalyses>();
return PA;
}
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
#include "llvm/CodeGen/RegUsageInfoPropagate.h"
#include "llvm/CodeGen/RegisterCoalescerPass.h"
#include "llvm/CodeGen/RegisterUsageInfo.h"
#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
#include "llvm/CodeGen/RenameIndependentSubregs.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SelectOptimize.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/MIR/X86/remove-redundant-dbg-vals.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: llc %s -o - -run-pass=removeredundantdebugvalues | FileCheck --implicit-check-not=DBG_VALUE %s
# RUN: llc %s -o - -passes=remove-redundant-debug-values | FileCheck --implicit-check-not=DBG_VALUE %s

## This checks that the RemoveRedundantDebugValues removes redundant
## DBG_VALUEs. The MIR was hand-written, and foo{[2-6]}() are just
Expand Down
Loading