Skip to content

Commit 339cb19

Browse files
clementvalmemfrob
authored and
memfrob
committed
[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
1 parent 3b6ffb8 commit 339cb19

File tree

5 files changed

+79
-115
lines changed

5 files changed

+79
-115
lines changed

flang/include/flang/Optimizer/Dialect/FIROps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef OPTIMIZER_DIALECT_FIROPS_H
1010
#define OPTIMIZER_DIALECT_FIROPS_H
1111

12+
#include "flang/Optimizer/Dialect/FIRType.h"
1213
#include "mlir/Dialect/StandardOps/IR/Ops.h"
1314
#include "mlir/Interfaces/LoopLikeInterface.h"
1415
#include "mlir/Interfaces/SideEffectInterfaces.h"

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ def fir_PointerType : Type<CPred<"$_self.isa<fir::PointerType>()">,
8383
def AnyReferenceLike : TypeConstraint<Or<[fir_ReferenceType.predicate,
8484
fir_HeapType.predicate, fir_PointerType.predicate]>, "any reference">;
8585

86-
// A descriptor tuple (captures a reference to an entity and other information)
87-
def fir_BoxType : Type<CPred<"$_self.isa<fir::BoxType>()">, "box type">;
88-
8986
// CHARACTER type descriptor. A pair of a data reference and a LEN value.
9087
def fir_BoxCharType : Type<CPred<"$_self.isa<fir::BoxCharType>()">,
9188
"box character type">;
@@ -94,11 +91,11 @@ def fir_BoxCharType : Type<CPred<"$_self.isa<fir::BoxCharType>()">,
9491
def fir_BoxProcType : Type<CPred<"$_self.isa<fir::BoxProcType>()">,
9592
"box procedure type">;
9693

97-
def AnyBoxLike : TypeConstraint<Or<[fir_BoxType.predicate,
94+
def AnyBoxLike : TypeConstraint<Or<[BoxType.predicate,
9895
fir_BoxCharType.predicate, fir_BoxProcType.predicate]>, "any box">;
9996

10097
def AnyRefOrBox : TypeConstraint<Or<[fir_ReferenceType.predicate,
101-
fir_HeapType.predicate, fir_PointerType.predicate, fir_BoxType.predicate]>,
98+
fir_HeapType.predicate, fir_PointerType.predicate, BoxType.predicate]>,
10299
"any reference or box">;
103100

104101
def AnyShapeLike : TypeConstraint<Or<[ShapeType.predicate,
@@ -1048,7 +1045,7 @@ def fir_EmboxOp : fir_Op<"embox", [NoSideEffect]> {
10481045

10491046
let arguments = (ins AnyReferenceLike:$memref, Variadic<AnyEmboxArg>:$args);
10501047

1051-
let results = (outs fir_BoxType);
1048+
let results = (outs BoxType);
10521049

10531050
let parser = "return parseEmboxOp(parser, result);";
10541051

@@ -1257,7 +1254,7 @@ def fir_UnboxOp : fir_SimpleOp<"unbox", [NoSideEffect]> {
12571254
```
12581255
}];
12591256

1260-
let arguments = (ins fir_BoxType:$box);
1257+
let arguments = (ins BoxType:$box);
12611258

12621259
let results = (outs
12631260
fir_ReferenceType, // pointer to data
@@ -1328,7 +1325,7 @@ def fir_BoxAddrOp : fir_SimpleOneResultOp<"box_addr", [NoSideEffect]> {
13281325
```
13291326
}];
13301327

1331-
let arguments = (ins fir_BoxType:$val);
1328+
let arguments = (ins BoxType:$val);
13321329

13331330
let results = (outs AnyReferenceLike);
13341331

@@ -1372,7 +1369,7 @@ def fir_BoxDimsOp : fir_Op<"box_dims", [NoSideEffect]> {
13721369
the box. The triple will be the lower bound, upper bound, and stride.
13731370
}];
13741371

1375-
let arguments = (ins fir_BoxType:$val, AnyIntegerLike:$dim);
1372+
let arguments = (ins BoxType:$val, AnyIntegerLike:$dim);
13761373

13771374
let results = (outs AnyIntegerLike, AnyIntegerLike, AnyIntegerLike);
13781375

@@ -1401,7 +1398,7 @@ def fir_BoxEleSizeOp : fir_SimpleOneResultOp<"box_elesize", [NoSideEffect]> {
14011398
must box an array of REAL values (with dynamic rank and extent).
14021399
}];
14031400

