| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Dialect/Tosa/IR/TargetEnv.h" |
| 10 | #include "llvm/Support/FormatVariadic.h" |
| 11 | |
| 12 | namespace mlir { |
| 13 | namespace tosa { |
| 14 | |
| 15 | TosaLevel (const Level level) { |
| 16 | switch (level) { |
| 17 | case Level::eightK: |
| 18 | return TOSA_LEVEL_EIGHTK; |
| 19 | case Level::none: |
| 20 | return TOSA_LEVEL_NONE; |
| 21 | } |
| 22 | llvm_unreachable("Unknown TOSA level" ); |
| 23 | } |
| 24 | |
| 25 | llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version) { |
| 26 | return llvm::formatv(Fmt: "{0}.{1}{2}" , Vals: version.getMajor(), Vals: version.getMinor(), |
| 27 | Vals: version.isDraft() ? ".draft" : "" ); |
| 28 | } |
| 29 | |
| 30 | TosaSpecificationVersion getMinVersion(const Profile &profile) { |
| 31 | switch (profile) { |
| 32 | case Profile::pro_int: |
| 33 | case Profile::pro_fp: |
| 34 | return TosaSpecificationVersion(1, 0); |
| 35 | case Profile::none: |
| 36 | return TosaSpecificationVersion(0, 0); |
| 37 | } |
| 38 | llvm_unreachable("Unknown TOSA profile" ); |
| 39 | } |
| 40 | |
| 41 | TosaSpecificationVersion getMinVersion(const Extension &extension) { |
| 42 | switch (extension) { |
| 43 | case Extension::int16: |
| 44 | case Extension::int4: |
| 45 | case Extension::bf16: |
| 46 | case Extension::fp8e4m3: |
| 47 | case Extension::fp8e5m2: |
| 48 | case Extension::fft: |
| 49 | case Extension::variable: |
| 50 | case Extension::controlflow: |
| 51 | case Extension::doubleround: |
| 52 | case Extension::inexactround: |
| 53 | case Extension::dynamic: |
| 54 | return TosaSpecificationVersion(1, 0); |
| 55 | case Extension::mxfp: |
| 56 | case Extension::int64: |
| 57 | case Extension::mxfp_conv: |
| 58 | case Extension::shape: |
| 59 | case Extension::mx_common: |
| 60 | case Extension::mx_fp4e2m1: |
| 61 | case Extension::mx_fp6e2m3: |
| 62 | case Extension::mx_fp6e3m2: |
| 63 | case Extension::mx_fp8e4m3: |
| 64 | case Extension::mx_fp8e5m2: |
| 65 | case Extension::mx_int8: |
| 66 | return TosaSpecificationVersion(1, 1, true); |
| 67 | case Extension::none: |
| 68 | return TosaSpecificationVersion(0, 0); |
| 69 | } |
| 70 | llvm_unreachable("Unknown TOSA extension" ); |
| 71 | } |
| 72 | |
| 73 | SmallVector<Profile, 2> getCooperativeProfiles(Extension ext) { |
| 74 | switch (ext) { |
| 75 | case Extension::int16: |
| 76 | case Extension::int4: |
| 77 | case Extension::doubleround: |
| 78 | case Extension::inexactround: |
| 79 | return {Profile::pro_int}; |
| 80 | case Extension::bf16: |
| 81 | case Extension::fp8e4m3: |
| 82 | case Extension::fp8e5m2: |
| 83 | case Extension::fft: |
| 84 | case Extension::mxfp: |
| 85 | case Extension::mxfp_conv: |
| 86 | case Extension::mx_common: |
| 87 | case Extension::mx_fp4e2m1: |
| 88 | case Extension::mx_fp6e2m3: |
| 89 | case Extension::mx_fp6e3m2: |
| 90 | case Extension::mx_fp8e4m3: |
| 91 | case Extension::mx_fp8e5m2: |
| 92 | case Extension::mx_int8: |
| 93 | return {Profile::pro_fp}; |
| 94 | case Extension::variable: |
| 95 | case Extension::controlflow: |
| 96 | case Extension::dynamic: |
| 97 | case Extension::int64: |
| 98 | case Extension::shape: |
| 99 | return {Profile::pro_fp, Profile::pro_int}; |
| 100 | case Extension::none: |
| 101 | return {}; |
| 102 | }; |
| 103 | llvm_unreachable("bad Extension type" ); |
| 104 | } |
| 105 | |
| 106 | TosaSpecificationVersion getMinVersion(const Level &level) { |
| 107 | switch (level) { |
| 108 | case Level::eightK: |
| 109 | case Level::none: |
| 110 | return TosaSpecificationVersion(1, 0); |
| 111 | } |
| 112 | llvm_unreachable("Unknown TOSA level" ); |
| 113 | } |
| 114 | |
| 115 | FailureOr<TargetEnv> |
| 116 | TargetEnv::createTargetEnvFromAttr(TargetEnvAttr targetAttr, |
| 117 | Location targetEnvAttrLoc) { |
| 118 | if (failed(Result: verifyTargetInformation(targetAttr, targetAttrLoc: targetEnvAttrLoc))) |
| 119 | return failure(); |
| 120 | |
| 121 | return TargetEnv(targetAttr.getSpecificationVersion(), targetAttr.getLevel(), |
| 122 | targetAttr.getProfiles(), targetAttr.getExtensions()); |
| 123 | } |
| 124 | |
| 125 | LogicalResult TargetEnv::verifyTargetInformation(TargetEnvAttr targetAttr, |
| 126 | Location targetAttrLoc) { |
| 127 | TosaSpecificationVersion targetVersion(targetAttr.getSpecificationVersion()); |
| 128 | |
| 129 | const auto isCompatibleWithTargetVersion = |
| 130 | [&](const auto &targetEnum, Location targetAttrLoc, |
| 131 | StringRef enumName) -> LogicalResult { |
| 132 | const TosaSpecificationVersion minRequiredVersion = |
| 133 | getMinVersion(targetEnum); |
| 134 | if (!targetVersion.isBackwardsCompatibleWith(baseVersion: minRequiredVersion)) |
| 135 | return emitError(loc: targetAttrLoc, message: enumName) |
| 136 | << " '" << stringifyEnum(targetEnum) |
| 137 | << "' is not compatible with the target version " |
| 138 | << stringifyVersion(version: targetVersion) |
| 139 | << ", minimum required version is " |
| 140 | << stringifyVersion(version: minRequiredVersion); |
| 141 | return success(); |
| 142 | }; |
| 143 | |
| 144 | const auto isExtensionCooperativeWithProfile = |
| 145 | [&](Extension ext) -> LogicalResult { |
| 146 | const auto cooperativeProfiles = getCooperativeProfiles(ext); |
| 147 | |
| 148 | const ArrayRef<Profile> targetProfiles = targetAttr.getProfiles(); |
| 149 | if (!llvm::any_of(Range: cooperativeProfiles, |
| 150 | P: [&targetProfiles](const auto &profile) { |
| 151 | return llvm::is_contained(targetProfiles, profile); |
| 152 | })) |
| 153 | return emitError(loc: targetAttrLoc) |
| 154 | << "use of extension '" << stringifyEnum(enumValue: ext) |
| 155 | << "' requires any of profiles: [" << cooperativeProfiles |
| 156 | << "] to be enabled in the target" ; |
| 157 | |
| 158 | return success(); |
| 159 | }; |
| 160 | |
| 161 | for (const auto &profile : targetAttr.getProfiles()) |
| 162 | if (failed( |
| 163 | Result: isCompatibleWithTargetVersion(profile, targetAttrLoc, "profile" ))) |
| 164 | return failure(); |
| 165 | for (const auto &extension : targetAttr.getExtensions()) { |
| 166 | if (failed(Result: isCompatibleWithTargetVersion(extension, targetAttrLoc, |
| 167 | "extension" ))) |
| 168 | return failure(); |
| 169 | if (failed(Result: isExtensionCooperativeWithProfile(extension))) |
| 170 | return failure(); |
| 171 | } |
| 172 | if (failed(Result: isCompatibleWithTargetVersion(targetAttr.getLevel(), targetAttrLoc, |
| 173 | "level" ))) |
| 174 | return failure(); |
| 175 | |
| 176 | return success(); |
| 177 | } |
| 178 | |
| 179 | TargetEnvAttr lookupTargetEnv(Operation *op) { |
| 180 | while (op) { |
| 181 | op = SymbolTable::getNearestSymbolTable(from: op); |
| 182 | if (!op) |
| 183 | break; |
| 184 | |
| 185 | if (auto attr = |
| 186 | op->getDiscardableAttrOfType<TargetEnvAttr>(name: TargetEnvAttr::name)) |
| 187 | return attr; |
| 188 | |
| 189 | op = op->getParentOp(); |
| 190 | } |
| 191 | |
| 192 | return {}; |
| 193 | } |
| 194 | |
| 195 | TargetEnvAttr getDefaultTargetEnv(MLIRContext *context) { |
| 196 | return TargetEnvAttr::get(context, specification_version: SpecificationVersion::V_1_0, level: Level::eightK, |
| 197 | profiles: {Profile::pro_int, Profile::pro_fp}, extensions: {}); |
| 198 | } |
| 199 | |
| 200 | TargetEnvAttr lookupTargetEnvOrDefault(Operation *op) { |
| 201 | if (auto attr = lookupTargetEnv(op)) |
| 202 | return attr; |
| 203 | |
| 204 | return getDefaultTargetEnv(context: op->getContext()); |
| 205 | } |
| 206 | |
| 207 | } // namespace tosa |
| 208 | } // namespace mlir |
| 209 | |