Skip to content

Commit 8004b94

Browse files
authored
Merge pull request #1138 from schweitzpgi/ch-alloca
Fix bug in alloca.
2 parents a9e2159 + 28b66d1 commit 8004b94

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ struct AllocaOpConversion : public FIROpConversion<fir::AllocaOp> {
374374
}
375375
}
376376
if (alloc.hasShapeOperands()) {
377+
auto allocEleTy = fir::unwrapRefType(alloc.getType());
378+
// Scale the size by constant factors encoded in the array type.
379+
if (auto seqTy = allocEleTy.dyn_cast<fir::SequenceType>()) {
380+
fir::SequenceType::Extent constSize = 1;
381+
for (auto extent : seqTy.getShape())
382+
if (extent != fir::SequenceType::getUnknownExtent())
383+
constSize *= extent;
384+
auto constVal =
385+
genConstantIndex(loc, ity, rewriter, constSize).getResult();
386+
size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, constVal);
387+
}
377388
unsigned end = operands.size();
378389
for (; i < end; ++i)
379390
size = rewriter.create<mlir::LLVM::MulOp>(

flang/test/Fir/alloc.fir

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,21 @@ func @f5() -> !fir.ref<!fir.ptr<!fir.array<?xi32>>> {
4141
// CHECK-SAME: i32 %[[l:.*]], i64 %[[e:.*]])
4242
func @char_array_alloca(%l: i32, %e : index) -> !fir.ref<!fir.array<?x?x!fir.char<1,?>>> {
4343
// CHECK: %[[lcast:.*]] = sext i32 %[[l]] to i64
44-
// CHECK: %[[m1:.*]] = mul i64 %[[lcast]], %[[e]]
44+
// CHECK: %[[prod:.*]] = mul i64 %[[lcast]], 1
45+
// CHECK: %[[m1:.*]] = mul i64 %[[prod]], %[[e]]
4546
// CHECK: %[[size:.*]] = mul i64 %[[m1]], %[[e]]
4647
// CHECK: alloca i8, i64 %[[size]]
4748
%a = fir.alloca !fir.array<?x?x!fir.char<1,?>>(%l : i32), %e, %e
4849
return %a : !fir.ref<!fir.array<?x?x!fir.char<1,?>>>
4950
}
51+
52+
// Constant factor of 60 (4*3*5) must be included.
53+
// CHECK-LABEL: define i32* @array_with_holes(
54+
// CHECK-SAME: i64 %[[a:.*]], i64 %[[b:.*]])
55+
func @array_with_holes(%0 : index, %1 : index) -> !fir.ref<!fir.array<4x?x3x?x5xi32>> {
56+
// CHECK: %[[prod1:.*]] = mul i64 60, %[[a]]
57+
// CHECK: %[[prod2:.*]] = mul i64 %[[prod1]], %[[b]]
58+
// CHECK: alloca i32, i64 %[[prod2]]
59+
%a = fir.alloca !fir.array<4x?x3x?x5xi32>, %0, %1
60+
return %a : !fir.ref<!fir.array<4x?x3x?x5xi32>>
61+
}

0 commit comments

Comments
 (0)