[flang-commits] [flang] a48bee2 - [flang][fir][NFC] Move BoxType to TableGen type definition

via flang-commits flang-commits at lists.llvm.org
Thu Feb 11 15:10:28 PST 2021


Author: Valentin Clement
Date: 2021-02-11T18:10:22-05:00
New Revision: a48bee2294b608f5c530f783757692eb2ca44100

URL: https://github.com/llvm/llvm-project/commit/a48bee2294b608f5c530f783757692eb2ca44100
DIFF: https://github.com/llvm/llvm-project/commit/a48bee2294b608f5c530f783757692eb2ca44100.diff

LOG: [flang][fir][NFC] Move BoxType to TableGen type definition

This patch is a follow up of D96422 and move BoxType to TableGen.

Reviewed By: mehdi_amini

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

Added: 
    

Modified: 
    flang/include/flang/Optimizer/Dialect/FIROps.h
    flang/include/flang/Optimizer/Dialect/FIROps.td
    flang/include/flang/Optimizer/Dialect/FIRType.h
    flang/include/flang/Optimizer/Dialect/FIRTypes.td
    flang/lib/Optimizer/Dialect/FIRType.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Optimizer/Dialect/FIROps.h b/flang/include/flang/Optimizer/Dialect/FIROps.h
index 66477846589a..c9541c8b1e3a 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.h
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.h
@@ -9,6 +9,7 @@
 #ifndef OPTIMIZER_DIALECT_FIROPS_H
 #define OPTIMIZER_DIALECT_FIROPS_H
 
+#include "flang/Optimizer/Dialect/FIRType.h"
 #include "mlir/Dialect/StandardOps/IR/Ops.h"
 #include "mlir/Interfaces/LoopLikeInterface.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"

diff  --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td
index a8d610771c2c..3aabaea40882 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.td
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -83,9 +83,6 @@ def fir_PointerType : Type<CPred<"$_self.isa<fir::PointerType>()">,
 def AnyReferenceLike : TypeConstraint<Or<[fir_ReferenceType.predicate,
     fir_HeapType.predicate, fir_PointerType.predicate]>, "any reference">;
 
-// A descriptor tuple (captures a reference to an entity and other information)
-def fir_BoxType : Type<CPred<"$_self.isa<fir::BoxType>()">, "box type">;
-
 // CHARACTER type descriptor. A pair of a data reference and a LEN value.
 def fir_BoxCharType : Type<CPred<"$_self.isa<fir::BoxCharType>()">,
     "box character type">;
@@ -94,11 +91,11 @@ def fir_BoxCharType : Type<CPred<"$_self.isa<fir::BoxCharType>()">,
 def fir_BoxProcType : Type<CPred<"$_self.isa<fir::BoxProcType>()">,
     "box procedure type">;
 
-def AnyBoxLike : TypeConstraint<Or<[fir_BoxType.predicate,
+def AnyBoxLike : TypeConstraint<Or<[BoxType.predicate,
     fir_BoxCharType.predicate, fir_BoxProcType.predicate]>, "any box">;
 
 def AnyRefOrBox : TypeConstraint<Or<[fir_ReferenceType.predicate,
-    fir_HeapType.predicate, fir_PointerType.predicate, fir_BoxType.predicate]>,
+    fir_HeapType.predicate, fir_PointerType.predicate, BoxType.predicate]>,
     "any reference or box">;
 
 def AnyShapeLike : TypeConstraint<Or<[ShapeType.predicate,
@@ -1048,7 +1045,7 @@ def fir_EmboxOp : fir_Op<"embox", [NoSideEffect]> {
 
   let arguments = (ins AnyReferenceLike:$memref, Variadic<AnyEmboxArg>:$args);
 
-  let results = (outs fir_BoxType);
+  let results = (outs BoxType);
 
   let parser = "return parseEmboxOp(parser, result);";
 
@@ -1257,7 +1254,7 @@ def fir_UnboxOp : fir_SimpleOp<"unbox", [NoSideEffect]> {
     ```
   }];
 
-  let arguments = (ins fir_BoxType:$box);
+  let arguments = (ins BoxType:$box);
 
   let results = (outs
     fir_ReferenceType,  // pointer to data
@@ -1328,7 +1325,7 @@ def fir_BoxAddrOp : fir_SimpleOneResultOp<"box_addr", [NoSideEffect]> {
     ```
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs AnyReferenceLike);
 
@@ -1372,7 +1369,7 @@ def fir_BoxDimsOp : fir_Op<"box_dims", [NoSideEffect]> {
     the box. The triple will be the lower bound, upper bound, and stride.
   }];
 
-  let arguments = (ins fir_BoxType:$val, AnyIntegerLike:$dim);
+  let arguments = (ins BoxType:$val, AnyIntegerLike:$dim);
 
   let results = (outs AnyIntegerLike, AnyIntegerLike, AnyIntegerLike);
 
@@ -1401,7 +1398,7 @@ def fir_BoxEleSizeOp : fir_SimpleOneResultOp<"box_elesize", [NoSideEffect]> {
     must box an array of REAL values (with dynamic rank and extent).
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs AnyIntegerLike);
 }
