Skip to content

Commit a1b7e13

Browse files
Add new llvm.dbg.declare_value intrinsic.
For swift async code, we need to use a debug intrinsic that behaves like an llvm.dbg.declare but can take any location type rather than just a pointer or integer. To solve this, a new debug instrinsic called llvm.dbg.declare_value has been created, which behaves exactly like an llvm.dbg.declare but can take non pointer and integer location types.
1 parent 3f61402 commit a1b7e13

File tree

16 files changed

+151
-5
lines changed

16 files changed

+151
-5
lines changed

llvm/docs/SourceLevelDebugging.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,27 @@ directly, not its address. Note that the value operand of this intrinsic may
308308
be indirect (i.e, a pointer to the source variable), provided that interpreting
309309
the complex expression derives the direct value.
310310

311+
312+
``#dbg_declare_value``
313+
^^^^^^^^^^^^^^^^
314+
315+
.. code-block:: llvm
316+
317+
#dbg_declare_value([Value|MDNode], DILocalVariable, DIExpression, DILocation)
318+
319+
This record provides information about a local element (e.g., variable). The
320+
first argument is is the new value, and in the function entry block. The
321+
second argument is a :ref:`local variable <dilocalvariable>` containing a
322+
description of the variable. The third argument is a :ref:`complex expression
323+
<diexpression>`. The fourth argument is a :ref:`source location <dilocation>`.
324+
A ``#dbg_declare_value`` record describes describes the *value* of a source
325+
variable directly, not its address. The difference between a ``#dbg_value``
326+
and a ``#dbg_declare_value`` is that, just like a ``#dbg_declare``, a frontend
327+
should generate exactly one ``#dbg_declare_value`` record. The idea is to have
328+
``#dbg_declare`` guarantees but be able to describe a value rather than the
329+
address of a value.
330+
331+
311332
``#dbg_assign``
312333
^^^^^^^^^^^^^^^
313334
.. toctree::

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ enum FunctionCodes {
688688
FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE =
689689
64, // [DILocation, DILocalVariable, DIExpression, Value]
690690
FUNC_CODE_DEBUG_RECORD_LABEL = 65, // [DILocation, DILabel]
691+
FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE =
692+
66, // [DILocation, DILocalVariable, DIExpression, ValueAsMetadata]
691693
};
692694

