• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Optimizer/Dialect/FIRAttr.h -- FIR attributes -----------*- 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 OPTIMIZER_DIALECT_FIRATTR_H
10 #define OPTIMIZER_DIALECT_FIRATTR_H
11 
12 #include "mlir/IR/Attributes.h"
13 
14 namespace mlir {
15 class DialectAsmParser;
16 class DialectAsmPrinter;
17 } // namespace mlir
18 
19 namespace fir {
20 
21 class FIROpsDialect;
22 
23 namespace detail {
24 struct RealAttributeStorage;
25 struct TypeAttributeStorage;
26 } // namespace detail
27 
28 class ExactTypeAttr
29     : public mlir::Attribute::AttrBase<ExactTypeAttr, mlir::Attribute,
30                                        detail::TypeAttributeStorage> {
31 public:
32   using Base::Base;
33   using ValueType = mlir::Type;
34 
getAttrName()35   static constexpr llvm::StringRef getAttrName() { return "instance"; }
36   static ExactTypeAttr get(mlir::Type value);
37 
38   mlir::Type getType() const;
39 };
40 
41 class SubclassAttr
42     : public mlir::Attribute::AttrBase<SubclassAttr, mlir::Attribute,
43                                        detail::TypeAttributeStorage> {
44 public:
45   using Base::Base;
46   using ValueType = mlir::Type;
47 
getAttrName()48   static constexpr llvm::StringRef getAttrName() { return "subsumed"; }
49   static SubclassAttr get(mlir::Type value);
50 
51   mlir::Type getType() const;
52 };
53 
54 // Attributes for building SELECT CASE multiway branches
55 
56 /// A closed interval (including the bound values) is an interval with both an
57 /// upper and lower bound as given as ssa-values.
58 /// A case selector of `CASE (n:m)` corresponds to any value from `n` to `m` and
59 /// is encoded as `#fir.interval, %n, %m`.
60 class ClosedIntervalAttr
61     : public mlir::Attribute::AttrBase<ClosedIntervalAttr, mlir::Attribute,
62                                        mlir::AttributeStorage> {
63 public:
64   using Base::Base;
65 
getAttrName()66   static constexpr llvm::StringRef getAttrName() { return "interval"; }
67   static ClosedIntervalAttr get(mlir::MLIRContext *ctxt);
68 };
69 
70 /// An upper bound is an open interval (including the bound value) as given as
71 /// an ssa-value.
72 /// A case selector of `CASE (:m)` corresponds to any value up to and including
73 /// `m` and is encoded as `#fir.upper, %m`.
74 class UpperBoundAttr
75     : public mlir::Attribute::AttrBase<UpperBoundAttr, mlir::Attribute,
76                                        mlir::AttributeStorage> {
77 public:
78   using Base::Base;
79 
getAttrName()80   static constexpr llvm::StringRef getAttrName() { return "upper"; }
81   static UpperBoundAttr get(mlir::MLIRContext *ctxt);
82 };
83 
84 /// A lower bound is an open interval (including the bound value) as given as
85 /// an ssa-value.
86 /// A case selector of `CASE (n:)` corresponds to any value down to and
87 /// including `n` and is encoded as `#fir.lower, %n`.
88 class LowerBoundAttr
89     : public mlir::Attribute::AttrBase<LowerBoundAttr, mlir::Attribute,
90                                        mlir::AttributeStorage> {
91 public:
92   using Base::Base;
93 
getAttrName()94   static constexpr llvm::StringRef getAttrName() { return "lower"; }
95   static LowerBoundAttr get(mlir::MLIRContext *ctxt);
96 };
97 
98 /// A pointer interval is a closed interval as given as an ssa-value. The
99 /// interval contains exactly one value.
100 /// A case selector of `CASE (p)` corresponds to exactly the value `p` and is
101 /// encoded as `#fir.point, %p`.
102 class PointIntervalAttr
103     : public mlir::Attribute::AttrBase<PointIntervalAttr, mlir::Attribute,
104                                        mlir::AttributeStorage> {
105 public:
106   using Base::Base;
107 
getAttrName()108   static constexpr llvm::StringRef getAttrName() { return "point"; }
109   static PointIntervalAttr get(mlir::MLIRContext *ctxt);
110 };
111 
112 /// A real attribute is used to workaround MLIR's default parsing of a real
113 /// constant.
114 /// `#fir.real<10, 3.14>` is used to introduce a real constant of value `3.14`
115 /// with a kind of `10`.
116 class RealAttr
117     : public mlir::Attribute::AttrBase<RealAttr, mlir::Attribute,
118                                        detail::RealAttributeStorage> {
119 public:
120   using Base::Base;
121   using ValueType = std::pair<int, llvm::APFloat>;
122 
getAttrName()123   static constexpr llvm::StringRef getAttrName() { return "real"; }
124   static RealAttr get(mlir::MLIRContext *ctxt, const ValueType &key);
125 
126   int getFKind() const;
127   llvm::APFloat getValue() const;
128 };
129 
130 mlir::Attribute parseFirAttribute(FIROpsDialect *dialect,
131                                   mlir::DialectAsmParser &parser,
132                                   mlir::Type type);
133 
134 void printFirAttribute(FIROpsDialect *dialect, mlir::Attribute attr,
135                        mlir::DialectAsmPrinter &p);
136 
137 } // namespace fir
138 
139 #endif // OPTIMIZER_DIALECT_FIRATTR_H
140