Skip to content

[DebugInfo][GVN] Propagate DebugLoc from load-of-select to select #114233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

SLTozer
Copy link
Contributor

@SLTozer SLTozer commented Oct 30, 2024

When replacing a load from a selected pointer with a select of the known stored values, we currently assign no DebugLoc to the select; this patch propagates the load's DebugLoc to the new select, since it is a direct replacement.

Found using #107279.

When replacing a load from a selected pointer with a select of the loaded
values, we currently assign no DebugLoc to the select; this patch propagates
the load's DebugLoc to the new select, since it is a direct replacement.
@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2024

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-debuginfo

Author: Stephen Tozer (SLTozer)

Changes

When replacing a load from a selected pointer with a select of the known stored values, we currently assign no DebugLoc to the select; this patch propagates the load's DebugLoc to the new select, since it is a direct replacement.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/GVN.cpp (+3)
  • (added) llvm/test/Transforms/GVN/debugloc-load-select.ll (+55)
diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 2ba600497e00d3..291ae882da937c 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -1122,6 +1122,9 @@ Value *AvailableValue::MaterializeAdjustedValue(LoadInst *Load,
     assert(V1 && V2 && "both value operands of the select must be present");
     Res =
         SelectInst::Create(Sel->getCondition(), V1, V2, "", Sel->getIterator());
+    // We use the DebugLoc from the original load here, as this instruction
+    // materializes the value that would previously have been loaded.
+    cast<SelectInst>(Res)->setDebugLoc(Load->getDebugLoc());
   } else {
     llvm_unreachable("Should not materialize value from dead block");
   }
diff --git a/llvm/test/Transforms/GVN/debugloc-load-select.ll b/llvm/test/Transforms/GVN/debugloc-load-select.ll
new file mode 100644
index 00000000000000..da0def0622ae38
--- /dev/null
+++ b/llvm/test/Transforms/GVN/debugloc-load-select.ll
@@ -0,0 +1,55 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -p=gvn -S | FileCheck %s
+;; Check that when GVN replaces a load of a select with a select of the values
+;; that would be loaded, we propagate the debug location from the load to the
+;; select, since the select is a replacement that produces the same value.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @loadtree(ptr %topol, i1 %cmp168) {
+; CHECK-LABEL: define void @loadtree(
+; CHECK-SAME: ptr [[TOPOL:%.*]], i1 [[CMP168:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr null, align 4
+; CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[TOPOL]], align 4
+; CHECK-NEXT:    [[CMP1681:%.*]] = icmp sgt i32 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[CMP168]], i32 [[TMP1]], i32 [[TMP0]], !dbg [[DBG3:![0-9]+]]
+; CHECK-NEXT:    [[DOT:%.*]] = select i1 [[CMP168]], ptr [[TOPOL]], ptr null, !dbg [[DBG9:![0-9]+]]
+; CHECK-NEXT:    [[CMP174_NOT473:%.*]] = icmp eq i32 [[TMP2]], 0
+; CHECK-NEXT:    ret void
+;
+entry:
+  %0 = load i32, ptr null, align 4
+  %1 = load i32, ptr %topol, align 4
+  %cmp1681 = icmp sgt i32 %0, %1
+  %. = select i1 %cmp168, ptr %topol, ptr null, !dbg !9
+  %2 = load i32, ptr %., align 4, !dbg !3
+  %cmp174.not473 = icmp eq i32 %2, 0
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git")
+!1 = !DIFile(filename: "/tmp/mltaln9.c", directory: "/tmp")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !DILocation(line: 4, column: 22, scope: !5)
+!4 = !DIFile(filename: "mltaln9.c", directory: "/tmp")
+!5 = distinct !DISubprogram(name: "loadtree", scope: !4, file: !4, line: 869, type: !6, scopeLine: 870, unit: !0, retainedNodes: !8)
+!6 = distinct !DISubroutineType(types: !7)
+!7 = !{null}
+!8 = !{}
+!9 = !DILocation(line: 9, column: 22, scope: !5)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; CHECK: [[META1]] = !DIFile(filename: "/tmp/mltaln9.c", directory: {{.*}})
+; CHECK: [[DBG3]] = !DILocation(line: 4, column: 22, scope: [[META4:![0-9]+]])
+; CHECK: [[META4]] = distinct !DISubprogram(name: "loadtree", scope: [[META5:![0-9]+]], file: [[META5]], line: 869, type: [[META6:![0-9]+]], scopeLine: 870, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META8:![0-9]+]])
+; CHECK: [[META5]] = !DIFile(filename: "mltaln9.c", directory: {{.*}})
+; CHECK: [[META6]] = distinct !DISubroutineType(types: [[META7:![0-9]+]])
+; CHECK: [[META7]] = !{null}
+; CHECK: [[META8]] = !{}
+; CHECK: [[DBG9]] = !DILocation(line: 9, column: 22, scope: [[META4]])
+;.

@SLTozer SLTozer merged commit f37bc8c into llvm:main Nov 15, 2024
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants