diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h @@ -625,6 +625,8 @@ void visitInlineAsm(const CallBase &Call, const BasicBlock *EHPadBB = nullptr); + + bool visitEntryValueDbgValue(const DbgValueInst &I); void visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); void visitConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -5829,26 +5829,6 @@ if (!Op) return false; - // If the expression refers to the entry value of an Argument, use the - // corresponding livein physical register. As per the Verifier, this is only - // allowed for swiftasync Arguments. - if (Op->isReg() && Expr->isEntryValue()) { - assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync)); - auto OpReg = Op->getReg(); - for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins()) - if (OpReg == VirtReg || OpReg == PhysReg) { - SDDbgValue *SDV = DAG.getVRegDbgValue( - Variable, Expr, PhysReg, - Kind != FuncArgumentDbgValueKind::Value /*is indirect*/, DL, - SDNodeOrder); - DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/); - return true; - } - LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but " - "couldn't find a physical register\n"); - return true; - } - assert(Variable->isValidLocationForIntrinsic(DL) && "Expected inlined-at fields to agree"); MachineInstr *NewMI = nullptr; @@ -5937,6 +5917,41 @@ llvm_unreachable("expected corresponding call to preallocated setup/arg"); } +/// If DI is a debug value with an EntryValue expression, lower it using the +/// corresponding physical register of the associated Argument value +/// (guaranteed to exist by the verifier). +bool SelectionDAGBuilder::visitEntryValueDbgValue(const DbgValueInst &DI) { + DILocalVariable *Variable = DI.getVariable(); + DIExpression *Expr = DI.getExpression(); + if (!Expr->isEntryValue() || !hasSingleElement(DI.getValues())) + return false; + + // These properties are guaranteed by the verifier. + Argument *Arg = cast(DI.getValue(0)); + assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync)); + + auto ArgIt = FuncInfo.ValueMap.find(Arg); + if (ArgIt == FuncInfo.ValueMap.end()) { + LLVM_DEBUG( + dbgs() << "Dropping dbg.value: expression is entry_value but " + "couldn't find an associated register for the Argument\n"); + return true; + } + Register ArgVReg = ArgIt->getSecond(); + + for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins()) + if (ArgVReg == VirtReg || ArgVReg == PhysReg) { + SDDbgValue *SDV = + DAG.getVRegDbgValue(Variable, Expr, PhysReg, false /*IsIndidrect*/, + DI.getDebugLoc(), SDNodeOrder); + DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/); + return true; + } + LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but " + "couldn't find a physical register\n"); + return true; +} + /// Lower the call to the specified intrinsic function. void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { @@ -6266,6 +6281,9 @@ DIExpression *Expression = DI.getExpression(); dropDanglingDebugInfo(Variable, Expression); + if (visitEntryValueDbgValue(DI)) + return; + if (DI.isKillLocation()) { handleKillDebugValue(Variable, Expression, DI.getDebugLoc(), SDNodeOrder); return; diff --git a/llvm/test/CodeGen/AArch64/dbg-value-swift-async-non-entry-block.ll b/llvm/test/CodeGen/AArch64/dbg-value-swift-async-non-entry-block.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/dbg-value-swift-async-non-entry-block.ll @@ -0,0 +1,49 @@ +; RUN: llc --mtriple="aarch64-" -O0 -global-isel -stop-after=irtranslator -verify-machineinstrs %s -o - | FileCheck %s --check-prefix=AARCH +; RUN: llc --mtriple="aarch64-" -O0 -fast-isel=false -global-isel=false -stop-after=finalize-isel %s -o - | FileCheck %s --check-prefix=AARCH +; RUN: llc --mtriple="aarch64-" -O0 -fast-isel -stop-after=finalize-isel %s -o - | FileCheck %s --check-prefix=AARCH + + +; RUN: llc --mtriple="x86_64-" -O0 -fast-isel=false -global-isel=false -stop-after=finalize-isel %s -o - | FileCheck %s --check-prefix=X86 +; RUN: llc --mtriple="x86_64-" -O0 -fast-isel -stop-after=finalize-isel %s -o - | FileCheck %s --check-prefix=X86 + +; AARCH-NOT: DBG_VALUE +; AARCH: DBG_VALUE $x22, $noreg, !{{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1) +; AARCH-NEXT: DBG_VALUE $x22, $noreg, !{{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1) +; AARCH-NOT: DBG_VALUE + +; X86-NOT: DBG_VALUE +; X86: DBG_VALUE $r14, $noreg, !{{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1) +; X86-NEXT: DBG_VALUE $r14, $noreg, !{{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1) +; X86-NOT: DBG_VALUE + + +define void @foo(ptr %unused_arg, ptr swiftasync %async_arg) !dbg !6 { +entry: + br label %other + +other: + call void @llvm.dbg.value(metadata ptr %async_arg, metadata !12, metadata !DIExpression(DW_OP_LLVM_entry_value, 1)), !dbg !14 + call void @llvm.dbg.value(metadata ptr %async_arg, metadata !12, metadata !DIExpression(DW_OP_LLVM_entry_value, 1)), !dbg !14 + call void @consume(ptr %async_arg) + ret void, !dbg !15 +} + +declare void @llvm.dbg.value(metadata, metadata, metadata) +declare void @consume(ptr %ptr) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug) +!1 = !DIFile(filename: "x.c", directory: "/") +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, unit: !0) +!7 = !DISubroutineType(types: !8) +!8 = !{null, !9, !9, !9} +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!10 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !11) +!11 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!12 = !DILocalVariable(name: "a", scope: !6, file: !1, line: 1, type: !9) +!14 = !DILocation(line: 1, column: 29, scope: !6) +!15 = !DILocation(line: 1, column: 37, scope: !6)