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 /// Extract the string from `attr` into `storage`. If `attr` is not a 47 /// `StringAttr`, return failure and emit an error into the diagnostic from 48 /// `emitError`. 49 LogicalResult 50 convertFromAttribute(std::string &storage, Attribute attr, 51 function_ref<InFlightDiagnostic()> emitError); 52 53 /// Convert the given string into a StringAttr. Note that this takes a reference 54 /// to the storage of a string property, which is an std::string. 55 Attribute convertToAttribute(MLIRContext *ctx, const std::string &storage); 56 57 /// Extract the boolean from `attr` into `storage`. If `attr` is not a 58 /// `BoolAttr`, return failure and emit an error into the diagnostic from 59 /// `emitError`. 60 LogicalResult 61 convertFromAttribute(bool &storage, Attribute attr, 62 function_ref<InFlightDiagnostic()> emitError); 63 64 /// Convert the given string into a BooleanAttr. 65 Attribute convertToAttribute(MLIRContext *ctx, bool storage); 66 67 /// Convert a DenseI64ArrayAttr to the provided storage. It is expected that the 68 /// storage has the same size as the array. An error is returned if the 69 /// attribute isn't a DenseI64ArrayAttr or it does not have the same size. If 70 /// the optional diagnostic is provided an error message is also emitted. 71 LogicalResult 72 convertFromAttribute(MutableArrayRef<int64_t> storage, Attribute attr, 73 function_ref<InFlightDiagnostic()> emitError); 74 75 /// Convert a DenseI32ArrayAttr to the provided storage. It is expected that the 76 /// storage has the same size as the array. An error is returned if the 77 /// attribute isn't a DenseI32ArrayAttr or it does not have the same size. If 78 /// the optional diagnostic is provided an error message is also emitted. 79 LogicalResult 80 convertFromAttribute(MutableArrayRef<int32_t> storage, Attribute attr, 81 function_ref<InFlightDiagnostic()> emitError); 82 83 /// Convert a DenseI64ArrayAttr to the provided storage, which will be 84 /// cleared before writing. An error is returned and emitted to the optional 85 /// `emitError` function if the attribute isn't a DenseI64ArrayAttr. 86 LogicalResult 87 convertFromAttribute(SmallVectorImpl<int64_t> &storage, Attribute attr, 88 function_ref<InFlightDiagnostic()> emitError); 89 90 /// Convert a DenseI32ArrayAttr to the provided storage, which will be 91 /// cleared before writing. It is expected that the storage has the same size as 92 /// the array. An error is returned and emitted to the optional `emitError` 93 /// function if the attribute isn't a DenseI32ArrayAttr. 94 LogicalResult 95 convertFromAttribute(SmallVectorImpl<int32_t> &storage, Attribute attr, 96 function_ref<InFlightDiagnostic()> emitError); 97 98 /// Convert the provided ArrayRef<int64_t> to a DenseI64ArrayAttr attribute. 99 Attribute convertToAttribute(MLIRContext *ctx, ArrayRef<int64_t> storage); 100 101 } // namespace mlir 102 103 #endif // MLIR_IR_ODSSUPPORT_H 104