Skip to content

[RemoveDIs] Enable DPLabels conversion [3b/3] #82639

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
Feb 23, 2024

Conversation

OCHyams
Copy link
Contributor

@OCHyams OCHyams commented Feb 22, 2024

Enables conversion between llvm.dbg.label and DPLabel.

@OCHyams OCHyams requested review from jmorse and SLTozer February 22, 2024 15:48
@llvmbot
Copy link
Member

llvmbot commented Feb 22, 2024

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-ir

Author: Orlando Cazalet-Hyams (OCHyams)

Changes

Enables conversion between llvm.dbg.label and DPLabel.


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

1 Files Affected:

  • (modified) llvm/lib/IR/BasicBlock.cpp (+20-3)
diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index 0680754444f17f..8a5bfdb0506854 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -81,6 +81,12 @@ void BasicBlock::convertToNewDbgValues() {
       continue;
     }
 
+    if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(&I)) {
+      DPVals.push_back(new DPLabel(DLI->getLabel(), DLI->getDebugLoc()));
+      DLI->eraseFromParent();
+      continue;
+    }
+
     if (DPVals.empty())
       continue;
 
@@ -108,15 +114,26 @@ void BasicBlock::convertFromNewDbgValues() {
 
     DPMarker &Marker = *Inst.DbgMarker;
     for (DbgRecord &DR : Marker.getDbgValueRange()) {
-      if (auto *DPV = dyn_cast<DPValue>(&DR))
+      if (auto *DPV = dyn_cast<DPValue>(&DR)) {
         InstList.insert(Inst.getIterator(),
                         DPV->createDebugIntrinsic(getModule(), nullptr));
-      else
+      } else if (auto *DPL = dyn_cast<DPLabel>(&DR)) {
+        auto *LabelFn =
+            Intrinsic::getDeclaration(getModule(), Intrinsic::dbg_label);
+        Value *Args[] = {
+            MetadataAsValue::get(getModule()->getContext(), DPL->getLabel())};
+        DbgLabelInst *DbgLabel = cast<DbgLabelInst>(
+            CallInst::Create(LabelFn->getFunctionType(), LabelFn, Args));
+        DbgLabel->setTailCall();
+        DbgLabel->setDebugLoc(DPL->getDebugLoc());
+        InstList.insert(Inst.getIterator(), DbgLabel);
+      } else {
         llvm_unreachable("unsupported DbgRecord kind");
+      }
     }
 
     Marker.eraseFromParent();
-  };
+  }
 
   // Assume no trailing DPValues: we could technically create them at the end
   // of the block, after a terminator, but this would be non-cannonical and

OCHyams added a commit that referenced this pull request Feb 23, 2024
Patch 2 of 3 to add llvm.dbg.label support to the RemoveDIs project. The
patch stack adds the DPLabel class, which is the RemoveDIs llvm.dbg.label
equivalent.

   1. Add DbgRecord base class for DPValue and the not-yet-added
       DPLabel class.
   2. Add the DPLabel class.
-> 3. Add support to passes.

The next patch, #82639, will enable conversion between dbg.labels and DPLabels.

AssignemntTrackingAnalysis support could have gone two ways:

1. Have the analysis store a DPLabel representation in its results -
   SelectionDAGBuilder reads the analysis results and ignores all DbgRecord
   kinds.
2. Ignore DPLabels in the analysis - SelectionDAGBuilder reads the analysis
   results but still needs to iterate over DPLabels from the IR.

I went with option 2 because it's less work and is no less correct than 1. It's
worth noting that causes labels to sink to the bottom of packs of debug records.
e.g., [value, label, value] becomes [value, value, label]. This shouldn't be a
problem because labels and variable locations don't have an ordering requirement.
The ordering between variable locations is maintained and the label movement is
deterministic
@OCHyams OCHyams force-pushed the ddd/labels-upstream-new-4 branch from 86fc290 to f4ebc40 Compare February 23, 2024 11:46
Comment on lines 121 to 129
auto *LabelFn =
Intrinsic::getDeclaration(getModule(), Intrinsic::dbg_label);
Value *Args[] = {
MetadataAsValue::get(getModule()->getContext(), DPL->getLabel())};
DbgLabelInst *DbgLabel = cast<DbgLabelInst>(
CallInst::Create(LabelFn->getFunctionType(), LabelFn, Args));
DbgLabel->setTailCall();
DbgLabel->setDebugLoc(DPL->getDebugLoc());
InstList.insert(Inst.getIterator(), DbgLabel);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we make createDebugIntrinsic a dispatch method that covers all debug records?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM - done

@OCHyams OCHyams merged commit 71d47a0 into llvm:main Feb 23, 2024
slackito added a commit that referenced this pull request Feb 24, 2024
This reverts commit 71d47a0 because
it causes clang to crash in some cases. See repro posted at
71d47a0
OCHyams added a commit that referenced this pull request Feb 26, 2024
Enables conversion between llvm.dbg.label and DPLabel.
@OCHyams OCHyams deleted the ddd/labels-upstream-new-4 branch May 27, 2025 16:01
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.

3 participants