[lldb] [clang] [lld] [clang-tools-extra] [openmp] [flang] [mlir] [llvm] [libunwind] [GVNSink] Fix #77415: GVNSink fails to optimize LLVM IR with debug info (PR #77602)

2024-01-10 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-transforms

Author: Shan Huang (Apochens)


Changes

This PR fixes issue #77415 and is revised from PR #77419 .

PR #77419 breaks the newly added test in the same PR on windows, 
because GVNSink is non-deterministic when sorting `BasicBlock*` pointers. This 
is reflected in the failure report.

```
# | 
C:\src\llvm-project\llvm\test\Transforms\GVNSink\sink-ignore-dbg-intrinsics.ll:28:10:
 error: CHECK: expected string not found in input
# | ; CHECK: %a.sink = phi i32 [ %a, %if.then ], [ %b, %if.else ]
# |  ^
# | stdin:24:8: note: scanning from here
# | if.end: ; preds = %if.else, %if.then
# |^
# | stdin:25:2: note: possible intended match here
# |  %b.sink = phi i32 [ %b, %if.else ], [ %a, %if.then ]
# |  ^
# | 
# | Input file: stdin
# | Check file: 
C:\src\llvm-project\llvm\test\Transforms\GVNSink\sink-ignore-dbg-intrinsics.ll
```

According to the report, what the CheckFile wants to match is the `%a.sink`, 
however there is `%b.sink`. But this mismatch does not mean that this commit is 
wrong, since the occurrence of either `%a.sink` or `%b.sink` is correct. The 
root cause of this test failure is the strict check rule in the regression test 
committed.

So I refined the regression test with a more general check rule to only detect 
whether there is an instruction with suffix `.sink` in the `if.end` block. Hope 
this won't fail the test. If this PR still fails to build, I will close this PR 
and try to find another right way to fix this.

---
Full diff: https://github.com/llvm/llvm-project/pull/77602.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/GVNSink.cpp (+3-3) 
- (added) llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll (+86) 


``diff
diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp 
b/llvm/lib/Transforms/Scalar/GVNSink.cpp
index 2b38831139a580..9db66b3793b8dd 100644
--- a/llvm/lib/Transforms/Scalar/GVNSink.cpp
+++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp
@@ -132,7 +132,7 @@ class LockstepReverseIterator {
 ActiveBlocks.remove(BB);
 continue;
   }
-  Insts.push_back(BB->getTerminator()->getPrevNode());
+  Insts.push_back(BB->getTerminator()->getPrevNonDebugInstruction());
 }
 if (Insts.empty())
   Fail = true;
@@ -168,7 +168,7 @@ class LockstepReverseIterator {
   if (Inst == >getParent()->front())
 ActiveBlocks.remove(Inst->getParent());
   else
-NewInsts.push_back(Inst->getPrevNode());
+NewInsts.push_back(Inst->getPrevNonDebugInstruction());
 }
 if (NewInsts.empty()) {
   Fail = true;
@@ -834,7 +834,7 @@ void GVNSink::sinkLastInstruction(ArrayRef 
Blocks,
   BasicBlock *BBEnd) {
   SmallVector Insts;
   for (BasicBlock *BB : Blocks)
-Insts.push_back(BB->getTerminator()->getPrevNode());
+Insts.push_back(BB->getTerminator()->getPrevNonDebugInstruction());
   Instruction *I0 = Insts.front();
 
   SmallVector NewOperands;
diff --git a/llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll 
b/llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll
new file mode 100644
index 00..ee6039a0d806ab
--- /dev/null
+++ b/llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll
@@ -0,0 +1,86 @@
+; RUN: opt < %s -passes=gvn-sink -S | FileCheck %s
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @fun(i32 noundef %a, i32 noundef %b) #0 !dbg !10 {
+entry:
+  tail call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata 
!DIExpression()), !dbg !16
+  tail call void @llvm.dbg.value(metadata i32 %b, metadata !17, metadata 
!DIExpression()), !dbg !16
+  %cmp = icmp sgt i32 %b, 10, !dbg !18
+  br i1 %cmp, label %if.then, label %if.else, !dbg !20
+
+if.then:  ; preds = %entry
+  %add = add nsw i32 %a, 1, !dbg !21
+  tail call void @llvm.dbg.value(metadata i32 %add, metadata !23, metadata 
!DIExpression()), !dbg !24
+  %xor = xor i32 %add, 1, !dbg !25
+  tail call void @llvm.dbg.value(metadata i32 %xor, metadata !26, metadata 
!DIExpression()), !dbg !24
+  tail call void @llvm.dbg.value(metadata i32 %xor, metadata !27, metadata 
!DIExpression()), !dbg !16
+  br label %if.end, !dbg !28
+
+if.else:  ; preds = %entry
+  %add1 = add nsw i32 %b, 1, !dbg !29
+  tail call void @llvm.dbg.value(metadata i32 %add1, metadata !31, metadata 
!DIExpression()), !dbg !32
+  %xor2 = xor i32 %add1, 1, !dbg !33
+  tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !34, metadata 
!DIExpression()), !dbg !32
+  tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !27, metadata 
!DIExpression()), !dbg !16
+  br label %if.end
+
+; CHECK-LABEL: if.end:
+; CHECK: [[SINK:%.*.sink]] = phi i32 [ %a, %if.then ], [ %b, %if.else ]
+; CHECK: %add = add nsw i32 [[SINK]], 1
+; CHECK: %xor = xor i32 %add, 1
+if.end:   ; preds = %if.else, %if.then
+  

