brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.2 KiB · eb47e85 Raw
135 lines · cpp
1//===-------------- TosaTarget.cpp - TOSA Target utilities ----------------===//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#include "mlir/Dialect/Tosa/IR/TargetEnv.h"10#include "llvm/Support/FormatVariadic.h"11 12namespace mlir {13namespace tosa {14 15llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version) {16  return llvm::formatv("{0}.{1}", version.getMajor(), version.getMinor());17}18 19TosaSpecificationVersion getMinVersion(const Profile &profile) {20  switch (profile) {21  case Profile::pro_int:22  case Profile::pro_fp:23    return TosaSpecificationVersion(1, 0);24  case Profile::none:25    return TosaSpecificationVersion(0, 0);26  }27  llvm_unreachable("Unknown TOSA profile");28}29 30TosaSpecificationVersion getMinVersion(const Extension &extension) {31  switch (extension) {32  case Extension::int16:33  case Extension::int4:34  case Extension::bf16:35  case Extension::fp8e4m3:36  case Extension::fp8e5m2:37  case Extension::fft:38  case Extension::variable:39  case Extension::controlflow:40  case Extension::doubleround:41  case Extension::inexactround:42  case Extension::dynamic:43    return TosaSpecificationVersion(1, 0);44  case Extension::mxfp:45  case Extension::int64:46    return TosaSpecificationVersion(1, 1);47  case Extension::none:48    return TosaSpecificationVersion(0, 0);49  }50  llvm_unreachable("Unknown TOSA extension");51}52 53TosaSpecificationVersion getMinVersion(const Level &level) {54  switch (level) {55  case Level::eightK:56  case Level::none:57    return TosaSpecificationVersion(1, 0);58  }59  llvm_unreachable("Unknown TOSA level");60}61 62FailureOr<TargetEnv>63TargetEnv::createTargetEnvFromAttr(TargetEnvAttr targetAttr,64                                   Location targetEnvAttrLoc) {65  if (failed(verifyTargetInformation(targetAttr, targetEnvAttrLoc)))66    return failure();67 68  return TargetEnv(targetAttr.getSpecificationVersion(), targetAttr.getLevel(),69                   targetAttr.getProfiles(), targetAttr.getExtensions());70}71 72LogicalResult TargetEnv::verifyTargetInformation(TargetEnvAttr targetAttr,73                                                 Location targetAttrLoc) {74  TosaSpecificationVersion targetVersion(targetAttr.getSpecificationVersion());75 76  const auto isCompatibleWithTargetVersion =77      [&](const auto &targetEnum, Location targetAttrLoc,78          StringRef enumName) -> LogicalResult {79    const TosaSpecificationVersion minRequiredVersion =80        getMinVersion(targetEnum);81    if (!targetVersion.isBackwardsCompatibleWith(minRequiredVersion))82      return emitError(targetAttrLoc, enumName)83             << " '" << stringifyEnum(targetEnum)84             << "' is not compatible with the target version "85             << stringifyVersion(targetVersion)86             << ", minimum required version is "87             << stringifyVersion(minRequiredVersion);88    return success();89  };90 91  for (const auto &profile : targetAttr.getProfiles())92    if (failed(93            isCompatibleWithTargetVersion(profile, targetAttrLoc, "profile")))94      return failure();95  for (const auto &extension : targetAttr.getExtensions())96    if (failed(isCompatibleWithTargetVersion(extension, targetAttrLoc,97                                             "extension")))98      return failure();99  if (failed(isCompatibleWithTargetVersion(targetAttr.getLevel(), targetAttrLoc,100                                           "level")))101    return failure();102 103  return success();104}105 106TargetEnvAttr lookupTargetEnv(Operation *op) {107  while (op) {108    op = SymbolTable::getNearestSymbolTable(op);109    if (!op)110      break;111 112    if (auto attr = op->getAttrOfType<TargetEnvAttr>(TargetEnvAttr::name))113      return attr;114 115    op = op->getParentOp();116  }117 118  return {};119}120 121TargetEnvAttr getDefaultTargetEnv(MLIRContext *context) {122  return TargetEnvAttr::get(context, SpecificationVersion::V_1_0, Level::eightK,123                            {Profile::pro_int, Profile::pro_fp}, {});124}125 126TargetEnvAttr lookupTargetEnvOrDefault(Operation *op) {127  if (auto attr = lookupTargetEnv(op))128    return attr;129 130  return getDefaultTargetEnv(op->getContext());131}132 133} // namespace tosa134} // namespace mlir135