1 //===- SDBMExprDetail.h - MLIR SDBM Expression storage details --*- 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 // This holds implementation details of SDBMExpr, in particular underlying
10 // storage types.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_IR_SDBMEXPRDETAIL_H
15 #define MLIR_IR_SDBMEXPRDETAIL_H
16 
17 #include "mlir/Dialect/SDBM/SDBMExpr.h"
18 #include "mlir/Support/StorageUniquer.h"
19 
20 namespace mlir {
21 
22 class SDBMDialect;
23 
24 namespace detail {
25 
26 // Base storage class for SDBMExpr.
27 struct SDBMExprStorage : public StorageUniquer::BaseStorage {
getKindSDBMExprStorage28   SDBMExprKind getKind() { return kind; }
29 
30   SDBMDialect *dialect;
31   SDBMExprKind kind;
32 };
33 
34 // Storage class for SDBM sum and stripe expressions.
35 struct SDBMBinaryExprStorage : public SDBMExprStorage {
36   using KeyTy = std::tuple<unsigned, SDBMDirectExpr, SDBMConstantExpr>;
37 
38   bool operator==(const KeyTy &key) const {
39     return static_cast<SDBMExprKind>(std::get<0>(key)) == kind &&
40            std::get<1>(key) == lhs && std::get<2>(key) == rhs;
41   }
42 
43   static SDBMBinaryExprStorage *
constructSDBMBinaryExprStorage44   construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) {
45     auto *result = allocator.allocate<SDBMBinaryExprStorage>();
46     result->lhs = std::get<1>(key);
47     result->rhs = std::get<2>(key);
48     result->dialect = result->lhs.getDialect();
49     result->kind = static_cast<SDBMExprKind>(std::get<0>(key));
50     return result;
51   }
52 
53   SDBMDirectExpr lhs;
54   SDBMConstantExpr rhs;
55 };
56 
57 // Storage class for SDBM difference expressions.
58 struct SDBMDiffExprStorage : public SDBMExprStorage {
59   using KeyTy = std::pair<SDBMDirectExpr, SDBMTermExpr>;
60 
61   bool operator==(const KeyTy &key) const {
62     return std::get<0>(key) == lhs && std::get<1>(key) == rhs;
63   }
64 
65   static SDBMDiffExprStorage *
constructSDBMDiffExprStorage66   construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) {
67     auto *result = allocator.allocate<SDBMDiffExprStorage>();
68     result->lhs = std::get<0>(key);
69     result->rhs = std::get<1>(key);
70     result->dialect = result->lhs.getDialect();
71     result->kind = SDBMExprKind::Diff;
72     return result;
73   }
74 
75   SDBMDirectExpr lhs;
76   SDBMTermExpr rhs;
77 };
78 
79 // Storage class for SDBM constant expressions.
80 struct SDBMConstantExprStorage : public SDBMExprStorage {
81   using KeyTy = int64_t;
82 
83   bool operator==(const KeyTy &key) const { return constant == key; }
84 
85   static SDBMConstantExprStorage *
constructSDBMConstantExprStorage86   construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) {
87     auto *result = allocator.allocate<SDBMConstantExprStorage>();
88     result->constant = key;
89     result->kind = SDBMExprKind::Constant;
90     return result;
91   }
92 
93   int64_t constant;
94 };
95 
96 // Storage class for SDBM dimension and symbol expressions.
97 struct SDBMTermExprStorage : public SDBMExprStorage {
98   using KeyTy = std::pair<unsigned, unsigned>;
99 
100   bool operator==(const KeyTy &key) const {
101     return kind == static_cast<SDBMExprKind>(key.first) &&
102            position == key.second;
103   }
104 
105   static SDBMTermExprStorage *
constructSDBMTermExprStorage106   construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) {
107     auto *result = allocator.allocate<SDBMTermExprStorage>();
108     result->kind = static_cast<SDBMExprKind>(key.first);
109     result->position = key.second;
110     return result;
111   }
112 
113   unsigned position;
114 };
115 
116 // Storage class for SDBM negation expressions.
117 struct SDBMNegExprStorage : public SDBMExprStorage {
118   using KeyTy = SDBMDirectExpr;
119 
120   bool operator==(const KeyTy &key) const { return key == expr; }
121 
122   static SDBMNegExprStorage *
constructSDBMNegExprStorage123   construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) {
124     auto *result = allocator.allocate<SDBMNegExprStorage>();
125     result->expr = key;
126     result->dialect = key.getDialect();
127     result->kind = SDBMExprKind::Neg;
128     return result;
129   }
130 
131   SDBMDirectExpr expr;
132 };
133 
134 } // end namespace detail
135 } // end namespace mlir
136 
137 #endif // MLIR_IR_SDBMEXPRDETAIL_H
138