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

Daniel Chen via flang-commits flang-commits at lists.llvm.org
Wed Jul 16 11:56:33 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;
+      } else {
+        size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, arg);
----------------
DanielCChen wrote:

I may read it wrong, but should this be also called for the `if` part at the above?

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


More information about the flang-commits mailing list