Skip to content

Commit

Permalink
[SVE][IR] Scalable Vector IR Type
Browse files Browse the repository at this point in the history
* Adds a 'scalable' flag to VectorType
* Adds an 'ElementCount' class to VectorType to pass (possibly scalable) vector lengths, with overloaded operators.
* Modifies existing helper functions to use ElementCount
* Adds support for serializing/deserializing to/from both textual and bitcode IR formats
* Extends the verifier to reject global variables of scalable types
* Updates documentation

See the latest version of the RFC here: http://lists.llvm.org/pipermail/llvm-dev/2018-July/124396.html

Reviewers: rengolin, lattner, echristo, chandlerc, hfinkel, rkruppe, samparker, SjoerdMeijer, greened, sebpop

Reviewed By: hfinkel, sebpop

Differential Revision: https://reviews.llvm.org/D32530

llvm-svn: 361953
  • Loading branch information
huntergr-arm committed May 29, 2019
1 parent 4c5a0d1 commit f4fc01f
Show file tree
Hide file tree
Showing 19 changed files with 479 additions and 39 deletions.
54 changes: 39 additions & 15 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ an optional list of attached :ref:`metadata <metadata>`.
Variables and aliases can have a
:ref:`Thread Local Storage Model <tls_model>`.

:ref:`Scalable vectors <t_vector>` cannot be global variables or members of
structs or arrays because their size is unknown at compile time.

Syntax::

@<GlobalVarName> = [Linkage] [PreemptionSpecifier] [Visibility]
Expand Down Expand Up @@ -2730,30 +2733,40 @@ Vector Type
A vector type is a simple derived type that represents a vector of
elements. Vector types are used when multiple primitive data are
operated in parallel using a single instruction (SIMD). A vector type
requires a size (number of elements) and an underlying primitive data
type. Vector types are considered :ref:`first class <t_firstclass>`.
requires a size (number of elements), an underlying primitive data type,
and a scalable property to represent vectors where the exact hardware
vector length is unknown at compile time. Vector types are considered
:ref:`first class <t_firstclass>`.

:Syntax:

::

< <# elements> x <elementtype> >
< <# elements> x <elementtype> > ; Fixed-length vector
< vscale x <# elements> x <elementtype> > ; Scalable vector

The number of elements is a constant integer value larger than 0;
elementtype may be any integer, floating-point or pointer type. Vectors
of size zero are not allowed.
of size zero are not allowed. For scalable vectors, the total number of
elements is a constant multiple (called vscale) of the specified number
of elements; vscale is a positive integer that is unknown at compile time
and the same hardware-dependent constant for all scalable vectors at run
time. The size of a specific scalable vector type is thus constant within
IR, even if the exact size in bytes cannot be determined until run time.

:Examples:

+-------------------+--------------------------------------------------+
| ``<4 x i32>`` | Vector of 4 32-bit integer values. |
+-------------------+--------------------------------------------------+
| ``<8 x float>`` | Vector of 8 32-bit floating-point values. |
+-------------------+--------------------------------------------------+
| ``<2 x i64>`` | Vector of 2 64-bit integer values. |
+-------------------+--------------------------------------------------+
| ``<4 x i64*>`` | Vector of 4 pointers to 64-bit integer values. |
+-------------------+--------------------------------------------------+
+------------------------+----------------------------------------------------+
| ``<4 x i32>`` | Vector of 4 32-bit integer values. |
+------------------------+----------------------------------------------------+
| ``<8 x float>`` | Vector of 8 32-bit floating-point values. |
+------------------------+----------------------------------------------------+
| ``<2 x i64>`` | Vector of 2 64-bit integer values. |
+------------------------+----------------------------------------------------+
| ``<4 x i64*>`` | Vector of 4 pointers to 64-bit integer values. |
+------------------------+----------------------------------------------------+
| ``<vscale x 4 x i32>`` | Vector with a multiple of 4 32-bit integer values. |
+------------------------+----------------------------------------------------+

.. _t_label:

Expand Down Expand Up @@ -8135,6 +8148,7 @@ Syntax:
::

<result> = extractelement <n x <ty>> <val>, <ty2> <idx> ; yields <ty>
<result> = extractelement <vscale x n x <ty>> <val>, <ty2> <idx> ; yields <ty>

Overview:
"""""""""
Expand All @@ -8155,7 +8169,9 @@ Semantics:

The result is a scalar of the same type as the element type of ``val``.
Its value is the value at position ``idx`` of ``val``. If ``idx``
exceeds the length of ``val``, the result is a
exceeds the length of ``val`` for a fixed-length vector, the result is a
:ref:`poison value <poisonvalues>`. For a scalable vector, if the value
of ``idx`` exceeds the runtime length of the vector, the result is a
:ref:`poison value <poisonvalues>`.

Example:
Expand All @@ -8176,6 +8192,7 @@ Syntax:
::

<result> = insertelement <n x <ty>> <val>, <ty> <elt>, <ty2> <idx> ; yields <n x <ty>>
<result> = insertelement <vscale x n x <ty>> <val>, <ty> <elt>, <ty2> <idx> ; yields <vscale x n x <ty>>

Overview:
"""""""""
Expand All @@ -8197,7 +8214,9 @@ Semantics:

