diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h index 5ee4aaad9fd2..d443ce55d428 100644 --- a/llvm/include/llvm/CodeGen/TargetLowering.h +++ b/llvm/include/llvm/CodeGen/TargetLowering.h @@ -1245,7 +1245,7 @@ public: EltTy = PointerTy.getTypeForEVT(Ty->getContext()); } return EVT::getVectorVT(Ty->getContext(), EVT::getEVT(EltTy, false), - VTy->getNumElements()); + VTy->getElementCount()); } return EVT::getEVT(Ty, AllowUnknown); diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h index c540c94f79d9..cd4c4ca64081 100644 --- a/llvm/include/llvm/CodeGen/ValueTypes.h +++ b/llvm/include/llvm/CodeGen/ValueTypes.h @@ -81,7 +81,7 @@ namespace llvm { /// Returns the EVT that represents a vector EC.Min elements in length, /// where each element is of type VT. - static EVT getVectorVT(LLVMContext &Context, EVT VT, MVT::ElementCount EC) { + static EVT getVectorVT(LLVMContext &Context, EVT VT, ElementCount EC) { MVT M = MVT::getVectorVT(VT.V, EC); if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE) return M; @@ -277,7 +277,7 @@ namespace llvm { } // Given a (possibly scalable) vector type, return the ElementCount - MVT::ElementCount getVectorElementCount() const { + ElementCount getVectorElementCount() const { assert((isVector()) && "Invalid vector type!"); if (isSimple()) return V.getVectorElementCount(); diff --git a/llvm/include/llvm/Support/MachineValueType.h b/llvm/include/llvm/Support/MachineValueType.h index b94d2c4836cc..96bc7b680b19 100644 --- a/llvm/include/llvm/Support/MachineValueType.h +++ b/llvm/include/llvm/Support/MachineValueType.h @@ -17,6 +17,7 @@ #include "llvm/ADT/iterator_range.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/ScalableSize.h" #include namespace llvm { @@ -253,41 +254,6 @@ namespace llvm { SimpleValueType SimpleTy = INVALID_SIMPLE_VALUE_TYPE; - // A class to represent the number of elements in a vector - // - // For fixed-length vectors, the total number of elements is equal to 'Min' - // For scalable vectors, the total number of elements is a multiple of 'Min' - class ElementCount { - public: - unsigned Min; - bool Scalable; - - ElementCount(unsigned Min, bool Scalable) - : Min(Min), Scalable(Scalable) {} - - ElementCount operator*(unsigned RHS) { - return { Min * RHS, Scalable }; - } - - ElementCount& operator*=(unsigned RHS) { - Min *= RHS; - return *this; - } - - ElementCount operator/(unsigned RHS) { - return { Min / RHS, Scalable }; - } - - ElementCount& operator/=(unsigned RHS) { - Min /= RHS; - return *this; - } - - bool operator==(const ElementCount& RHS) { - return Min == RHS.Min && Scalable == RHS.Scalable; - } - }; - constexpr MVT() = default; constexpr MVT(SimpleValueType SVT) : SimpleTy(SVT) {} @@ -664,7 +630,7 @@ namespace llvm { } } - MVT::ElementCount getVectorElementCount() const { + ElementCount getVectorElementCount() const { return { getVectorNumElements(), isScalableVector() }; } @@ -1054,7 +1020,7 @@ namespace llvm { return getVectorVT(VT, NumElements); } - static MVT getVectorVT(MVT VT, MVT::ElementCount EC) { + static MVT getVectorVT(MVT VT, ElementCount EC) { if (EC.Scalable) return getScalableVectorVT(VT, EC.Min); return getVectorVT(VT, EC.Min); diff --git a/llvm/lib/CodeGen/ValueTypes.cpp b/llvm/lib/CodeGen/ValueTypes.cpp index a911cdcbec9d..40c147923174 100644 --- a/llvm/lib/CodeGen/ValueTypes.cpp +++ b/llvm/lib/CodeGen/ValueTypes.cpp @@ -115,8 +115,8 @@ std::string EVT::getEVTString() const { switch (V.SimpleTy) { default: if (isVector()) - return "v" + utostr(getVectorNumElements()) + - getVectorElementType().getEVTString(); + return (isScalableVector() ? "nxv" : "v") + utostr(getVectorNumElements()) + + getVectorElementType().getEVTString(); if (isInteger()) return "i" + utostr(getSizeInBits()); llvm_unreachable("Invalid EVT!"); @@ -205,6 +205,48 @@ std::string EVT::getEVTString() const { case MVT::v2f64: return "v2f64"; case MVT::v4f64: return "v4f64"; case MVT::v8f64: return "v8f64"; + case MVT::nxv1i1: return "nxv1i1"; + case MVT::nxv2i1: return "nxv2i1"; + case MVT::nxv4i1: return "nxv4i1"; + case MVT::nxv8i1: return "nxv8i1"; + case MVT::nxv16i1: return "nxv16i1"; + case MVT::nxv32i1: return "nxv32i1"; + case MVT::nxv1i8: return "nxv1i8"; + case MVT::nxv2i8: return "nxv2i8"; + case MVT::nxv4i8: return "nxv4i8"; + case MVT::nxv8i8: return "nxv8i8"; + case MVT::nxv16i8: return "nxv16i8"; + case MVT::nxv32i8: return "nxv32i8"; + case MVT::nxv1i16: return "nxv1i16"; + case MVT::nxv2i16: return "nxv2i16"; + case MVT::nxv4i16: return "nxv4i16"; + case MVT::nxv8i16: return "nxv8i16"; + case MVT::nxv16i16:return "nxv16i16"; + case MVT::nxv32i16:return "nxv32i16"; + case MVT::nxv1i32: return "nxv1i32"; + case MVT::nxv2i32: return "nxv2i32"; + case MVT::nxv4i32: return "nxv4i32"; + case MVT::nxv8i32: return "nxv8i32"; + case MVT::nxv16i32:return "nxv16i32"; + case MVT::nxv32i32:return "nxv32i32"; + case MVT::nxv1i64: return "nxv1i64"; + case MVT::nxv2i64: return "nxv2i64"; + case MVT::nxv4i64: return "nxv4i64"; + case MVT::nxv8i64: return "nxv8i64"; + case MVT::nxv16i64:return "nxv16i64"; + case MVT::nxv32i64:return "nxv32i64"; + case MVT::nxv2f16: return "nxv2f16"; + case MVT::nxv4f16: return "nxv4f16"; + case MVT::nxv8f16: return "nxv8f16"; + case MVT::nxv1f32: return "nxv1f32"; + case MVT::nxv2f32: return "nxv2f32"; + case MVT::nxv4f32: return "nxv4f32"; + case MVT::nxv8f32: return "nxv8f32"; + case MVT::nxv16f32:return "nxv16f32"; + case MVT::nxv1f64: return "nxv1f64"; + case MVT::nxv2f64: return "nxv2f64"; + case MVT::nxv4f64: return "nxv4f64"; + case MVT::nxv8f64: return "nxv8f64"; case MVT::Metadata:return "Metadata"; case MVT::Untyped: return "Untyped"; case MVT::exnref : return "exnref"; @@ -302,8 +344,92 @@ Type *EVT::getTypeForEVT(LLVMContext &Context) const { case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2); case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4); case MVT::v8f64: return VectorType::get(Type::getDoubleTy(Context), 8); + case MVT::nxv1i1: + return VectorType::get(Type::getInt1Ty(Context), 1, /*Scalable=*/ true); + case MVT::nxv2i1: + return VectorType::get(Type::getInt1Ty(Context), 2, /*Scalable=*/ true); + case MVT::nxv4i1: + return VectorType::get(Type::getInt1Ty(Context), 4, /*Scalable=*/ true); + case MVT::nxv8i1: + return VectorType::get(Type::getInt1Ty(Context), 8, /*Scalable=*/ true); + case MVT::nxv16i1: + return VectorType::get(Type::getInt1Ty(Context), 16, /*Scalable=*/ true); + case MVT::nxv32i1: + return VectorType::get(Type::getInt1Ty(Context), 32, /*Scalable=*/ true); + case MVT::nxv1i8: + return VectorType::get(Type::getInt8Ty(Context), 1, /*Scalable=*/ true); + case MVT::nxv2i8: + return VectorType::get(Type::getInt8Ty(Context), 2, /*Scalable=*/ true); + case MVT::nxv4i8: + return VectorType::get(Type::getInt8Ty(Context), 4, /*Scalable=*/ true); + case MVT::nxv8i8: + return VectorType::get(Type::getInt8Ty(Context), 8, /*Scalable=*/ true); + case MVT::nxv16i8: + return VectorType::get(Type::getInt8Ty(Context), 16, /*Scalable=*/ true); + case MVT::nxv32i8: + return VectorType::get(Type::getInt8Ty(Context), 32, /*Scalable=*/ true); + case MVT::nxv1i16: + return VectorType::get(Type::getInt16Ty(Context), 1, /*Scalable=*/ true); + case MVT::nxv2i16: + return VectorType::get(Type::getInt16Ty(Context), 2, /*Scalable=*/ true); + case MVT::nxv4i16: + return VectorType::get(Type::getInt16Ty(Context), 4, /*Scalable=*/ true); + case MVT::nxv8i16: + return VectorType::get(Type::getInt16Ty(Context), 8, /*Scalable=*/ true); + case MVT::nxv16i16: + return VectorType::get(Type::getInt16Ty(Context), 16, /*Scalable=*/ true); + case MVT::nxv32i16: + return VectorType::get(Type::getInt16Ty(Context), 32, /*Scalable=*/ true); + case MVT::nxv1i32: + return VectorType::get(Type::getInt32Ty(Context), 1, /*Scalable=*/ true); + case MVT::nxv2i32: + return VectorType::get(Type::getInt32Ty(Context), 2, /*Scalable=*/ true); + case MVT::nxv4i32: + return VectorType::get(Type::getInt32Ty(Context), 4, /*Scalable=*/ true); + case MVT::nxv8i32: + return VectorType::get(Type::getInt32Ty(Context), 8, /*Scalable=*/ true); + case MVT::nxv16i32: + return VectorType::get(Type::getInt32Ty(Context), 16,/*Scalable=*/ true); + case MVT::nxv32i32: + return VectorType::get(Type::getInt32Ty(Context), 32,/*Scalable=*/ true); + case MVT::nxv1i64: + return VectorType::get(Type::getInt64Ty(Context), 1, /*Scalable=*/ true); + case MVT::nxv2i64: + return VectorType::get(Type::getInt64Ty(Context), 2, /*Scalable=*/ true); + case MVT::nxv4i64: + return VectorType::get(Type::getInt64Ty(Context), 4, /*Scalable=*/ true); + case MVT::nxv8i64: + return VectorType::get(Type::getInt64Ty(Context), 8, /*Scalable=*/ true); + case MVT::nxv16i64: + return VectorType::get(Type::getInt64Ty(Context), 16, /*Scalable=*/ true); + case MVT::nxv32i64: + return VectorType::get(Type::getInt64Ty(Context), 32, /*Scalable=*/ true); + case MVT::nxv2f16: + return VectorType::get(Type::getHalfTy(Context), 2, /*Scalable=*/ true); + case MVT::nxv4f16: + return VectorType::get(Type::getHalfTy(Context), 4, /*Scalable=*/ true); + case MVT::nxv8f16: + return VectorType::get(Type::getHalfTy(Context), 8, /*Scalable=*/ true); + case MVT::nxv1f32: + return VectorType::get(Type::getFloatTy(Context), 1, /*Scalable=*/ true); + case MVT::nxv2f32: + return VectorType::get(Type::getFloatTy(Context), 2, /*Scalable=*/ true); + case MVT::nxv4f32: + return VectorType::get(Type::getFloatTy(Context), 4, /*Scalable=*/ true); + case MVT::nxv8f32: + return VectorType::get(Type::getFloatTy(Context), 8, /*Scalable=*/ true); + case MVT::nxv16f32: + return VectorType::get(Type::getFloatTy(Context), 16, /*Scalable=*/ true); + case MVT::nxv1f64: + return VectorType::get(Type::getDoubleTy(Context), 1, /*Scalable=*/ true); + case MVT::nxv2f64: + return VectorType::get(Type::getDoubleTy(Context), 2, /*Scalable=*/ true); + case MVT::nxv4f64: + return VectorType::get(Type::getDoubleTy(Context), 4, /*Scalable=*/ true); + case MVT::nxv8f64: + return VectorType::get(Type::getDoubleTy(Context), 8, /*Scalable=*/ true); case MVT::Metadata: return Type::getMetadataTy(Context); - } + } } /// Return the value type corresponding to the specified type. This returns all @@ -329,7 +455,8 @@ MVT MVT::getVT(Type *Ty, bool HandleUnknown){ case Type::VectorTyID: { VectorType *VTy = cast(Ty); return getVectorVT( - getVT(VTy->getElementType(), false), VTy->getNumElements()); + getVT(VTy->getElementType(), /*HandleUnknown=*/ false), + VTy->getElementCount()); } } } @@ -345,8 +472,9 @@ EVT EVT::getEVT(Type *Ty, bool HandleUnknown){ return getIntegerVT(Ty->getContext(), cast(Ty)->getBitWidth()); case Type::VectorTyID: { VectorType *VTy = cast(Ty); - return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false), - VTy->getNumElements()); + return getVectorVT(Ty->getContext(), + getEVT(VTy->getElementType(), /*HandleUnknown=*/ false), + VTy->getElementCount()); } } } diff --git a/llvm/unittests/CodeGen/ScalableVectorMVTsTest.cpp b/llvm/unittests/CodeGen/ScalableVectorMVTsTest.cpp index 3dd3bec8db79..fcb9848dcdcc 100644 --- a/llvm/unittests/CodeGen/ScalableVectorMVTsTest.cpp +++ b/llvm/unittests/CodeGen/ScalableVectorMVTsTest.cpp @@ -7,8 +7,10 @@ //===----------------------------------------------------------------------===// #include "llvm/CodeGen/ValueTypes.h" +#include "llvm/IR/DerivedTypes.h" #include "llvm/IR/LLVMContext.h" #include "llvm/Support/MachineValueType.h" +#include "llvm/Support/ScalableSize.h" #include "gtest/gtest.h" using namespace llvm; @@ -46,12 +48,12 @@ TEST(ScalableVectorMVTsTest, HelperFuncs) { EVT Vnx4i32 = EVT::getVectorVT(Ctx, MVT::i32, 4, /*Scalable=*/true); ASSERT_TRUE(Vnx4i32.isScalableVector()); - // Create with separate MVT::ElementCount - auto EltCnt = MVT::ElementCount(2, true); + // Create with separate llvm::ElementCount + auto EltCnt = ElementCount(2, true); EVT Vnx2i32 = EVT::getVectorVT(Ctx, MVT::i32, EltCnt); ASSERT_TRUE(Vnx2i32.isScalableVector()); - // Create with inline MVT::ElementCount + // Create with inline llvm::ElementCount EVT Vnx2i64 = EVT::getVectorVT(Ctx, MVT::i64, {2, true}); ASSERT_TRUE(Vnx2i64.isScalableVector()); @@ -67,7 +69,7 @@ TEST(ScalableVectorMVTsTest, HelperFuncs) { EVT Vnx2f64 = EVT::getVectorVT(Ctx, MVT::f64, {2, true}); EXPECT_EQ(Vnx2f64.changeTypeToInteger(), Vnx2i64); - // Check fields inside MVT::ElementCount + // Check fields inside llvm::ElementCount EltCnt = Vnx4i32.getVectorElementCount(); EXPECT_EQ(EltCnt.Min, 4U); ASSERT_TRUE(EltCnt.Scalable); @@ -78,10 +80,44 @@ TEST(ScalableVectorMVTsTest, HelperFuncs) { EVT V4f64 = EVT::getVectorVT(Ctx, MVT::f64, {4, false}); ASSERT_FALSE(V4f64.isScalableVector()); - // Check that MVT::ElementCount works for fixed-length types. + // Check that llvm::ElementCount works for fixed-length types. EltCnt = V8i32.getVectorElementCount(); EXPECT_EQ(EltCnt.Min, 8U); ASSERT_FALSE(EltCnt.Scalable); } +TEST(ScalableVectorMVTsTest, IRToVTTranslation) { + LLVMContext Ctx; + + Type *Int64Ty = Type::getInt64Ty(Ctx); + VectorType *ScV8Int64Ty = VectorType::get(Int64Ty, {8, true}); + + // Check that we can map a scalable IR type to an MVT + MVT Mnxv8i64 = MVT::getVT(ScV8Int64Ty); + ASSERT_TRUE(Mnxv8i64.isScalableVector()); + ASSERT_EQ(ScV8Int64Ty->getElementCount(), Mnxv8i64.getVectorElementCount()); + ASSERT_EQ(MVT::getVT(ScV8Int64Ty->getElementType()), + Mnxv8i64.getScalarType()); + + // Check that we can map a scalable IR type to an EVT + EVT Enxv8i64 = EVT::getEVT(ScV8Int64Ty); + ASSERT_TRUE(Enxv8i64.isScalableVector()); + ASSERT_EQ(ScV8Int64Ty->getElementCount(), Enxv8i64.getVectorElementCount()); + ASSERT_EQ(EVT::getEVT(ScV8Int64Ty->getElementType()), + Enxv8i64.getScalarType()); } + +TEST(ScalableVectorMVTsTest, VTToIRTranslation) { + LLVMContext Ctx; + + EVT Enxv4f64 = EVT::getVectorVT(Ctx, MVT::f64, {4, true}); + + Type *Ty = Enxv4f64.getTypeForEVT(Ctx); + VectorType *ScV4Float64Ty = cast(Ty); + ASSERT_TRUE(ScV4Float64Ty->isScalable()); + ASSERT_EQ(Enxv4f64.getVectorElementCount(), ScV4Float64Ty->getElementCount()); + ASSERT_EQ(Enxv4f64.getScalarType().getTypeForEVT(Ctx), + ScV4Float64Ty->getElementType()); +} + +} // end anonymous namespace