Skip to content

[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

Merged
merged 2 commits into from
Jul 18, 2025
Merged

Conversation

kkwli
Copy link
Collaborator

@kkwli kkwli commented Jul 16, 2025

This PR handles the allocation of zero-sized objects for different implementations. One byte is allocated for the zero-sized objects.

@kkwli kkwli self-assigned this Jul 16, 2025
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:codegen labels Jul 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2025

@llvm/pr-subscribers-flang-codegen

@llvm/pr-subscribers-flang-fir-hlfir

Author: Kelvin Li (kkwli)

Changes

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 --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

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

@kkwli kkwli requested review from DanielCChen and vzakhari July 17, 2025 03:46
Copy link
Contributor

@vzakhari vzakhari left a 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

Copy link
Contributor

@DanielCChen DanielCChen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thanks!

@kkwli kkwli merged commit df56b1a into llvm:main Jul 18, 2025
9 checks passed
@kkwli kkwli deleted the malloc-zero branch July 18, 2025 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:codegen flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants