[llvm] [LoopInterchange] Initialize new_var to InitValue on first iteration (PR #178370)
Yingying Wang via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 28 01:02:13 PST 2026
https://github.com/buggfg updated https://github.com/llvm/llvm-project/pull/178370
>From b7d94a10e74e8bd361eb1cd5391856d10e9b660b Mon Sep 17 00:00:00 2001
From: buggfg <3171290993 at qq.com>
Date: Wed, 28 Jan 2026 14:10:41 +0800
Subject: [PATCH 1/2] fix reduction2mem
---
llvm/lib/Transforms/Scalar/LoopInterchange.cpp | 3 ++-
.../Transforms/LoopInterchange/reduction2mem-limitation.ll | 2 +-
llvm/test/Transforms/LoopInterchange/reduction2mem.ll | 2 +-
3 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 4f2f13bbf6333..de900c434188f 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -1911,6 +1911,7 @@ void LoopInterchangeTransform::reduction2Memory() {
InnerLoop->getLoopPreheader());
FirstIter->addIncoming(ConstantInt::get(Type::getInt1Ty(Context), 0),
InnerLoop->getLoopLatch());
+ FirstIter->dropDbgRecords();
assert(FirstIter->isComplete() && "The FirstIter PHI node is not complete.");
// When the reduction is initialized from a constant value, we need to add
@@ -1919,7 +1920,7 @@ void LoopInterchangeTransform::reduction2Memory() {
Instruction *LoadMem = Builder.CreateLoad(SR.ElemTy, SR.MemRef);
// Init new_var to MEM_REF or CONST depending on if it is the first iteration.
- Value *NewVar = Builder.CreateSelect(FirstIter, LoadMem, SR.Init, "new.var");
+ Value *NewVar = Builder.CreateSelect(FirstIter, SR.Init, LoadMem, "new.var");
// Replace all uses of the reduction variable with a new variable.
SR.Reduction->replaceAllUsesWith(NewVar);
diff --git a/llvm/test/Transforms/LoopInterchange/reduction2mem-limitation.ll b/llvm/test/Transforms/LoopInterchange/reduction2mem-limitation.ll
index d5303a576262d..f9888f43dcb6e 100644
--- a/llvm/test/Transforms/LoopInterchange/reduction2mem-limitation.ll
+++ b/llvm/test/Transforms/LoopInterchange/reduction2mem-limitation.ll
@@ -1,4 +1,4 @@
-; Several inner-loop reduction patterns not yet supported.
+; Several inner-loop reduction patterns are not yet supported.
; RUN: opt < %s -passes="loop-interchange" -loop-interchange-reduction-to-mem -pass-remarks-missed='loop-interchange' \
; RUN: -pass-remarks-output=%t -S | FileCheck -check-prefix=IR %s
; RUN: FileCheck --input-file=%t %s
diff --git a/llvm/test/Transforms/LoopInterchange/reduction2mem.ll b/llvm/test/Transforms/LoopInterchange/reduction2mem.ll
index 1c19781d19eac..de05b5ce4e5de 100644
--- a/llvm/test/Transforms/LoopInterchange/reduction2mem.ll
+++ b/llvm/test/Transforms/LoopInterchange/reduction2mem.ll
@@ -32,7 +32,7 @@ define void @func(ptr noalias readonly %a, ptr noalias readonly %b, ptr noalias
; CHECK-NEXT: br label %[[OUTERLOOPHEADER_PREHEADER]]
; CHECK: [[INNERLOOP_SPLIT1]]:
; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[ADDR_S]], align 8
-; CHECK-NEXT: [[NEW_VAR:%.*]] = select i1 [[FIRSTITER]], double [[TMP0]], double 0.000000e+00
+; CHECK-NEXT: [[NEW_VAR:%.*]] = select i1 [[FIRSTITER]], double 0.000000e+00, double [[TMP0]]
; CHECK-NEXT: [[ADDR_A_J_I:%.*]] = getelementptr inbounds nuw double, ptr [[ADDR_A]], i64 [[INDEX_J]]
; CHECK-NEXT: [[A_J_I:%.*]] = load double, ptr [[ADDR_A_J_I]], align 8
; CHECK-NEXT: [[ADDR_B_J_I:%.*]] = getelementptr inbounds nuw double, ptr [[ADDR_B]], i64 [[INDEX_J]]
>From 71df79d3e0b635b34b5809e65a8e956cd3a02bec Mon Sep 17 00:00:00 2001
From: buggfg <3171290993 at qq.com>
Date: Wed, 28 Jan 2026 17:01:44 +0800
Subject: [PATCH 2/2] Remove the changes for the second bug.
---
llvm/lib/Transforms/Scalar/LoopInterchange.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index de900c434188f..aa3bef91d602a 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -1911,7 +1911,6 @@ void LoopInterchangeTransform::reduction2Memory() {
InnerLoop->getLoopPreheader());
FirstIter->addIncoming(ConstantInt::get(Type::getInt1Ty(Context), 0),
InnerLoop->getLoopLatch());
- FirstIter->dropDbgRecords();
assert(FirstIter->isComplete() && "The FirstIter PHI node is not complete.");
// When the reduction is initialized from a constant value, we need to add
More information about the llvm-commits
mailing list