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"
15#include "mlir/IR/BuiltinAttributes.h"
16#include "mlir/IR/BuiltinTypes.h"
17#include "mlir/IR/Diagnostics.h"
18
19using namespace mlir;
20
21LogicalResult
22mlir::convertFromAttribute(int64_t &storage, Attribute attr,
23 function_ref<InFlightDiagnostic()> emitError) {
24 auto valueAttr = dyn_cast<IntegerAttr>(Val&: attr);
25 if (!valueAttr) {
26 emitError() << "expected IntegerAttr";
27 return failure();
28 }
29 storage = valueAttr.getValue().getSExtValue();
30 return success();
31}
32Attribute mlir::convertToAttribute(MLIRContext *ctx, int64_t storage) {
33 return IntegerAttr::get(type: IntegerType::get(context: ctx, width: 64), value: storage);
34}
35
36LogicalResult
37mlir::convertFromAttribute(int32_t &storage, Attribute attr,
38 function_ref<InFlightDiagnostic()> emitError) {
39 auto valueAttr = dyn_cast<IntegerAttr>(Val&: attr);
40 if (!valueAttr) {
41 emitError() << "expected IntegerAttr";
42 return failure();
43 }
44 storage = valueAttr.getValue().getSExtValue();
45 return success();
46}
47Attribute mlir::convertToAttribute(MLIRContext *ctx, int32_t storage) {
48 return IntegerAttr::get(type: IntegerType::get(context: ctx, width: 32), value: storage);
49}
50
51LogicalResult
52mlir::convertFromAttribute(int8_t &storage, Attribute attr,
53 function_ref<InFlightDiagnostic()> emitError) {
54 auto valueAttr = dyn_cast<IntegerAttr>(Val&: attr);
55 if (!valueAttr) {
56 emitError() << "expected IntegerAttr";
57 return failure();
58 }
59 storage = valueAttr.getValue().getSExtValue();
60 return success();
61}
62
63Attribute mlir::convertToAttribute(MLIRContext *ctx, int8_t storage) {
64 /// Convert the provided int8_t to an IntegerAttr attribute.
65 return IntegerAttr::get(type: IntegerType::get(context: ctx, width: 8), value: storage);
66}
67
68LogicalResult
69mlir::convertFromAttribute(uint8_t &storage, Attribute attr,
70 function_ref<InFlightDiagnostic()> emitError) {
71 auto valueAttr = dyn_cast<IntegerAttr>(Val&: attr);
72 if (!valueAttr) {
73 emitError() << "expected IntegerAttr";
74 return failure();
75 }
76 storage = valueAttr.getValue().getZExtValue();
77 return success();
78}
79
80Attribute mlir::convertToAttribute(MLIRContext *ctx, uint8_t storage) {
81 /// Convert the provided uint8_t to an IntegerAttr attribute.
82 return IntegerAttr::get(type: IntegerType::get(context: ctx, width: 8), value: storage);
83}
84
85LogicalResult
86mlir::convertFromAttribute(std::string &storage, Attribute attr,
87 function_ref<InFlightDiagnostic()> emitError) {
88 auto valueAttr = dyn_cast<StringAttr>(Val&: attr);
89 if (!valueAttr)
90 return emitError() << "expected StringAttr";
91 storage = valueAttr.getValue().str();
92 return success();
93}
94Attribute mlir::convertToAttribute(MLIRContext *ctx,
95 const std::string &storage) {
96 return StringAttr::get(context: ctx, bytes: storage);
97}
98
99LogicalResult
100mlir::convertFromAttribute(bool &storage, Attribute attr,
101 function_ref<InFlightDiagnostic()> emitError) {
102 auto valueAttr = dyn_cast<BoolAttr>(Val&: attr);
103 if (!valueAttr)
104 return emitError() << "expected BoolAttr";
105 storage = valueAttr.getValue();
106 return success();
107}
108Attribute mlir::convertToAttribute(MLIRContext *ctx, bool storage) {
109 return BoolAttr::get(context: ctx, value: storage);
110}
111
112template <typename DenseArrayTy, typename T>
113static LogicalResult
114convertDenseArrayFromAttr(MutableArrayRef<T> storage, Attribute attr,
115 function_ref<InFlightDiagnostic()> emitError,
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
131mlir::convertFromAttribute(MutableArrayRef<int64_t> storage, Attribute attr,
132 function_ref<InFlightDiagnostic()> emitError) {
133 return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
134 denseArrayTyStr: "DenseI64ArrayAttr");
135}
136LogicalResult
137mlir::convertFromAttribute(MutableArrayRef<int32_t> storage, Attribute attr,
138 function_ref<InFlightDiagnostic()> emitError) {
139 return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
140 denseArrayTyStr: "DenseI32ArrayAttr");
141}
142
143template <typename DenseArrayTy, typename T>
144static LogicalResult
145convertDenseArrayFromAttr(SmallVectorImpl<T> &storage, Attribute attr,
146 function_ref<InFlightDiagnostic()> emitError,
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
158mlir::convertFromAttribute(SmallVectorImpl<int64_t> &storage, Attribute attr,
159 function_ref<InFlightDiagnostic()> emitError) {
160 return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
161 denseArrayTyStr: "DenseI64ArrayAttr");
162}
163LogicalResult
164mlir::convertFromAttribute(SmallVectorImpl<int32_t> &storage, Attribute attr,
165 function_ref<InFlightDiagnostic()> emitError) {
166 return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
167 denseArrayTyStr: "DenseI32ArrayAttr");
168}
169
170Attribute mlir::convertToAttribute(MLIRContext *ctx,
171 ArrayRef<int64_t> storage) {
172 return DenseI64ArrayAttr::get(context: ctx, content: storage);
173}
174
175Attribute mlir::convertToAttribute(MLIRContext *ctx,
176 ArrayRef<int32_t> storage) {
177 return DenseI32ArrayAttr::get(context: ctx, content: storage);
178}
179