@@ -1424,7 +1421,7 @@ def fir_BoxIsAllocOp : fir_SimpleOp<"box_isalloc", [NoSideEffect]> {
     variable is an `ALLOCATABLE`.
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs BoolLike);
 }
@@ -1445,7 +1442,7 @@ def fir_BoxIsArrayOp : fir_SimpleOp<"box_isarray", [NoSideEffect]> {
     ```
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs BoolLike);
 }
@@ -1463,7 +1460,7 @@ def fir_BoxIsPtrOp : fir_SimpleOp<"box_isptr", [NoSideEffect]> {
     ```
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs BoolLike);
 }
@@ -1507,7 +1504,7 @@ def fir_BoxRankOp : fir_SimpleOneResultOp<"box_rank", [NoSideEffect]> {
     descriptor may be either an array or a scalar, so the value is nonnegative.
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs AnyIntegerType);
 }
@@ -1525,7 +1522,7 @@ def fir_BoxTypeDescOp : fir_SimpleOneResultOp<"box_tdesc", [NoSideEffect]> {
     ```
   }];
 
-  let arguments = (ins fir_BoxType:$val);
+  let arguments = (ins BoxType:$val);
 
   let results = (outs fir_TypeDescType);
 }
@@ -2100,7 +2097,7 @@ def fir_DispatchOp : fir_Op<"dispatch",
 
   let arguments = (ins
     StrAttr:$method,
-    fir_BoxType:$object,
+    BoxType:$object,
     Variadic<AnyType>:$args
   );
 

diff  --git a/flang/include/flang/Optimizer/Dialect/FIRType.h b/flang/include/flang/Optimizer/Dialect/FIRType.h
index 44e676ce9736..a531a514f224 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRType.h
+++ b/flang/include/flang/Optimizer/Dialect/FIRType.h
@@ -42,7 +42,6 @@ class FIROpsDialect;
 using KindTy = unsigned;
 
 namespace detail {
-struct BoxTypeStorage;
 struct BoxCharTypeStorage;
 struct BoxProcTypeStorage;
 struct CharacterTypeStorage;
@@ -178,23 +177,6 @@ class RealType : public mlir::Type::TypeBase<RealType, mlir::Type,
 
 // FIR support types
 
-/// The type of a Fortran descriptor. Descriptors are tuples of information that
-/// describe an entity being passed from a calling context. This information
-/// might include (but is not limited to) whether the entity is an array, its
-/// size, or what type it has.
-class BoxType
-    : public mlir::Type::TypeBase<BoxType, mlir::Type, detail::BoxTypeStorage> {
-public:
-  using Base::Base;
-  static BoxType get(mlir::Type eleTy, mlir::AffineMapAttr map = {});
-  mlir::Type getEleTy() const;
-  mlir::AffineMapAttr getLayoutMap() const;
-
-  static mlir::LogicalResult
-  verifyConstructionInvariants(mlir::Location, mlir::Type eleTy,
-                               mlir::AffineMapAttr map);
-};
-
 /// The type of a pair that describes a CHARACTER variable. Specifically, a
 /// CHARACTER consists of a reference to a buffer (the string value) and a LEN
 /// type parameter (the runtime length of the buffer).

diff  --git a/flang/include/flang/Optimizer/Dialect/FIRTypes.td b/flang/include/flang/Optimizer/Dialect/FIRTypes.td
index 99f1c179da87..7adef6b33d3f 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRTypes.td
+++ b/flang/include/flang/Optimizer/Dialect/FIRTypes.td
@@ -21,6 +21,29 @@ class FIR_Type<string name, string typeMnemonic> : TypeDef<fir_Dialect, name> {
   let mnemonic = typeMnemonic;
 }
 
+def BoxType : FIR_Type<"Box", "box"> {
+  let summary = "The type of a Fortran descriptor";
+
+  let description = [{
+    Descriptors are tuples of information that describe an entity being passed
+    from a calling context. This information might include (but is not limited
+    to) whether the entity is an array, its size, or what type it has.
+  }];
+
+  let parameters = (ins "mlir::Type":$eleTy, "mlir::AffineMapAttr":$map);
+
+  let extraClassDeclaration = [{
+    mlir::Type getElementType() const { return getEleTy(); }
+    mlir::AffineMapAttr getLayoutMap() const { return getMap(); }
+    static BoxType get(mlir::Type eleTy, mlir::AffineMapAttr map = {}) {
+      return get(eleTy.getContext(), eleTy, map);
+    }
+  }];
+
+  let genAccessors = 1;
+  let genVerifyInvariantsDecl = 1;
+}
+
 def ShapeType : FIR_Type<"Shape", "shape"> {
   let summary = "shape of a multidimensional array object";
 

diff  --git a/flang/lib/Optimizer/Dialect/FIRType.cpp b/flang/lib/Optimizer/Dialect/FIRType.cpp
index c0fd3d3f86df..35e71a7560ca 100644
--- a/flang/lib/Optimizer/Dialect/FIRType.cpp
+++ b/flang/lib/Optimizer/Dialect/FIRType.cpp
@@ -58,27 +58,6 @@ TYPE parseTypeSingleton(mlir::DialectAsmParser &parser, mlir::Location) {
   return TYPE::get(ty);
 }
 
-// `box` `<` type (',' affine-map)? `>`
-BoxType parseBox(mlir::DialectAsmParser &parser, mlir::Location loc) {
-  mlir::Type ofTy;
-  if (parser.parseLess() || parser.parseType(ofTy)) {
-    parser.emitError(parser.getCurrentLocation(), "expected type parameter");
-    return {};
-  }
-
-  mlir::AffineMapAttr map;
-  if (!parser.parseOptionalComma())
-    if (parser.parseAttribute(map)) {
-      parser.emitError(parser.getCurrentLocation(), "expected affine map");
-      return {};
-    }
-  if (parser.parseGreater()) {
-    parser.emitError(parser.getCurrentLocation(), "expected '>'");
-    return {};
-  }
-  return BoxType::get(ofTy, map);
-}
-
 // `boxchar` `<` kind `>`
 BoxCharType parseBoxChar(mlir::DialectAsmParser &parser) {
   return parseKindSingleton<BoxCharType>(parser);
@@ -355,7 +334,7 @@ mlir::Type fir::parseFirType(FIROpsDialect *dialect,
   if (typeNameLit == "array")
     return parseSequence(parser, loc);
   if (typeNameLit == "box")
-    return parseBox(parser, loc);
+    return generatedTypeParser(dialect->getContext(), parser, typeNameLit);
   if (typeNameLit == "boxchar")
     return parseBoxChar(parser);
   if (typeNameLit == "boxproc")
@@ -594,41 +573,6 @@ struct RealTypeStorage : public mlir::TypeStorage {
   explicit RealTypeStorage(KindTy kind) : kind{kind} {}
 };
 
-/// Boxed object (a Fortran descriptor)
-struct BoxTypeStorage : public mlir::TypeStorage {
-  using KeyTy = std::tuple<mlir::Type, mlir::AffineMapAttr>;
-
-  static unsigned hashKey(const KeyTy &key) {
-    auto hashVal{llvm::hash_combine(std::get<mlir::Type>(key))};
-    return llvm::hash_combine(
-        hashVal, llvm::hash_combine(std::get<mlir::AffineMapAttr>(key)));
-  }
-
-  bool operator==(const KeyTy &key) const {
-    return std::get<mlir::Type>(key) == getElementType() &&
-           std::get<mlir::AffineMapAttr>(key) == getLayoutMap();
-  }
-
-  static BoxTypeStorage *construct(mlir::TypeStorageAllocator &allocator,
-                                   const KeyTy &key) {
-    auto *storage = allocator.allocate<BoxTypeStorage>();
-    return new (storage) BoxTypeStorage{std::get<mlir::Type>(key),
-                                        std::get<mlir::AffineMapAttr>(key)};
-  }
-
-  mlir::Type getElementType() const { return eleTy; }
-  mlir::AffineMapAttr getLayoutMap() const { return map; }
-
-protected:
-  mlir::Type eleTy;
-  mlir::AffineMapAttr map;
-
-private:
-  BoxTypeStorage() = delete;
-  explicit BoxTypeStorage(mlir::Type eleTy, mlir::AffineMapAttr map)
-      : eleTy{eleTy}, map{map} {}
-};
-
 /// Boxed CHARACTER object type
 struct BoxCharTypeStorage : public mlir::TypeStorage {
   using KeyTy = KindTy;
@@ -1013,20 +957,6 @@ RealType fir::RealType::get(mlir::MLIRContext *ctxt, KindTy kind) {
 
 KindTy fir::RealType::getFKind() const { return getImpl()->getFKind(); }
 
-// Box<T>
-
-BoxType fir::BoxType::get(mlir::Type elementType, mlir::AffineMapAttr map) {
-  return Base::get(elementType.getContext(), elementType, map);
-}
-
-mlir::Type fir::BoxType::getEleTy() const {
-  return getImpl()->getElementType();
-}
-
-mlir::AffineMapAttr fir::BoxType::getLayoutMap() const {
-  return getImpl()->getLayoutMap();
-}
-
 mlir::LogicalResult
 fir::BoxType::verifyConstructionInvariants(mlir::Location, mlir::Type eleTy,
                                            mlir::AffineMapAttr map) {
@@ -1340,16 +1270,6 @@ void fir::verifyIntegralType(mlir::Type type) {
 void fir::printFirType(FIROpsDialect *, mlir::Type ty,
                        mlir::DialectAsmPrinter &p) {
   auto &os = p.getStream();
-  if (auto type = ty.dyn_cast<BoxType>()) {
-    os << "box<";
-    p.printType(type.getEleTy());
-    if (auto map = type.getLayoutMap()) {
-      os << ", ";
-      p.printAttribute(map);
-    }
-    os << '>';
-    return;
-  }
   if (auto type = ty.dyn_cast<BoxCharType>()) {
     os << "boxchar<" << type.getEleTy().cast<fir::CharacterType>().getFKind()
        << '>';
@@ -1500,3 +1420,44 @@ bool fir::isa_unknown_size_box(mlir::Type t) {
   }
   return false;
 }
+
+namespace fir {
+
+//===----------------------------------------------------------------------===//
+// BoxType
+//===----------------------------------------------------------------------===//
+
+// `box` `<` type (',' affine-map)? `>`
+mlir::Type BoxType::parse(mlir::MLIRContext *context,
+                          mlir::DialectAsmParser &parser) {
+  mlir::Type ofTy;
+  if (parser.parseLess() || parser.parseType(ofTy)) {
+    parser.emitError(parser.getCurrentLocation(), "expected type parameter");
+    return Type();
+  }
+
+  mlir::AffineMapAttr map;
+  if (!parser.parseOptionalComma()) {
+    if (parser.parseAttribute(map)) {
+      parser.emitError(parser.getCurrentLocation(), "expected affine map");
+      return Type();
+    }
+  }
+  if (parser.parseGreater()) {
+    parser.emitError(parser.getCurrentLocation(), "expected '>'");
+    return Type();
+  }
+  return get(ofTy, map);
+}
+
+void BoxType::print(::mlir::DialectAsmPrinter &printer) const {
+  printer << "box<";
+  printer.printType(getEleTy());
+  if (auto map = getLayoutMap()) {
+    printer << ", ";
+    printer.printAttribute(map);
+  }
+  printer << '>';
+}
+
+} // namespace fir


        


More information about the flang-commits mailing list