diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h --- a/llvm/include/llvm/IR/DerivedTypes.h +++ b/llvm/include/llvm/IR/DerivedTypes.h @@ -391,9 +391,8 @@ SequentialType(const SequentialType &) = delete; SequentialType &operator=(const SequentialType &) = delete; - /// For scalable vectors, this will return the minimum number of elements - /// in the vector. - uint64_t getNumElements() const { return NumElements; } + /// For scalable vectors this value is undefined + uint64_t getNumElements() const; Type *getElementType() const { return ContainedType; } /// Methods for support type inquiry through isa, cast, and dyn_cast. @@ -441,13 +440,12 @@ /// - a vector containing an unknown integer multiple /// of 4 i32s - VectorType(Type *ElType, unsigned NumEl, bool Scalable = false); VectorType(Type *ElType, ElementCount EC); - // If true, the total number of elements is an unknown multiple of the - // minimum 'NumElements' from SequentialType. Otherwise the total number - // of elements is exactly equal to 'NumElements'. - bool Scalable; + // If EC.Scalable is true, the total number of elements is an unknown multiple + // of the EC.Min. Otherwise the total number of elements is exactly equal to + // SequentialType::getNumElements(). + ElementCount EC; public: VectorType(const VectorType &) = delete; @@ -538,15 +536,21 @@ /// Return an ElementCount instance to represent the (possibly scalable) /// number of elements in the vector. ElementCount getElementCount() const { - uint64_t MinimumEltCnt = getNumElements(); - assert(MinimumEltCnt <= UINT_MAX && "Too many elements in vector"); - return { (unsigned)MinimumEltCnt, Scalable }; +#ifndef NDEBUG + if (!EC.Scalable) { + uint64_t MinimumEltCnt = getNumElements(); + assert(MinimumEltCnt <= UINT_MAX && "Too many elements in vector"); + assert(MinimumEltCnt == EC.Min && + "Mismatch between stored element count and getNumElements()"); + } +#endif + return EC; } /// Returns whether or not this is a scalable vector (meaning the total /// element count is a multiple of the minimum). bool isScalable() const { - return Scalable; + return EC.Scalable; } /// Return the minimum number of bits in the Vector type. diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -577,6 +577,21 @@ return true; } +//===----------------------------------------------------------------------===// +// SequentialType Implementation +//===----------------------------------------------------------------------===// + +uint64_t SequentialType::getNumElements() const { +#ifndef NDEBUG + if (auto *VTy = dyn_cast(this)) { + assert(!VTy->isScalable() && + "The length of a scalable vector is unknown at compile time"); + } +#endif + + return NumElements; +} + //===----------------------------------------------------------------------===// // ArrayType Implementation //===----------------------------------------------------------------------===// @@ -609,7 +624,7 @@ //===----------------------------------------------------------------------===// VectorType::VectorType(Type *ElType, ElementCount EC) - : SequentialType(VectorTyID, ElType, EC.Min), Scalable(EC.Scalable) {} + : SequentialType(VectorTyID, ElType, EC.Min), EC(EC) {} VectorType *VectorType::get(Type *ElementType, ElementCount EC) { assert(EC.Min > 0 && "#Elements of a VectorType must be greater than 0");