-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[flang] handle allocation of zero-sized objects #149165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@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
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the update! LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thanks!
This PR handles the allocation of zero-sized objects for different implementations. One byte is allocated for the zero-sized objects.