MLIR  21.0.0git
ODSSupport.h
Go to the documentation of this file.
1 //===- ODSSupport.h ---------------------------------------------*- C++ -*-===//
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 defines a number of support method for ODS generated code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_ODSSUPPORT_H
14 #define MLIR_IR_ODSSUPPORT_H
15 
16 #include "mlir/IR/Attributes.h"
17 #include "mlir/IR/MLIRContext.h"
18 #include "mlir/Support/LLVM.h"
19 
20 namespace mlir {
21 
22 //===----------------------------------------------------------------------===//
23 // Support for properties
24 //===----------------------------------------------------------------------===//
25 
26 /// Convert an IntegerAttr attribute to an int64_t, or return an error if the
27 /// attribute isn't an IntegerAttr. If the optional diagnostic is provided an
28 /// error message is also emitted.
29 LogicalResult
30 convertFromAttribute(int64_t &storage, Attribute attr,
31  function_ref<InFlightDiagnostic()> emitError);
32 
33 /// Convert the provided int64_t to an IntegerAttr attribute.
34 Attribute convertToAttribute(MLIRContext *ctx, int64_t storage);
35 
36 /// Convert an IntegerAttr attribute to an int32_t, or return an error if the
37 /// attribute isn't an IntegerAttr. If the optional diagnostic is provided an
38 /// error message is also emitted.
39 LogicalResult
40 convertFromAttribute(int32_t &storage, Attribute attr,
41  function_ref<InFlightDiagnostic()> emitError);
42 
43 /// Convert the provided int32_t to an IntegerAttr attribute.
44 Attribute convertToAttribute(MLIRContext *ctx, int32_t storage);
45 
46 /// Convert an IntegerAttr attribute to an int8_t, or return an error if the
47 /// attribute isn't an IntegerAttr. If the optional diagnostic is provided an
48 /// error message is also emitted.
49 LogicalResult
50 convertFromAttribute(int8_t &storage, Attribute attr,
51  function_ref<InFlightDiagnostic()> emitError);
52 
53 /// Convert the provided int8_t to an IntegerAttr attribute.
54 Attribute convertToAttribute(MLIRContext *ctx, int8_t storage);
55 
56 /// Convert an IntegerAttr attribute to an uint8_t, or return an error if the
57 /// attribute isn't an IntegerAttr. If the optional diagnostic is provided an
58 /// error message is also emitted.
59 LogicalResult
60 convertFromAttribute(uint8_t &storage, Attribute attr,
61  function_ref<InFlightDiagnostic()> emitError);
62 
63 /// Convert the provided uint8_t to an IntegerAttr attribute.
64 Attribute convertToAttribute(MLIRContext *ctx, uint8_t storage);
65 
66 /// Extract the string from `attr` into `storage`. If `attr` is not a
67 /// `StringAttr`, return failure and emit an error into the diagnostic from
68 /// `emitError`.
69 LogicalResult
70 convertFromAttribute(std::string &storage, Attribute attr,
71  function_ref<InFlightDiagnostic()> emitError);
72 
73 /// Convert the given string into a StringAttr. Note that this takes a reference
74 /// to the storage of a string property, which is an std::string.
75 Attribute convertToAttribute(MLIRContext *ctx, const std::string &storage);
76 
77 /// Extract the boolean from `attr` into `storage`. If `attr` is not a
78 /// `BoolAttr`, return failure and emit an error into the diagnostic from
79 /// `emitError`.
80 LogicalResult
81 convertFromAttribute(bool &storage, Attribute attr,
82  function_ref<InFlightDiagnostic()> emitError);
83 
84 /// Convert the given string into a BooleanAttr.
85 Attribute convertToAttribute(MLIRContext *ctx, bool storage);
86 
87 /// Convert a DenseI64ArrayAttr to the provided storage. It is expected that the
88 /// storage has the same size as the array. An error is returned if the
89 /// attribute isn't a DenseI64ArrayAttr or it does not have the same size. If
90 /// the optional diagnostic is provided an error message is also emitted.
91 LogicalResult
92 convertFromAttribute(MutableArrayRef<int64_t> storage, Attribute attr,
93  function_ref<InFlightDiagnostic()> emitError);
94 
95 /// Convert a DenseI32ArrayAttr to the provided storage. It is expected that the
96 /// storage has the same size as the array. An error is returned if the
97 /// attribute isn't a DenseI32ArrayAttr or it does not have the same size. If
98 /// the optional diagnostic is provided an error message is also emitted.
99 LogicalResult
100 convertFromAttribute(MutableArrayRef<int32_t> storage, Attribute attr,
101  function_ref<InFlightDiagnostic()> emitError);
102 
103 /// Convert a DenseI64ArrayAttr to the provided storage, which will be
104 /// cleared before writing. An error is returned and emitted to the optional
105 /// `emitError` function if the attribute isn't a DenseI64ArrayAttr.
106 LogicalResult
107 convertFromAttribute(SmallVectorImpl<int64_t> &storage, Attribute attr,
108  function_ref<InFlightDiagnostic()> emitError);
109 
110 /// Convert a DenseI32ArrayAttr to the provided storage, which will be
111 /// cleared before writing. It is expected that the storage has the same size as
112 /// the array. An error is returned and emitted to the optional `emitError`
113 /// function if the attribute isn't a DenseI32ArrayAttr.
114 LogicalResult
115 convertFromAttribute(SmallVectorImpl<int32_t> &storage, Attribute attr,
116  function_ref<InFlightDiagnostic()> emitError);
117 
118 /// Convert the provided ArrayRef<int64_t> to a DenseI64ArrayAttr attribute.
119 Attribute convertToAttribute(MLIRContext *ctx, ArrayRef<int64_t> storage);
120 
121 /// Convert the provided ArrayRef<int32_t> to a DenseI32ArrayAttr attribute.
122 Attribute convertToAttribute(MLIRContext *ctx, ArrayRef<int32_t> storage);
123 
124 } // namespace mlir
125 
126 #endif // MLIR_IR_ODSSUPPORT_H
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition: LLVM.h:152
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.
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