MLIR  21.0.0git
ODSSupport.cpp
Go to the documentation of this file.
1 //===- ODSSupport.cpp -----------------------------------------------------===//
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 file contains out-of-line implementations of the support types that
10 // Operation and related classes build on top of.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/IR/ODSSupport.h"
16 #include "mlir/IR/BuiltinTypes.h"
17 #include "mlir/IR/Diagnostics.h"
18 
19 using namespace mlir;
20 
21 LogicalResult
22 mlir::convertFromAttribute(int64_t &storage, Attribute attr,
24  auto valueAttr = dyn_cast<IntegerAttr>(attr);
25  if (!valueAttr) {
26  emitError() << "expected IntegerAttr for key `value`";
27  return failure();
28  }
29  storage = valueAttr.getValue().getSExtValue();
30  return success();
31 }
33  return IntegerAttr::get(IntegerType::get(ctx, 64), storage);
34 }
35 
36 LogicalResult
37 mlir::convertFromAttribute(int32_t &storage, Attribute attr,
39  auto valueAttr = dyn_cast<IntegerAttr>(attr);
40  if (!valueAttr) {
41  emitError() << "expected IntegerAttr for key `value`";
42  return failure();
43  }
44  storage = valueAttr.getValue().getSExtValue();
45  return success();
46 }
48  return IntegerAttr::get(IntegerType::get(ctx, 32), storage);
49 }
50 
51 LogicalResult
52 mlir::convertFromAttribute(int8_t &storage, Attribute attr,
54  auto valueAttr = dyn_cast<IntegerAttr>(attr);
55  if (!valueAttr) {
56  emitError() << "expected IntegerAttr for key `value`";
57  return failure();
58  }
59  storage = valueAttr.getValue().getSExtValue();
60  return success();
61 }
62 
64  /// Convert the provided int8_t to an IntegerAttr attribute.
65  return IntegerAttr::get(IntegerType::get(ctx, 8), storage);
66 }
67 
68 LogicalResult
69 mlir::convertFromAttribute(uint8_t &storage, Attribute attr,
71  auto valueAttr = dyn_cast<IntegerAttr>(attr);
72  if (!valueAttr) {
73  emitError() << "expected IntegerAttr for key `value`";
74  return failure();
75  }
76  storage = valueAttr.getValue().getZExtValue();
77  return success();
78 }
79 
81  /// Convert the provided uint8_t to an IntegerAttr attribute.
82  return IntegerAttr::get(IntegerType::get(ctx, 8), storage);
83 }
84 
85 LogicalResult
86 mlir::convertFromAttribute(std::string &storage, Attribute attr,
88  auto valueAttr = dyn_cast<StringAttr>(attr);
89  if (!valueAttr)
90  return emitError()
91  << "expected string property to come from string attribute";
92  storage = valueAttr.getValue().str();
93  return success();
94 }
96  const std::string &storage) {
97  return StringAttr::get(ctx, storage);
98 }
99 
100 LogicalResult
103  auto valueAttr = dyn_cast<BoolAttr>(attr);
104  if (!valueAttr)
105  return emitError()
106  << "expected string property to come from string attribute";
107  storage = valueAttr.getValue();
108  return success();
109 }
111  return BoolAttr::get(ctx, storage);
112 }
113 
114 template <typename DenseArrayTy, typename T>
115 LogicalResult
118  StringRef denseArrayTyStr) {
119  auto valueAttr = dyn_cast<DenseArrayTy>(attr);
120  if (!valueAttr) {
121  emitError() << "expected " << denseArrayTyStr << " for key `value`";
122  return failure();
123  }
124  if (valueAttr.size() != static_cast<int64_t>(storage.size())) {
125  emitError() << "size mismatch in attribute conversion: " << valueAttr.size()
126  << " vs " << storage.size();
127  return failure();
128  }
129  llvm::copy(valueAttr.asArrayRef(), storage.begin());
130  return success();
131 }
132 LogicalResult
135  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
136  "DenseI64ArrayAttr");
137 }
138 LogicalResult
141  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
142  "DenseI32ArrayAttr");
143 }
144 
145 template <typename DenseArrayTy, typename T>
146 LogicalResult
149  StringRef denseArrayTyStr) {
150  auto valueAttr = dyn_cast<DenseArrayTy>(attr);
151  if (!valueAttr) {
152  emitError() << "expected " << denseArrayTyStr << " for key `value`";
153  return failure();
154  }
155  storage.resize_for_overwrite(valueAttr.size());
156  llvm::copy(valueAttr.asArrayRef(), storage.begin());
157  return success();
158 }
159 LogicalResult
162  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
163  "DenseI64ArrayAttr");
164 }
165 LogicalResult
168  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
169  "DenseI32ArrayAttr");
170 }
171 
173  ArrayRef<int64_t> storage) {
174  return DenseI64ArrayAttr::get(ctx, storage);
175 }
176 
178  ArrayRef<int32_t> storage) {
179  return DenseI32ArrayAttr::get(ctx, storage);
180 }
static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder)
Copies the given number of bytes from src to dst pointers.
LogicalResult convertDenseArrayFromAttr(MutableArrayRef< T > storage, Attribute attr, function_ref< InFlightDiagnostic()> emitError, StringRef denseArrayTyStr)
Definition: ODSSupport.cpp:116
Attributes are known-constant values of operations.
Definition: Attributes.h:25
static BoolAttr get(MLIRContext *context, bool value)
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:314
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< T > content)
Builder from ArrayRef<T>.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
Attribute convertToAttribute(MLIRContext *ctx, int64_t storage)
Convert the provided int64_t to an IntegerAttr attribute.
Definition: ODSSupport.cpp:32
LogicalResult convertFromAttribute(int64_t &storage, Attribute attr, function_ref< InFlightDiagnostic()> emitError)
Convert an IntegerAttr attribute to an int64_t, or return an error if the attribute isn't an IntegerA...
Definition: ODSSupport.cpp:22