MLIR  20.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(std::string &storage, Attribute attr,
54  auto valueAttr = dyn_cast<StringAttr>(attr);
55  if (!valueAttr)
56  return emitError()
57  << "expected string property to come from string attribute";
58  storage = valueAttr.getValue().str();
59  return success();
60 }
62  const std::string &storage) {
63  return StringAttr::get(ctx, storage);
64 }
65 
66 LogicalResult
69  auto valueAttr = dyn_cast<BoolAttr>(attr);
70  if (!valueAttr)
71  return emitError()
72  << "expected string property to come from string attribute";
73  storage = valueAttr.getValue();
74  return success();
75 }
77  return BoolAttr::get(ctx, storage);
78 }
79 
80 template <typename DenseArrayTy, typename T>
81 LogicalResult
84  StringRef denseArrayTyStr) {
85  auto valueAttr = dyn_cast<DenseArrayTy>(attr);
86  if (!valueAttr) {
87  emitError() << "expected " << denseArrayTyStr << " for key `value`";
88  return failure();
89  }
90  if (valueAttr.size() != static_cast<int64_t>(storage.size())) {
91  emitError() << "size mismatch in attribute conversion: " << valueAttr.size()
92  << " vs " << storage.size();
93  return failure();
94  }
95  llvm::copy(valueAttr.asArrayRef(), storage.begin());
96  return success();
97 }
98 LogicalResult
101  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
102  "DenseI64ArrayAttr");
103 }
104 LogicalResult
107  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
108  "DenseI32ArrayAttr");
109 }
110 
111 template <typename DenseArrayTy, typename T>
112 LogicalResult
115  StringRef denseArrayTyStr) {
116  auto valueAttr = dyn_cast<DenseArrayTy>(attr);
117  if (!valueAttr) {
118  emitError() << "expected " << denseArrayTyStr << " for key `value`";
119  return failure();
120  }
121  storage.resize_for_overwrite(valueAttr.size());
122  llvm::copy(valueAttr.asArrayRef(), storage.begin());
123  return success();
124 }
125 LogicalResult
128  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
129  "DenseI64ArrayAttr");
130 }
131 LogicalResult
134  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
135  "DenseI32ArrayAttr");
136 }
137 
139  ArrayRef<int64_t> storage) {
140  return DenseI64ArrayAttr::get(ctx, storage);
141 }
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:82
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