[llvm] [LoopInterchange] Reject interchange if non-reassociative reduction exists (PR #148612)
Sjoerd Meijer via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 14 06:07:01 PDT 2025
================
@@ -812,7 +812,62 @@ static PHINode *findInnerReductionPhi(Loop *L, Value *V) {
// Detect floating point reduction only when it can be reordered.
if (RD.getExactFPMathInst() != nullptr)
return nullptr;
- return PHI;
+
+ RecurKind RK = RD.getRecurrenceKind();
+ switch (RK) {
+ case RecurKind::Or:
+ case RecurKind::And:
+ case RecurKind::Xor:
+ case RecurKind::SMin:
+ case RecurKind::SMax:
+ case RecurKind::UMin:
+ case RecurKind::UMax:
+ case RecurKind::FAdd:
+ case RecurKind::FMul:
+ case RecurKind::FMin:
+ case RecurKind::FMax:
+ case RecurKind::FMinimum:
+ case RecurKind::FMaximum:
+ case RecurKind::FMinimumNum:
+ case RecurKind::FMaximumNum:
+ case RecurKind::FMulAdd:
+ case RecurKind::AnyOf:
+ return PHI;
+
+ // Change the order of integer addition/multiplication may change the
+ // semantics. Consider the following case:
+ //
+ // int A[2][2] = {{ INT_MAX, INT_MAX }, { INT_MIN, INT_MIN }};
+ // int sum = 0;
+ // for (int i = 0; i < 2; i++)
+ // for (int j = 0; j < 2; j++)
+ // sum += A[j][i];
+ //
+ // If the above loops are exchanged, the addition will cause an
+ // overflow. To prove the legality, we must ensure that all reduction
+ // operations don't have nuw/nsw flags.
+ case RecurKind::Add:
+ case RecurKind::Mul: {
+ unsigned OpCode = RecurrenceDescriptor::getOpcode(RK);
+ SmallVector<Instruction *, 4> Ops = RD.getReductionOpChain(PHI, L);
+
+ // FIXME: Is this check necessary?
+ if (Ops.empty())
+ return nullptr;
+ for (Instruction *I : Ops) {
+ // FIXME: Is this check necessary?
+ if (I->getOpcode() != OpCode)
----------------
sjoerdmeijer wrote:
Aren't these two different opcodes: the instruction opcode, and the recurrence descriptor opcode? And then we are checking whether these are the same for all instructions in the reduction chain. I can see how that maybe is not the case (maybe with adds and subs), but more importantly, I don't really see what this is guaranteeing, or what you would like to achieve with this.
https://github.com/llvm/llvm-project/pull/148612
More information about the llvm-commits
mailing list