diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -2066,6 +2066,10 @@ assert(coor.shift().empty() || coor.shift().size() == rank); assert(coor.slice().empty() || coor.slice().size() == 3 * rank); mlir::Type idxTy = lowerTy().indexType(); + unsigned indexOffset = coor.indicesOffset(); + unsigned shapeOffset = coor.shapeOffset(); + unsigned shiftOffset = coor.shiftOffset(); + unsigned sliceOffset = coor.sliceOffset(); mlir::Value one = genConstantIndex(loc, idxTy, rewriter, 1); mlir::Value prevExt = one; mlir::Value zero = genConstantIndex(loc, idxTy, rewriter, 0); @@ -2075,29 +2079,30 @@ const bool baseIsBoxed = coor.memref().getType().isa(); // For each dimension of the array, generate the offset calculation. - for (unsigned i = 0; i < rank; ++i) { + for (unsigned i = 0; i < rank; + ++i, ++indexOffset, ++shapeOffset, ++shiftOffset, sliceOffset += 3) { mlir::Value index = - integerCast(loc, rewriter, idxTy, operands[coor.indicesOffset() + i]); - mlir::Value lb = isShifted ? integerCast(loc, rewriter, idxTy, - operands[coor.shiftOffset() + i]) - : one; + integerCast(loc, rewriter, idxTy, operands[indexOffset]); + mlir::Value lb = + isShifted ? integerCast(loc, rewriter, idxTy, operands[shiftOffset]) + : one; mlir::Value step = one; bool normalSlice = isSliced; // Compute zero based index in dimension i of the element, applying // potential triplets and lower bounds. if (isSliced) { - mlir::Value ub = operands[coor.sliceOffset() + i + 1]; - normalSlice = !mlir::isa_and_nonnull(ub.getDefiningOp()); + mlir::Value ub = operands[sliceOffset + 1]; + normalSlice = + !mlir::isa_and_nonnull(ub.getDefiningOp()); if (normalSlice) - step = integerCast(loc, rewriter, idxTy, - operands[coor.sliceOffset() + i + 2]); + step = integerCast(loc, rewriter, idxTy, operands[sliceOffset + 2]); } auto idx = rewriter.create(loc, idxTy, index, lb); mlir::Value diff = rewriter.create(loc, idxTy, idx, step); if (normalSlice) { mlir::Value sliceLb = - integerCast(loc, rewriter, idxTy, operands[coor.sliceOffset() + i]); + integerCast(loc, rewriter, idxTy, operands[sliceOffset]); auto adj = rewriter.create(loc, idxTy, sliceLb, lb); diff = rewriter.create(loc, idxTy, diff, adj); } @@ -2115,8 +2120,7 @@ offset = rewriter.create(loc, idxTy, sc, offset); // Compute next stride assuming contiguity of the base array // (in element number). - auto nextExt = - integerCast(loc, rewriter, idxTy, operands[coor.shapeOffset() + i]); + auto nextExt = integerCast(loc, rewriter, idxTy, operands[shapeOffset]); prevExt = rewriter.create(loc, idxTy, prevExt, nextExt); } diff --git a/flang/test/Fir/convert-to-llvm.fir b/flang/test/Fir/convert-to-llvm.fir --- a/flang/test/Fir/convert-to-llvm.fir +++ b/flang/test/Fir/convert-to-llvm.fir @@ -2117,15 +2117,15 @@ // CHECK: %[[VAL_13:.*]] = llvm.add %[[VAL_12]], %[[VAL_7]] : i64 // CHECK: %[[VAL_14:.*]] = llvm.mul %[[VAL_6]], %[[VAL_1]] : i64 // CHECK: %[[VAL_15:.*]] = llvm.sub %[[VAL_5]], %[[VAL_6]] : i64 -// CHECK: %[[VAL_16:.*]] = llvm.mul %[[VAL_15]], %[[VAL_2]] : i64 -// CHECK: %[[VAL_17:.*]] = llvm.sub %[[VAL_3]], %[[VAL_6]] : i64 +// CHECK: %[[VAL_16:.*]] = llvm.mul %[[VAL_15]], %[[VAL_4]] : i64 +// CHECK: %[[VAL_17:.*]] = llvm.sub %[[VAL_2]], %[[VAL_6]] : i64 // CHECK: %[[VAL_18:.*]] = llvm.add %[[VAL_16]], %[[VAL_17]] : i64 // CHECK: %[[VAL_19:.*]] = llvm.mul %[[VAL_18]], %[[VAL_14]] : i64 // CHECK: %[[VAL_20:.*]] = llvm.add %[[VAL_19]], %[[VAL_13]] : i64 // CHECK: %[[VAL_21:.*]] = llvm.mul %[[VAL_14]], %[[VAL_1]] : i64 // CHECK: %[[VAL_22:.*]] = llvm.sub %[[VAL_5]], %[[VAL_6]] : i64 -// CHECK: %[[VAL_23:.*]] = llvm.mul %[[VAL_22]], %[[VAL_3]] : i64 -// CHECK: %[[VAL_24:.*]] = llvm.sub %[[VAL_4]], %[[VAL_6]] : i64 +// CHECK: %[[VAL_23:.*]] = llvm.mul %[[VAL_22]], %[[VAL_4]] : i64 +// CHECK: %[[VAL_24:.*]] = llvm.sub %[[VAL_2]], %[[VAL_6]] : i64 // CHECK: %[[VAL_25:.*]] = llvm.add %[[VAL_23]], %[[VAL_24]] : i64 // CHECK: %[[VAL_26:.*]] = llvm.mul %[[VAL_25]], %[[VAL_21]] : i64 // CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_26]], %[[VAL_20]] : i64