Skip to content

Commit 4f29ec7

Browse files
committed
[mlir][vector] Add convenience types for scalable vectors
This PR adds two small convenience Vector types: * `ScalableVectorType` and `FixedWidthVectorType`. The goal of these new types is two-fold: * enable idiomatic checks like `isa<ScalableVectorType>(...)`, * make the split into "Scalable" and "Fixed-wdith" vectors a bit more explicit and more visible in the code-base.
1 parent 662c626 commit 4f29ec7

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- VectorTypes.h - MLIR Vector Types ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_
10+
#define MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_
11+
12+
#include "mlir/IR/BuiltinTypes.h"
13+
#include "mlir/IR/Diagnostics.h"
14+
#include "mlir/IR/Types.h"
15+
16+
namespace mlir {
17+
namespace vector {
18+
19+
class ScalableVectorType : public VectorType {
20+
public:
21+
using VectorType::VectorType;
22+
23+
static bool classof(Type type);
24+
};
25+
26+
class FixedWidthVectorType : public VectorType {
27+
public:
28+
using VectorType::VectorType;
29+
static bool classof(Type type);
30+
};
31+
32+
} // namespace vector
33+
} // namespace mlir
34+
35+
#endif // MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_

mlir/lib/Dialect/Arith/IR/ArithOps.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "mlir/Dialect/Arith/IR/Arith.h"
1515
#include "mlir/Dialect/CommonFolders.h"
1616
#include "mlir/Dialect/UB/IR/UBOps.h"
17+
#include "mlir/Dialect/Vector/IR/VectorTypes.h"
1718
#include "mlir/IR/Builders.h"
1819
#include "mlir/IR/BuiltinAttributeInterfaces.h"
1920
#include "mlir/IR/BuiltinAttributes.h"
@@ -214,8 +215,8 @@ LogicalResult arith::ConstantOp::verify() {
214215
"value must be an integer, float, or elements attribute");
215216
}
216217

217-
auto vecType = dyn_cast<VectorType>(type);
218-
if (vecType && vecType.isScalable() && !isa<SplatElementsAttr>(getValue()))
218+
if (isa<vector::ScalableVectorType>(type) &&
219+
!isa<SplatElementsAttr>(getValue()))
219220
return emitOpError(
220221
"intializing scalable vectors with elements attribute is not supported"
221222
" unless it's a vector splat");

mlir/lib/Dialect/Vector/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_mlir_dialect_library(MLIRVectorDialect
22
VectorOps.cpp
3+
VectorTypes.cpp
34
ValueBoundsOpInterfaceImpl.cpp
45
ScalableValueBoundsConstraintSet.cpp
56

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- VectorTypes.cpp - MLIR Vector Types --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir/Dialect/Vector/IR/VectorTypes.h"
10+
#include "mlir/IR/BuiltinTypes.h"
11+
12+
using namespace mlir;
13+
using namespace mlir::vector;
14+
15+
bool ScalableVectorType::classof(Type type) {
16+
auto vecTy = llvm::dyn_cast<VectorType>(type);
17+
if (!vecTy)
18+
return false;
19+
return vecTy.isScalable();
20+
}
21+
22+
bool FixedWidthVectorType::classof(Type type) {
23+
auto vecTy = llvm::dyn_cast<VectorType>(type);
24+
if (!vecTy)
25+
return false;
26+
return !vecTy.isScalable();
27+
}

0 commit comments

Comments
 (0)