693695
enum UseListCodes {

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,18 @@ namespace llvm {
11561156
DIExpression *Expr, const DILocation *DL,
11571157
InsertPosition InsertPt);
11581158

1159+
/// Insert a new llvm.dbg.declare_value intrinsic call.
1160+
/// \param Storage llvm::Value of the variable
1161+
/// \param VarInfo Variable's debug info descriptor.
1162+
/// \param Expr A complex location expression.
1163+
/// \param DL Debug info location.
1164+
/// \param InsertPt Location for the new intrinsic.
1165+
LLVM_ABI DbgInstPtr insertDeclareValue(llvm::Value *Storage,
1166+
DILocalVariable *VarInfo,
1167+
DIExpression *Expr,
1168+
const DILocation *DL,
1169+
InsertPosition InsertPt);
1170+
11591171
/// Insert a new llvm.dbg.label intrinsic call.
11601172
/// \param LabelInfo Label's debug info descriptor.
11611173
/// \param DL Debug info location.

llvm/include/llvm/IR/DebugInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Module;
4444
LLVM_ABI TinyPtrVector<DbgVariableRecord *> findDVRDeclares(Value *V);
4545
/// As above, for DVRValues.
4646
LLVM_ABI TinyPtrVector<DbgVariableRecord *> findDVRValues(Value *V);
47+
/// As above, for DVRDeclareValues.
48+
LLVM_ABI TinyPtrVector<DbgVariableRecord *> findDVRDeclareValues(Value *V);
4749

4850
/// Finds the debug info records describing a value.
4951
LLVM_ABI void

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
282282
Declare,
283283
Value,
284284
Assign,
285+
DeclareValue,
285286

286287
End, ///< Marks the end of the concrete types.
287288
Any, ///< To indicate all LocationTypes in searches.
@@ -364,6 +365,13 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
364365
createDVRDeclare(Value *Address, DILocalVariable *DV, DIExpression *Expr,
365366
const DILocation *DI, DbgVariableRecord &InsertBefore);
366367

368+
LLVM_ABI static DbgVariableRecord *
369+
createDVRDeclareValue(Value *Address, DILocalVariable *DV, DIExpression *Expr,
370+
const DILocation *DI);
371+
LLVM_ABI static DbgVariableRecord *
372+
createDVRDeclareValue(Value *Address, DILocalVariable *DV, DIExpression *Expr,
373+
const DILocation *DI, DbgVariableRecord &InsertBefore);
374+
367375
/// Iterator for ValueAsMetadata that internally uses direct pointer iteration
368376
/// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
369377
/// ValueAsMetadata .
@@ -414,6 +422,7 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
414422

415423
bool isDbgDeclare() const { return Type == LocationType::Declare; }
416424
bool isDbgValue() const { return Type == LocationType::Value; }
425+
bool isDbgDeclareValue() const { return Type == LocationType::DeclareValue; }
417426

418427
/// Get the locations corresponding to the variable referenced by the debug
419428
/// info intrinsic. Depending on the intrinsic, this could be the
@@ -439,12 +448,16 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
439448
bool hasValidLocation() const { return getVariableLocationOp(0) != nullptr; }
440449

441450
/// Does this describe the address of a local variable. True for dbg.addr
442-
/// and dbg.declare, but not dbg.value, which describes its value.
451+
/// and dbg.declare, but not dbg.value or dbg.declare_value, which describes
452+
/// its value.
443453
bool isAddressOfVariable() const { return Type == LocationType::Declare; }
444454

445455
/// Determine if this describes the value of a local variable. It is false for
446-
/// dbg.declare, but true for dbg.value, which describes its value.
447-
bool isValueOfVariable() const { return Type == LocationType::Value; }
456+
/// dbg.declare, but true for dbg.value and dbg.declare_value, which describes
457+
/// its value.
458+
bool isValueOfVariable() const {
459+
return Type == LocationType::Value || Type == LocationType::DeclareValue;
460+
}
448461

449462
LocationType getType() const { return Type; }
450463

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ lltok::Kind LLLexer::LexIdentifier() {
10071007
DBGRECORDTYPEKEYWORD(declare);
10081008
DBGRECORDTYPEKEYWORD(assign);
10091009
DBGRECORDTYPEKEYWORD(label);
1010+
DBGRECORDTYPEKEYWORD(declare_value);
10101011
#undef DBGRECORDTYPEKEYWORD
10111012

10121013
if (Keyword.starts_with("DIFlag")) {

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7155,7 +7155,8 @@ bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
71557155
.Case("declare", RecordKind::ValueKind)
71567156
.Case("value", RecordKind::ValueKind)
71577157
.Case("assign", RecordKind::ValueKind)
7158-
.Case("label", RecordKind::LabelKind);
7158+
.Case("label", RecordKind::LabelKind)
7159+
.Case("declare_value", RecordKind::ValueKind);
71597160

71607161
// Parsing labels is trivial; parse here and early exit, otherwise go into the
71617162
// full DbgVariableRecord processing stage.
@@ -7180,7 +7181,8 @@ bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
71807181
LocType ValueType = StringSwitch<LocType>(Lex.getStrVal())
71817182
.Case("declare", LocType::Declare)
71827183
.Case("value", LocType::Value)
7183-
.Case("assign", LocType::Assign);
7184+
.Case("assign", LocType::Assign)
7185+
.Case("declare_value", LocType::DeclareValue);
71847186

71857187
Lex.Lex();
71867188
if (parseToken(lltok::lparen, "Expected '(' here"))

llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ GetCodeName(unsigned CodeID, unsigned BlockID,
272272
STRINGIFY_CODE(FUNC_CODE, INST_CALLBR)
273273
STRINGIFY_CODE(FUNC_CODE, BLOCKADDR_USERS)
274274
STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_DECLARE)
275+
STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_DECLARE_VALUE)
275276
STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_VALUE)
276277
STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_ASSIGN)
277278
STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_VALUE_SIMPLE)

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6655,6 +6655,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
66556655
case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE:
66566656
case bitc::FUNC_CODE_DEBUG_RECORD_VALUE:
66576657
case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE:
6658+
case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE:
66586659
case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
66596660
// DbgVariableRecords are placed after the Instructions that they are
66606661
// attached to.
@@ -6671,6 +6672,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
66716672
// ..., Value
66726673
// dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)
66736674
// ..., LocationMetadata
6675+
// dbg_declare_value (FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE)
6676+
// ..., LocationMetadata
66746677
// dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)
66756678
// ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata
66766679
unsigned Slot = 0;
@@ -6712,6 +6715,11 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
67126715
DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL,
67136716
DbgVariableRecord::LocationType::Declare);
67146717
break;
6718+
case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE:
6719+
DVR = new DbgVariableRecord(
6720+
RawLocation, Var, Expr, DIL,
6721+
DbgVariableRecord::LocationType::DeclareValue);
6722+
break;
67156723
case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
67166724
DIAssignID *ID = cast<DIAssignID>(getFnMetadataByID(Record[Slot++]));
67176725
DIExpression *AddrExpr =

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,6 +3844,9 @@ void ModuleBitcodeWriter::writeFunction(
38443844
} else if (DVR.isDbgDeclare()) {
38453845
Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
38463846
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_DECLARE, Vals);
3847+
} else if (DVR.isDbgDeclareValue()) {
3848+
Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3849+
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE, Vals);
38473850
} else {
38483851
assert(DVR.isDbgAssign() && "Unexpected DbgRecord kind");
38493852
Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));

0 commit comments

Comments
 (0)