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

Conversation

optimisan
Copy link
Contributor

No description provided.

Copy link
Contributor Author

optimisan commented Mar 17, 2025

@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-backend-arm

@llvm/pr-subscribers-debuginfo

Author: Akshat Oke (optimisan)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/131563.diff

6 Files Affected:

  • (modified) llvm/include/llvm/InitializePasses.h (+1-1)
  • (modified) llvm/include/llvm/Passes/CodeGenPassBuilder.h (+2-1)
  • (modified) llvm/include/llvm/Passes/MachinePassRegistry.def (+12-1)
  • (modified) llvm/lib/CodeGen/CodeGen.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp (+41-15)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 36be3d552f556..06fd686a4a43d 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -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 &);
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index 90a0cdf803560..c580f5a3eb3e9 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -43,6 +43,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"
@@ -999,7 +1000,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
   addPass(FuncletLayoutPass());
 
   addPass(StackMapLivenessPass());
-  addPass(LiveDebugValuesPass());
+  addPass(LiveDebugValuesPass(getTM<TargetMachine>().Options.ShouldEmitDebugEntryValues()));
   addPass(MachineSanitizerBinaryMetadata());
 
   if (TM.Options.EnableMachineOutliner &&
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index ebfdaf82169d8..77d9c41864424 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -195,6 +195,18 @@ MACHINE_FUNCTION_PASS("verify<machine-trace-metrics>", MachineTraceMetricsVerifi
 #define MACHINE_FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER,    \
                                           PARAMS)
 #endif
+
+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) {
@@ -262,7 +274,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)
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index b36d2f743d512..94ce0a072f91f 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -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);
diff --git a/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp b/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
index 484143a03fca1..585eff9a8417a 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
@@ -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"
@@ -63,12 +64,12 @@ 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;
@@ -77,36 +78,63 @@ class LiveDebugValues : public MachineFunctionPass {
     AU.setPreservesCFG();
     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,
+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();
+  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 = getAnalysisIfAvailable<TargetPassConfig>();
+  assert(TPC && "TargetPassConfig must be available");
+  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;
@@ -116,10 +144,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) {
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 2e62d6e6f4cc6..648d853b03016 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -102,6 +102,7 @@
 #include "llvm/CodeGen/InterleavedAccess.h"
 #include "llvm/CodeGen/InterleavedLoadCombine.h"
 #include "llvm/CodeGen/JMCInstrumenter.h"
+#include "llvm/CodeGen/LiveDebugValuesPass.h"
 #include "llvm/CodeGen/LiveDebugVariables.h"
 #include "llvm/CodeGen/LiveIntervals.h"
 #include "llvm/CodeGen/LiveRegMatrix.h"

Copy link

github-actions bot commented Mar 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@@ -102,6 +102,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)

@optimisan optimisan force-pushed the users/optimisan/remove-tpc-from-ldvimpl branch from bb1d6f1 to cd06f54 Compare March 17, 2025 05:47
@optimisan optimisan force-pushed the users/optimisan/port-live-debug-values branch from b4613ea to be6e70f Compare March 17, 2025 05:47
@optimisan optimisan force-pushed the users/optimisan/port-live-debug-values branch from 5c87dc3 to 768f70a Compare March 17, 2025 10:39
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

@arsenm arsenm requested a review from jmorse March 17, 2025 11:01
Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

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

LGTM, although I'm not especially familiar with the transition to the new pass manager.

MachineFunctionAnalysisManager &MFAM) {
if (!LiveDebugValues().run(MF, ShouldEmitDebugEntryValues))
return PreservedAnalyses::all();
auto PA = getMachineFunctionPassPreservedAnalyses();
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.

Base automatically changed from users/optimisan/remove-tpc-from-ldvimpl to main March 18, 2025 05:34
@optimisan optimisan force-pushed the users/optimisan/port-live-debug-values branch from 768f70a to 6404e9b Compare March 18, 2025 06:09
@optimisan optimisan force-pushed the users/optimisan/port-live-debug-values branch from ba355c0 to 7a01e19 Compare March 21, 2025 08:10
@optimisan optimisan merged commit 174110b into main Mar 24, 2025
11 checks passed
@optimisan optimisan deleted the users/optimisan/port-live-debug-values branch March 24, 2025 06:04
@llvm-ci
Copy link
Collaborator

llvm-ci commented Mar 24, 2025

LLVM Buildbot has detected a new failure on builder clang-cmake-x86_64-avx512-win running on avx512-intel64-win while building llvm at step 4 "cmake stage 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/81/builds/5578

Here is the relevant piece of the build log for the reference
Step 4 (cmake stage 1) failure: 'cmake -G ...' (failure)
'cmake' is not recognized as an internal or external command,
operable program or batch file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants