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 19 using namespace mlir; 20 21 LogicalResult 22 mlir::convertFromAttribute(int64_t &storage, Attribute attr, 23 function_ref<InFlightDiagnostic()> emitError) { 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 } 32 Attribute mlir::convertToAttribute(MLIRContext *ctx, int64_t storage) { 33 return IntegerAttr::get(IntegerType::get(ctx, 64), storage); 34 } 35 36 LogicalResult 37 mlir::convertFromAttribute(int32_t &storage, Attribute attr, 38 function_ref<InFlightDiagnostic()> emitError) { 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 } 47 Attribute mlir::convertToAttribute(MLIRContext *ctx, int32_t storage) { 48 return IntegerAttr::get(IntegerType::get(ctx, 32), storage); 49 } 50 51 LogicalResult 52 mlir::convertFromAttribute(std::string &storage, Attribute attr, 53 function_ref<InFlightDiagnostic()> emitError) { 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 } 61 Attribute mlir::convertToAttribute(MLIRContext *ctx, 62 const std::string &storage) { 63 return StringAttr::get(ctx, storage); 64 } 65 66 LogicalResult 67 mlir::convertFromAttribute(bool &storage, Attribute attr, 68 function_ref<InFlightDiagnostic()> emitError) { 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 } 76 Attribute mlir::convertToAttribute(MLIRContext *ctx, bool storage) { 77 return BoolAttr::get(ctx, storage); 78 } 79 80 template <typename DenseArrayTy, typename T> 81 LogicalResult 82 convertDenseArrayFromAttr(MutableArrayRef<T> storage, Attribute attr, 83 function_ref<InFlightDiagnostic()> emitError, 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 99 mlir::convertFromAttribute(MutableArrayRef<int64_t> storage, Attribute attr, 100 function_ref<InFlightDiagnostic()> emitError) { 101 return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError, 102 "DenseI64ArrayAttr"); 103 } 104 LogicalResult 105 mlir::convertFromAttribute(MutableArrayRef<int32_t> storage, Attribute attr, 106 function_ref<InFlightDiagnostic()> emitError) { 107 return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError, 108 "DenseI32ArrayAttr"); 109 } 110 111 template <typename DenseArrayTy, typename T> 112 LogicalResult 113 convertDenseArrayFromAttr(SmallVectorImpl<T> &storage, Attribute attr, 114 function_ref<InFlightDiagnostic()> emitError, 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 126 mlir::convertFromAttribute(SmallVectorImpl<int64_t> &storage, Attribute attr, 127 function_ref<InFlightDiagnostic()> emitError) { 128 return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError, 129 "DenseI64ArrayAttr"); 130 } 131 LogicalResult 132 mlir::convertFromAttribute(SmallVectorImpl<int32_t> &storage, Attribute attr, 133 function_ref<InFlightDiagnostic()> emitError) { 134 return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError, 135 "DenseI32ArrayAttr"); 136 } 137 138 Attribute mlir::convertToAttribute(MLIRContext *ctx, 139 ArrayRef<int64_t> storage) { 140 return DenseI64ArrayAttr::get(ctx, storage); 141 } 142