[llvm] [DebugInfo][RemoveDIs] Add documentation for updating code to handle debug records (PR #93562)
J. Ryan Stinnett via llvm-commits
llvm-commits at lists.llvm.org
Thu May 30 08:24:44 PDT 2024
================
@@ -106,21 +106,204 @@ Not shown are the links from DbgRecord to other parts of the `Value`/`Metadata`
The various kinds of debug intrinsic (value, declare, assign, label) are all stored in `DbgRecord` subclasses, with a "RecordKind" field distinguishing `DbgLabelRecord`s from `DbgVariableRecord`s, and a `LocationType` field in the `DbgVariableRecord` class further disambiguating the various debug variable intrinsics it can represent.
-## Finding debug info records
+# How to update existing code
+
+Any existing code that interacts with debug intrinsics in some way will need to be updated to interact with debug records in the same way. A few quick rules to keep in mind when updating code:
+
+- Debug records will not be seen when iterating over instructions; to find the debug records that appear immediately before an instruction, you'll need to iterate over `Instruction::getDbgRecordRange()`.
+- Debug records have interfaces that are identical to those of debug intrinsics, meaning that any code that operates on debug intrinsics can be trivially applied to debug records as well. The exceptions for this are Instruction or CallInst methods that don't logically apply to debug records, and `isa/cast/dyn_cast` methods, are replaced by methods on the `DbgRecord` class itself.
+- Debug records cannot appear in a module that also contains debug intrinsics; the two are mutually exclusive. As debug records are the future format, handling records correctly should be prioritized in new code.
+- Until support for intrinsics is no longer present, a valid hotfix for code that only handles debug intrinsics and is non-trivial to update is to convert the module to the intrinsic format using `Module::setIsNewDbgInfoFormat`, and convert it back afterwards.
+ - This can also be performed within a lexical scope for a module or an individual function using the class `ScopedDbgInfoFormatSetter`:
+ ```
+ void handleModule(Module &M) {
+ {
+ ScopedDbgInfoFormatSetter FormatSetter(M, false);
+ handleModuleWithDebugIntrinsics(M);
+ }
+ // Module returns to previous debug info format after exiting the above block.
+ }
+ ```
+
+Below is a rough guide on how existing code that currently supports debug intrinsics can be updated to support debug records.
+
+## Creating debug records
+
+Debug records will automatically be created by the `DIBuilder` class when the new format is enabled. As with instructions, it is also possible to call `DbgRecord::clone` to create an unattached copy of an existing record.
+
+## Skipping debug records, ignoring debug-uses of Values, stably counting instructions...
+
+This will all happen transparently without needing to think about it!
+
+```
+for (Instruction &I : BB) {
+ // Old: Skips debug intrinsics
+ if (isa<DbgInfoIntrinsic>(&I))
+ continue;
+ // New: No extra code needed, debug records are skipped by default.
+ ...
+}
+```
+
+## Finding debug records
Utilities such as `findDbgUsers` and the like now have an optional argument that will return the set of `DbgVariableRecord` records that refer to a `Value`. You should be able to treat them the same as intrinsics.
-## Examining debug info records at positions
+```
+// Old:
+ SmallVector<DbgVariableIntrinsic *> DbgUsers;
+ findDbgUsers(DbgUsers, V);
+ for (auto *DVI : DbgUsers) {
+ if (DVI->getParent() != BB)
+ DVI->replaceVariableLocationOp(V, New);
+ }
+// New:
+ SmallVector<DbgVariableIntrinsic *> DbgUsers;
+ SmallVector<DbgVariableRecord *> DVRUsers;
+ findDbgUsers(DbgUsers, V, &DVRUsers);
+ for (auto *DVI : DbgUsers)
+ if (DVI->getParent() != BB)
+ DVI->replaceVariableLocationOp(V, New);
+ for (auto *DVR : DVRUsers)
+ if (DVR->getParent() != BB)
+ DVR->replaceVariableLocationOp(V, New);
+```
+
+## Examining debug records at positions
Call `Instruction::getDbgRecordRange()` to get the range of `DbgRecord` objects that are attached to an instruction.
-## Moving around, deleting
+```
+for (Instruction &I : BB) {
+ // Old: Uses a data member of a debug intrinsic, and then skips to the next
+ // instruction.
+ if (DbgInfoIntrinsic *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
+ recordDebugLocation(DII->getDebugLoc());
+ continue;
+ }
+ // New: Iterates over the debug records that appear before `I`, and treats
+ // them identically to the intrinsic block above.
+ // NB: This should always appear at the top of the for-loop, so that we
+ // process the debug records preceding `I` before `I` itself.
+ for (DbgRecord &DR = I.getDbgRecordRange()) {
+ recordDebugLocation(DR.getDebugLoc());
+ }
+ processInstruction(I);
+}
+```
+
+This can also be passed through the function `filterDbgVars` to specifically
+iterate over DbgVariableRecords, which are more commonly used.
-You can use `DbgRecord::removeFromParent` to unlink a `DbgRecord` from it's marker, and then `BasicBlock::insertDbgRecordBefore` or `BasicBlock::insertDbgRecordAfter` to re-insert the `DbgRecord` somewhere else. You cannot insert a `DbgRecord` at an arbitary point in a list of `DbgRecord`s (if you're doing this with `dbg.value`s then it's unlikely to be correct).
+```
+for (Instruction &I : BB) {
+ // Old: If `I` is a DbgVariableIntrinsic we record the variable, and apply
+ // extra logic if it is an `llvm.dbg.declare`.
+ if (DbgVariableIntrinsic *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
+ recordVariable(DVI->getVariable());
+ if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(DVI))
+ recordDeclareAddress(DDI->getAddress());
+ continue;
+ }
+ // New: `filterDbgVars` is used to iterate over only DbgVariableRecords.
+ for (DbgVariableRecord &DVR = filterDbgVars(I.getDbgRecordRange())) {
+ recordVariable(DVR.getVariable());
+ // Debug variable records are not cast to subclasses; simply call the
+ // appropriate `isDbgX()` check, and use the methods as normal.
+ if (DVR.isDbgDeclare())
+ recordDeclareAddress(DVR.getAddress());
+ }
+ // ...
+}
+```
+
+## Processing individual debug records
-Erase `DbgRecord`s by calling `eraseFromParent` or `deleteInstr` if it's already been removed.
+In most cases, any code that operates on debug intrinsics can be extracted to a template function or auto lambda (if it is not already in one) that can be applied to both debug intrinsics and debug records - though keep in mind the main exception that isa/cast/dyn_cast do not apply to DbgVariableRecord types.
----------------
jryans wrote:
```suggestion
In most cases, any code that operates on debug intrinsics can be extracted to a template function or auto lambda (if it is not already in one) that can be applied to both debug intrinsics and debug records - though keep in mind the main exception that `isa`/`cast`/`dyn_cast` do not apply to `DbgVariableRecord` types.
```
https://github.com/llvm/llvm-project/pull/93562
More information about the llvm-commits
mailing list