[flang-commits] [flang] [flang] disable SimplifyPassThroughCondBranch pattern at O0 to preserve line location (PR #227215)
via flang-commits
flang-commits at lists.llvm.org
Tue Sep 29 01:33:56 PDT 2026
https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/227215
This patch is part of https://github.com/llvm/llvm-project/issues/202065 effort to preserve at least one loop header instructions for each expanded array expressions at O0 so that breakpoint set on array expression statements that are inlined fire once (an not at each iteration which happens when the only instruction with the statement line location is in the loop body).
A previous FastIsel change ensured that the location set on the entry branch was propagated. Doing more extensive testing, I found a case where we were deleting such entry branch before LLVM at the MLIR level. The reason was that `SimplifyPassThroughCondBranch` can merge the exit and entry branch of two array expressions. Hence this patch disable this patterns at O0.
>From 12f023956efb8a7023519513b5040ba2ca4c3465 Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Tue, 29 Sep 2026 01:20:55 -0700
Subject: [PATCH] [flang] disable SimplifyPassThroughCondBranch pattern at O0
to preserve line location
---
flang/lib/Optimizer/Passes/Pipelines.cpp | 9 ++++-
.../array-assignment-entry-debug.f90 | 36 +++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)
create mode 100644 flang/test/Integration/array-assignment-entry-debug.f90
diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp
index ef18ff1cde945f..9bff7f936b49dc 100644
--- a/flang/lib/Optimizer/Passes/Pipelines.cpp
+++ b/flang/lib/Optimizer/Passes/Pipelines.cpp
@@ -241,7 +241,14 @@ void createDefaultFIRPostCFGOptimizerPassPipeline(
pm.addPass(mlir::createSCFToControlFlowPass());
- pm.addPass(mlir::createCanonicalizerPass(config));
+ // Keep the entry branch of expanded array assignments at O0. Bypassing it
+ // moves the next loop's PHI initializations into the preceding loop header,
+ // losing the statement's breakpoint location outside the loop. Disabling
+ // region simplification alone does not disable this operation pattern.
+ llvm::SmallVector<std::string> disabledPatterns;
+ if (pc.OptLevel == llvm::OptimizationLevel::O0)
+ disabledPatterns.push_back("SimplifyPassThroughCondBranch");
+ pm.addPass(mlir::createCanonicalizerPass(config, disabledPatterns));
pm.addPass(fir::createSimplifyRegionLite());
if (!pc.SkipConvertComplexPow)
pm.addPass(fir::createConvertComplexPow());
diff --git a/flang/test/Integration/array-assignment-entry-debug.f90 b/flang/test/Integration/array-assignment-entry-debug.f90
new file mode 100644
index 00000000000000..a2f0d905a09993
--- /dev/null
+++ b/flang/test/Integration/array-assignment-entry-debug.f90
@@ -0,0 +1,36 @@
+subroutine arrays(a, b, c)
+ integer :: a(4), b(4), c(4)
+ b = 1
+ c = 2
+ a = 11
+ a = b + c
+end subroutine
+
+! Expand the assignments explicitly, without enabling other optimizations.
+! Each following assignment must retain a separate entry branch carrying its
+! source location, outside both the preceding and following loops.
+! RUN: %flang_fc1 -emit-hlfir -mmlir --mlir-print-debuginfo -o - %s | fir-opt --inline-elementals --inline-hlfir-assign --mlir-print-debuginfo -o %t.fir && %flang_fc1 -O0 -debug-info-kind=line-tables-only -emit-llvm %t.fir -o - | FileCheck %s
+
+! CHECK-LABEL: define void @arrays_(
+! CHECK: br label %[[FIRST:[0-9]+]], !dbg ![[LOC1:[0-9]+]]
+! CHECK: [[FIRST]]:
+! CHECK: br i1 {{.*}}, label %{{[0-9]+}}, label %[[ENTRY2:[0-9]+]], !dbg ![[LOC1]]
+! CHECK: [[ENTRY2]]:
+! CHECK-NEXT: br label %[[SECOND:[0-9]+]], !dbg ![[LOC2:[0-9]+]]
+! CHECK: [[SECOND]]:
+! CHECK-NEXT: {{.*}} = phi i64 {{.*}}[ {{[01]}}, %[[ENTRY2]] ]
+! CHECK: br i1 {{.*}}, label %{{[0-9]+}}, label %[[ENTRY3:[0-9]+]], !dbg ![[LOC2]]
+! CHECK: [[ENTRY3]]:
+! CHECK-NEXT: br label %[[THIRD:[0-9]+]], !dbg ![[LOC3:[0-9]+]]
+! CHECK: [[THIRD]]:
+! CHECK-NEXT: {{.*}} = phi i64 {{.*}}[ {{[01]}}, %[[ENTRY3]] ]
+! CHECK: br i1 {{.*}}, label %{{[0-9]+}}, label %[[ENTRY4:[0-9]+]], !dbg ![[LOC3]]
+! CHECK: [[ENTRY4]]:
+! CHECK-NEXT: br label %[[FOURTH:[0-9]+]], !dbg ![[LOC4:[0-9]+]]
+! CHECK: [[FOURTH]]:
+! CHECK-NEXT: {{.*}} = phi i64 {{.*}}[ {{[01]}}, %[[ENTRY4]] ]
+! CHECK: br i1 {{.*}}, !dbg ![[LOC4]]
+! CHECK-DAG: ![[LOC1]] = !DILocation(line: 3, column: 3,
+! CHECK-DAG: ![[LOC2]] = !DILocation(line: 4, column: 3,
+! CHECK-DAG: ![[LOC3]] = !DILocation(line: 5, column: 3,
+! CHECK-DAG: ![[LOC4]] = !DILocation(line: 6, column: 3,
More information about the flang-commits
mailing list