[lldb] [clang] [lld] [clang-tools-extra] [openmp] [flang] [mlir] [llvm] [libunwind] [GVNSink] Fix #77415: GVNSink fails to optimize LLVM IR with debug info (PR #77602)

2024-01-10 Thread Shan Huang via cfe-commits

https://github.com/Apochens created 
https://github.com/llvm/llvm-project/pull/77602

This PR fixes issue #77415 and is revised from PR #77419 .

PR #77419 breaks the newly added test in the same PR on windows, because 
GVNSink is non-deterministic when sorting `BasicBlock*` pointers. This is 
reflected in the failure report.

```
# | 
C:\src\llvm-project\llvm\test\Transforms\GVNSink\sink-ignore-dbg-intrinsics.ll:28:10:
 error: CHECK: expected string not found in input
# | ; CHECK: %a.sink = phi i32 [ %a, %if.then ], [ %b, %if.else ]
# |  ^
# | :24:8: note: scanning from here
# | if.end: ; preds = %if.else, %if.then
# |^
# | :25:2: note: possible intended match here
# |  %b.sink = phi i32 [ %b, %if.else ], [ %a, %if.then ]
# |  ^
# | 
# | Input file: 
# | Check file: 
C:\src\llvm-project\llvm\test\Transforms\GVNSink\sink-ignore-dbg-intrinsics.ll
```

According to the report, what the CheckFile wants to match is the `%a.sink`, 
however there is `%b.sink`. But this mismatch does not mean that this commit is 
wrong, since the occurrence of either `%a.sink` or `%b.sink` is correct. The 
root cause of this test failure is the strict check rule in the regression test 
committed.

So I refined the regression test with a more general check rule to only detect 
whether there is an instruction with suffix `.sink` in the `if.end` block. Hope 
this won't fail the test. If this PR still fails to build, I will close this PR 
and try to find another right way to fix this.

>From d08af0b38d28726bc78c8da675ea01d7c188c446 Mon Sep 17 00:00:00 2001
From: Apochens <52285902...@stu.ecnu.edu.cn>
Date: Wed, 10 Jan 2024 07:10:22 +
Subject: [PATCH] [GVNSink] Fix #77415: GVNSink fails to optimize LLVM IR with
 debug info

---
 llvm/lib/Transforms/Scalar/GVNSink.cpp|  6 +-
 .../GVNSink/sink-ignore-dbg-intrinsics.ll | 86 +++
 2 files changed, 89 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll

diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp 
b/llvm/lib/Transforms/Scalar/GVNSink.cpp
index 2b38831139a580..9db66b3793b8dd 100644
--- a/llvm/lib/Transforms/Scalar/GVNSink.cpp
+++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp
@@ -132,7 +132,7 @@ class LockstepReverseIterator {
 ActiveBlocks.remove(BB);
 continue;
   }
-  Insts.push_back(BB->getTerminator()->getPrevNode());
+  Insts.push_back(BB->getTerminator()->getPrevNonDebugInstruction());
 }
 if (Insts.empty())
   Fail = true;
@@ -168,7 +168,7 @@ class LockstepReverseIterator {
   if (Inst == >getParent()->front())
 ActiveBlocks.remove(Inst->getParent());
   else
-NewInsts.push_back(Inst->getPrevNode());
+NewInsts.push_back(Inst->getPrevNonDebugInstruction());
 }
 if (NewInsts.empty()) {
   Fail = true;
@@ -834,7 +834,7 @@ void GVNSink::sinkLastInstruction(ArrayRef 
Blocks,
   BasicBlock *BBEnd) {
   SmallVector Insts;
   for (BasicBlock *BB : Blocks)
-Insts.push_back(BB->getTerminator()->getPrevNode());
+Insts.push_back(BB->getTerminator()->getPrevNonDebugInstruction());
   Instruction *I0 = Insts.front();
 
   SmallVector NewOperands;
diff --git a/llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll 
b/llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll
new file mode 100644
index 00..ee6039a0d806ab
--- /dev/null
+++ b/llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll
@@ -0,0 +1,86 @@
+; RUN: opt < %s -passes=gvn-sink -S | FileCheck %s
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @fun(i32 noundef %a, i32 noundef %b) #0 !dbg !10 {
+entry:
+  tail call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata 
!DIExpression()), !dbg !16
+  tail call void @llvm.dbg.value(metadata i32 %b, metadata !17, metadata 
!DIExpression()), !dbg !16
+  %cmp = icmp sgt i32 %b, 10, !dbg !18
+  br i1 %cmp, label %if.then, label %if.else, !dbg !20
+
+if.then:  ; preds = %entry
+  %add = add nsw i32 %a, 1, !dbg !21
+  tail call void @llvm.dbg.value(metadata i32 %add, metadata !23, metadata 
!DIExpression()), !dbg !24
+  %xor = xor i32 %add, 1, !dbg !25
+  tail call void @llvm.dbg.value(metadata i32 %xor, metadata !26, metadata 
!DIExpression()), !dbg !24
+  tail call void @llvm.dbg.value(metadata i32 %xor, metadata !27, metadata 
!DIExpression()), !dbg !16
+  br label %if.end, !dbg !28
+
+if.else:  ; preds = %entry
+  %add1 = add nsw i32 %b, 1, !dbg !29
+  tail call void @llvm.dbg.value(metadata i32 %add1, metadata !31, metadata 
!DIExpression()), !dbg !32
+  %xor2 = xor i32 %add1, 1, !dbg !33
+  tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !34, metadata 
!DIExpression()), !dbg !32
+  tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !27, metadata 
!DIExpression()), !dbg !16
+  br label %if.end
+
+; CHECK-LABEL: