[llvm] [VFABI] Add support for vector functions that return struct types (PR #119000)
Benjamin Maxwell via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 13 04:33:46 PST 2024
================
@@ -0,0 +1,149 @@
+//===------- VectorUtilsTest.cpp - Vector utils 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/VectorUtils.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/LLVMContext.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+class VectorUtilsTest : public ::testing::Test {};
+
+TEST(VectorUtilsTest, TestToWideTy) {
+ LLVMContext C;
+
+ Type *ITy = Type::getInt32Ty(C);
+ Type *FTy = Type::getFloatTy(C);
+ Type *HomogeneousStructTy = StructType::get(FTy, FTy, FTy);
+ Type *MixedStructTy = StructType::get(FTy, ITy);
+ Type *VoidTy = Type::getVoidTy(C);
+
+ for (ElementCount VF :
+ {ElementCount::getFixed(4), ElementCount::getScalable(2)}) {
+ Type *IntVec = ToWideTy(ITy, VF);
+ EXPECT_TRUE(isa<VectorType>(IntVec));
+ EXPECT_EQ(IntVec, VectorType::get(ITy, VF));
+
+ Type *FloatVec = ToWideTy(FTy, VF);
+ EXPECT_TRUE(isa<VectorType>(FloatVec));
+ EXPECT_EQ(FloatVec, VectorType::get(FTy, VF));
+
+ Type *WideHomogeneousStructTy = ToWideTy(HomogeneousStructTy, VF);
+ EXPECT_TRUE(isa<StructType>(WideHomogeneousStructTy));
+ EXPECT_TRUE(
+ cast<StructType>(WideHomogeneousStructTy)->containsHomogeneousTypes());
+ EXPECT_TRUE(cast<StructType>(WideHomogeneousStructTy)->getNumElements() ==
+ 3);
+ EXPECT_TRUE(cast<StructType>(WideHomogeneousStructTy)->getElementType(0) ==
+ VectorType::get(FTy, VF));
+
+ Type *WideMixedStructTy = ToWideTy(MixedStructTy, VF);
+ EXPECT_TRUE(isa<StructType>(WideMixedStructTy));
+ EXPECT_TRUE(cast<StructType>(WideMixedStructTy)->getNumElements() == 2);
+ EXPECT_TRUE(cast<StructType>(WideMixedStructTy)->getElementType(0) ==
+ VectorType::get(FTy, VF));
+ EXPECT_TRUE(cast<StructType>(WideMixedStructTy)->getElementType(1) ==
+ VectorType::get(ITy, VF));
+
+ EXPECT_EQ(ToWideTy(VoidTy, VF), VoidTy);
+ }
+
+ ElementCount ScalarVF = ElementCount::getFixed(1);
+ for (Type *Ty : {ITy, FTy, HomogeneousStructTy, MixedStructTy, VoidTy}) {
+ EXPECT_EQ(ToWideTy(Ty, ScalarVF), Ty);
+ }
+}
+
+TEST(VectorUtilsTest, TestToNarrowTy) {
+ LLVMContext C;
+
+ Type *ITy = Type::getInt32Ty(C);
+ Type *FTy = Type::getFloatTy(C);
+ Type *HomogeneousStructTy = StructType::get(FTy, FTy, FTy);
+ Type *MixedStructTy = StructType::get(FTy, ITy);
+ Type *VoidTy = Type::getVoidTy(C);
+
+ for (ElementCount VF : {ElementCount::getFixed(1), ElementCount::getFixed(4),
+ ElementCount::getScalable(2)}) {
+ for (Type *Ty : {ITy, FTy, HomogeneousStructTy, MixedStructTy, VoidTy}) {
+ // ToNarrowTy should be the inverse of ToWideTy.
+ EXPECT_EQ(ToNarrowTy(ToWideTy(Ty, VF)), Ty);
+ };
+ }
+}
+
+TEST(VectorUtilsTest, TestGetContainedTypes) {
+ LLVMContext C;
+
+ Type *ITy = Type::getInt32Ty(C);
+ Type *FTy = Type::getFloatTy(C);
+ Type *HomogeneousStructTy = StructType::get(FTy, FTy, FTy);
+ Type *MixedStructTy = StructType::get(FTy, ITy);
+ Type *VoidTy = Type::getVoidTy(C);
+
+ EXPECT_EQ(getContainedTypes(ITy), ArrayRef<Type *>({ITy}));
+ EXPECT_EQ(getContainedTypes(FTy), ArrayRef<Type *>({FTy}));
+ EXPECT_EQ(getContainedTypes(VoidTy), ArrayRef<Type *>({VoidTy}));
+ EXPECT_EQ(getContainedTypes(HomogeneousStructTy),
+ ArrayRef<Type *>({FTy, FTy, FTy}));
+ EXPECT_EQ(getContainedTypes(MixedStructTy), ArrayRef<Type *>({FTy, ITy}));
----------------
MacDue wrote:
Done :+1: (added negative test)
https://github.com/llvm/llvm-project/pull/119000
More information about the llvm-commits
mailing list