[clang] [SystemZ][z/OS] Implement z/OS XPLINK ABI (PR #91384)

Fanbo Meng via cfe-commits cfe-commits at lists.llvm.org
Thu May 30 06:06:25 PDT 2024


https://github.com/fanbo-meng updated https://github.com/llvm/llvm-project/pull/91384

>From 84e5ca4d8987d071d20b9dcba673b0c856762487 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Tue, 7 May 2024 13:36:38 -0400
Subject: [PATCH 1/9] [SystemZ][z/OS] Implement z/OS XPLINK ABI

The XPLINK calling convention is specified in the Language
Environment Vendor Interface, chapter 22,
(https://www.ibm.com/support/knowledgecenter/SSLTBW_2.4.0/com.ibm.zos.v2r4.cee/cee.htm)
and in Redbook XPLink: OS/390 Extra Performance Linkage
(http://www.redbooks.ibm.com/abstracts/sg245991.html?Open)
---
 clang/lib/CodeGen/CodeGenModule.cpp   |   2 +
 clang/lib/CodeGen/TargetInfo.h        |   4 +
 clang/lib/CodeGen/Targets/SystemZ.cpp | 393 ++++++++++++++++++++++++++
 clang/test/CodeGen/zos-abi.c          | 162 +++++++++++
 4 files changed, 561 insertions(+)
 create mode 100644 clang/test/CodeGen/zos-abi.c

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 60ef28a0effaa..ffdce2417c67c 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -242,6 +242,8 @@ createTargetCodeGenInfo(CodeGenModule &CGM) {
   case llvm::Triple::systemz: {
     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
     bool HasVector = !SoftFloat && Target.getABI() == "vector";
+    if (Triple.getOS() == llvm::Triple::ZOS)
+      return createSystemZ_ZOS_TargetCodeGenInfo(CGM, HasVector, SoftFloat);
     return createSystemZTargetCodeGenInfo(CGM, HasVector, SoftFloat);
   }
 
diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h
index f242d9e36ed40..e15f9bdf39356 100644
--- a/clang/lib/CodeGen/TargetInfo.h
+++ b/clang/lib/CodeGen/TargetInfo.h
@@ -527,6 +527,10 @@ std::unique_ptr<TargetCodeGenInfo>
 createSystemZTargetCodeGenInfo(CodeGenModule &CGM, bool HasVector,
                                bool SoftFloatABI);
 
+std::unique_ptr<TargetCodeGenInfo>
+createSystemZ_ZOS_TargetCodeGenInfo(CodeGenModule &CGM, bool HasVector,
+                                    bool SoftFloatABI);
+
 std::unique_ptr<TargetCodeGenInfo>
 createTCETargetCodeGenInfo(CodeGenModule &CGM);
 
diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index deaafc85a3157..d420286c71c16 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -10,6 +10,7 @@
 #include "TargetInfo.h"
 #include "clang/Basic/Builtins.h"
 #include "llvm/IR/IntrinsicsS390.h"
+#include <optional>
 
 using namespace clang;
 using namespace clang::CodeGen;
@@ -529,9 +530,401 @@ bool SystemZTargetCodeGenInfo::isVectorTypeBased(const Type *Ty,
   return false;
 }
 
+//===----------------------------------------------------------------------===//
+// z/OS XPLINK ABI Implementation
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+class ZOSXPLinkABIInfo : public ABIInfo {
+  const unsigned GPRBits = 64;
+  bool HasVector;
+
+public:
+  ZOSXPLinkABIInfo(CodeGenTypes &CGT, bool HV) : ABIInfo(CGT), HasVector(HV) {}
+
+  bool isPromotableIntegerType(QualType Ty) const;
+  bool isCompoundType(QualType Ty) const;
+  bool isVectorArgumentType(QualType Ty) const;
+  bool isFPArgumentType(QualType Ty) const;
+  QualType getSingleElementType(QualType Ty) const;
+  unsigned getMaxAlignFromTypeDefs(QualType Ty) const;
+  std::optional<QualType> getFPTypeOfComplexLikeType(QualType Ty) const;
+
+  ABIArgInfo classifyReturnType(QualType RetTy,
+                                unsigned functionCallConv) const;
+  ABIArgInfo classifyArgumentType(QualType ArgTy, bool IsNamedArg,
+                                  unsigned functionCallConv) const;
+
+  void computeInfo(CGFunctionInfo &FI) const override {
+    if (!getCXXABI().classifyReturnType(FI))
+      FI.getReturnInfo() =
+          classifyReturnType(FI.getReturnType(), FI.getCallingConvention());
+
+    unsigned NumRequiredArgs = FI.getNumRequiredArgs();
+    unsigned ArgNo = 0;
+
+    for (auto &I : FI.arguments()) {
+      bool IsNamedArg = ArgNo < NumRequiredArgs;
+      I.info =
+          classifyArgumentType(I.type, IsNamedArg, FI.getCallingConvention());
+      ++ArgNo;
+    }
+  }
+
+  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
+                    QualType Ty) const override;
+};
+
+class ZOSXPLinkTargetCodeGenInfo : public TargetCodeGenInfo {
+public:
+  ZOSXPLinkTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
+      : TargetCodeGenInfo(std::make_unique<ZOSXPLinkABIInfo>(CGT, HasVector)) {
+    SwiftInfo =
+        std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
+  }
+};
+
+} // namespace
+
+// Return true if the ABI requires Ty to be passed sign- or zero-
+// extended to 64 bits.
+bool ZOSXPLinkABIInfo::isPromotableIntegerType(QualType Ty) const {
+  // Treat an enum type as its underlying type.
+  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
+    Ty = EnumTy->getDecl()->getIntegerType();
+
+  // Promotable integer types are required to be promoted by the ABI.
+  if (getContext().isPromotableIntegerType(Ty))
+    return true;
+
+  if (const auto *EIT = Ty->getAs<BitIntType>())
+    if (EIT->getNumBits() < 64)
+      return true;
+
+  // In addition to the usual promotable integer types, we also need to
+  // extend all 32-bit types, since the ABI requires promotion to 64 bits.
+  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
+    switch (BT->getKind()) {
+    case BuiltinType::Int:
+    case BuiltinType::UInt:
+      return true;
+    default:
+      break;
+    }
+
+  return false;
+}
+
+bool ZOSXPLinkABIInfo::isCompoundType(QualType Ty) const {
+  return (Ty->isAnyComplexType() || Ty->isVectorType() ||
+          isAggregateTypeForABI(Ty));
+}
+
+bool ZOSXPLinkABIInfo::isVectorArgumentType(QualType Ty) const {
+  return (HasVector && Ty->isVectorType() &&
+          getContext().getTypeSize(Ty) <= 128);
+}
+
+bool ZOSXPLinkABIInfo::isFPArgumentType(QualType Ty) const {
+  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
+    switch (BT->getKind()) {
+    case BuiltinType::Float:
+    case BuiltinType::Double:
+    case BuiltinType::LongDouble:
+      return true;
+    default:
+      return false;
+    }
+
+  return false;
+}
+
+QualType ZOSXPLinkABIInfo::getSingleElementType(QualType Ty) const {
+  if (const RecordType *RT = Ty->getAsStructureType()) {
+    const RecordDecl *RD = RT->getDecl();
+    QualType Found;
+
+    // If this is a C++ record, check the bases first.
+    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
+      for (const auto &I : CXXRD->bases()) {
+        QualType Base = I.getType();
+
+        // Empty bases don't affect things either way.
+        if (isEmptyRecord(getContext(), Base, true))
+          continue;
+
+        if (!Found.isNull())
+          return Ty;
+        Found = getSingleElementType(Base);
+      }
+
+    // Check the fields.
+    for (const auto *FD : RD->fields()) {
+      // Unlike isSingleElementStruct(), empty structure and array fields
+      // do count.  So do anonymous bitfields that aren't zero-sized.
+      if (getContext().getLangOpts().CPlusPlus &&
+          FD->isZeroLengthBitField(getContext()))
+        continue;
+
+      // Unlike isSingleElementStruct(), arrays do not count.
+      // Nested structures still do though.
+      if (!Found.isNull())
+        return Ty;
+      Found = getSingleElementType(FD->getType());
+    }
+
+    // Unlike isSingleElementStruct(), trailing padding is allowed.
+    if (!Found.isNull())
+      return Found;
+  }
+
+  return Ty;
+}
+
+unsigned ZOSXPLinkABIInfo::getMaxAlignFromTypeDefs(QualType Ty) const {
+  unsigned MaxAlign = 0;
+  while (Ty != Ty.getSingleStepDesugaredType(getContext())) {
+    auto *DesugaredType =
+        Ty.getSingleStepDesugaredType(getContext()).getTypePtr();
+    if (auto *TypedefTy = dyn_cast<TypedefType>(DesugaredType)) {
+      auto *TyDecl = TypedefTy->getDecl();
+      unsigned CurrAlign = TyDecl->getMaxAlignment();
+      MaxAlign = std::max(CurrAlign, MaxAlign);
+    }
+    Ty = Ty.getSingleStepDesugaredType(getContext());
+  }
+  return MaxAlign;
+}
+
+std::optional<QualType>
+ZOSXPLinkABIInfo::getFPTypeOfComplexLikeType(QualType Ty) const {
+  if (const RecordType *RT = Ty->getAsStructureType()) {
+    const RecordDecl *RD = RT->getDecl();
+
+    // Check for non-empty base classes.
+    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
+      if (CXXRD->hasDefinition())
+        for (const auto &I : CXXRD->bases()) {
+          QualType Base = I.getType();
+          if (!isEmptyRecord(getContext(), Base, true))
+            return std::nullopt;
+        }
+
+    // Check for exactly two elements with exactly the same floating point type.
+    // A single-element struct containing only a float, double, or long double
+    // counts as a field of that type. If the struct has one field consisting
+    // of a complex type, it does not count. This design may be somewhat
+    // inconsistent but it matches the behavior of the legacy C compiler.
+    int Count = 0;
+    clang::BuiltinType::Kind elemKind;
+    QualType RetTy;
+    for (const auto *FD : RD->fields()) {
+      if (Count >= 2)
+        return std::nullopt;
+
+      unsigned MaxAlignOnDecl = FD->getMaxAlignment();
+      QualType FT = FD->getType();
+      QualType FTSingleTy = getSingleElementType(FT);
+      unsigned MaxAlign =
+          std::max(getMaxAlignFromTypeDefs(FTSingleTy), MaxAlignOnDecl);
+
+      // The first element of a complex type may have an alignment enforced
+      // that is less strict than twice its size, since that would be naturally
+      // enforced by any complex type anyways. The second element may have an
+      // alignment enforced that is less strict than its size.
+      if (Count == 0) {
+        if (MaxAlign > 2 * getContext().getTypeSize(FTSingleTy))
+          return std::nullopt;
+      } else if (Count == 1) {
+        if (MaxAlign > getContext().getTypeSize(FTSingleTy))
+          return std::nullopt;
+      }
+
+      if (const BuiltinType *BT = FTSingleTy->getAs<BuiltinType>()) {
+        switch (BT->getKind()) {
+        case BuiltinType::Float:
+        case BuiltinType::Double:
+        case BuiltinType::LongDouble:
+          if (Count == 0) {
+            elemKind = BT->getKind();
+            RetTy = FTSingleTy;
+            break;
+          } else if (elemKind == BT->getKind()) {
+            break;
+          } else {
+            return std::nullopt;
+          }
+        default:
+          return std::nullopt;
+        }
+      } else {
+        return std::nullopt;
+      }
+
+      Count++;
+    }
+    if (Count == 2) {
+      // The last thing that needs to be checked is the alignment of the struct.
+      // If we have to emit any padding (eg. because of attribute aligned), this
+      // disqualifies the type from being complex.
+      unsigned MaxAlign = RT->getDecl()->getMaxAlignment();
+      unsigned ElemSize = getContext().getTypeSize(RetTy);
+      if (MaxAlign > 2 * ElemSize)
+        return std::nullopt;
+      return RetTy;
+    }
+  }
+  return std::nullopt;
+}
+
+ABIArgInfo ZOSXPLinkABIInfo::classifyReturnType(QualType RetTy,
+                                                unsigned CallConv) const {
+
+  // Ignore void types.
+  if (RetTy->isVoidType())
+    return ABIArgInfo::getIgnore();
+
+  // For non-C calling convention, indirect by value for structs and complex.
+  if ((CallConv != llvm::CallingConv::C) &&
+      (isAggregateTypeForABI(RetTy) || RetTy->isAnyComplexType())) {
+    return getNaturalAlignIndirect(RetTy);
+  }
+
+  // Vectors are returned directly.
+  if (isVectorArgumentType(RetTy))
+    return ABIArgInfo::getDirect();
+
+  // Complex types are returned by value as per the XPLINK docs.
+  // Their members will be placed in FPRs.
+  if (RetTy->isAnyComplexType())
+    return ABIArgInfo::getDirect();
+
+  // Complex LIKE structures are returned by value as per the XPLINK docs.
+  // Their members will be placed in FPRs.
+  if (RetTy->getAs<RecordType>()) {
+    if (getFPTypeOfComplexLikeType(RetTy))
+      return ABIArgInfo::getDirect();
+  }
+
+  // Aggregates with a size of less than 3 GPRs are returned in GRPs 1, 2 and 3.
+  // Other aggregates are passed in memory as an implicit first parameter.
+  if (isAggregateTypeForABI(RetTy)) {
+    uint64_t AggregateTypeSize = getContext().getTypeSize(RetTy);
+
+    if (AggregateTypeSize <= 3 * GPRBits) {
+      uint64_t NumElements =
+          AggregateTypeSize / GPRBits + (AggregateTypeSize % GPRBits != 0);
+
+      // Types up to 8 bytes are passed as an integer type in GPR1.
+      // Types between 8 and 16 bytes are passed as integer types in GPR1, 2.
+      // Types between 16 and 24 bytes are passed as integer types in GPR1, 2
+      // and 3.
+      llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
+      if (NumElements > 1)
+        CoerceTy = llvm::ArrayType::get(CoerceTy, NumElements);
+      return ABIArgInfo::getDirectInReg(CoerceTy);
+    }
+    return getNaturalAlignIndirect(RetTy);
+  }
+
+  return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy)
+                                         : ABIArgInfo::getDirect());
+}
+
+ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty, bool IsNamedArg,
+                                                  unsigned CallConv) const {
+  // Handle the generic C++ ABI.
+  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
+    return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
+
+  // Integers and enums are extended to full register width.
+  if (isPromotableIntegerType(Ty))
+    return ABIArgInfo::getExtend(Ty);
+
+  // For non-C calling conventions, compound types passed by address copy.
+  if ((CallConv != llvm::CallingConv::C) && isCompoundType(Ty))
+    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
+
+  // Complex types are passed by value as per the XPLINK docs.
+  // If place available, their members will be placed in FPRs.
+  auto CompTy = getFPTypeOfComplexLikeType(Ty);
+  if (IsNamedArg) {
+    if (Ty->isComplexType()) {
+      auto AI = ABIArgInfo::getDirectInReg(CGT.ConvertType(Ty));
+      AI.setCanBeFlattened(false);
+      return AI;
+    }
+
+    if (CompTy.has_value()) {
+      llvm::Type *FPTy = CGT.ConvertType(*CompTy);
+      llvm::Type *CoerceTy = llvm::StructType::get(FPTy, FPTy);
+      auto AI = ABIArgInfo::getDirectInReg(CoerceTy);
+      AI.setCanBeFlattened(false);
+      return AI;
+    }
+  }
+
+  // Vectors are passed directly.
+  if (isVectorArgumentType(Ty))
+    return ABIArgInfo::getDirect();
+
+  // Handle structures. They are returned by value.
+  // If not complex like types, they are passed in GPRs, if possible.
+  // If place available, complex like types will have their members
+  // placed in FPRs.
+  if (Ty->getAs<RecordType>() || Ty->isAnyComplexType() || CompTy.has_value()) {
+    if (isAggregateTypeForABI(Ty) || Ty->isAnyComplexType() ||
+        CompTy.has_value()) {
+      // Since an aggregate may end up in registers, pass the aggregate as
+      // array. This is usually beneficial since we avoid forcing the back-end
+      // to store the argument to memory.
+      uint64_t Bits = getContext().getTypeSize(Ty);
+      llvm::Type *CoerceTy;
+
+      if (Bits <= GPRBits) {
+        // Struct types up to 8 bytes are passed as integer type (which will be
+        // properly aligned in the argument save area doubleword).
+        CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
+      } else {
+        // Larger types are passed as arrays, with the base type selected
+        // according to the required alignment in the save area.
+        uint64_t NumRegs = llvm::alignTo(Bits, GPRBits) / GPRBits;
+        llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), GPRBits);
+        CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
+      }
+
+      return ABIArgInfo::getDirect(CoerceTy);
+    }
+
+    return ABIArgInfo::getDirect();
+  }
+
+  // Non-structure compounds are passed indirectly, i.e. arrays.
+  if (isCompoundType(Ty))
+    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
+
+  return ABIArgInfo::getDirect();
+}
+
+Address ZOSXPLinkABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
+                                    QualType Ty) const {
+  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
+                          CGF.getContext().getTypeInfoInChars(Ty),
+                          CGF.getPointerSize(),
+                          /*allowHigherAlign*/ false);
+}
+
 std::unique_ptr<TargetCodeGenInfo>
 CodeGen::createSystemZTargetCodeGenInfo(CodeGenModule &CGM, bool HasVector,
                                         bool SoftFloatABI) {
   return std::make_unique<SystemZTargetCodeGenInfo>(CGM.getTypes(), HasVector,
                                                     SoftFloatABI);
 }
