Skip to content

[mlir] Add ScalableVectorType and FixedVectorType #87986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions mlir/include/mlir/IR/VectorTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===- VectorTypes.h - MLIR Vector Types ------------------------*- 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
//
//===----------------------------------------------------------------------===//
//
// Convenience wrappers for `VectorType` to allow idiomatic code like
// * isa<vector::ScalableVectorType>(type)
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_IR_VECTORTYPES_H
#define MLIR_IR_VECTORTYPES_H

#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Types.h"

namespace mlir {
namespace vector {

/// A vector type containing at least one scalable dimension.
class ScalableVectorType : public VectorType {
public:
using VectorType::VectorType;

static bool classof(Type type) {
auto vecTy = llvm::dyn_cast<VectorType>(type);
if (!vecTy)
return false;
return vecTy.isScalable();
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need something akin to resolveTypeID? I saw that @joker-eph added those for ops in #87170 but I'm not sure if the same thing applies to types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to only be required for operations for now I believe. Worst case they could probably be easy to make available using using VectorType::resolveTypeID;


/// A vector type with no scalable dimensions.
class FixedVectorType : public VectorType {
public:
using VectorType::VectorType;
static bool classof(Type type) {
auto vecTy = llvm::dyn_cast<VectorType>(type);
if (!vecTy)
return false;
return !vecTy.isScalable();
}
};

} // namespace vector
} // namespace mlir

#endif // MLIR_IR_VECTORTYPES_H
6 changes: 4 additions & 2 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/IR/VectorTypes.h"
#include "mlir/Support/LogicalResult.h"

#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
Expand Down Expand Up @@ -224,8 +226,8 @@ LogicalResult arith::ConstantOp::verify() {
// Note, we could relax this for vectors with 1 scalable dim, e.g.:
// * arith.constant dense<[[3, 3], [1, 1]]> : vector<2 x [2] x i32>
// However, this would most likely require updating the lowerings to LLVM.
auto vecType = dyn_cast<VectorType>(type);
if (vecType && vecType.isScalable() && !isa<SplatElementsAttr>(getValue()))
if (isa<vector::ScalableVectorType>(type) &&
!isa<SplatElementsAttr>(getValue()))
return emitOpError(
"intializing scalable vectors with elements attribute is not supported"
" unless it's a vector splat");
Expand Down
Loading