This is the first patch of a serie to move FIR types to TableGen format as suggested in D96172.
This patch is setting up the files for FIR types and move the ShapeType to TableGen.
As discussed with @schweitz, I'm taking over this task to help the FIR upstreaming effort.
Reviewed By: mehdi_amini
Differential Revision: https://reviews.llvm.org/D96422
set(LLVM_TARGET_DEFINITIONS FIROps.td)
mlir_tablegen(FIROps.h.inc -gen-op-decls)
mlir_tablegen(FIROps.cpp.inc -gen-op-defs)
+mlir_tablegen(FIROpsTypes.h.inc --gen-typedef-decls)
+mlir_tablegen(FIROpsTypes.cpp.inc --gen-typedef-defs)
add_public_tablegen_target(FIROpsIncGen)
add_custom_target(flang-doc)
let cppNamespace = "::fir";
}
+include "flang/Optimizer/Dialect/FIRTypes.td"
+
// Types and predicates
def fir_Type : Type<CPred<"fir::isa_fir_or_std_type($_self)">,
fir_HeapType.predicate, fir_PointerType.predicate, fir_BoxType.predicate]>,
"any reference or box">;
-def fir_ShapeType : Type<CPred<"$_self.isa<fir::ShapeType>()">, "shape type">;
def fir_ShapeShiftType : Type<CPred<"$_self.isa<fir::ShapeShiftType>()">,
"shape shift type">;
-def AnyShapeLike : TypeConstraint<Or<[fir_ShapeType.predicate,
+def AnyShapeLike : TypeConstraint<Or<[ShapeType.predicate,
fir_ShapeShiftType.predicate]>, "any legal shape type">;
def AnyShapeType : Type<AnyShapeLike.predicate, "any legal shape type">;
def fir_SliceType : Type<CPred<"$_self.isa<fir::SliceType>()">, "slice type">;
#include "mlir/IR/BuiltinTypes.h"
#include "llvm/ADT/SmallVector.h"
+#define GET_TYPEDEF_CLASSES
+#include "flang/Optimizer/Dialect/FIROpsTypes.h.inc"
+
namespace llvm {
class raw_ostream;
class StringRef;
struct RecordTypeStorage;
struct ReferenceTypeStorage;
struct SequenceTypeStorage;
-struct ShapeTypeStorage;
struct ShapeShiftTypeStorage;
struct SliceTypeStorage;
struct TypeDescTypeStorage;
mlir::Type eleTy);
};
-/// Type of a vector of runtime values that define the shape of a
-/// multidimensional array object. The vector is the extents of each array
-/// dimension. The rank of a ShapeType must be at least 1.
-class ShapeType : public mlir::Type::TypeBase<ShapeType, mlir::Type,
- detail::ShapeTypeStorage> {
-public:
- using Base::Base;
- static ShapeType get(mlir::MLIRContext *ctx, unsigned rank);
- unsigned getRank() const;
-};
/// Type of a vector of runtime values that define the shape and the origin of a
/// multidimensional array object. The vector is of pairs, origin offset and
--- /dev/null
+//===- FIRTypes.td - FIR types -----------------------------*- tablegen -*-===//
+//
+// 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 declares the FIR dialect types.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FIR_DIALECT_FIR_TYPES
+#define FIR_DIALECT_FIR_TYPES
+
+//===----------------------------------------------------------------------===//
+// FIR Types
+//===----------------------------------------------------------------------===//
+
+class FIR_Type<string name, string typeMnemonic> : TypeDef<fir_Dialect, name> {
+ let mnemonic = typeMnemonic;
+}
+
+def ShapeType : FIR_Type<"Shape", "shape"> {
+ let summary = "shape of a multidimensional array object";
+
+ let description = [{
+ Type of a vector of runtime values that define the shape of a
+ multidimensional array object. The vector is the extents of each array
+ dimension. The rank of a ShapeType must be at least 1.
+ }];
+
+ let parameters = (ins "unsigned":$rank);
+
+ let printer = [{
+ $_printer << "shape<" << getImpl()->rank << ">";
+ }];
+
+ let parser = [{
+ int rank;
+ if ($_parser.parseLess() || $_parser.parseInteger(rank) ||
+ $_parser.parseGreater())
+ return Type();
+ return get(context, rank);
+ }];
+}
+
+#endif // FIR_DIALECT_FIR_TYPES
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/ErrorHandling.h"
+#define GET_TYPEDEF_CLASSES
+#include "flang/Optimizer/Dialect/FIROpsTypes.cpp.inc"
+
using namespace fir;
namespace {
return parseKindSingleton<fir::ComplexType>(parser);
}
-// `shape` `<` rank `>`
-ShapeType parseShape(mlir::DialectAsmParser &parser) {
- return parseRankSingleton<ShapeType>(parser);
-}
-
// `shapeshift` `<` rank `>`
ShapeShiftType parseShapeShift(mlir::DialectAsmParser &parser) {
return parseRankSingleton<ShapeShiftType>(parser);
// Implementation of the thin interface from dialect to type parser
-mlir::Type fir::parseFirType(FIROpsDialect *, mlir::DialectAsmParser &parser) {
+mlir::Type fir::parseFirType(FIROpsDialect *dialect,
+ mlir::DialectAsmParser &parser) {
llvm::StringRef typeNameLit;
if (mlir::failed(parser.parseKeyword(&typeNameLit)))
return {};
if (typeNameLit == "ref")
return parseReference(parser, loc);
if (typeNameLit == "shape")
- return parseShape(parser);
+ // TODO move to generatedTypeParser when all types have been moved
+ return ShapeType::parse(dialect->getContext(), parser);
if (typeNameLit == "shapeshift")
return parseShapeShift(parser);
if (typeNameLit == "slice")
: kind{kind}, len{len} {}
};
-struct ShapeTypeStorage : public mlir::TypeStorage {
- using KeyTy = unsigned;
-
- static unsigned hashKey(const KeyTy &key) { return llvm::hash_combine(key); }
-
- bool operator==(const KeyTy &key) const { return key == getRank(); }
-
- static ShapeTypeStorage *construct(mlir::TypeStorageAllocator &allocator,
- unsigned rank) {
- auto *storage = allocator.allocate<ShapeTypeStorage>();
- return new (storage) ShapeTypeStorage{rank};
- }
-
- unsigned getRank() const { return rank; }
-
-protected:
- unsigned rank;
-
-private:
- ShapeTypeStorage() = delete;
- explicit ShapeTypeStorage(unsigned rank) : rank{rank} {}
-};
-
struct ShapeShiftTypeStorage : public mlir::TypeStorage {
using KeyTy = unsigned;
return llvm::hash_combine(0);
}
-// Shape
-
-ShapeType fir::ShapeType::get(mlir::MLIRContext *ctxt, unsigned rank) {
- return Base::get(ctxt, rank);
-}
-
-unsigned fir::ShapeType::getRank() const { return getImpl()->getRank(); }
-
// Shapeshift
ShapeShiftType fir::ShapeShiftType::get(mlir::MLIRContext *ctxt,
return;
}
if (auto type = ty.dyn_cast<ShapeType>()) {
- os << "shape<" << type.getRank() << '>';
+ // TODO when all type are moved to TableGen can be replaced by
+ // generatedTypePrinter
+ type.print(p);
return;
}
if (auto type = ty.dyn_cast<ShapeShiftType>()) {