1404-
let arguments = (ins fir_BoxType:$val);
1401+
let arguments = (ins BoxType:$val);
14051402

14061403
let results = (outs AnyIntegerLike);
14071404
}
@@ -1424,7 +1421,7 @@ def fir_BoxIsAllocOp : fir_SimpleOp<"box_isalloc", [NoSideEffect]> {
14241421
variable is an `ALLOCATABLE`.
14251422
}];
14261423

1427-
let arguments = (ins fir_BoxType:$val);
1424+
let arguments = (ins BoxType:$val);
14281425

14291426
let results = (outs BoolLike);
14301427
}
@@ -1445,7 +1442,7 @@ def fir_BoxIsArrayOp : fir_SimpleOp<"box_isarray", [NoSideEffect]> {
14451442
```
14461443
}];
14471444

1448-
let arguments = (ins fir_BoxType:$val);
1445+
let arguments = (ins BoxType:$val);
14491446

14501447
let results = (outs BoolLike);
14511448
}
@@ -1463,7 +1460,7 @@ def fir_BoxIsPtrOp : fir_SimpleOp<"box_isptr", [NoSideEffect]> {
14631460
```
14641461
}];
14651462

1466-
let arguments = (ins fir_BoxType:$val);
1463+
let arguments = (ins BoxType:$val);
14671464

14681465
let results = (outs BoolLike);
14691466
}
@@ -1507,7 +1504,7 @@ def fir_BoxRankOp : fir_SimpleOneResultOp<"box_rank", [NoSideEffect]> {
15071504
descriptor may be either an array or a scalar, so the value is nonnegative.
15081505
}];
15091506

1510-
let arguments = (ins fir_BoxType:$val);
1507+
let arguments = (ins BoxType:$val);
15111508

