MLIR  19.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 
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 template <typename DenseArrayTy, typename T>
40  StringRef denseArrayTyStr) {
41  auto valueAttr = dyn_cast<DenseArrayTy>(attr);
42  if (!valueAttr) {
43  emitError() << "expected " << denseArrayTyStr << " for key `value`";
44  return failure();
45  }
46  if (valueAttr.size() != static_cast<int64_t>(storage.size())) {
47  emitError() << "size mismatch in attribute conversion: " << valueAttr.size()
48  << " vs " << storage.size();
49  return failure();
50  }
51  llvm::copy(valueAttr.asArrayRef(), storage.begin());
52  return success();
53 }
57  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,
58  "DenseI64ArrayAttr");
59 }
63  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,
64  "DenseI32ArrayAttr");
65 }
66 
68  ArrayRef<int64_t> storage) {
69  return DenseI64ArrayAttr::get(ctx, storage);
70 }
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:38
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:308
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.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
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
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26