[flang-commits] [flang] [flang] AArch64 support for BIND(C) derived return types (PR #114051)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Wed Oct 30 09:20:34 PDT 2024


================
@@ -825,6 +825,51 @@ struct TargetAArch64 : public GenericTarget<TargetAArch64> {
     }
     return marshal;
   }
+
+  // Determine if the type is a Homogenous Floating-point Aggregate (HFA). An
+  // HFA is a record type with up to 4 floating-point members of the same type.
+  static bool isHFA(fir::RecordType ty) {
+    auto types = ty.getTypeList();
+    if (types.empty() || types.size() > 4) {
+      return false;
+    }
+
+    if (!isa_real(types.front().second)) {
+      return false;
+    }
+
+    return llvm::all_equal(llvm::make_second_range(types));
+  }
+
+  // AArch64 procedure call ABI:
+  // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#parameter-passing
+  CodeGenSpecifics::Marshalling
+  structReturnType(mlir::Location loc, fir::RecordType ty) const override {
+    CodeGenSpecifics::Marshalling marshal;
+
+    if (isHFA(ty)) {
+      auto newTy = fir::SequenceType::get({ty.getNumFields()}, ty.getType(0));
+      marshal.emplace_back(newTy, AT{});
+      return marshal;
+    }
+
+    auto [size, align] =
+        fir::getTypeSizeAndAlignmentOrCrash(loc, ty, getDataLayout(), kindMap);
+
+    // return in registers if size <= 16 bytes
+    if (size <= 16) {
+      std::size_t dwordSize = (size + 7) / 8;
+      auto newTy = fir::SequenceType::get(
+          dwordSize, mlir::IntegerType::get(ty.getContext(), 64));
+      marshal.emplace_back(newTy, AT{});
+      return marshal;
+    }
+
+    unsigned short stackAlign = std::max<unsigned short>(align, 8u);
+    marshal.emplace_back(fir::ReferenceType::get(ty),
+                         AT{stackAlign, false, true});
----------------
tblah wrote:

```suggestion
                         AT{/*alignment=*/stackAlign, /*byval=*/false, /*sret=*/true});
```

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


More information about the flang-commits mailing list