Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,9 +1119,19 @@ struct AllocMemOpConversion : public fir::FIROpConversion<fir::AllocMemOp> {
mlir::Value size = genTypeSizeInBytes(loc, ity, rewriter, llvmObjectTy);
if (auto scaleSize = genAllocationScaleSize(heap, ity, rewriter))
size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, scaleSize);
for (mlir::Value opnd : adaptor.getOperands())
size = rewriter.create<mlir::LLVM::MulOp>(
loc, ity, size, integerCast(loc, rewriter, ity, opnd));
for (mlir::Value opnd : adaptor.getOperands()) {
auto arg = integerCast(loc, rewriter, ity, opnd);
auto val = fir::getIntIfConstant(arg);
if (val && *val == 0) {
// As the return value of malloc(0) is implementation defined, allocate
// one byte to ensure the allocation status being true. This behavior
// aligns to what the runtime has.
size = genConstantIndex(loc, ity, rewriter, 1);
break;
} else {
size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, arg);
}
}
auto mallocTyWidth = lowerTy().getIndexTypeBitwidth();
auto mallocTy =
mlir::IntegerType::get(rewriter.getContext(), mallocTyWidth);
Expand Down
37 changes: 37 additions & 0 deletions flang/test/Lower/zero-size2.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
! RUN: %flang_fc1 -emit-llvm -o - %s | FileCheck %s

subroutine sub1()
integer, allocatable :: arr(:)
allocate(arr(0))
! CHECK-LABEL: @sub1_
! CHECK: %[[p:.*]] = call ptr @malloc(i64 1)
end

subroutine sub2()
real, allocatable :: arr(:,:)
allocate(arr(10,0))
! CHECK-LABEL: @sub2_
! CHECK: %[[p:.*]] = call ptr @malloc(i64 1)
end

subroutine sub3(i)
integer :: i
real, allocatable :: arr(:,:)
allocate(arr(i,0))
! CHECK-LABEL: @sub3_
! CHECK: %[[p:.*]] = call ptr @malloc(i64 1)
end

subroutine sub4()
character(:), allocatable :: c
allocate(character(0)::c)
! CHECK-LABEL: @sub4_
! CHECK: %[[p:.*]] = call ptr @malloc(i64 1)
end

subroutine sub5()
character(:), allocatable :: c(:)
allocate(character(5)::c(0))
! CHECK-LABEL: @sub5_
! CHECK: %[[p:.*]] = call ptr @malloc(i64 1)
end
Loading