15e118f93SMehdi Amini //===- ODSSupport.cpp -----------------------------------------------------===// 25e118f93SMehdi Amini // 35e118f93SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45e118f93SMehdi Amini // See https://llvm.org/LICENSE.txt for license information. 55e118f93SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65e118f93SMehdi Amini // 75e118f93SMehdi Amini //===----------------------------------------------------------------------===// 85e118f93SMehdi Amini // 95e118f93SMehdi Amini // This file contains out-of-line implementations of the support types that 105e118f93SMehdi Amini // Operation and related classes build on top of. 115e118f93SMehdi Amini // 125e118f93SMehdi Amini //===----------------------------------------------------------------------===// 135e118f93SMehdi Amini 145e118f93SMehdi Amini #include "mlir/IR/ODSSupport.h" 155e118f93SMehdi Amini #include "mlir/IR/BuiltinAttributes.h" 165e118f93SMehdi Amini #include "mlir/IR/BuiltinTypes.h" 175e118f93SMehdi Amini #include "mlir/IR/Diagnostics.h" 185e118f93SMehdi Amini 195e118f93SMehdi Amini using namespace mlir; 205e118f93SMehdi Amini 218c2bff1aSMehdi Amini LogicalResult 228c2bff1aSMehdi Amini mlir::convertFromAttribute(int64_t &storage, Attribute attr, 23c50617daSMehdi Amini function_ref<InFlightDiagnostic()> emitError) { 245e118f93SMehdi Amini auto valueAttr = dyn_cast<IntegerAttr>(attr); 255e118f93SMehdi Amini if (!valueAttr) { 26c50617daSMehdi Amini emitError() << "expected IntegerAttr for key `value`"; 275e118f93SMehdi Amini return failure(); 285e118f93SMehdi Amini } 295e118f93SMehdi Amini storage = valueAttr.getValue().getSExtValue(); 305e118f93SMehdi Amini return success(); 315e118f93SMehdi Amini } 325e118f93SMehdi Amini Attribute mlir::convertToAttribute(MLIRContext *ctx, int64_t storage) { 335e118f93SMehdi Amini return IntegerAttr::get(IntegerType::get(ctx, 64), storage); 345e118f93SMehdi Amini } 359ea6b30aSMehdi Amini 36*8955e285SKrzysztof Drewniak LogicalResult 37*8955e285SKrzysztof Drewniak mlir::convertFromAttribute(int32_t &storage, Attribute attr, 38*8955e285SKrzysztof Drewniak function_ref<InFlightDiagnostic()> emitError) { 39*8955e285SKrzysztof Drewniak auto valueAttr = dyn_cast<IntegerAttr>(attr); 40*8955e285SKrzysztof Drewniak if (!valueAttr) { 41*8955e285SKrzysztof Drewniak emitError() << "expected IntegerAttr for key `value`"; 42*8955e285SKrzysztof Drewniak return failure(); 43*8955e285SKrzysztof Drewniak } 44*8955e285SKrzysztof Drewniak storage = valueAttr.getValue().getSExtValue(); 45*8955e285SKrzysztof Drewniak return success(); 46*8955e285SKrzysztof Drewniak } 47*8955e285SKrzysztof Drewniak Attribute mlir::convertToAttribute(MLIRContext *ctx, int32_t storage) { 48*8955e285SKrzysztof Drewniak return IntegerAttr::get(IntegerType::get(ctx, 32), storage); 49*8955e285SKrzysztof Drewniak } 50*8955e285SKrzysztof Drewniak 51*8955e285SKrzysztof Drewniak LogicalResult 52*8955e285SKrzysztof Drewniak mlir::convertFromAttribute(std::string &storage, Attribute attr, 53*8955e285SKrzysztof Drewniak function_ref<InFlightDiagnostic()> emitError) { 54*8955e285SKrzysztof Drewniak auto valueAttr = dyn_cast<StringAttr>(attr); 55*8955e285SKrzysztof Drewniak if (!valueAttr) 56*8955e285SKrzysztof Drewniak return emitError() 57*8955e285SKrzysztof Drewniak << "expected string property to come from string attribute"; 58*8955e285SKrzysztof Drewniak storage = valueAttr.getValue().str(); 59*8955e285SKrzysztof Drewniak return success(); 60*8955e285SKrzysztof Drewniak } 61*8955e285SKrzysztof Drewniak Attribute mlir::convertToAttribute(MLIRContext *ctx, 62*8955e285SKrzysztof Drewniak const std::string &storage) { 63*8955e285SKrzysztof Drewniak return StringAttr::get(ctx, storage); 64*8955e285SKrzysztof Drewniak } 65*8955e285SKrzysztof Drewniak 66*8955e285SKrzysztof Drewniak LogicalResult 67*8955e285SKrzysztof Drewniak mlir::convertFromAttribute(bool &storage, Attribute attr, 68*8955e285SKrzysztof Drewniak function_ref<InFlightDiagnostic()> emitError) { 69*8955e285SKrzysztof Drewniak auto valueAttr = dyn_cast<BoolAttr>(attr); 70*8955e285SKrzysztof Drewniak if (!valueAttr) 71*8955e285SKrzysztof Drewniak return emitError() 72*8955e285SKrzysztof Drewniak << "expected string property to come from string attribute"; 73*8955e285SKrzysztof Drewniak storage = valueAttr.getValue(); 74*8955e285SKrzysztof Drewniak return success(); 75*8955e285SKrzysztof Drewniak } 76*8955e285SKrzysztof Drewniak Attribute mlir::convertToAttribute(MLIRContext *ctx, bool storage) { 77*8955e285SKrzysztof Drewniak return BoolAttr::get(ctx, storage); 78*8955e285SKrzysztof Drewniak } 79*8955e285SKrzysztof Drewniak 809ea6b30aSMehdi Amini template <typename DenseArrayTy, typename T> 818c2bff1aSMehdi Amini LogicalResult 828c2bff1aSMehdi Amini convertDenseArrayFromAttr(MutableArrayRef<T> storage, Attribute attr, 83c50617daSMehdi Amini function_ref<InFlightDiagnostic()> emitError, 849ea6b30aSMehdi Amini StringRef denseArrayTyStr) { 859ea6b30aSMehdi Amini auto valueAttr = dyn_cast<DenseArrayTy>(attr); 865e118f93SMehdi Amini if (!valueAttr) { 87c50617daSMehdi Amini emitError() << "expected " << denseArrayTyStr << " for key `value`"; 885e118f93SMehdi Amini return failure(); 895e118f93SMehdi Amini } 905e118f93SMehdi Amini if (valueAttr.size() != static_cast<int64_t>(storage.size())) { 91c50617daSMehdi Amini emitError() << "size mismatch in attribute conversion: " << valueAttr.size() 925e118f93SMehdi Amini << " vs " << storage.size(); 935e118f93SMehdi Amini return failure(); 945e118f93SMehdi Amini } 955e118f93SMehdi Amini llvm::copy(valueAttr.asArrayRef(), storage.begin()); 965e118f93SMehdi Amini return success(); 975e118f93SMehdi Amini } 988c2bff1aSMehdi Amini LogicalResult 998c2bff1aSMehdi Amini mlir::convertFromAttribute(MutableArrayRef<int64_t> storage, Attribute attr, 100c50617daSMehdi Amini function_ref<InFlightDiagnostic()> emitError) { 101c50617daSMehdi Amini return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError, 1029ea6b30aSMehdi Amini "DenseI64ArrayAttr"); 1039ea6b30aSMehdi Amini } 1048c2bff1aSMehdi Amini LogicalResult 1058c2bff1aSMehdi Amini mlir::convertFromAttribute(MutableArrayRef<int32_t> storage, Attribute attr, 106c50617daSMehdi Amini function_ref<InFlightDiagnostic()> emitError) { 107c50617daSMehdi Amini return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError, 1089ea6b30aSMehdi Amini "DenseI32ArrayAttr"); 1099ea6b30aSMehdi Amini } 1109ea6b30aSMehdi Amini 111*8955e285SKrzysztof Drewniak template <typename DenseArrayTy, typename T> 112*8955e285SKrzysztof Drewniak LogicalResult 113*8955e285SKrzysztof Drewniak convertDenseArrayFromAttr(SmallVectorImpl<T> &storage, Attribute attr, 114*8955e285SKrzysztof Drewniak function_ref<InFlightDiagnostic()> emitError, 115*8955e285SKrzysztof Drewniak StringRef denseArrayTyStr) { 116*8955e285SKrzysztof Drewniak auto valueAttr = dyn_cast<DenseArrayTy>(attr); 117*8955e285SKrzysztof Drewniak if (!valueAttr) { 118*8955e285SKrzysztof Drewniak emitError() << "expected " << denseArrayTyStr << " for key `value`"; 119*8955e285SKrzysztof Drewniak return failure(); 120*8955e285SKrzysztof Drewniak } 121*8955e285SKrzysztof Drewniak storage.resize_for_overwrite(valueAttr.size()); 122*8955e285SKrzysztof Drewniak llvm::copy(valueAttr.asArrayRef(), storage.begin()); 123*8955e285SKrzysztof Drewniak return success(); 124*8955e285SKrzysztof Drewniak } 125*8955e285SKrzysztof Drewniak LogicalResult 126*8955e285SKrzysztof Drewniak mlir::convertFromAttribute(SmallVectorImpl<int64_t> &storage, Attribute attr, 127*8955e285SKrzysztof Drewniak function_ref<InFlightDiagnostic()> emitError) { 128*8955e285SKrzysztof Drewniak return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError, 129*8955e285SKrzysztof Drewniak "DenseI64ArrayAttr"); 130*8955e285SKrzysztof Drewniak } 131*8955e285SKrzysztof Drewniak LogicalResult 132*8955e285SKrzysztof Drewniak mlir::convertFromAttribute(SmallVectorImpl<int32_t> &storage, Attribute attr, 133*8955e285SKrzysztof Drewniak function_ref<InFlightDiagnostic()> emitError) { 134*8955e285SKrzysztof Drewniak return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError, 135*8955e285SKrzysztof Drewniak "DenseI32ArrayAttr"); 136*8955e285SKrzysztof Drewniak } 137*8955e285SKrzysztof Drewniak 1385e118f93SMehdi Amini Attribute mlir::convertToAttribute(MLIRContext *ctx, 1395e118f93SMehdi Amini ArrayRef<int64_t> storage) { 1405e118f93SMehdi Amini return DenseI64ArrayAttr::get(ctx, storage); 1415e118f93SMehdi Amini } 142