+
+std::unique_ptr<TargetCodeGenInfo>
+CodeGen::createSystemZ_ZOS_TargetCodeGenInfo(CodeGenModule &CGM, bool HasVector,
+                                             bool SoftFloatABI) {
+  return std::make_unique<ZOSXPLinkTargetCodeGenInfo>(CGM.getTypes(),
+                                                      HasVector);
+}
diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
new file mode 100644
index 0000000000000..1fee351475b34
--- /dev/null
+++ b/clang/test/CodeGen/zos-abi.c
@@ -0,0 +1,162 @@
+// RUN: %clang_cc1 -triple s390x-ibm-zos \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-feature +vector \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu z13 \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu arch11 \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu z14 \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu arch12 \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu z15 \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu arch13 \
+// RUN:   -emit-llvm -no-enable-noundef-analysis -o - %s | FileCheck %s
+
+// RUN: %clang_cc1 -triple s390x-ibm-zos -target-cpu arch11 \
+// RUN:   -DTEST_VEC -fzvector -emit-llvm -no-enable-noundef-analysis \
+// RUN:   -o - %s | FileCheck --check-prefixes=CHECKVEC %s
+
+// Scalar types
+
+signed char pass_schar(signed char arg) { return arg; }
+// CHECK-LABEL: define signext i8 @pass_schar(i8 signext %{{.*}})
+
+unsigned char pass_uchar(unsigned char arg) { return arg; }
+// CHECK-LABEL: define zeroext i8 @pass_uchar(i8 zeroext %{{.*}})
+
+short pass_short(short arg) { return arg; }
+// CHECK-LABEL: define signext i16 @pass_short(i16 signext %{{.*}})
+
+int pass_int(int arg) { return arg; }
+// CHECK-LABEL: define signext i32 @pass_int(i32 signext %{{.*}})
+
+long pass_long(long arg) { return arg; }
+// CHECK-LABEL: define i64 @pass_long(i64 %{{.*}})
+
+long long pass_longlong(long long arg) { return arg; }
+// CHECK-LABEL: define i64 @pass_longlong(i64 %{{.*}})
+
+float pass_float(float arg) { return arg; }
+// CHECK-LABEL: define float @pass_float(float %{{.*}})
+
+double pass_double(double arg) { return arg; }
+// CHECK-LABEL: define double @pass_double(double %{{.*}})
+
+long double pass_longdouble(long double arg) { return arg; }
+// CHECK-LABEL: define fp128 @pass_longdouble(fp128 %{{.*}})
+
+enum Color { Red, Blue };
+enum Color pass_enum(enum Color arg) { return arg; }
+// CHECK-LABEL: define zeroext i32 @pass_enum(i32 zeroext %{{.*}})
+
+#ifdef TEST_VEC
+vector unsigned int pass_vector(vector unsigned int arg) { return arg; };
+// CHECKVEC-LABEL: define <4 x i32> @pass_vector(<4 x i32> %{{.*}})
+
+struct SingleVec { vector unsigned int v; };
+struct SingleVec pass_SingleVec_agg(struct SingleVec arg) { return arg; };
+// CHECKVEC-LABEL: define inreg [2 x i64] @pass_SingleVec_agg([2 x i64] %{{.*}})
+#endif
+
+// Complex types
+
+_Complex float pass_complex_float(_Complex float arg) { return arg; }
+// CHECK-LABEL: define { float, float } @pass_complex_float({ float, float } inreg %{{.*}})
+
+_Complex double pass_complex_double(_Complex double arg) { return arg; }
+// CHECK-LABEL: define { double, double } @pass_complex_double({ double, double } inreg %{{.*}})
+
+_Complex long double pass_complex_longdouble(_Complex long double arg) { return arg; }
+// CHECK-LABEL: define { fp128, fp128 } @pass_complex_longdouble({ fp128, fp128 } inreg %{{.*}})
+
+// Verify that the following are complex-like types
+struct complexlike_float { float re, im; };
+struct complexlike_float pass_complexlike_float(struct complexlike_float arg) { return arg; }
+// CHECK-LABEL: define %struct.complexlike_float @pass_complexlike_float({ float, float } inreg %{{.*}})
+
+struct complexlike_double { double re, im; };
+struct complexlike_double pass_complexlike_double(struct complexlike_double arg) { return arg; }
+// CHECK-LABEL: define %struct.complexlike_double @pass_complexlike_double({ double, double } inreg %{{.*}})
+
+struct complexlike_longdouble { long double re, im; };
+struct complexlike_longdouble pass_complexlike_longdouble(struct complexlike_longdouble arg) { return arg; }
+// CHECK-LABEL: define %struct.complexlike_longdouble @pass_complexlike_longdouble({ fp128, fp128 } inreg %{{.*}})
+
+// Unnamed types
+
+int pass_unnamed_int(int) { return 0; }
+// CHECK-LABEL: define signext i32 @pass_unnamed_int(i32 signext %{{.*}})
+
+signed char pass_unnamed_schar(signed char) { return '0'; }
+// CHECK-LABEL: define signext i8 @pass_unnamed_schar(i8 signext %{{.*}})
+
+long double pass_unnamed_longdouble(long double) { return 0; }
+// CHECK-LABEL: define fp128 @pass_unnamed_longdouble(fp128 %{{.*}})
+
+// Aggregate types
+
+struct agg_1byte { char a[1]; };
+struct agg_1byte pass_agg_1byte(struct agg_1byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_1byte(i64 %{{.*}})
+
+struct agg_2byte { char a[2]; };
+struct agg_2byte pass_agg_2byte(struct agg_2byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_2byte(i64 %{{.*}})
+
+struct agg_3byte { char a[3]; };
+struct agg_3byte pass_agg_3byte(struct agg_3byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_3byte(i64 %{{.*}})
+
+struct agg_4byte { char a[4]; };
+struct agg_4byte pass_agg_4byte(struct agg_4byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_4byte(i64 %{{.*}})
+
+struct agg_5byte { char a[5]; };
+struct agg_5byte pass_agg_5byte(struct agg_5byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_5byte(i64 %{{.*}})
+
+struct agg_6byte { char a[6]; };
+struct agg_6byte pass_agg_6byte(struct agg_6byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_6byte(i64 %{{.*}})
+
+struct agg_7byte { char a[7]; };
+struct agg_7byte pass_agg_7byte(struct agg_7byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_7byte(i64 %{{.*}})
+
+struct agg_8byte { char a[8]; };
+struct agg_8byte pass_agg_8byte(struct agg_8byte arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_8byte(i64 %{{.*}})
+
+struct agg_9byte { char a[9]; };
+struct agg_9byte pass_agg_9byte(struct agg_9byte arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @pass_agg_9byte([2 x i64] %{{.*}})
+
+struct agg_16byte { char a[16]; };
+struct agg_16byte pass_agg_16byte(struct agg_16byte arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @pass_agg_16byte([2 x i64] %{{.*}})
+
+struct agg_24byte { char a[24]; };
+struct agg_24byte pass_agg_24byte(struct agg_24byte arg) { return arg; }
+// CHECK-LABEL: define inreg [3 x i64] @pass_agg_24byte([3 x i64] %{{.*}})
+
+struct agg_25byte { char a[25]; };
+struct agg_25byte pass_agg_25byte(struct agg_25byte arg) { return arg; }
+// CHECK-LABEL: define void @pass_agg_25byte(ptr dead_on_unwind noalias writable sret{{.*}} align 1 %{{.*}}, [4 x i64] %{{.*}})
+
+// Check that a float-like aggregate type is really passed as aggregate
+struct agg_float { float a; };
+struct agg_float pass_agg_float(struct agg_float arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_float(i64 %{{.*}})
+
+// Verify that the following are *not* float-like aggregate types
+
+struct agg_nofloat2 { float a; int b; };
+struct agg_nofloat2 pass_agg_nofloat2(struct agg_nofloat2 arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_nofloat2(i64 %{{.*}})
+
+struct agg_nofloat3 { float a; int : 0; };
+struct agg_nofloat3 pass_agg_nofloat3(struct agg_nofloat3 arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_agg_nofloat3(i64 %{{.*}})

>From b578f195523c9ba84d73a6d2b4c55c30c0f4af85 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Wed, 15 May 2024 09:40:38 -0400
Subject: [PATCH 2/9] change getDirectInReg() calls to getDirect()

---
 clang/lib/CodeGen/Targets/SystemZ.cpp |  4 ++--
 clang/test/CodeGen/zos-abi.c          | 12 ++++++------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index d420286c71c16..0f853ebd70d7f 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -851,7 +851,7 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty, bool IsNamedArg,
   auto CompTy = getFPTypeOfComplexLikeType(Ty);
   if (IsNamedArg) {
     if (Ty->isComplexType()) {
-      auto AI = ABIArgInfo::getDirectInReg(CGT.ConvertType(Ty));
+      auto AI = ABIArgInfo::getDirect(CGT.ConvertType(Ty));
       AI.setCanBeFlattened(false);
       return AI;
     }
@@ -859,7 +859,7 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty, bool IsNamedArg,
     if (CompTy.has_value()) {
       llvm::Type *FPTy = CGT.ConvertType(*CompTy);
       llvm::Type *CoerceTy = llvm::StructType::get(FPTy, FPTy);
-      auto AI = ABIArgInfo::getDirectInReg(CoerceTy);
+      auto AI = ABIArgInfo::getDirect(CoerceTy);
       AI.setCanBeFlattened(false);
       return AI;
     }
diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
index 1fee351475b34..4d75b0b0444ee 100644
--- a/clang/test/CodeGen/zos-abi.c
+++ b/clang/test/CodeGen/zos-abi.c
@@ -64,26 +64,26 @@ struct SingleVec pass_SingleVec_agg(struct SingleVec arg) { return arg; };
 // Complex types
 
 _Complex float pass_complex_float(_Complex float arg) { return arg; }
-// CHECK-LABEL: define { float, float } @pass_complex_float({ float, float } inreg %{{.*}})
+// CHECK-LABEL: define { float, float } @pass_complex_float({ float, float } %{{.*}})
 
 _Complex double pass_complex_double(_Complex double arg) { return arg; }
-// CHECK-LABEL: define { double, double } @pass_complex_double({ double, double } inreg %{{.*}})
+// CHECK-LABEL: define { double, double } @pass_complex_double({ double, double } %{{.*}})
 
 _Complex long double pass_complex_longdouble(_Complex long double arg) { return arg; }
-// CHECK-LABEL: define { fp128, fp128 } @pass_complex_longdouble({ fp128, fp128 } inreg %{{.*}})
+// CHECK-LABEL: define { fp128, fp128 } @pass_complex_longdouble({ fp128, fp128 } %{{.*}})
 
 // Verify that the following are complex-like types
 struct complexlike_float { float re, im; };
 struct complexlike_float pass_complexlike_float(struct complexlike_float arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_float @pass_complexlike_float({ float, float } inreg %{{.*}})
+// CHECK-LABEL: define %struct.complexlike_float @pass_complexlike_float({ float, float } %{{.*}})
 
 struct complexlike_double { double re, im; };
 struct complexlike_double pass_complexlike_double(struct complexlike_double arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_double @pass_complexlike_double({ double, double } inreg %{{.*}})
+// CHECK-LABEL: define %struct.complexlike_double @pass_complexlike_double({ double, double } %{{.*}})
 
 struct complexlike_longdouble { long double re, im; };
 struct complexlike_longdouble pass_complexlike_longdouble(struct complexlike_longdouble arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_longdouble @pass_complexlike_longdouble({ fp128, fp128 } inreg %{{.*}})
+// CHECK-LABEL: define %struct.complexlike_longdouble @pass_complexlike_longdouble({ fp128, fp128 } %{{.*}})
 
 // Unnamed types
 

>From 40c7e6472715c43601aa3e4b607d82b7b3f1cb2d Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Wed, 15 May 2024 09:50:30 -0400
Subject: [PATCH 3/9] remove calling conventions

---
 clang/lib/CodeGen/Targets/SystemZ.cpp | 29 +++++++--------------------
 1 file changed, 7 insertions(+), 22 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index 0f853ebd70d7f..8200c0a7e1a5b 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -551,23 +551,19 @@ class ZOSXPLinkABIInfo : public ABIInfo {
   unsigned getMaxAlignFromTypeDefs(QualType Ty) const;
   std::optional<QualType> getFPTypeOfComplexLikeType(QualType Ty) const;
 
-  ABIArgInfo classifyReturnType(QualType RetTy,
-                                unsigned functionCallConv) const;
-  ABIArgInfo classifyArgumentType(QualType ArgTy, bool IsNamedArg,
-                                  unsigned functionCallConv) const;
+  ABIArgInfo classifyReturnType(QualType RetTy) const;
+  ABIArgInfo classifyArgumentType(QualType ArgTy, bool IsNamedArg) const;
 
   void computeInfo(CGFunctionInfo &FI) const override {
     if (!getCXXABI().classifyReturnType(FI))
-      FI.getReturnInfo() =
-          classifyReturnType(FI.getReturnType(), FI.getCallingConvention());
+      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
 
     unsigned NumRequiredArgs = FI.getNumRequiredArgs();
     unsigned ArgNo = 0;
 
     for (auto &I : FI.arguments()) {
       bool IsNamedArg = ArgNo < NumRequiredArgs;
-      I.info =
-          classifyArgumentType(I.type, IsNamedArg, FI.getCallingConvention());
+      I.info = classifyArgumentType(I.type, IsNamedArg);
       ++ArgNo;
     }
   }
@@ -778,19 +774,12 @@ ZOSXPLinkABIInfo::getFPTypeOfComplexLikeType(QualType Ty) const {
   return std::nullopt;
 }
 
-ABIArgInfo ZOSXPLinkABIInfo::classifyReturnType(QualType RetTy,
-                                                unsigned CallConv) const {
+ABIArgInfo ZOSXPLinkABIInfo::classifyReturnType(QualType RetTy) const {
 
   // Ignore void types.
   if (RetTy->isVoidType())
     return ABIArgInfo::getIgnore();
 
-  // For non-C calling convention, indirect by value for structs and complex.
-  if ((CallConv != llvm::CallingConv::C) &&
-      (isAggregateTypeForABI(RetTy) || RetTy->isAnyComplexType())) {
-    return getNaturalAlignIndirect(RetTy);
-  }
-
   // Vectors are returned directly.
   if (isVectorArgumentType(RetTy))
     return ABIArgInfo::getDirect();
@@ -832,8 +821,8 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyReturnType(QualType RetTy,
                                          : ABIArgInfo::getDirect());
 }
 
-ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty, bool IsNamedArg,
-                                                  unsigned CallConv) const {
+ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty,
+                                                  bool IsNamedArg) const {
   // Handle the generic C++ ABI.
   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
@@ -842,10 +831,6 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty, bool IsNamedArg,
   if (isPromotableIntegerType(Ty))
     return ABIArgInfo::getExtend(Ty);
 
-  // For non-C calling conventions, compound types passed by address copy.
-  if ((CallConv != llvm::CallingConv::C) && isCompoundType(Ty))
-    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
-
   // Complex types are passed by value as per the XPLINK docs.
   // If place available, their members will be placed in FPRs.
   auto CompTy = getFPTypeOfComplexLikeType(Ty);

>From 5f7ab8aaa35a75434c3ac37499cd154bdc7823e6 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Wed, 15 May 2024 14:27:51 -0400
Subject: [PATCH 4/9] Use only one check on struct size; Get rid of all the
 alignment checks

---
 clang/lib/CodeGen/Targets/SystemZ.cpp | 21 +++------------------
 clang/test/CodeGen/zos-abi.c          | 22 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index 8200c0a7e1a5b..10324ba17a7ac 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -719,23 +719,8 @@ ZOSXPLinkABIInfo::getFPTypeOfComplexLikeType(QualType Ty) const {
       if (Count >= 2)
         return std::nullopt;
 
-      unsigned MaxAlignOnDecl = FD->getMaxAlignment();
       QualType FT = FD->getType();
       QualType FTSingleTy = getSingleElementType(FT);
-      unsigned MaxAlign =
-          std::max(getMaxAlignFromTypeDefs(FTSingleTy), MaxAlignOnDecl);
-
-      // The first element of a complex type may have an alignment enforced
-      // that is less strict than twice its size, since that would be naturally
-      // enforced by any complex type anyways. The second element may have an
-      // alignment enforced that is less strict than its size.
-      if (Count == 0) {
-        if (MaxAlign > 2 * getContext().getTypeSize(FTSingleTy))
-          return std::nullopt;
-      } else if (Count == 1) {
-        if (MaxAlign > getContext().getTypeSize(FTSingleTy))
-          return std::nullopt;
-      }
 
       if (const BuiltinType *BT = FTSingleTy->getAs<BuiltinType>()) {
         switch (BT->getKind()) {
@@ -761,12 +746,12 @@ ZOSXPLinkABIInfo::getFPTypeOfComplexLikeType(QualType Ty) const {
       Count++;
     }
     if (Count == 2) {
-      // The last thing that needs to be checked is the alignment of the struct.
+      // The last thing that needs to be checked is the size of the struct.
       // If we have to emit any padding (eg. because of attribute aligned), this
       // disqualifies the type from being complex.
-      unsigned MaxAlign = RT->getDecl()->getMaxAlignment();
+      unsigned RecordSize = getContext().getTypeSize(RT);
       unsigned ElemSize = getContext().getTypeSize(RetTy);
-      if (MaxAlign > 2 * ElemSize)
+      if (RecordSize > 2 * ElemSize)
         return std::nullopt;
       return RetTy;
     }
diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
index 4d75b0b0444ee..9964f8c4d1bfc 100644
--- a/clang/test/CodeGen/zos-abi.c
+++ b/clang/test/CodeGen/zos-abi.c
@@ -85,6 +85,28 @@ struct complexlike_longdouble { long double re, im; };
 struct complexlike_longdouble pass_complexlike_longdouble(struct complexlike_longdouble arg) { return arg; }
 // CHECK-LABEL: define %struct.complexlike_longdouble @pass_complexlike_longdouble({ fp128, fp128 } %{{.*}})
 
+// Structures with extra padding are not considered complex types.
+struct complexlike_float_padded1 {
+  float x __attribute__((aligned(8)));
+  float y __attribute__((aligned(8)));
+};
+struct complexlike_float_padded1 pass_complexlike_float_padded1(struct complexlike_float_padded1 arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @pass_complexlike_float_padded1([2 x i64] %{{.*}})
+struct complexlike_float_padded2 {
+  float x;
+  float y;
+} __attribute__((aligned(16)));
+struct complexlike_float_padded2 pass_complexlike_float_padded2(struct complexlike_float_padded2 arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @pass_complexlike_float_padded2([2 x i64] %{{.*}})
+
+typedef double align32_double __attribute__((aligned(32)));
+struct complexlike_double_padded {
+  align32_double x;
+  double y;
+};
+struct complexlike_double_padded pass_complexlike_double_padded(struct complexlike_double_padded arg) { return arg; }
+// CHECK-LABEL: define void @pass_complexlike_double_padded(ptr {{.*}} sret(%struct.complexlike_double_padded) align 32 %{{.*}}, [4 x i64] %{{.*}})
+
 // Unnamed types
 
 int pass_unnamed_int(int) { return 0; }

>From 68df066851c3feb19af74e48e21cb1e6f1b57600 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Wed, 15 May 2024 15:48:55 -0400
Subject: [PATCH 5/9] remove unwanted tests

---
 clang/test/CodeGen/zos-abi.c | 11 -----------
 1 file changed, 11 deletions(-)

diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
index 9964f8c4d1bfc..3c2f290bc6ebb 100644
--- a/clang/test/CodeGen/zos-abi.c
+++ b/clang/test/CodeGen/zos-abi.c
@@ -107,17 +107,6 @@ struct complexlike_double_padded {
 struct complexlike_double_padded pass_complexlike_double_padded(struct complexlike_double_padded arg) { return arg; }
 // CHECK-LABEL: define void @pass_complexlike_double_padded(ptr {{.*}} sret(%struct.complexlike_double_padded) align 32 %{{.*}}, [4 x i64] %{{.*}})
 
-// Unnamed types
-
-int pass_unnamed_int(int) { return 0; }
-// CHECK-LABEL: define signext i32 @pass_unnamed_int(i32 signext %{{.*}})
-
-signed char pass_unnamed_schar(signed char) { return '0'; }
-// CHECK-LABEL: define signext i8 @pass_unnamed_schar(i8 signext %{{.*}})
-
-long double pass_unnamed_longdouble(long double) { return 0; }
-// CHECK-LABEL: define fp128 @pass_unnamed_longdouble(fp128 %{{.*}})
-
 // Aggregate types
 
 struct agg_1byte { char a[1]; };

>From 77459fa9ee42bf90eab20889deaf18b657927914 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Thu, 16 May 2024 13:22:08 -0400
Subject: [PATCH 6/9] remove Ty->getAs<RecordType>() and keep only
 isAggregateTypeForABI(Ty)

---
 clang/lib/CodeGen/Targets/SystemZ.cpp | 42 ++++++++++++---------------
 1 file changed, 19 insertions(+), 23 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index 10324ba17a7ac..cd76ce319c64b 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -843,31 +843,27 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty,
   // If not complex like types, they are passed in GPRs, if possible.
   // If place available, complex like types will have their members
   // placed in FPRs.
-  if (Ty->getAs<RecordType>() || Ty->isAnyComplexType() || CompTy.has_value()) {
-    if (isAggregateTypeForABI(Ty) || Ty->isAnyComplexType() ||
-        CompTy.has_value()) {
-      // Since an aggregate may end up in registers, pass the aggregate as
-      // array. This is usually beneficial since we avoid forcing the back-end
-      // to store the argument to memory.
-      uint64_t Bits = getContext().getTypeSize(Ty);
-      llvm::Type *CoerceTy;
-
-      if (Bits <= GPRBits) {
-        // Struct types up to 8 bytes are passed as integer type (which will be
-        // properly aligned in the argument save area doubleword).
-        CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
-      } else {
-        // Larger types are passed as arrays, with the base type selected
-        // according to the required alignment in the save area.
-        uint64_t NumRegs = llvm::alignTo(Bits, GPRBits) / GPRBits;
-        llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), GPRBits);
-        CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
-      }
-
-      return ABIArgInfo::getDirect(CoerceTy);
+  if (isAggregateTypeForABI(Ty) || Ty->isAnyComplexType() ||
+      CompTy.has_value()) {
+    // Since an aggregate may end up in registers, pass the aggregate as
+    // array. This is usually beneficial since we avoid forcing the back-end
+    // to store the argument to memory.
+    uint64_t Bits = getContext().getTypeSize(Ty);
+    llvm::Type *CoerceTy;
+
+    if (Bits <= GPRBits) {
+      // Struct types up to 8 bytes are passed as integer type (which will be
+      // properly aligned in the argument save area doubleword).
+      CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
+    } else {
+      // Larger types are passed as arrays, with the base type selected
+      // according to the required alignment in the save area.
+      uint64_t NumRegs = llvm::alignTo(Bits, GPRBits) / GPRBits;
+      llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), GPRBits);
+      CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
     }
 
-    return ABIArgInfo::getDirect();
+    return ABIArgInfo::getDirect(CoerceTy);
   }
 
   // Non-structure compounds are passed indirectly, i.e. arrays.

>From ca5e1e3aebaaa75117ec4fe9e446f165db378742 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Wed, 22 May 2024 09:35:58 -0400
Subject: [PATCH 7/9] Modify getSingleElementType() function and usage for
 z/OS. Add more test cases for single elem structs as requested

---
 clang/lib/CodeGen/Targets/SystemZ.cpp | 46 ++++++++++++--------
 clang/test/CodeGen/zos-abi.c          | 61 +++++++++++++++++++++++++++
 clang/test/CodeGen/zos-abi.cpp        | 24 +++++++++++
 3 files changed, 114 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/zos-abi.cpp

diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index cd76ce319c64b..13ef5a1537fa0 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -637,37 +637,47 @@ bool ZOSXPLinkABIInfo::isFPArgumentType(QualType Ty) const {
 }
 
 QualType ZOSXPLinkABIInfo::getSingleElementType(QualType Ty) const {
-  if (const RecordType *RT = Ty->getAsStructureType()) {
+  const RecordType *RT = Ty->getAs<RecordType>();
+
+  if (RT && RT->isStructureOrClassType()) {
     const RecordDecl *RD = RT->getDecl();
     QualType Found;
 
     // If this is a C++ record, check the bases first.
     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
-      for (const auto &I : CXXRD->bases()) {
-        QualType Base = I.getType();
+      if (CXXRD->hasDefinition())
+        for (const auto &I : CXXRD->bases()) {
+          QualType Base = I.getType();
 
-        // Empty bases don't affect things either way.
-        if (isEmptyRecord(getContext(), Base, true))
-          continue;
+          // Empty bases don't affect things either way.
+          if (isEmptyRecord(getContext(), Base, true))
+            continue;
 
-        if (!Found.isNull())
-          return Ty;
-        Found = getSingleElementType(Base);
-      }
+          if (!Found.isNull())
+            return Ty;
+          Found = getSingleElementType(Base);
+        }
 
     // Check the fields.
     for (const auto *FD : RD->fields()) {
-      // Unlike isSingleElementStruct(), empty structure and array fields
-      // do count.  So do anonymous bitfields that aren't zero-sized.
-      if (getContext().getLangOpts().CPlusPlus &&
-          FD->isZeroLengthBitField(getContext()))
+      QualType FT = FD->getType();
+
+      // Ignore empty fields.
+      if (isEmptyField(getContext(), FD, true))
         continue;
 
-      // Unlike isSingleElementStruct(), arrays do not count.
-      // Nested structures still do though.
       if (!Found.isNull())
         return Ty;
-      Found = getSingleElementType(FD->getType());
+
+      // Treat single element arrays as the element.
+      while (const ConstantArrayType *AT =
+                 getContext().getAsConstantArrayType(FT)) {
+        if (AT->getZExtSize() != 1)
+          break;
+        FT = AT->getElementType();
+      }
+
+      Found = getSingleElementType(FT);
     }
 
     // Unlike isSingleElementStruct(), trailing padding is allowed.
@@ -721,6 +731,8 @@ ZOSXPLinkABIInfo::getFPTypeOfComplexLikeType(QualType Ty) const {
 
       QualType FT = FD->getType();
       QualType FTSingleTy = getSingleElementType(FT);
+      if (getContext().getTypeSize(FTSingleTy) != getContext().getTypeSize(FT))
+        return std::nullopt;
 
       if (const BuiltinType *BT = FTSingleTy->getAs<BuiltinType>()) {
         switch (BT->getKind()) {
diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
index 3c2f290bc6ebb..dfe81dfce3a60 100644
--- a/clang/test/CodeGen/zos-abi.c
+++ b/clang/test/CodeGen/zos-abi.c
@@ -85,6 +85,47 @@ struct complexlike_longdouble { long double re, im; };
 struct complexlike_longdouble pass_complexlike_longdouble(struct complexlike_longdouble arg) { return arg; }
 // CHECK-LABEL: define %struct.complexlike_longdouble @pass_complexlike_longdouble({ fp128, fp128 } %{{.*}})
 
+struct single_element_float { float f; };
+struct complexlike_struct {
+  struct single_element_float x;
+  struct single_element_float y;
+};
+struct complexlike_struct pass_complexlike_struct(struct complexlike_struct arg) { return arg; }
+// CHECK-LABEL: define %struct.complexlike_struct @pass_complexlike_struct({ float, float } %{{.*}})
+
+struct single_element_float_arr {
+  unsigned int :0;
+  float f[1];
+};
+struct complexlike_struct2 {
+  struct single_element_float_arr x;
+  struct single_element_float_arr y;
+};
+struct complexlike_struct2 pass_complexlike_struct2(struct complexlike_struct2 arg) { return arg; }
+// CHECK-LABEL: define %struct.complexlike_struct2 @pass_complexlike_struct2({ float, float } %{{.*}})
+
+struct float_and_empties {
+  struct S {} s;
+  int a[0];
+  float f;
+};
+struct complexlike_struct3 {
+  struct float_and_empties x;
+  struct float_and_empties y;
+};
+struct complexlike_struct3 pass_complexlike_struct3(struct complexlike_struct3 arg) { return arg; }
+// CHECK-LABEL: define %struct.complexlike_struct3 @pass_complexlike_struct3({ float, float } %{{.*}})
+
+
+
+// structures with one field as complex type are not considered complex types.
+
+struct single_complex_struct {
+  _Complex float f;
+};
+struct single_complex_struct pass_single_complex_struct(struct single_complex_struct arg) {return arg; }
+// CHECK-LABEL: define inreg i64 @pass_single_complex_struct(i64 %{{.*}})
+
 // Structures with extra padding are not considered complex types.
 struct complexlike_float_padded1 {
   float x __attribute__((aligned(8)));
@@ -92,6 +133,7 @@ struct complexlike_float_padded1 {
 };
 struct complexlike_float_padded1 pass_complexlike_float_padded1(struct complexlike_float_padded1 arg) { return arg; }
 // CHECK-LABEL: define inreg [2 x i64] @pass_complexlike_float_padded1([2 x i64] %{{.*}})
+
 struct complexlike_float_padded2 {
   float x;
   float y;
@@ -99,6 +141,25 @@ struct complexlike_float_padded2 {
 struct complexlike_float_padded2 pass_complexlike_float_padded2(struct complexlike_float_padded2 arg) { return arg; }
 // CHECK-LABEL: define inreg [2 x i64] @pass_complexlike_float_padded2([2 x i64] %{{.*}})
 
+struct single_padded_struct {
+  float f;
+  unsigned int :2;
+};
+struct complexlike_float_padded3 {
+  struct single_padded_struct x;
+  struct single_padded_struct y;
+};
+struct complexlike_float_padded3 pass_complexlike_float_padded3(struct complexlike_float_padded3 arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @pass_complexlike_float_padded3([2 x i64] %{{.*}})
+
+struct multi_element_float_arr { float f[2]; };
+struct complexlike_struct4 {
+  struct multi_element_float_arr x;
+  struct multi_element_float_arr y;
+};
+struct complexlike_struct4 pass_complexlike_struct4(struct complexlike_struct4 arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @pass_complexlike_struct4([2 x i64] %{{.*}})
+
 typedef double align32_double __attribute__((aligned(32)));
 struct complexlike_double_padded {
   align32_double x;
diff --git a/clang/test/CodeGen/zos-abi.cpp b/clang/test/CodeGen/zos-abi.cpp
new file mode 100644
index 0000000000000..7dd7efefdfb28
--- /dev/null
+++ b/clang/test/CodeGen/zos-abi.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple s390x-ibm-zos -emit-llvm -x c++ -o - %s | FileCheck %s
+
+struct empty { };
+struct agg_nofloat_empty { float a; empty dummy; };
+struct complex_like_agg_nofloat_empty { struct agg_nofloat_empty a; struct agg_nofloat_empty b; };
+struct complex_like_agg_nofloat_empty pass_complex_like_agg_nofloat_empty(struct complex_like_agg_nofloat_empty arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @_Z35pass_complex_like_agg_nofloat_empty30complex_like_agg_nofloat_empty([2 x i64] %{{.*}})
+
+struct agg_float_empty { float a; [[no_unique_address]] empty dummy; };
+struct complex_like_agg_float_empty { struct agg_float_empty a; struct agg_float_empty b; };
+struct complex_like_agg_float_empty pass_complex_like_agg_float_empty(struct complex_like_agg_float_empty arg) { return arg; }
+// CHECK-LABEL: define %struct.complex_like_agg_float_empty @_Z33pass_complex_like_agg_float_empty28complex_like_agg_float_empty({ float, float } %{{.*}})
+
+struct noemptybase { empty dummy; };
+struct agg_nofloat_emptybase : noemptybase { float a; };
+struct complex_like_agg_nofloat_emptybase { struct agg_nofloat_emptybase a; struct agg_nofloat_emptybase b; };
+struct complex_like_agg_nofloat_emptybase pass_agg_nofloat_emptybase(struct complex_like_agg_nofloat_emptybase arg) { return arg; }
+// CHECK-LABEL: define inreg [2 x i64] @_Z26pass_agg_nofloat_emptybase34complex_like_agg_nofloat_emptybase([2 x i64] %{{.*}})
+
+struct emptybase { [[no_unique_address]] empty dummy; };
+struct agg_float_emptybase : emptybase { float a; };
+struct complex_like_agg_float_emptybase { struct agg_float_emptybase a; struct agg_float_emptybase b; };
+struct complex_like_agg_float_emptybase pass_agg_float_emptybase(struct complex_like_agg_float_emptybase arg) { return arg; }
+// CHECK-LABEL: define %struct.complex_like_agg_float_emptybase @_Z24pass_agg_float_emptybase32complex_like_agg_float_emptybase({ float, float } %{{.*}})

>From 5e89c20f16516274142f442c9d1d6a36bf0373f5 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Wed, 29 May 2024 10:34:58 -0400
Subject: [PATCH 8/9] Address Kai's comments, modify complex-like return type
 to return by value

---
 clang/lib/CodeGen/Targets/SystemZ.cpp | 22 ++++++++++------------
 clang/test/CodeGen/zos-abi.c          | 12 ++++++------
 clang/test/CodeGen/zos-abi.cpp        |  4 ++--
 3 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp
index 13ef5a1537fa0..e17d3b35e0750 100644
--- a/clang/lib/CodeGen/Targets/SystemZ.cpp
+++ b/clang/lib/CodeGen/Targets/SystemZ.cpp
@@ -789,8 +789,13 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyReturnType(QualType RetTy) const {
   // Complex LIKE structures are returned by value as per the XPLINK docs.
   // Their members will be placed in FPRs.
   if (RetTy->getAs<RecordType>()) {
-    if (getFPTypeOfComplexLikeType(RetTy))
-      return ABIArgInfo::getDirect();
+    if (auto CompTy = getFPTypeOfComplexLikeType(RetTy)) {
+      llvm::Type *FPTy = CGT.ConvertType(*CompTy);
+      llvm::Type *CoerceTy = llvm::StructType::get(FPTy, FPTy);
+      auto AI = ABIArgInfo::getDirect(CoerceTy);
+      AI.setCanBeFlattened(false);
+      return AI;
+    }
   }
 
   // Aggregates with a size of less than 3 GPRs are returned in GRPs 1, 2 and 3.
@@ -830,7 +835,6 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty,
 
   // Complex types are passed by value as per the XPLINK docs.
   // If place available, their members will be placed in FPRs.
-  auto CompTy = getFPTypeOfComplexLikeType(Ty);
   if (IsNamedArg) {
     if (Ty->isComplexType()) {
       auto AI = ABIArgInfo::getDirect(CGT.ConvertType(Ty));
@@ -838,7 +842,7 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty,
       return AI;
     }
 
-    if (CompTy.has_value()) {
+    if (auto CompTy = getFPTypeOfComplexLikeType(Ty)) {
       llvm::Type *FPTy = CGT.ConvertType(*CompTy);
       llvm::Type *CoerceTy = llvm::StructType::get(FPTy, FPTy);
       auto AI = ABIArgInfo::getDirect(CoerceTy);
@@ -853,10 +857,7 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty,
 
   // Handle structures. They are returned by value.
   // If not complex like types, they are passed in GPRs, if possible.
-  // If place available, complex like types will have their members
-  // placed in FPRs.
-  if (isAggregateTypeForABI(Ty) || Ty->isAnyComplexType() ||
-      CompTy.has_value()) {
+  if (isAggregateTypeForABI(Ty) || Ty->isAnyComplexType()) {
     // Since an aggregate may end up in registers, pass the aggregate as
     // array. This is usually beneficial since we avoid forcing the back-end
     // to store the argument to memory.
@@ -878,10 +879,7 @@ ABIArgInfo ZOSXPLinkABIInfo::classifyArgumentType(QualType Ty,
     return ABIArgInfo::getDirect(CoerceTy);
   }
 
-  // Non-structure compounds are passed indirectly, i.e. arrays.
-  if (isCompoundType(Ty))
-    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
-
+  // Other types. E,g. pointers.
   return ABIArgInfo::getDirect();
 }
 
diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
index dfe81dfce3a60..83f7c5cff16ee 100644
--- a/clang/test/CodeGen/zos-abi.c
+++ b/clang/test/CodeGen/zos-abi.c
@@ -75,15 +75,15 @@ _Complex long double pass_complex_longdouble(_Complex long double arg) { return
 // Verify that the following are complex-like types
 struct complexlike_float { float re, im; };
 struct complexlike_float pass_complexlike_float(struct complexlike_float arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_float @pass_complexlike_float({ float, float } %{{.*}})
+// CHECK-LABEL: define { float, float } @pass_complexlike_float({ float, float } %{{.*}})
 
 struct complexlike_double { double re, im; };
 struct complexlike_double pass_complexlike_double(struct complexlike_double arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_double @pass_complexlike_double({ double, double } %{{.*}})
+// CHECK-LABEL: define { double, double } @pass_complexlike_double({ double, double } %{{.*}})
 
 struct complexlike_longdouble { long double re, im; };
 struct complexlike_longdouble pass_complexlike_longdouble(struct complexlike_longdouble arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_longdouble @pass_complexlike_longdouble({ fp128, fp128 } %{{.*}})
+// CHECK-LABEL: define { fp128, fp128 } @pass_complexlike_longdouble({ fp128, fp128 } %{{.*}})
 
 struct single_element_float { float f; };
 struct complexlike_struct {
@@ -91,7 +91,7 @@ struct complexlike_struct {
   struct single_element_float y;
 };
 struct complexlike_struct pass_complexlike_struct(struct complexlike_struct arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_struct @pass_complexlike_struct({ float, float } %{{.*}})
+// CHECK-LABEL: define { float, float } @pass_complexlike_struct({ float, float } %{{.*}})
 
 struct single_element_float_arr {
   unsigned int :0;
@@ -102,7 +102,7 @@ struct complexlike_struct2 {
   struct single_element_float_arr y;
 };
 struct complexlike_struct2 pass_complexlike_struct2(struct complexlike_struct2 arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_struct2 @pass_complexlike_struct2({ float, float } %{{.*}})
+// CHECK-LABEL: define { float, float } @pass_complexlike_struct2({ float, float } %{{.*}})
 
 struct float_and_empties {
   struct S {} s;
@@ -114,7 +114,7 @@ struct complexlike_struct3 {
   struct float_and_empties y;
 };
 struct complexlike_struct3 pass_complexlike_struct3(struct complexlike_struct3 arg) { return arg; }
-// CHECK-LABEL: define %struct.complexlike_struct3 @pass_complexlike_struct3({ float, float } %{{.*}})
+// CHECK-LABEL: define { float, float } @pass_complexlike_struct3({ float, float } %{{.*}})
 
 
 
diff --git a/clang/test/CodeGen/zos-abi.cpp b/clang/test/CodeGen/zos-abi.cpp
index 7dd7efefdfb28..a662db6f59d1b 100644
--- a/clang/test/CodeGen/zos-abi.cpp
+++ b/clang/test/CodeGen/zos-abi.cpp
@@ -9,7 +9,7 @@ struct complex_like_agg_nofloat_empty pass_complex_like_agg_nofloat_empty(struct
 struct agg_float_empty { float a; [[no_unique_address]] empty dummy; };
 struct complex_like_agg_float_empty { struct agg_float_empty a; struct agg_float_empty b; };
 struct complex_like_agg_float_empty pass_complex_like_agg_float_empty(struct complex_like_agg_float_empty arg) { return arg; }
-// CHECK-LABEL: define %struct.complex_like_agg_float_empty @_Z33pass_complex_like_agg_float_empty28complex_like_agg_float_empty({ float, float } %{{.*}})
+// CHECK-LABEL: define { float, float } @_Z33pass_complex_like_agg_float_empty28complex_like_agg_float_empty({ float, float } %{{.*}})
 
 struct noemptybase { empty dummy; };
 struct agg_nofloat_emptybase : noemptybase { float a; };
@@ -21,4 +21,4 @@ struct emptybase { [[no_unique_address]] empty dummy; };
 struct agg_float_emptybase : emptybase { float a; };
 struct complex_like_agg_float_emptybase { struct agg_float_emptybase a; struct agg_float_emptybase b; };
 struct complex_like_agg_float_emptybase pass_agg_float_emptybase(struct complex_like_agg_float_emptybase arg) { return arg; }
-// CHECK-LABEL: define %struct.complex_like_agg_float_emptybase @_Z24pass_agg_float_emptybase32complex_like_agg_float_emptybase({ float, float } %{{.*}})
+// CHECK-LABEL: define { float, float } @_Z24pass_agg_float_emptybase32complex_like_agg_float_emptybase({ float, float } %{{.*}})

>From 4680962e496f5a78dc668ec7f95d0b855d532a53 Mon Sep 17 00:00:00 2001
From: Fanbo Meng <Fanbo.Meng at ibm.com>
Date: Thu, 30 May 2024 09:06:08 -0400
Subject: [PATCH 9/9] add test for union, ptr, vector

---
 clang/test/CodeGen/zos-abi.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/CodeGen/zos-abi.c b/clang/test/CodeGen/zos-abi.c
index 83f7c5cff16ee..f0896edc8ac69 100644
--- a/clang/test/CodeGen/zos-abi.c
+++ b/clang/test/CodeGen/zos-abi.c
@@ -116,7 +116,13 @@ struct complexlike_struct3 {
 struct complexlike_struct3 pass_complexlike_struct3(struct complexlike_struct3 arg) { return arg; }
 // CHECK-LABEL: define { float, float } @pass_complexlike_struct3({ float, float } %{{.*}})
 
-
+union two_float_union { float a; float b; };
+struct complexlike_struct_with_union {
+  float a;
+  union two_float_union b;
+};
+struct complexlike_struct_with_union pass_complexlike_struct_with_union(struct complexlike_struct_with_union arg) { return arg; }
+// CHECK-LABEL: define inreg i64 @pass_complexlike_struct_with_union(i64 %{{.*}})
 
 // structures with one field as complex type are not considered complex types.
 
@@ -232,3 +238,10 @@ struct agg_nofloat2 pass_agg_nofloat2(struct agg_nofloat2 arg) { return arg; }
 struct agg_nofloat3 { float a; int : 0; };
 struct agg_nofloat3 pass_agg_nofloat3(struct agg_nofloat3 arg) { return arg; }
 // CHECK-LABEL: define inreg i64 @pass_agg_nofloat3(i64 %{{.*}})
+
+char * pass_pointer(char * arg) { return arg; }
+// CHECK-LABEL: define ptr @pass_pointer(ptr %{{.*}})
+
+typedef int vecint __attribute__ ((vector_size(16)));
+vecint pass_vector_type(vecint arg) { return arg; }
+// CHECK-LABEL: define <4 x i32> @pass_vector_type(<4 x i32> %{{.*}})



More information about the cfe-commits mailing list