1 From e7c9a6cae09d99388d8384ca7c0fb5b24b353975 Mon Sep 17 00:00:00 2001
2 From: Nikita Popov <npopov@redhat.com>
3 Date: Mon, 17 Jan 2022 15:48:01 +0100
4 Subject: [PATCH] [SDAG] Don't move DBG_VALUE instructions after insertion
5 point during scheduling (PR53243)
7 EmitSchedule() shouldn't be touching instructions after the provided
8 insertion point. The change introduced in D83561 performs a scan to
9 the end of the block, and thus may move unrelated instructions. In
10 particular, this ends up moving instructions that have been produced
11 by FastISel and will later be deleted. Moving them means that more
12 instructions than intended are removed.
14 Fix this by stopping the iteration when the insertion point is
17 Fixes https://github.com/llvm/llvm-project/issues/53243.
19 Differential Revision: https://reviews.llvm.org/D117489
21 .../SelectionDAG/ScheduleDAGSDNodes.cpp | 7 ++--
22 .../CodeGen/X86/pr53243-tail-call-fastisel.ll | 39 +++++++++++++++++++
23 2 files changed, 43 insertions(+), 3 deletions(-)
24 create mode 100644 llvm/test/CodeGen/X86/pr53243-tail-call-fastisel.ll
26 diff --git a/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp b/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
27 index bec240d6c4d4..403f34573899 100644
28 --- a/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
29 +++ b/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
30 @@ -1057,12 +1057,13 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
31 "first terminator cannot be a debug value");
32 for (MachineInstr &MI : make_early_inc_range(
33 make_range(std::next(FirstTerm), InsertBB->end()))) {
34 + // Only scan up to insertion point.
35 + if (&MI == InsertPos)
38 if (!MI.isDebugValue())
41 - if (&MI == InsertPos)
42 - InsertPos = std::prev(InsertPos->getIterator());
44 // The DBG_VALUE was referencing a value produced by a terminator. By
45 // moving the DBG_VALUE, the referenced value also needs invalidating.
46 MI.getOperand(0).ChangeToRegister(0, false);
47 diff --git a/llvm/test/CodeGen/X86/pr53243-tail-call-fastisel.ll b/llvm/test/CodeGen/X86/pr53243-tail-call-fastisel.ll
49 index 000000000000..333eff8fb008
51 +++ b/llvm/test/CodeGen/X86/pr53243-tail-call-fastisel.ll
53 +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
54 +; RUN: llc -O0 -fast-isel -mtriple=x86_64-- < %s | FileCheck %s
56 +define void @test() {
59 +; CHECK-NEXT: jmp set_state@PLT # TAILCALL
60 + tail call void @set_state()
61 + call void @llvm.dbg.value(metadata i64 0, metadata !10, metadata !DIExpression()), !dbg !16
65 +declare void @set_state()
67 +; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
68 +declare void @llvm.dbg.value(metadata, metadata, metadata) #0
70 +attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
72 +!llvm.module.flags = !{!0}
75 +!0 = !{i32 2, !"Debug Info Version", i32 3}
76 +!1 = distinct !DICompileUnit(language: DW_LANG_Rust, file: !2, producer: "clang LLVM (rustc version 1.60.0-nightly (ec4bcaac4 2022-01-15))", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !3)
77 +!2 = !DIFile(filename: "src/lib.rs/@/bug.63e521cd-cgu.0", directory: "/tmp/rust-bug")
79 +!4 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "Option", file: !5, baseType: !6, size: 8, align: 8, flags: DIFlagEnumClass, elements: !7)
80 +!5 = !DIFile(filename: "<unknown>", directory: "")
81 +!6 = !DIBasicType(name: "u8", size: 8, encoding: DW_ATE_unsigned)
83 +!8 = !DIEnumerator(name: "None", value: 0)
84 +!9 = !DIEnumerator(name: "Some", value: 1)
85 +!10 = !DILocalVariable(name: "msg", arg: 2, scope: !11, file: !12, line: 689, type: !6)
86 +!11 = distinct !DISubprogram(name: "expect<()>", linkageName: "_ZN4core6option15Option$LT$T$GT$6expect17h9a574c18f194c213E", scope: !4, file: !12, line: 689, type: !13, scopeLine: 689, flags: DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !1, templateParams: !15, retainedNodes: !15)
87 +!12 = !DIFile(filename: "/rustc/ec4bcaac450279b029f3480b8b8f1b82ab36a5eb/library/core/src/option.rs", directory: "", checksumkind: CSK_MD5, checksum: "4120c8557937a0772190a676ec193800")
88 +!13 = !DISubroutineType(types: !14)
91 +!16 = !DILocation(line: 0, scope: !11)