[flang-commits] [flang] [flang] handle allocation of zero-sized objects (PR #149165)

Kelvin Li via flang-commits flang-commits at lists.llvm.org
Wed Jul 16 12:00:09 PDT 2025


================
@@ -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;
----------------
kkwli wrote:

No. This break is to stop computing the remaining dimensions of the same object. The other allocatable variables on the same allocate statement are handled separately.

https://github.com/llvm/llvm-project/pull/149165


More information about the flang-commits mailing list