[llvm] [LoopInterchange] Fix crash when dbg_value exists right after PHI (PR #208147)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 7 23:03:05 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Ryotaro Kasuga (kasuga-fj)
<details>
<summary>Changes</summary>
When enabling reduction2mem with `-loop-interchange-reduction-to-mem` and the input IR contains `dbg_value`s immediately after a certain PHI node, assertion failure could be triggered due to the insertion position of newly created PHI node. This patch fixes the issue by changing IRBuilder ctor to be called. TBH, I'm not entirely sure what the difference is between `IRBuilder::IRBuilder(Instruction *)` and `IRBuilder::IRBuilder(BasicBlock *, BasicBlock::iterator)`, but judging from the generated code, the behavior with this patch seems reasonable.
Taken over from #<!-- -->183273
---
Full diff: https://github.com/llvm/llvm-project/pull/208147.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/LoopInterchange.cpp (+1-1)
- (added) llvm/test/Transforms/LoopInterchange/debug-record-after-phi.ll (+96)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 96a4e4d54c6f8..938b5137f6284 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -2074,7 +2074,7 @@ void LoopInterchangeTransform::reduction2Memory() {
LoopInterchangeLegality::InnerReduction SR = InnerReductions[0];
BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
- IRBuilder<> Builder(&*(InnerLoopHeader->getFirstNonPHIIt()));
+ IRBuilder<> Builder(InnerLoopHeader, InnerLoopHeader->getFirstNonPHIIt());
// Check if it's the first iteration.
LLVMContext &Context = InnerLoopHeader->getContext();
diff --git a/llvm/test/Transforms/LoopInterchange/debug-record-after-phi.ll b/llvm/test/Transforms/LoopInterchange/debug-record-after-phi.ll
new file mode 100644
index 0000000000000..d1944eb2f4803
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/debug-record-after-phi.ll
@@ -0,0 +1,96 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore -loop-interchange-reduction-to-mem -S | FileCheck %s
+
+define void @f(ptr %A) {
+; CHECK-LABEL: define void @f(
+; CHECK-SAME: ptr [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[FOR_J_PREHEADER:.*]]
+; CHECK: [[FOR_I_HEADER_PREHEADER:.*]]:
+; CHECK-NEXT: br label %[[FOR_I_HEADER:.*]]
+; CHECK: [[FOR_I_HEADER]]:
+; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[FOR_I_LATCH:.*]] ], [ 0, %[[FOR_I_HEADER_PREHEADER]] ]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr double, ptr [[A]], i64 [[I]]
+; CHECK-NEXT: br label %[[FOR_J_SPLIT1:.*]]
+; CHECK: [[FOR_J_PREHEADER]]:
+; CHECK-NEXT: br label %[[FOR_J:.*]]
+; CHECK: [[FOR_J]]:
+; CHECK-NEXT: [[J:%.*]] = phi i64 [ [[TMP1:%.*]], %[[FOR_J_SPLIT:.*]] ], [ 0, %[[FOR_J_PREHEADER]] ]
+; CHECK-NEXT: [[RED:%.*]] = phi double [ [[RED_NEXT_LCSSA2:%.*]], %[[FOR_J_SPLIT]] ], [ 0.000000e+00, %[[FOR_J_PREHEADER]] ]
+; CHECK-NEXT: [[FIRST_ITER:%.*]] = phi i1 [ false, %[[FOR_J_SPLIT]] ], [ true, %[[FOR_J_PREHEADER]] ]
+; CHECK-NEXT: br label %[[FOR_I_HEADER_PREHEADER]]
+; CHECK: [[FOR_J_SPLIT1]]:
+; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[GEP]], align 8
+; CHECK-NEXT: [[NEW_VAR:%.*]] = select i1 [[FIRST_ITER]], double 0.000000e+00, double [[TMP0]]
+; CHECK-NEXT: #dbg_value(i64 [[J]], [[META3:![0-9]+]], !DIExpression(), [[META10:![0-9]+]])
+; CHECK-NEXT: [[J_NEXT:%.*]] = add i64 [[J]], 1
+; CHECK-NEXT: [[RED_NEXT:%.*]] = fadd reassoc double 1.000000e+00, [[NEW_VAR]]
+; CHECK-NEXT: store double [[RED_NEXT]], ptr [[GEP]], align 8
+; CHECK-NEXT: [[EC_J:%.*]] = icmp eq i64 [[J_NEXT]], 10
+; CHECK-NEXT: br label %[[FOR_I_LATCH]]
+; CHECK: [[FOR_J_SPLIT]]:
+; CHECK-NEXT: [[RED_NEXT_LCSSA2]] = phi double [ [[RED_NEXT]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: [[RED_NEXT_LCSSA:%.*]] = phi double [ [[RED_NEXT]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: [[TMP1]] = add i64 [[J]], 1
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[TMP1]], 10
+; CHECK-NEXT: br i1 [[TMP2]], label %[[EXIT:.*]], label %[[FOR_J]]
+; CHECK: [[FOR_I_LATCH]]:
+; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
+; CHECK-NEXT: [[EC_I:%.*]] = icmp eq i64 [[I_NEXT]], 10
+; CHECK-NEXT: br i1 [[EC_I]], label %[[FOR_J_SPLIT]], label %[[FOR_I_HEADER]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %for.i.header
+
+for.i.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %for.i.latch ]
+ %gep = getelementptr double, ptr %A, i64 %i
+ br label %for.j
+
+for.j:
+ %j = phi i64 [ 0, %for.i.header ], [ %j.next, %for.j ]
+ %red = phi double [ 0.000000e+00, %for.i.header ], [ %red.next, %for.j ]
+ #dbg_value(i64 %j, !3, !DIExpression(), !13)
+ %j.next = add i64 %j, 1
+ %red.next = fadd reassoc double 1.000000e+00, %red
+ %ec.j = icmp eq i64 %j.next, 10
+ br i1 %ec.j, label %for.i.latch, label %for.j
+
+for.i.latch:
+ store double %red.next, ptr %gep
+ %i.next = add i64 %i, 1
+ %ec.i = icmp eq i64 %i.next, 10
+ br i1 %ec.i, label %exit, label %for.i.header
+
+exit:
+ ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+!1 = !DIFile(filename: "test.c", directory: "/home/abc/wyy/test/FirstIter_DbgRecords", checksumkind: CSK_MD5, checksum: "69b1c47396b37bc8b48d5517efe4fb54")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !DILocalVariable(name: "j", scope: !4, file: !1, line: 7, type: !12)
+!4 = distinct !DILexicalBlock(scope: !8, file: !1, line: 7, column: 5)
+!8 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 2, type: !9, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11, keyInstructions: true)
+!9 = !DISubroutineType(types: !10)
+!10 = !{null}
+!11 = !{}
+!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!13 = !DILocation(line: 0, scope: !4)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; CHECK: [[META1]] = !DIFile(filename: "{{.*}}test.c", directory: {{.*}}, checksumkind: CSK_MD5, checksum: "69b1c47396b37bc8b48d5517efe4fb54")
+; CHECK: [[META3]] = !DILocalVariable(name: "j", scope: [[META4:![0-9]+]], file: [[META1]], line: 7, type: [[META9:![0-9]+]])
+; CHECK: [[META4]] = distinct !DILexicalBlock(scope: [[META5:![0-9]+]], file: [[META1]], line: 7, column: 5)
+; CHECK: [[META5]] = distinct !DISubprogram(name: "func", scope: [[META1]], file: [[META1]], line: 2, type: [[META6:![0-9]+]], scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META8:![0-9]+]], keyInstructions: true)
+; CHECK: [[META6]] = !DISubroutineType(types: [[META7:![0-9]+]])
+; CHECK: [[META7]] = !{null}
+; CHECK: [[META8]] = !{}
+; CHECK: [[META9]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+; CHECK: [[META10]] = !DILocation(line: 0, scope: [[META4]])
+;.
``````````
</details>
https://github.com/llvm/llvm-project/pull/208147
More information about the llvm-commits
mailing list