15121509
let results = (outs AnyIntegerType);
15131510
}
@@ -1525,7 +1522,7 @@ def fir_BoxTypeDescOp : fir_SimpleOneResultOp<"box_tdesc", [NoSideEffect]> {
15251522
```
15261523
}];
15271524

1528-
let arguments = (ins fir_BoxType:$val);
1525+
let arguments = (ins BoxType:$val);
15291526

15301527
let results = (outs fir_TypeDescType);
15311528
}
@@ -2100,7 +2097,7 @@ def fir_DispatchOp : fir_Op<"dispatch",
21002097

21012098
let arguments = (ins
21022099
StrAttr:$method,
2103-
fir_BoxType:$object,
2100+
BoxType:$object,
21042101
Variadic<AnyType>:$args
21052102
);
21062103

flang/include/flang/Optimizer/Dialect/FIRType.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class FIROpsDialect;
4242
using KindTy = unsigned;
4343

4444
namespace detail {
45-
struct BoxTypeStorage;
4645
struct BoxCharTypeStorage;
4746
struct BoxProcTypeStorage;
4847
struct CharacterTypeStorage;
@@ -178,23 +177,6 @@ class RealType : public mlir::Type::TypeBase<RealType, mlir::Type,
178177

179178
// FIR support types
180179

181-
/// The type of a Fortran descriptor. Descriptors are tuples of information that
182-
/// describe an entity being passed from a calling context. This information
183-
/// might include (but is not limited to) whether the entity is an array, its
184-
/// size, or what type it has.
185-
class BoxType
186-
: public mlir::Type::TypeBase<BoxType, mlir::Type, detail::BoxTypeStorage> {
187-
public:
188-
using Base::Base;
189-
static BoxType get(mlir::Type eleTy, mlir::AffineMapAttr map = {});
190-
mlir::Type getEleTy() const;
191-
mlir::AffineMapAttr getLayoutMap() const;
192-
193-
static mlir::LogicalResult
194-
verifyConstructionInvariants(mlir::Location, mlir::Type eleTy,
195-
mlir::AffineMapAttr map);
196-
};
197-
198180
/// The type of a pair that describes a CHARACTER variable. Specifically, a
199181
/// CHARACTER consists of a reference to a buffer (the string value) and a LEN
200182
/// type parameter (the runtime length of the buffer).

flang/include/flang/Optimizer/Dialect/FIRTypes.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ class FIR_Type<string name, string typeMnemonic> : TypeDef<fir_Dialect, name> {
2121
let mnemonic = typeMnemonic;
2222
}
2323

24+
def BoxType : FIR_Type<"Box", "box"> {
25+
let summary = "The type of a Fortran descriptor";
26+
27+
let description = [{
28+
Descriptors are tuples of information that describe an entity being passed
29+
from a calling context. This information might include (but is not limited
30+
to) whether the entity is an array, its size, or what type it has.
31+
}];
32+
33+
let parameters = (ins "mlir::Type":$eleTy, "mlir::AffineMapAttr":$map);
34+
35+
let extraClassDeclaration = [{
36+
mlir::Type getElementType() const { return getEleTy(); }
37+
mlir::AffineMapAttr getLayoutMap() const { return getMap(); }
38+
static BoxType get(mlir::Type eleTy, mlir::AffineMapAttr map = {}) {
39+
return get(eleTy, map);
40+
}
41+
}];
42+
43+
let genAccessors = 1;
44+
let genVerifyInvariantsDecl = 1;
45+
}
46+
2447
def ShapeType : FIR_Type<"Shape", "shape"> {
2548
let summary = "shape of a multidimensional array object";
2649

flang/lib/Optimizer/Dialect/FIRType.cpp

Lines changed: 42 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,6 @@ TYPE parseTypeSingleton(mlir::DialectAsmParser &parser, mlir::Location) {
5858
return TYPE::get(ty);
5959
}
6060

61-
// `box` `<` type (',' affine-map)? `>`
62-
BoxType parseBox(mlir::DialectAsmParser &parser, mlir::Location loc) {
63-
mlir::Type ofTy;
64-
if (parser.parseLess() || parser.parseType(ofTy)) {
65-
parser.emitError(parser.getCurrentLocation(), "expected type parameter");
66-
return {};
67-
}
68-
69-
mlir::AffineMapAttr map;
70-
if (!parser.parseOptionalComma())
71-
if (parser.parseAttribute(map)) {
72-
parser.emitError(parser.getCurrentLocation(), "expected affine map");
73-
return {};
74-
}
75-
if (parser.parseGreater()) {
76-
parser.emitError(parser.getCurrentLocation(), "expected '>'");
77-
return {};
78-
}
79-
return BoxType::get(ofTy, map);
80-
}
81-
8261
// `boxchar` `<` kind `>`
8362
BoxCharType parseBoxChar(mlir::DialectAsmParser &parser) {
8463
return parseKindSingleton<BoxCharType>(parser);
@@ -355,7 +334,7 @@ mlir::Type fir::parseFirType(FIROpsDialect *dialect,
355334
if (typeNameLit == "array")
356335
return parseSequence(parser, loc);
357336
if (typeNameLit == "box")
358-
return parseBox(parser, loc);
337+
return generatedTypeParser(dialect->getContext(), parser, typeNameLit);
359338
if (typeNameLit == "boxchar")
360339
return parseBoxChar(parser);
361340
if (typeNameLit == "boxproc")
@@ -594,41 +573,6 @@ struct RealTypeStorage : public mlir::TypeStorage {
594573
explicit RealTypeStorage(KindTy kind) : kind{kind} {}
595574
};
596575

597-
/// Boxed object (a Fortran descriptor)
598-
struct BoxTypeStorage : public mlir::TypeStorage {
599-
using KeyTy = std::tuple<mlir::Type, mlir::AffineMapAttr>;
600-
601-
static unsigned hashKey(const KeyTy &key) {
602-
auto hashVal{llvm::hash_combine(std::get<mlir::Type>(key))};
603-
return llvm::hash_combine(
604-
hashVal, llvm::hash_combine(std::get<mlir::AffineMapAttr>(key)));
605-
}
606-
607-
bool operator==(const KeyTy &key) const {
608-
return std::get<mlir::Type>(key) == getElementType() &&
609-
std::get<mlir::AffineMapAttr>(key) == getLayoutMap();
610-
}
611-
612-
static BoxTypeStorage *construct(mlir::TypeStorageAllocator &allocator,
613-
const KeyTy &key) {
614-
auto *storage = allocator.allocate<BoxTypeStorage>();
615-
return new (storage) BoxTypeStorage{std::get<mlir::Type>(key),
616-
std::get<mlir::AffineMapAttr>(key)};
617-
}
618-
619-
mlir::Type getElementType() const { return eleTy; }
620-
mlir::AffineMapAttr getLayoutMap() const { return map; }
621-
622-
protected:
623-
mlir::Type eleTy;
624-
mlir::AffineMapAttr map;
625-
626-
private:
627-
BoxTypeStorage() = delete;
628-
explicit BoxTypeStorage(mlir::Type eleTy, mlir::AffineMapAttr map)
629-
: eleTy{eleTy}, map{map} {}
630-
};
631-
632576
/// Boxed CHARACTER object type
633577
struct BoxCharTypeStorage : public mlir::TypeStorage {
634578
using KeyTy = KindTy;
@@ -1013,20 +957,6 @@ RealType fir::RealType::get(mlir::MLIRContext *ctxt, KindTy kind) {
1013957

1014958
KindTy fir::RealType::getFKind() const { return getImpl()->getFKind(); }
1015959

1016-
// Box<T>
1017-
1018-
BoxType fir::BoxType::get(mlir::Type elementType, mlir::AffineMapAttr map) {
1019-
return Base::get(elementType.getContext(), elementType, map);
1020-
}
1021-
1022-
mlir::Type fir::BoxType::getEleTy() const {
1023-
return getImpl()->getElementType();
1024-
}
1025-
1026-
mlir::AffineMapAttr fir::BoxType::getLayoutMap() const {
1027-
return getImpl()->getLayoutMap();
1028-
}
1029-
1030960
mlir::LogicalResult
1031961
fir::BoxType::verifyConstructionInvariants(mlir::Location, mlir::Type eleTy,
1032962
mlir::AffineMapAttr map) {
@@ -1340,16 +1270,6 @@ void fir::verifyIntegralType(mlir::Type type) {
13401270
void fir::printFirType(FIROpsDialect *, mlir::Type ty,
13411271
mlir::DialectAsmPrinter &p) {
13421272
auto &os = p.getStream();
1343-
if (auto type = ty.dyn_cast<BoxType>()) {
1344-
os << "box<";
1345-
p.printType(type.getEleTy());
1346-
if (auto map = type.getLayoutMap()) {
1347-
os << ", ";
1348-
p.printAttribute(map);
1349-
}
1350-
os << '>';
1351-
return;
1352-
}
13531273
if (auto type = ty.dyn_cast<BoxCharType>()) {
13541274
os << "boxchar<" << type.getEleTy().cast<fir::CharacterType>().getFKind()
13551275
<< '>';
@@ -1500,3 +1420,44 @@ bool fir::isa_unknown_size_box(mlir::Type t) {
15001420
}
15011421
return false;
15021422
}
1423+
1424+
namespace fir {
1425+
1426+
//===----------------------------------------------------------------------===//
1427+
// BoxType
1428+
//===----------------------------------------------------------------------===//
1429+
1430+
// `box` `<` type (',' affine-map)? `>`
1431+
mlir::Type BoxType::parse(mlir::MLIRContext *context,
1432+
mlir::DialectAsmParser &parser) {
1433+
mlir::Type ofTy;
1434+
if (parser.parseLess() || parser.parseType(ofTy)) {
1435+
parser.emitError(parser.getCurrentLocation(), "expected type parameter");
1436+
return Type();
1437+
}
1438+
1439+
mlir::AffineMapAttr map;
1440+
if (!parser.parseOptionalComma()) {
1441+
if (parser.parseAttribute(map)) {
1442+
parser.emitError(parser.getCurrentLocation(), "expected affine map");
1443+
return Type();
1444+
}
1445+
}
1446+
if (parser.parseGreater()) {
1447+
parser.emitError(parser.getCurrentLocation(), "expected '>'");
1448+
return Type();
1449+
}
1450+
return get(ofTy, map);
1451+
}
1452+
1453+
void BoxType::print(::mlir::DialectAsmPrinter &printer) const {
1454+
printer << "box<";
1455+
printer.printType(getEleTy());
1456+
if (auto map = getLayoutMap()) {
1457+
printer << ", ";
1458+
printer.printAttribute(map);
1459+
}
1460+
printer << '>';
1461+
}
1462+
1463+
} // namespace fir

0 commit comments

Comments
 (0)