6 changes: 4 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1878,15 +1878,17 @@ Error BitcodeReader::parseTypeTableBody() {
return error("Invalid type");
ResultTy = ArrayType::get(ResultTy, Record[0]);
break;
case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or
// [numelts, eltty, scalable]
if (Record.size() < 2)
return error("Invalid record");
if (Record[0] == 0)
return error("Invalid vector length");
ResultTy = getTypeByID(Record[1]);
if (!ResultTy || !StructType::isValidElementType(ResultTy))
return error("Invalid type");
ResultTy = VectorType::get(ResultTy, Record[0]);
bool Scalable = Record.size() > 2 ? Record[2] : false;
ResultTy = VectorType::get(ResultTy, Record[0], Scalable);
break;
}

Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,10 +941,13 @@ void ModuleBitcodeWriter::writeTypeTable() {
}
case Type::VectorTyID: {
VectorType *VT = cast<VectorType>(T);
// VECTOR [numelts, eltty]
// VECTOR [numelts, eltty] or
// [numelts, eltty, scalable]
Code = bitc::TYPE_CODE_VECTOR;
TypeVals.push_back(VT->getNumElements());
TypeVals.push_back(VE.getTypeID(VT->getElementType()));
if (VT->isScalable())
TypeVals.push_back(VT->isScalable());
break;
}
}
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,10 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
}
case Type::VectorTyID: {
VectorType *PTy = cast<VectorType>(Ty);
OS << "<" << PTy->getNumElements() << " x ";
OS << "<";
if (PTy->isScalable())
OS << "vscale x ";
OS << PTy->getNumElements() << " x ";
print(PTy->getElementType(), OS);
OS << '>';
return;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/LLVMContextImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ class LLVMContextImpl {
unsigned NamedStructTypesUniqueID = 0;

DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
DenseMap<std::pair<Type *, ElementCount>, VectorType*> VectorTypes;
DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;

Expand Down
17 changes: 10 additions & 7 deletions llvm/lib/IR/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ StringRef StructType::getName() const {
}

bool StructType::isValidElementType(Type *ElemTy) {
if (auto *VTy = dyn_cast<VectorType>(ElemTy))
return !VTy->isScalable();
return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
!ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
!ElemTy->isTokenTy();
Expand Down Expand Up @@ -590,6 +592,8 @@ ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
}

bool ArrayType::isValidElementType(Type *ElemTy) {
if (auto *VTy = dyn_cast<VectorType>(ElemTy))
return !VTy->isScalable();
return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
!ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
!ElemTy->isTokenTy();
Expand All @@ -599,21 +603,20 @@ bool ArrayType::isValidElementType(Type *ElemTy) {
// VectorType Implementation
//===----------------------------------------------------------------------===//

VectorType::VectorType(Type *ElType, unsigned NumEl)
: SequentialType(VectorTyID, ElType, NumEl) {}
VectorType::VectorType(Type *ElType, ElementCount EC)
: SequentialType(VectorTyID, ElType, EC.Min), Scalable(EC.Scalable) {}

VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
assert(EC.Min > 0 && "#Elements of a VectorType must be greater than 0");
assert(isValidElementType(ElementType) && "Element type of a VectorType must "
"be an integer, floating point, or "
"pointer type.");

LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
VectorType *&Entry = ElementType->getContext().pImpl
->VectorTypes[std::make_pair(ElementType, NumElements)];

->VectorTypes[std::make_pair(ElementType, EC)];
if (!Entry)
Entry = new (pImpl->Alloc) VectorType(ElementType, NumElements);
Entry = new (pImpl->Alloc) VectorType(ElementType, EC);
return Entry;
}

Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
"DIGlobalVariableExpression");
}

// Scalable vectors cannot be global variables, since we don't know
// the runtime size. If the global is a struct or an array containing
// scalable vectors, that will be caught by the isValidElementType methods
// in StructType or ArrayType instead.
if (auto *VTy = dyn_cast<VectorType>(GV.getValueType()))
Assert(!VTy->isScalable(), "Globals cannot contain scalable vectors", &GV);

if (!GV.hasInitializer()) {
visitGlobalValue(GV);
return;
Expand Down
4 changes: 4 additions & 0 deletions llvm/test/Bitcode/compatibility.ll
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,10 @@ define void @typesystem() {
; CHECK: %t7 = alloca x86_mmx
%t8 = alloca %opaquety*
; CHECK: %t8 = alloca %opaquety*
%t9 = alloca <4 x i32>
; CHECK: %t9 = alloca <4 x i32>
%t10 = alloca <vscale x 4 x i32>
; CHECK: %t10 = alloca <vscale x 4 x i32>

ret void
}
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Other/scalable-vector-array.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; RUN: not opt -S -verify < %s 2>&1 | FileCheck %s

;; Arrays cannot contain scalable vectors; make sure we detect them even
;; when nested inside other aggregates.

%ty = type { i64, [4 x <vscale x 256 x i1>] }
; CHECK: error: invalid array element type
; CHECK: %ty = type { i64, [4 x <vscale x 256 x i1>] }
8 changes: 8 additions & 0 deletions llvm/test/Other/scalable-vector-struct.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; RUN: not opt -S -verify < %s 2>&1 | FileCheck %s

;; Structs cannot contain scalable vectors; make sure we detect them even
;; when nested inside other aggregates.

%ty = type [2 x { i32, <vscale x 1 x i32> }]
; CHECK: error: invalid element type for struct
; CHECK: %ty = type [2 x { i32, <vscale x 1 x i32> }]
12 changes: 12 additions & 0 deletions llvm/test/Verifier/scalable-global-vars.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; RUN: not opt -S -verify < %s 2>&1 | FileCheck %s

;; Global variables cannot be scalable vectors, since we don't
;; know the size at compile time.

; CHECK: Globals cannot contain scalable vectors
; CHECK-NEXT: <vscale x 4 x i32>* @ScalableVecGlobal
@ScalableVecGlobal = global <vscale x 4 x i32> zeroinitializer

;; Global _pointers_ to scalable vectors are fine
; CHECK-NOT: Globals cannot contain scalable vectors
@ScalableVecPtr = global <vscale x 8 x i16>* zeroinitializer
1 change: 1 addition & 0 deletions llvm/unittests/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_llvm_unittest(IRTests
ValueHandleTest.cpp
ValueMapTest.cpp
ValueTest.cpp
VectorTypesTest.cpp
VerifierTest.cpp
WaymarkTest.cpp
)
Expand Down
164 changes: 164 additions & 0 deletions llvm/unittests/IR/VectorTypesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//===--- llvm/unittest/IR/VectorTypesTest.cpp - vector types 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
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/ScalableSize.h"
#include "gtest/gtest.h"
using namespace llvm;

namespace {
TEST(VectorTypesTest, FixedLength) {
LLVMContext Ctx;

Type *Int16Ty = Type::getInt16Ty(Ctx);
Type *Int32Ty = Type::getInt32Ty(Ctx);
Type *Int64Ty = Type::getInt64Ty(Ctx);
Type *Float64Ty = Type::getDoubleTy(Ctx);

VectorType *V8Int32Ty = VectorType::get(Int32Ty, 8);
ASSERT_FALSE(V8Int32Ty->isScalable());
EXPECT_EQ(V8Int32Ty->getNumElements(), 8U);
EXPECT_EQ(V8Int32Ty->getElementType()->getScalarSizeInBits(), 32U);

VectorType *V8Int16Ty = VectorType::get(Int16Ty, {8, false});
ASSERT_FALSE(V8Int16Ty->isScalable());
EXPECT_EQ(V8Int16Ty->getNumElements(), 8U);
EXPECT_EQ(V8Int16Ty->getElementType()->getScalarSizeInBits(), 16U);

ElementCount EltCnt(4, false);
VectorType *V4Int64Ty = VectorType::get(Int64Ty, EltCnt);
ASSERT_FALSE(V4Int64Ty->isScalable());
EXPECT_EQ(V4Int64Ty->getNumElements(), 4U);
EXPECT_EQ(V4Int64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *V2Int64Ty = VectorType::get(Int64Ty, EltCnt/2);
ASSERT_FALSE(V2Int64Ty->isScalable());
EXPECT_EQ(V2Int64Ty->getNumElements(), 2U);
EXPECT_EQ(V2Int64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *V8Int64Ty = VectorType::get(Int64Ty, EltCnt*2);
ASSERT_FALSE(V8Int64Ty->isScalable());
EXPECT_EQ(V8Int64Ty->getNumElements(), 8U);
EXPECT_EQ(V8Int64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *V4Float64Ty = VectorType::get(Float64Ty, EltCnt);
ASSERT_FALSE(V4Float64Ty->isScalable());
EXPECT_EQ(V4Float64Ty->getNumElements(), 4U);
EXPECT_EQ(V4Float64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ExtTy = VectorType::getExtendedElementVectorType(V8Int16Ty);
EXPECT_EQ(ExtTy, V8Int32Ty);
ASSERT_FALSE(ExtTy->isScalable());
EXPECT_EQ(ExtTy->getNumElements(), 8U);
EXPECT_EQ(ExtTy->getElementType()->getScalarSizeInBits(), 32U);

VectorType *TruncTy = VectorType::getTruncatedElementVectorType(V8Int32Ty);
EXPECT_EQ(TruncTy, V8Int16Ty);
ASSERT_FALSE(TruncTy->isScalable());
EXPECT_EQ(TruncTy->getNumElements(), 8U);
EXPECT_EQ(TruncTy->getElementType()->getScalarSizeInBits(), 16U);

VectorType *HalvedTy = VectorType::getHalfElementsVectorType(V4Int64Ty);
EXPECT_EQ(HalvedTy, V2Int64Ty);
ASSERT_FALSE(HalvedTy->isScalable());
EXPECT_EQ(HalvedTy->getNumElements(), 2U);
EXPECT_EQ(HalvedTy->getElementType()->getScalarSizeInBits(), 64U);

VectorType *DoubledTy = VectorType::getDoubleElementsVectorType(V4Int64Ty);
EXPECT_EQ(DoubledTy, V8Int64Ty);
ASSERT_FALSE(DoubledTy->isScalable());
EXPECT_EQ(DoubledTy->getNumElements(), 8U);
EXPECT_EQ(DoubledTy->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ConvTy = VectorType::getInteger(V4Float64Ty);
EXPECT_EQ(ConvTy, V4Int64Ty);
ASSERT_FALSE(ConvTy->isScalable());
EXPECT_EQ(ConvTy->getNumElements(), 4U);
EXPECT_EQ(ConvTy->getElementType()->getScalarSizeInBits(), 64U);

EltCnt = V8Int64Ty->getElementCount();
EXPECT_EQ(EltCnt.Min, 8U);
ASSERT_FALSE(EltCnt.Scalable);
}

TEST(VectorTypesTest, Scalable) {
LLVMContext Ctx;

Type *Int16Ty = Type::getInt16Ty(Ctx);
Type *Int32Ty = Type::getInt32Ty(Ctx);
Type *Int64Ty = Type::getInt64Ty(Ctx);
Type *Float64Ty = Type::getDoubleTy(Ctx);

VectorType *ScV8Int32Ty = VectorType::get(Int32Ty, 8, true);
ASSERT_TRUE(ScV8Int32Ty->isScalable());
EXPECT_EQ(ScV8Int32Ty->getNumElements(), 8U);
EXPECT_EQ(ScV8Int32Ty->getElementType()->getScalarSizeInBits(), 32U);

VectorType *ScV8Int16Ty = VectorType::get(Int16Ty, {8, true});
ASSERT_TRUE(ScV8Int16Ty->isScalable());
EXPECT_EQ(ScV8Int16Ty->getNumElements(), 8U);
EXPECT_EQ(ScV8Int16Ty->getElementType()->getScalarSizeInBits(), 16U);

ElementCount EltCnt(4, true);
VectorType *ScV4Int64Ty = VectorType::get(Int64Ty, EltCnt);
ASSERT_TRUE(ScV4Int64Ty->isScalable());
EXPECT_EQ(ScV4Int64Ty->getNumElements(), 4U);
EXPECT_EQ(ScV4Int64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ScV2Int64Ty = VectorType::get(Int64Ty, EltCnt/2);
ASSERT_TRUE(ScV2Int64Ty->isScalable());
EXPECT_EQ(ScV2Int64Ty->getNumElements(), 2U);
EXPECT_EQ(ScV2Int64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ScV8Int64Ty = VectorType::get(Int64Ty, EltCnt*2);
ASSERT_TRUE(ScV8Int64Ty->isScalable());
EXPECT_EQ(ScV8Int64Ty->getNumElements(), 8U);
EXPECT_EQ(ScV8Int64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ScV4Float64Ty = VectorType::get(Float64Ty, EltCnt);
ASSERT_TRUE(ScV4Float64Ty->isScalable());
EXPECT_EQ(ScV4Float64Ty->getNumElements(), 4U);
EXPECT_EQ(ScV4Float64Ty->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ExtTy = VectorType::getExtendedElementVectorType(ScV8Int16Ty);
EXPECT_EQ(ExtTy, ScV8Int32Ty);
ASSERT_TRUE(ExtTy->isScalable());
EXPECT_EQ(ExtTy->getNumElements(), 8U);
EXPECT_EQ(ExtTy->getElementType()->getScalarSizeInBits(), 32U);

VectorType *TruncTy = VectorType::getTruncatedElementVectorType(ScV8Int32Ty);
EXPECT_EQ(TruncTy, ScV8Int16Ty);
ASSERT_TRUE(TruncTy->isScalable());
EXPECT_EQ(TruncTy->getNumElements(), 8U);
EXPECT_EQ(TruncTy->getElementType()->getScalarSizeInBits(), 16U);

VectorType *HalvedTy = VectorType::getHalfElementsVectorType(ScV4Int64Ty);
EXPECT_EQ(HalvedTy, ScV2Int64Ty);
ASSERT_TRUE(HalvedTy->isScalable());
EXPECT_EQ(HalvedTy->getNumElements(), 2U);
EXPECT_EQ(HalvedTy->getElementType()->getScalarSizeInBits(), 64U);

VectorType *DoubledTy = VectorType::getDoubleElementsVectorType(ScV4Int64Ty);
EXPECT_EQ(DoubledTy, ScV8Int64Ty);
ASSERT_TRUE(DoubledTy->isScalable());
EXPECT_EQ(DoubledTy->getNumElements(), 8U);
EXPECT_EQ(DoubledTy->getElementType()->getScalarSizeInBits(), 64U);

VectorType *ConvTy = VectorType::getInteger(ScV4Float64Ty);
EXPECT_EQ(ConvTy, ScV4Int64Ty);
ASSERT_TRUE(ConvTy->isScalable());
EXPECT_EQ(ConvTy->getNumElements(), 4U);
EXPECT_EQ(ConvTy->getElementType()->getScalarSizeInBits(), 64U);

EltCnt = ScV8Int64Ty->getElementCount();
EXPECT_EQ(EltCnt.Min, 8U);
ASSERT_TRUE(EltCnt.Scalable);
}

} // end anonymous namespace