[flang-commits] [flang] [flang] handle allocation of zero-sized objects (PR #149165)
via flang-commits
flang-commits at lists.llvm.org
Wed Jul 16 11:49:04 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-codegen
@llvm/pr-subscribers-flang-fir-hlfir
Author: Kelvin Li (kkwli)
<details>
<summary>Changes</summary>
This PR handles the allocation of zero-sized objects for different implementations. One byte is allocated for the zero-sized objects.
---
Full diff: https://github.com/llvm/llvm-project/pull/149165.diff
2 Files Affected:
- (modified) flang/lib/Optimizer/CodeGen/CodeGen.cpp (+13-3)
- (added) flang/test/Lower/zero-size2.f90 (+37)
``````````diff
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index ecc04a6c9a2be..06686005bf2c9 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -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);
diff --git a/flang/test/Lower/zero-size2.f90 b/flang/test/Lower/zero-size2.f90
new file mode 100644
index 0000000000000..efaf57a6ac59f
--- /dev/null
+++ b/flang/test/Lower/zero-size2.f90
@@ -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
``````````
</details>
https://github.com/llvm/llvm-project/pull/149165
More information about the flang-commits
mailing list