MLIR 23.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";
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";
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";
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";
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() << "expected StringAttr";
91 storage = valueAttr.getValue().str();
92 return success();
93}
95 const std::string &storage) {
96 return StringAttr::get(ctx, storage);
97}
98
99LogicalResult
102 auto valueAttr = dyn_cast<BoolAttr>(attr);
103 if (!valueAttr)
104 return emitError() << "expected BoolAttr";
105 storage = valueAttr.getValue();
106 return success();
107}
109 return BoolAttr::get(ctx, storage);
110}
111
112template <typename DenseArrayTy, typename T>
113static LogicalResult
116 StringRef denseArrayTyStr) {
117 auto valueAttr = dyn_cast<DenseArrayTy>(attr);
118 if (!valueAttr) {
119 emitError() << "expected " << denseArrayTyStr;
120 return failure();
121 }
122 if (valueAttr.size() != static_cast<int64_t>(storage.size())) {
123 emitError() << "size mismatch in attribute conversion: " << valueAttr.size()
124 << " vs " << storage.size();
125 return failure();
126 }
127 llvm::copy(valueAttr.asArrayRef(), storage.begin());
128 return success();
129}
130LogicalResult
136LogicalResult
142
143template <typename DenseArrayTy, typename T>
144static LogicalResult
147 StringRef denseArrayTyStr) {
148 auto valueAttr = dyn_cast<DenseArrayTy>(attr);
149 if (!valueAttr) {
150 emitError() << "expected " << denseArrayTyStr;
151 return failure();
152 }
153 storage.resize_for_overwrite(valueAttr.size());
154 llvm::copy(valueAttr.asArrayRef(), storage.begin());
155 return success();
156}
157LogicalResult
163LogicalResult
169
174
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:147