[flang] handle allocation of zero-sized objects#149165
Merged
Conversation
Member
|
@llvm/pr-subscribers-flang-codegen @llvm/pr-subscribers-flang-fir-hlfir Author: Kelvin Li (kkwli) ChangesThis 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:
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
|
|
|
DanielCChen
reviewed
Jul 16, 2025
vzakhari
reviewed
Jul 16, 2025
vzakhari
approved these changes
Jul 17, 2025
Contributor
vzakhari
left a comment
There was a problem hiding this comment.
Thank you for the update! LGTM
This was referenced Jul 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR handles the allocation of zero-sized objects for different implementations. One byte is allocated for the zero-sized objects.