brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.7 KiB · f2665d2 Raw
181 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file contains out-of-line implementations of the support types that10// 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 19using namespace mlir;20 21LogicalResult22mlir::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}32Attribute mlir::convertToAttribute(MLIRContext *ctx, int64_t storage) {33  return IntegerAttr::get(IntegerType::get(ctx, 64), storage);34}35 36LogicalResult37mlir::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}47Attribute mlir::convertToAttribute(MLIRContext *ctx, int32_t storage) {48  return IntegerAttr::get(IntegerType::get(ctx, 32), storage);49}50 51LogicalResult52mlir::convertFromAttribute(int8_t &storage, Attribute attr,53                           function_ref<InFlightDiagnostic()> emitError) {54  auto valueAttr = dyn_cast<IntegerAttr>(attr);55  if (!valueAttr) {56    emitError() << "expected IntegerAttr for key `value`";57    return failure();58  }59  storage = valueAttr.getValue().getSExtValue();60  return success();61}62 63Attribute mlir::convertToAttribute(MLIRContext *ctx, int8_t storage) {64  /// Convert the provided int8_t to an IntegerAttr attribute.65  return IntegerAttr::get(IntegerType::get(ctx, 8), storage);66}67 68LogicalResult69mlir::convertFromAttribute(uint8_t &storage, Attribute attr,70                           function_ref<InFlightDiagnostic()> emitError) {71  auto valueAttr = dyn_cast<IntegerAttr>(attr);72  if (!valueAttr) {73    emitError() << "expected IntegerAttr for key `value`";74    return failure();75  }76  storage = valueAttr.getValue().getZExtValue();77  return success();78}79 80Attribute mlir::convertToAttribute(MLIRContext *ctx, uint8_t storage) {81  /// Convert the provided uint8_t to an IntegerAttr attribute.82  return IntegerAttr::get(IntegerType::get(ctx, 8), storage);83}84 85LogicalResult86mlir::convertFromAttribute(std::string &storage, Attribute attr,87                           function_ref<InFlightDiagnostic()> emitError) {88  auto valueAttr = dyn_cast<StringAttr>(attr);89  if (!valueAttr)90    return emitError()91           << "expected string property to come from string attribute";92  storage = valueAttr.getValue().str();93  return success();94}95Attribute mlir::convertToAttribute(MLIRContext *ctx,96                                   const std::string &storage) {97  return StringAttr::get(ctx, storage);98}99 100LogicalResult101mlir::convertFromAttribute(bool &storage, Attribute attr,102                           function_ref<InFlightDiagnostic()> emitError) {103  auto valueAttr = dyn_cast<BoolAttr>(attr);104  if (!valueAttr)105    return emitError()106           << "expected string property to come from string attribute";107  storage = valueAttr.getValue();108  return success();109}110Attribute mlir::convertToAttribute(MLIRContext *ctx, bool storage) {111  return BoolAttr::get(ctx, storage);112}113 114template <typename DenseArrayTy, typename T>115static LogicalResult116convertDenseArrayFromAttr(MutableArrayRef<T> storage, Attribute attr,117                          function_ref<InFlightDiagnostic()> emitError,118                          StringRef denseArrayTyStr) {119  auto valueAttr = dyn_cast<DenseArrayTy>(attr);120  if (!valueAttr) {121    emitError() << "expected " << denseArrayTyStr << " for key `value`";122    return failure();123  }124  if (valueAttr.size() != static_cast<int64_t>(storage.size())) {125    emitError() << "size mismatch in attribute conversion: " << valueAttr.size()126                << " vs " << storage.size();127    return failure();128  }129  llvm::copy(valueAttr.asArrayRef(), storage.begin());130  return success();131}132LogicalResult133mlir::convertFromAttribute(MutableArrayRef<int64_t> storage, Attribute attr,134                           function_ref<InFlightDiagnostic()> emitError) {135  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,136                                                      "DenseI64ArrayAttr");137}138LogicalResult139mlir::convertFromAttribute(MutableArrayRef<int32_t> storage, Attribute attr,140                           function_ref<InFlightDiagnostic()> emitError) {141  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,142                                                      "DenseI32ArrayAttr");143}144 145template <typename DenseArrayTy, typename T>146static LogicalResult147convertDenseArrayFromAttr(SmallVectorImpl<T> &storage, Attribute attr,148                          function_ref<InFlightDiagnostic()> emitError,149                          StringRef denseArrayTyStr) {150  auto valueAttr = dyn_cast<DenseArrayTy>(attr);151  if (!valueAttr) {152    emitError() << "expected " << denseArrayTyStr << " for key `value`";153    return failure();154  }155  storage.resize_for_overwrite(valueAttr.size());156  llvm::copy(valueAttr.asArrayRef(), storage.begin());157  return success();158}159LogicalResult160mlir::convertFromAttribute(SmallVectorImpl<int64_t> &storage, Attribute attr,161                           function_ref<InFlightDiagnostic()> emitError) {162  return convertDenseArrayFromAttr<DenseI64ArrayAttr>(storage, attr, emitError,163                                                      "DenseI64ArrayAttr");164}165LogicalResult166mlir::convertFromAttribute(SmallVectorImpl<int32_t> &storage, Attribute attr,167                           function_ref<InFlightDiagnostic()> emitError) {168  return convertDenseArrayFromAttr<DenseI32ArrayAttr>(storage, attr, emitError,169                                                      "DenseI32ArrayAttr");170}171 172Attribute mlir::convertToAttribute(MLIRContext *ctx,173                                   ArrayRef<int64_t> storage) {174  return DenseI64ArrayAttr::get(ctx, storage);175}176 177Attribute mlir::convertToAttribute(MLIRContext *ctx,178                                   ArrayRef<int32_t> storage) {179  return DenseI32ArrayAttr::get(ctx, storage);180}181