[llvm] [ABI] Hoist isSingleElementStruct into TargetInfo and fix array reduction (PR #224994)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 20 23:00:38 PDT 2026
https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/224994
>From f11d157332a5662072f517e1e18ac26f4f651276 Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Mon, 21 Sep 2026 10:48:48 +0530
Subject: [PATCH 1/2] [ABI] Hoist isSingleElementStruct into TargetInfo and fix
array reduction
---
llvm/include/llvm/ABI/TargetInfo.h | 3 +
llvm/lib/ABI/TargetInfo.cpp | 57 ++++++++++++++++
llvm/lib/ABI/Targets/X86.cpp | 55 ---------------
llvm/unittests/ABI/X86TargetInfoTest.cpp | 87 ++++++++++++++++++++++++
4 files changed, 147 insertions(+), 55 deletions(-)
diff --git a/llvm/include/llvm/ABI/TargetInfo.h b/llvm/include/llvm/ABI/TargetInfo.h
index 6f7f29e70bb3d..50c2a591162be 100644
--- a/llvm/include/llvm/ABI/TargetInfo.h
+++ b/llvm/include/llvm/ABI/TargetInfo.h
@@ -92,6 +92,9 @@ class TargetInfo {
/// return Ty unchanged.
LLVM_ABI const Type *useFirstFieldIfTransparentUnion(const Type *Ty) const;
+ /// Returns the scalar a single-element struct reduces to, else null.
+ LLVM_ABI const Type *isSingleElementStruct(const Type *Ty) const;
+
/// Apply rules for classifying return types that are common to all targets.
LLVM_ABI bool maybeCommonClassifyReturnType(FunctionInfo &FI) const;
diff --git a/llvm/lib/ABI/TargetInfo.cpp b/llvm/lib/ABI/TargetInfo.cpp
index 9ad8cb8f5829a..933c7c53c069f 100644
--- a/llvm/lib/ABI/TargetInfo.cpp
+++ b/llvm/lib/ABI/TargetInfo.cpp
@@ -71,6 +71,63 @@ const Type *TargetInfo::useFirstFieldIfTransparentUnion(const Type *Ty) const {
return Ty;
}
+const Type *TargetInfo::isSingleElementStruct(const Type *Ty) const {
+ const auto *RT = dyn_cast<RecordType>(Ty);
+ if (!RT)
+ return nullptr;
+
+ if (RT->hasFlexibleArrayMember())
+ return nullptr;
+
+ const Type *Found = nullptr;
+
+ for (const auto &Base : RT->getBaseClasses()) {
+ const Type *BaseTy = Base.FieldType;
+ const auto *BaseRT = dyn_cast<RecordType>(BaseTy);
+
+ if (!BaseRT || BaseRT->isEmpty())
+ continue;
+
+ const Type *Elem = isSingleElementStruct(BaseTy);
+ if (!Elem || Found)
+ return nullptr;
+ Found = Elem;
+ }
+
+ for (const auto &FI : RT->getFields()) {
+ if (FI.isEmpty())
+ continue;
+
+ const Type *FTy = FI.FieldType;
+
+ // Treat single element arrays as the element.
+ while (const auto *AT = dyn_cast<ArrayType>(FTy)) {
+ if (AT->getNumElements() != 1)
+ break;
+ FTy = AT->getElementType();
+ }
+
+ const Type *Elem;
+ if (!isAggregateTypeForABI(FTy))
+ Elem = FTy;
+ else
+ Elem = isSingleElementStruct(FTy);
+ if (!Elem || Found)
+ return nullptr;
+ Found = Elem;
+ }
+
+ if (!Found)
+ return nullptr;
+
+ // We don't consider a struct a single-element struct if it has padding
+ // beyond the element type.
+ if (Found->getSizeInBits() != Ty->getSizeInBits())
+ return nullptr;
+
+ return Found;
+}
+
bool TargetInfo::maybeCommonClassifyReturnType(FunctionInfo &FI) const {
const abi::Type *Ty = FI.getReturnType();
diff --git a/llvm/lib/ABI/Targets/X86.cpp b/llvm/lib/ABI/Targets/X86.cpp
index 78aa9cd1a2519..20f174db5d698 100644
--- a/llvm/lib/ABI/Targets/X86.cpp
+++ b/llvm/lib/ABI/Targets/X86.cpp
@@ -98,7 +98,6 @@ class X86_64TargetInfo : public TargetInfo {
ArgInfo getIndirectReturnResult(const Type *Ty) const;
const Type *getFPTypeAtOffset(const Type *Ty, unsigned Offset) const;
- const Type *isSingleElementStruct(const Type *Ty) const;
const Type *getByteVectorType(const Type *Ty) const;
const Type *createPairType(const Type *Lo, const Type *Hi) const;
@@ -1261,60 +1260,6 @@ const Type *X86_64TargetInfo::getByteVectorType(const Type *Ty) const {
ElementCount::getFixed(Size / 64), Align(Size / 8));
}
-// Returns the single element if this is a single-element struct wrapper
-const Type *X86_64TargetInfo::isSingleElementStruct(const Type *Ty) const {
- const auto *RT = dyn_cast<RecordType>(Ty);
- if (!RT)
- return nullptr;
-
- if (RT->hasFlexibleArrayMember())
- return nullptr;
-
- const Type *Found = nullptr;
-
- for (const auto &Base : RT->getBaseClasses()) {
- const Type *BaseTy = Base.FieldType;
- auto *BaseRT = dyn_cast<RecordType>(BaseTy);
-
- if (!BaseRT || BaseRT->isEmpty())
- continue;
-
- const Type *Elem = isSingleElementStruct(BaseTy);
- if (!Elem || Found)
- return nullptr;
- Found = Elem;
- }
-
- for (const auto &FI : RT->getFields()) {
- if (FI.isEmpty())
- continue;
-
- const Type *FTy = FI.FieldType;
-
- while (auto *AT = dyn_cast<ArrayType>(FTy)) {
- if (AT->getNumElements() != 1)
- break;
- FTy = AT->getElementType();
- }
-
- const Type *Elem;
- if (auto *InnerRT = dyn_cast<RecordType>(FTy))
- Elem = isSingleElementStruct(InnerRT);
- else
- Elem = FTy;
- if (!Elem || Found)
- return nullptr;
- Found = Elem;
- }
-
- if (!Found)
- return nullptr;
- if (Found->getSizeInBits() != Ty->getSizeInBits())
- return nullptr;
-
- return Found;
-}
-
bool X86_64TargetInfo::isIllegalVectorType(const Type *Ty) const {
if (const auto *VecTy = dyn_cast<VectorType>(Ty)) {
uint64_t Size = VecTy->getSizeInBits().getFixedValue();
diff --git a/llvm/unittests/ABI/X86TargetInfoTest.cpp b/llvm/unittests/ABI/X86TargetInfoTest.cpp
index 4cf5bc68cc213..8455ca45d5885 100644
--- a/llvm/unittests/ABI/X86TargetInfoTest.cpp
+++ b/llvm/unittests/ABI/X86TargetInfoTest.cpp
@@ -33,11 +33,22 @@ using llvm::abi::TypeBuilder;
using llvm::abi::X86ABICompatInfo;
using llvm::abi::X86AVXABILevel;
+// A minimal concrete target that re-exposes the shared, protected
+// isSingleElementStruct helper so it can be exercised directly. The helper is
+// target-independent, so any concrete target classifies it the same way.
+class TestTargetInfo : public TargetInfo {
+public:
+ explicit TestTargetInfo(TypeBuilder &Builder) : TargetInfo(Builder) {}
+ void computeInfo(FunctionInfo &) const override {}
+ using TargetInfo::isSingleElementStruct;
+};
+
class X86TargetInfoTest : public ::testing::Test {
protected:
llvm::BumpPtrAllocator Alloc;
TypeBuilder TB;
const ABIType *I8;
+ const ABIType *I16;
const ABIType *I32;
const ABIType *F32;
const ABIType *F64;
@@ -50,6 +61,7 @@ class X86TargetInfoTest : public ::testing::Test {
X86TargetInfoTest()
: TB(Alloc), I8(TB.getIntegerType(8, llvm::Align(1), /*Signed=*/true)),
+ I16(TB.getIntegerType(16, llvm::Align(2), /*Signed=*/true)),
I32(TB.getIntegerType(32, llvm::Align(4), /*Signed=*/true)),
F32(TB.getFloatType(llvm::APFloat::IEEEsingle(), llvm::Align(4))),
F64(TB.getFloatType(llvm::APFloat::IEEEdouble(), llvm::Align(8))),
@@ -77,6 +89,20 @@ class X86TargetInfoTest : public ::testing::Test {
Flags | RecordFlags::CanPassInRegisters);
}
+ const ABIType *recordOf(llvm::ArrayRef<FieldInfo> Fields, uint64_t SizeInBits,
+ llvm::Align Alignment) {
+ return TB.getRecordType(Fields, llvm::TypeSize::getFixed(SizeInBits),
+ Alignment, Alignment, StructPacking::Default, {},
+ {}, RecordFlags::CanPassInRegisters);
+ }
+
+ /// The single-element reduction of \p Ty, or null if it is not a
+ /// single-element struct. Exercises the shared TargetInfo helper directly.
+ const ABIType *singleElement(const ABIType *Ty) {
+ TestTargetInfo TI(TB);
+ return TI.isSingleElementStruct(Ty);
+ }
+
/// The argument classification the target computes for a single parameter.
const ArgInfo &classifyArg(const ABIType *ArgTy,
std::unique_ptr<FunctionInfo> &FI,
@@ -397,4 +423,65 @@ TEST_F(X86TargetInfoTest, UnionTailPaddingNarrowsInsideShortUnion) {
expectInteger(Pair[1].FieldType, 8);
}
+// The shared single-element-struct reduction, used by getByteVectorType and by
+// other targets, looks through single-element wrappers to the scalar element.
+
+// A struct of one float reduces to its single float element.
+TEST_F(X86TargetInfoTest, SingleElementStructSingleScalarFieldReduces) {
+ const ABIType *S = recordOf({FieldInfo(F32, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), F32);
+}
+
+// A single-element array is transparent, so a struct of one float[1] reduces
+// to the element type.
+TEST_F(X86TargetInfoTest, SingleElementStructSingleElementArrayReduces) {
+ const ABIType *Arr =
+ TB.getArrayType(F32, /*NumElements=*/1, /*SizeInBits=*/32);
+ const ABIType *S = recordOf({FieldInfo(Arr, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), F32);
+}
+
+// A multi-element array is an aggregate that does not itself reduce, so a
+// struct of one short[2] is NOT a single-element struct.
+TEST_F(X86TargetInfoTest, SingleElementStructMultiElementArrayDoesNotReduce) {
+ const ABIType *Arr =
+ TB.getArrayType(I16, /*NumElements=*/2, /*SizeInBits=*/32);
+ const ABIType *S = recordOf({FieldInfo(Arr, 0)}, 32, llvm::Align(2));
+ EXPECT_EQ(singleElement(S), nullptr);
+}
+
+// Two data members: not a single-element struct.
+TEST_F(X86TargetInfoTest, SingleElementStructTwoFieldsDoNotReduce) {
+ const ABIType *S =
+ recordOf({FieldInfo(I32, 0), FieldInfo(I32, 32)}, 64, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), nullptr);
+}
+
+// A single member that leaves tail padding does not cover the record, so it
+// does not reduce.
+TEST_F(X86TargetInfoTest, SingleElementStructTailPaddingDoesNotReduce) {
+ const ABIType *S = recordOf({FieldInfo(I32, 0)}, 64, llvm::Align(8));
+ EXPECT_EQ(singleElement(S), nullptr);
+}
+
+// An empty member supplies no data and is skipped, so a struct of an empty
+// member plus an int reduces to the int.
+TEST_F(X86TargetInfoTest, SingleElementStructEmptyMemberIsSkipped) {
+ const ABIType *S =
+ recordOf({FieldInfo(Empty, 0), FieldInfo(I32, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), I32);
+}
+
+// A nested single-element struct reduces to the inner scalar.
+TEST_F(X86TargetInfoTest, SingleElementStructNestedSingleElementReduces) {
+ const ABIType *Inner = recordOf({FieldInfo(F32, 0)}, 32, llvm::Align(4));
+ const ABIType *Outer = recordOf({FieldInfo(Inner, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(Outer), F32);
+}
+
+// A non-record type is never a single-element struct.
+TEST_F(X86TargetInfoTest, SingleElementStructNonRecordReturnsNull) {
+ EXPECT_EQ(singleElement(I32), nullptr);
+}
+
} // namespace
>From 90a20bb15d6d612adeb3404cf7ee069011274476 Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Mon, 21 Sep 2026 11:29:25 +0530
Subject: [PATCH 2/2] move tests to TargetInfoTest.cpp
---
llvm/unittests/ABI/CMakeLists.txt | 1 +
llvm/unittests/ABI/TargetInfoTest.cpp | 147 ++++++++++++++++++
llvm/unittests/ABI/X86TargetInfoTest.cpp | 87 -----------
.../gn/secondary/llvm/unittests/ABI/BUILD.gn | 1 +
4 files changed, 149 insertions(+), 87 deletions(-)
create mode 100644 llvm/unittests/ABI/TargetInfoTest.cpp
diff --git a/llvm/unittests/ABI/CMakeLists.txt b/llvm/unittests/ABI/CMakeLists.txt
index 4a1910ab8809b..07c1cdbb9d80e 100644
--- a/llvm/unittests/ABI/CMakeLists.txt
+++ b/llvm/unittests/ABI/CMakeLists.txt
@@ -8,6 +8,7 @@ add_llvm_unittest(ABITests
AArch64TargetInfoTest.cpp
IRTypeMapperTest.cpp
FunctionInfoTest.cpp
+ TargetInfoTest.cpp
X86TargetInfoTest.cpp
TypesTest.cpp
)
diff --git a/llvm/unittests/ABI/TargetInfoTest.cpp b/llvm/unittests/ABI/TargetInfoTest.cpp
new file mode 100644
index 0000000000000..c26d2cf6bea9a
--- /dev/null
+++ b/llvm/unittests/ABI/TargetInfoTest.cpp
@@ -0,0 +1,147 @@
+//===- TargetInfoTest.cpp - shared ABI TargetInfo unit tests --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Tests for target-independent helpers on the TargetInfo base class. These are
+// exercised through a minimal concrete target so any new shared default lands
+// here rather than in a per-target test file.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ABI/TargetInfo.h"
+#include "llvm/ABI/FunctionInfo.h"
+#include "llvm/ABI/Types.h"
+#include "llvm/ADT/APFloat.h"
+#include "llvm/Support/Alignment.h"
+#include "llvm/Support/Allocator.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+// RecordFlags' bitmask operators are declared in namespace llvm, so combining
+// two of them needs that namespace visible.
+using namespace llvm;
+
+using ABIType = llvm::abi::Type;
+using llvm::abi::FieldInfo;
+using llvm::abi::FunctionInfo;
+using llvm::abi::RecordFlags;
+using llvm::abi::StructPacking;
+using llvm::abi::TargetInfo;
+using llvm::abi::TypeBuilder;
+
+// A minimal concrete target that re-exposes the shared, protected helpers so
+// they can be exercised directly, independent of any real target.
+class TestTargetInfo : public TargetInfo {
+public:
+ explicit TestTargetInfo(TypeBuilder &Builder) : TargetInfo(Builder) {}
+ void computeInfo(FunctionInfo &) const override {}
+ const llvm::abi::ABICompatInfo &getABICompatInfo() const override {
+ return Compat;
+ }
+ using TargetInfo::isSingleElementStruct;
+
+private:
+ llvm::abi::ABICompatInfo Compat;
+};
+
+class TargetInfoTest : public ::testing::Test {
+protected:
+ llvm::BumpPtrAllocator Alloc;
+ TypeBuilder TB;
+ const ABIType *I16;
+ const ABIType *I32;
+ const ABIType *F32;
+ /// An empty class: a record with no fields, one byte wide.
+ const ABIType *Empty;
+
+ TargetInfoTest()
+ : TB(Alloc), I16(TB.getIntegerType(16, llvm::Align(2), /*Signed=*/true)),
+ I32(TB.getIntegerType(32, llvm::Align(4), /*Signed=*/true)),
+ F32(TB.getFloatType(llvm::APFloat::IEEEsingle(), llvm::Align(4))),
+ Empty(TB.getRecordType({}, llvm::TypeSize::getFixed(8), llvm::Align(1),
+ /*UnadjustedAlign=*/llvm::Align(1),
+ StructPacking::Default, {}, {},
+ RecordFlags::CanPassInRegisters)) {}
+
+ const ABIType *recordOf(llvm::ArrayRef<FieldInfo> Fields, uint64_t SizeInBits,
+ llvm::Align Alignment) {
+ return TB.getRecordType(Fields, llvm::TypeSize::getFixed(SizeInBits),
+ Alignment, Alignment, StructPacking::Default, {},
+ {}, RecordFlags::CanPassInRegisters);
+ }
+
+ /// The single-element reduction of \p Ty, or null if it is not a
+ /// single-element struct. Exercises the shared TargetInfo helper directly.
+ const ABIType *singleElement(const ABIType *Ty) {
+ TestTargetInfo TI(TB);
+ return TI.isSingleElementStruct(Ty);
+ }
+};
+
+// The shared single-element-struct reduction, used by getByteVectorType and by
+// other targets, looks through single-element wrappers to the scalar element.
+
+// A struct of one float reduces to its single float element.
+TEST_F(TargetInfoTest, SingleElementStructSingleScalarFieldReduces) {
+ const ABIType *S = recordOf({FieldInfo(F32, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), F32);
+}
+
+// A single-element array is transparent, so a struct of one float[1] reduces
+// to the element type.
+TEST_F(TargetInfoTest, SingleElementStructSingleElementArrayReduces) {
+ const ABIType *Arr =
+ TB.getArrayType(F32, /*NumElements=*/1, /*SizeInBits=*/32);
+ const ABIType *S = recordOf({FieldInfo(Arr, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), F32);
+}
+
+// A multi-element array is an aggregate that does not itself reduce, so a
+// struct of one short[2] is NOT a single-element struct.
+TEST_F(TargetInfoTest, SingleElementStructMultiElementArrayDoesNotReduce) {
+ const ABIType *Arr =
+ TB.getArrayType(I16, /*NumElements=*/2, /*SizeInBits=*/32);
+ const ABIType *S = recordOf({FieldInfo(Arr, 0)}, 32, llvm::Align(2));
+ EXPECT_EQ(singleElement(S), nullptr);
+}
+
+// Two data members: not a single-element struct.
+TEST_F(TargetInfoTest, SingleElementStructTwoFieldsDoNotReduce) {
+ const ABIType *S =
+ recordOf({FieldInfo(I32, 0), FieldInfo(I32, 32)}, 64, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), nullptr);
+}
+
+// A single member that leaves tail padding does not cover the record, so it
+// does not reduce.
+TEST_F(TargetInfoTest, SingleElementStructTailPaddingDoesNotReduce) {
+ const ABIType *S = recordOf({FieldInfo(I32, 0)}, 64, llvm::Align(8));
+ EXPECT_EQ(singleElement(S), nullptr);
+}
+
+// An empty member supplies no data and is skipped, so a struct of an empty
+// member plus an int reduces to the int.
+TEST_F(TargetInfoTest, SingleElementStructEmptyMemberIsSkipped) {
+ const ABIType *S =
+ recordOf({FieldInfo(Empty, 0), FieldInfo(I32, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(S), I32);
+}
+
+// A nested single-element struct reduces to the inner scalar.
+TEST_F(TargetInfoTest, SingleElementStructNestedSingleElementReduces) {
+ const ABIType *Inner = recordOf({FieldInfo(F32, 0)}, 32, llvm::Align(4));
+ const ABIType *Outer = recordOf({FieldInfo(Inner, 0)}, 32, llvm::Align(4));
+ EXPECT_EQ(singleElement(Outer), F32);
+}
+
+// A non-record type is never a single-element struct.
+TEST_F(TargetInfoTest, SingleElementStructNonRecordReturnsNull) {
+ EXPECT_EQ(singleElement(I32), nullptr);
+}
+
+} // namespace
diff --git a/llvm/unittests/ABI/X86TargetInfoTest.cpp b/llvm/unittests/ABI/X86TargetInfoTest.cpp
index 8455ca45d5885..4cf5bc68cc213 100644
--- a/llvm/unittests/ABI/X86TargetInfoTest.cpp
+++ b/llvm/unittests/ABI/X86TargetInfoTest.cpp
@@ -33,22 +33,11 @@ using llvm::abi::TypeBuilder;
using llvm::abi::X86ABICompatInfo;
using llvm::abi::X86AVXABILevel;
-// A minimal concrete target that re-exposes the shared, protected
-// isSingleElementStruct helper so it can be exercised directly. The helper is
-// target-independent, so any concrete target classifies it the same way.
-class TestTargetInfo : public TargetInfo {
-public:
- explicit TestTargetInfo(TypeBuilder &Builder) : TargetInfo(Builder) {}
- void computeInfo(FunctionInfo &) const override {}
- using TargetInfo::isSingleElementStruct;
-};
-
class X86TargetInfoTest : public ::testing::Test {
protected:
llvm::BumpPtrAllocator Alloc;
TypeBuilder TB;
const ABIType *I8;
- const ABIType *I16;
const ABIType *I32;
const ABIType *F32;
const ABIType *F64;
@@ -61,7 +50,6 @@ class X86TargetInfoTest : public ::testing::Test {
X86TargetInfoTest()
: TB(Alloc), I8(TB.getIntegerType(8, llvm::Align(1), /*Signed=*/true)),
- I16(TB.getIntegerType(16, llvm::Align(2), /*Signed=*/true)),
I32(TB.getIntegerType(32, llvm::Align(4), /*Signed=*/true)),
F32(TB.getFloatType(llvm::APFloat::IEEEsingle(), llvm::Align(4))),
F64(TB.getFloatType(llvm::APFloat::IEEEdouble(), llvm::Align(8))),
@@ -89,20 +77,6 @@ class X86TargetInfoTest : public ::testing::Test {
Flags | RecordFlags::CanPassInRegisters);
}
- const ABIType *recordOf(llvm::ArrayRef<FieldInfo> Fields, uint64_t SizeInBits,
- llvm::Align Alignment) {
- return TB.getRecordType(Fields, llvm::TypeSize::getFixed(SizeInBits),
- Alignment, Alignment, StructPacking::Default, {},
- {}, RecordFlags::CanPassInRegisters);
- }
-
- /// The single-element reduction of \p Ty, or null if it is not a
- /// single-element struct. Exercises the shared TargetInfo helper directly.
- const ABIType *singleElement(const ABIType *Ty) {
- TestTargetInfo TI(TB);
- return TI.isSingleElementStruct(Ty);
- }
-
/// The argument classification the target computes for a single parameter.
const ArgInfo &classifyArg(const ABIType *ArgTy,
std::unique_ptr<FunctionInfo> &FI,
@@ -423,65 +397,4 @@ TEST_F(X86TargetInfoTest, UnionTailPaddingNarrowsInsideShortUnion) {
expectInteger(Pair[1].FieldType, 8);
}
-// The shared single-element-struct reduction, used by getByteVectorType and by
-// other targets, looks through single-element wrappers to the scalar element.
-
-// A struct of one float reduces to its single float element.
-TEST_F(X86TargetInfoTest, SingleElementStructSingleScalarFieldReduces) {
- const ABIType *S = recordOf({FieldInfo(F32, 0)}, 32, llvm::Align(4));
- EXPECT_EQ(singleElement(S), F32);
-}
-
-// A single-element array is transparent, so a struct of one float[1] reduces
-// to the element type.
-TEST_F(X86TargetInfoTest, SingleElementStructSingleElementArrayReduces) {
- const ABIType *Arr =
- TB.getArrayType(F32, /*NumElements=*/1, /*SizeInBits=*/32);
- const ABIType *S = recordOf({FieldInfo(Arr, 0)}, 32, llvm::Align(4));
- EXPECT_EQ(singleElement(S), F32);
-}
-
-// A multi-element array is an aggregate that does not itself reduce, so a
-// struct of one short[2] is NOT a single-element struct.
-TEST_F(X86TargetInfoTest, SingleElementStructMultiElementArrayDoesNotReduce) {
- const ABIType *Arr =
- TB.getArrayType(I16, /*NumElements=*/2, /*SizeInBits=*/32);
- const ABIType *S = recordOf({FieldInfo(Arr, 0)}, 32, llvm::Align(2));
- EXPECT_EQ(singleElement(S), nullptr);
-}
-
-// Two data members: not a single-element struct.
-TEST_F(X86TargetInfoTest, SingleElementStructTwoFieldsDoNotReduce) {
- const ABIType *S =
- recordOf({FieldInfo(I32, 0), FieldInfo(I32, 32)}, 64, llvm::Align(4));
- EXPECT_EQ(singleElement(S), nullptr);
-}
-
-// A single member that leaves tail padding does not cover the record, so it
-// does not reduce.
-TEST_F(X86TargetInfoTest, SingleElementStructTailPaddingDoesNotReduce) {
- const ABIType *S = recordOf({FieldInfo(I32, 0)}, 64, llvm::Align(8));
- EXPECT_EQ(singleElement(S), nullptr);
-}
-
-// An empty member supplies no data and is skipped, so a struct of an empty
-// member plus an int reduces to the int.
-TEST_F(X86TargetInfoTest, SingleElementStructEmptyMemberIsSkipped) {
- const ABIType *S =
- recordOf({FieldInfo(Empty, 0), FieldInfo(I32, 0)}, 32, llvm::Align(4));
- EXPECT_EQ(singleElement(S), I32);
-}
-
-// A nested single-element struct reduces to the inner scalar.
-TEST_F(X86TargetInfoTest, SingleElementStructNestedSingleElementReduces) {
- const ABIType *Inner = recordOf({FieldInfo(F32, 0)}, 32, llvm::Align(4));
- const ABIType *Outer = recordOf({FieldInfo(Inner, 0)}, 32, llvm::Align(4));
- EXPECT_EQ(singleElement(Outer), F32);
-}
-
-// A non-record type is never a single-element struct.
-TEST_F(X86TargetInfoTest, SingleElementStructNonRecordReturnsNull) {
- EXPECT_EQ(singleElement(I32), nullptr);
-}
-
} // namespace
diff --git a/llvm/utils/gn/secondary/llvm/unittests/ABI/BUILD.gn b/llvm/utils/gn/secondary/llvm/unittests/ABI/BUILD.gn
index 4c34fdf7647bd..e424496f45150 100644
--- a/llvm/utils/gn/secondary/llvm/unittests/ABI/BUILD.gn
+++ b/llvm/utils/gn/secondary/llvm/unittests/ABI/BUILD.gn
@@ -10,6 +10,7 @@ unittest("ABITests") {
"AArch64TargetInfoTest.cpp",
"IRTypeMapperTest.cpp",
"FunctionInfoTest.cpp",
+ "TargetInfoTest.cpp",
"X86TargetInfoTest.cpp",
"TypesTest.cpp",
]
More information about the llvm-commits
mailing list