MLIR 22.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"
17#include "mlir/IR/Diagnostics.h"
18
19using namespace mlir;
20
21LogicalResult
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
36LogicalResult
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
51LogicalResult
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
68LogicalResult
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
85LogicalResult
86mlir::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
100LogicalResult
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
114template <typename DenseArrayTy, typename T>
115static 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}
132LogicalResult
138LogicalResult
144
145template <typename DenseArrayTy, typename T>
146static 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}
159LogicalResult
165LogicalResult
171
176
return success()
static LogicalResult convertDenseArrayFromAttr(MutableArrayRef< T > storage, Attribute attr, function_ref< InFlightDiagnostic()> emitError, StringRef denseArrayTyStr)
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.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< int64_t > content)
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Attribute convertToAttribute(MLIRContext *ctx, int64_t storage)
Convert the provided int64_t to an IntegerAttr attribute.
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...
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152