Skip to content

Commit 3f6e4e5

Browse files
[IRTranslator][DebugInfo] Implement translation of entry_value vars
This commit implements IRTranslator lowering of dbg.declare intrinsics targeting swiftasync Arguments, by putting them in the MachineFunction's table of variables whose location doesn't change throughout the function. Depends on D149881 Differential Revision: https://reviews.llvm.org/D149882
1 parent 42bd814 commit 3f6e4e5

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CallLowering;
4141
class Constant;
4242
class ConstrainedFPIntrinsic;
4343
class DataLayout;
44+
class DbgDeclareInst;
4445
class Instruction;
4546
class MachineBasicBlock;
4647
class MachineFunction;
@@ -244,6 +245,14 @@ class IRTranslator : public MachineFunctionPass {
244245
bool translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
245246
MachineIRBuilder &MIRBuilder);
246247

248+
/// Returns the single livein physical register Arg was lowered to, if
249+
/// possible.
250+
std::optional<MCRegister> getArgPhysReg(Argument &Arg);
251+
252+
/// If DebugInst targets an Argument and its expression is an EntryValue,
253+
/// lower it as an entry in the MF debug table.
254+
bool translateIfEntryValueArgument(const DbgDeclareInst &DebugInst);
255+
247256
bool translateInlineAsm(const CallBase &CB, MachineIRBuilder &MIRBuilder);
248257

249258
/// Common code for translating normal calls or invokes.

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,37 @@ bool IRTranslator::translateConstrainedFPIntrinsic(
18791879
return true;
18801880
}
18811881

1882+
std::optional<MCRegister> IRTranslator::getArgPhysReg(Argument &Arg) {
1883+
auto VRegs = getOrCreateVRegs(Arg);
1884+
if (VRegs.size() != 1)
1885+
return std::nullopt;
1886+
1887+
// Arguments are lowered as a copy of a livein physical register.
1888+
auto *VRegDef = MF->getRegInfo().getVRegDef(VRegs[0]);
1889+
if (!VRegDef || !VRegDef->isCopy())
1890+
return std::nullopt;
1891+
return VRegDef->getOperand(1).getReg().asMCReg();
1892+
}
1893+
1894+
bool IRTranslator::translateIfEntryValueArgument(
1895+
const DbgDeclareInst &DebugInst) {
1896+
auto *Arg = dyn_cast<Argument>(DebugInst.getAddress());
1897+
if (!Arg)
1898+
return false;
1899+
1900+
const DIExpression *Expr = DebugInst.getExpression();
1901+
if (!Expr->isEntryValue())
1902+
return false;
1903+
1904+
std::optional<MCRegister> PhysReg = getArgPhysReg(*Arg);
1905+
if (!PhysReg)
1906+
return false;
1907+
1908+
MF->setVariableDbgInfo(DebugInst.getVariable(), Expr, *PhysReg,
1909+
DebugInst.getDebugLoc());
1910+
return true;
1911+
}
1912+
18821913
bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
18831914
MachineIRBuilder &MIRBuilder) {
18841915
if (auto *MI = dyn_cast<AnyMemIntrinsic>(&CI)) {
@@ -1945,12 +1976,16 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
19451976
// instructions (in fact, they get ignored if they *do* exist).
19461977
MF->setVariableDbgInfo(DI.getVariable(), DI.getExpression(),
19471978
getOrCreateFrameIndex(*AI), DI.getDebugLoc());
1948-
} else {
1949-
// A dbg.declare describes the address of a source variable, so lower it
1950-
// into an indirect DBG_VALUE.
1951-
MIRBuilder.buildIndirectDbgValue(getOrCreateVReg(*Address),
1952-
DI.getVariable(), DI.getExpression());
1979+
return true;
19531980
}
1981+
1982+
if (translateIfEntryValueArgument(DI))
1983+
return true;
1984+
1985+
// A dbg.declare describes the address of a source variable, so lower it
1986+
// into an indirect DBG_VALUE.
1987+
MIRBuilder.buildIndirectDbgValue(getOrCreateVReg(*Address),
1988+
DI.getVariable(), DI.getExpression());
19541989
return true;
19551990
}
19561991
case Intrinsic::dbg_label: {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; RUN: llc -O0 -global-isel -stop-after=irtranslator -verify-machineinstrs %s -o - | FileCheck %s
2+
3+
; CHECK: void @foo
4+
; CHECK-NEXT: dbg.declare(metadata {{.*}}, metadata ![[VAR:.*]], metadata ![[EXPR:.*]]), !dbg ![[LOC:.*]]
5+
; CHECK: entry_values:
6+
; CHECK-NEXT: entry-value-register: '$x22', debug-info-variable: '![[VAR]]', debug-info-expression: '![[EXPR]]',
7+
; CHECK-NEXT: debug-info-location: '![[LOC]]
8+
; CHECK-NEXT: entry-value-register: '$x22', debug-info-variable: '![[VAR]]', debug-info-expression: '![[EXPR]]'
9+
; CHECK-NEXT: debug-info-location: '![[LOC]]
10+
11+
; CHECK-NOT: DBG_VALUE
12+
13+
target triple="aarch64--"
14+
15+
define void @foo(ptr %unused_arg, ptr swiftasync %async_arg) !dbg !6 {
16+
call void @llvm.dbg.declare(metadata ptr %async_arg, metadata !12, metadata !DIExpression(DW_OP_LLVM_entry_value, 1)), !dbg !14
17+
call void @llvm.dbg.declare(metadata ptr %async_arg, metadata !12, metadata !DIExpression(DW_OP_LLVM_entry_value, 1)), !dbg !14
18+
call void @consume(ptr %async_arg)
19+
ret void, !dbg !15
20+
}
21+
22+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
23+
declare void @consume(ptr %ptr)
24+
25+
!llvm.dbg.cu = !{!0}
26+
!llvm.module.flags = !{!3, !4}
27+
28+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
29+
!1 = !DIFile(filename: "x.c", directory: "/")
30+
!3 = !{i32 2, !"Dwarf Version", i32 4}
31+
!4 = !{i32 2, !"Debug Info Version", i32 3}
32+
!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, unit: !0)
33+
!7 = !DISubroutineType(types: !8)
34+
!8 = !{null, !9, !9, !9}
35+
!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64)
36+
!10 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !11)
37+
!11 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
38+
!12 = !DILocalVariable(name: "a", scope: !6, file: !1, line: 1, type: !9)
39+
!14 = !DILocation(line: 1, column: 29, scope: !6)
40+
!15 = !DILocation(line: 1, column: 37, scope: !6)

0 commit comments

Comments
 (0)