The result is a vector of the same type as ``val``. Its element values
are those of ``val`` except at position ``idx``, where it gets the value
``elt``. If ``idx`` exceeds the length of ``val``, the result
``elt``. If ``idx`` exceeds the length of ``val`` for a fixed-length vector,
the result is a :ref:`poison value <poisonvalues>`. For a scalable vector,
if the value of ``idx`` exceeds the runtime length of the vector, the result
is a :ref:`poison value <poisonvalues>`.

Example:
Expand All @@ -8218,6 +8237,7 @@ Syntax:
::

<result> = shufflevector <n x <ty>> <v1>, <n x <ty>> <v2>, <m x i32> <mask> ; yields <m x <ty>>
<result> = shufflevector <vscale x n x <ty>> <v1>, <vscale x n x <ty>> v2, <vscale x m x i32> <mask> ; yields <vscale x m x <ty>>

Overview:
"""""""""
Expand Down Expand Up @@ -8249,6 +8269,10 @@ undef. If any element of the mask operand is undef, that element of the
result is undef. If the shuffle mask selects an undef element from one
of the input vectors, the resulting element is undef.

For scalable vectors, the only valid mask values at present are
``zeroinitializer`` and ``undef``, since we cannot write all indices as
literals for a vector with a length unknown at compile time.

Example:
""""""""

Expand Down
16 changes: 16 additions & 0 deletions llvm/include/llvm/ADT/DenseMapInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "llvm/Support/ScalableSize.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -268,6 +269,21 @@ template <> struct DenseMapInfo<hash_code> {
static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
};

template <> struct DenseMapInfo<ElementCount> {
static inline ElementCount getEmptyKey() { return {~0U, true}; }
static inline ElementCount getTombstoneKey() { return {~0U - 1, false}; }
static unsigned getHashValue(const ElementCount& EltCnt) {
if (EltCnt.Scalable)
return (EltCnt.Min * 37U) - 1U;

return EltCnt.Min * 37U;
}

static bool isEqual(const ElementCount& LHS, const ElementCount& RHS) {
return LHS == RHS;
}
};

} // end namespace llvm

#endif // LLVM_ADT_DENSEMAPINFO_H
68 changes: 57 additions & 11 deletions llvm/include/llvm/IR/DerivedTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ScalableSize.h"
#include <cassert>
#include <cstdint>

Expand Down Expand Up @@ -387,6 +388,8 @@ class SequentialType : public CompositeType {
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; }
Type *getElementType() const { return ContainedType; }

Expand Down Expand Up @@ -422,14 +425,37 @@ uint64_t Type::getArrayNumElements() const {

/// Class to represent vector types.
class VectorType : public SequentialType {
VectorType(Type *ElType, unsigned NumEl);
/// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the
/// minimum number of elements of type Ty contained within the vector, and
/// 'scalable' indicates that the total element count is an integer multiple
/// of 'n', where the multiple is either guaranteed to be one, or is
/// statically unknown at compile time.
///
/// If the multiple is known to be 1, then the extra term is discarded in
/// textual IR:
///
/// <4 x i32> - a vector containing 4 i32s
/// <vscale x 4 x i32> - 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;

public:
VectorType(const VectorType &) = delete;
VectorType &operator=(const VectorType &) = delete;

/// This static method is the primary way to construct an VectorType.
static VectorType *get(Type *ElementType, unsigned NumElements);
static VectorType *get(Type *ElementType, ElementCount EC);
static VectorType *get(Type *ElementType, unsigned NumElements,
bool Scalable = false) {
return VectorType::get(ElementType, {NumElements, Scalable});
}

/// This static method gets a VectorType with the same number of elements as
/// the input type, and the element type is an integer type of the same width
Expand All @@ -438,15 +464,15 @@ class VectorType : public SequentialType {
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
assert(EltBits && "Element size must be of a non-zero size");
Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
return VectorType::get(EltTy, VTy->getNumElements());
return VectorType::get(EltTy, VTy->getElementCount());
}

/// This static method is like getInteger except that the element types are
/// twice as wide as the elements in the input type.
static VectorType *getExtendedElementVectorType(VectorType *VTy) {
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
return VectorType::get(EltTy, VTy->getNumElements());
return VectorType::get(EltTy, VTy->getElementCount());
}

/// This static method is like getInteger except that the element types are
Expand All @@ -456,29 +482,45 @@ class VectorType : public SequentialType {
assert((EltBits & 1) == 0 &&
"Cannot truncate vector element with odd bit-width");
Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
return VectorType::get(EltTy, VTy->getNumElements());
return VectorType::get(EltTy, VTy->getElementCount());
}

/// This static method returns a VectorType with half as many elements as the
/// input type and the same element type.
static VectorType *getHalfElementsVectorType(VectorType *VTy) {
unsigned NumElts = VTy->getNumElements();
assert ((NumElts & 1) == 0 &&
auto EltCnt = VTy->getElementCount();
assert ((EltCnt.Min & 1) == 0 &&
"Cannot halve vector with odd number of elements.");
return VectorType::get(VTy->getElementType(), NumElts/2);
return VectorType::get(VTy->getElementType(), EltCnt/2);
}

/// This static method returns a VectorType with twice as many elements as the
/// input type and the same element type.
static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
unsigned NumElts = VTy->getNumElements();
return VectorType::get(VTy->getElementType(), NumElts*2);
auto EltCnt = VTy->getElementCount();
assert((VTy->getNumElements() * 2ull) <= UINT_MAX &&
"Too many elements in vector");
return VectorType::get(VTy->getElementType(), EltCnt*2);
}

/// Return true if the specified type is valid as a element type.
static bool isValidElementType(Type *ElemTy);

/// Return the number of bits in the Vector type.
/// 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 };
}

/// 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 the minimum number of bits in the Vector type.
/// Returns zero when the vector is a vector of pointers.
unsigned getBitWidth() const {
return getNumElements() * getElementType()->getPrimitiveSizeInBits();
Expand All @@ -494,6 +536,10 @@ unsigned Type::getVectorNumElements() const {
return cast<VectorType>(this)->getNumElements();
}

bool Type::getVectorIsScalable() const {
return cast<VectorType>(this)->isScalable();
}

/// Class to represent pointers.
class PointerType : public Type {
explicit PointerType(Type *ElType, unsigned AddrSpace);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ class Type {
return ContainedTys[0];
}

inline bool getVectorIsScalable() const;
inline unsigned getVectorNumElements() const;
Type *getVectorElementType() const {
assert(getTypeID() == VectorTyID);
Expand Down
43 changes: 43 additions & 0 deletions llvm/include/llvm/Support/ScalableSize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===- ScalableSize.h - Scalable vector size info ---------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file provides a struct that can be used to query the size of IR types
// which may be scalable vectors. It provides convenience operators so that
// it can be used in much the same way as a single scalar value.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_SCALABLESIZE_H
#define LLVM_SUPPORT_SCALABLESIZE_H

namespace llvm {

class ElementCount {
public:
unsigned Min; // Minimum number of vector elements.
bool Scalable; // If true, NumElements is a multiple of 'Min' determined
// at runtime rather than compile time.

ElementCount(unsigned Min, bool Scalable)
: Min(Min), Scalable(Scalable) {}

ElementCount operator*(unsigned RHS) {
return { Min * RHS, Scalable };
}
ElementCount operator/(unsigned RHS) {
return { Min / RHS, Scalable };
}

bool operator==(const ElementCount& RHS) const {
return Min == RHS.Min && Scalable == RHS.Scalable;
}
};

} // end namespace llvm

#endif // LLVM_SUPPORT_SCALABLESIZE_H
1 change: 1 addition & 0 deletions llvm/lib/AsmParser/LLLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ lltok::Kind LLLexer::LexIdentifier() {
KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
KEYWORD(umin);

KEYWORD(vscale);
KEYWORD(x);
KEYWORD(blockaddress);

Expand Down
13 changes: 12 additions & 1 deletion llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,18 @@ bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
/// Type
/// ::= '[' APSINTVAL 'x' Types ']'
/// ::= '<' APSINTVAL 'x' Types '>'
/// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
bool Scalable = false;

if (isVector && Lex.getKind() == lltok::kw_vscale) {
Lex.Lex(); // consume the 'vscale'
if (ParseToken(lltok::kw_x, "expected 'x' after vscale"))
return true;

Scalable = true;
}

if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
Lex.getAPSIntVal().getBitWidth() > 64)
return TokError("expected number in address space");
Expand All @@ -2748,7 +2759,7 @@ bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
return Error(SizeLoc, "size too large for vector");
if (!VectorType::isValidElementType(EltTy))
return Error(TypeLoc, "invalid vector element type");
Result = VectorType::get(EltTy, unsigned(Size));
Result = VectorType::get(EltTy, unsigned(Size), Scalable);
} else {
if (!ArrayType::isValidElementType(EltTy))
return Error(TypeLoc, "invalid array element type");
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/AsmParser/LLToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum Kind {
bar, // |
colon, // :

kw_vscale,
kw_x,
kw_true,
kw_false,
Expand Down
Loading

0 comments on commit f4fc01f

Please sign in to comment.