|
12 | 12 | namespace mlir { |
13 | 13 | namespace tosa { |
14 | 14 |
|
| 15 | +llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version) { |
| 16 | + return llvm::formatv("{0}.{1}", version.getMajor(), version.getMinor()); |
| 17 | +} |
| 18 | + |
| 19 | +TosaSpecificationVersion 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 | + |
| 30 | +TosaSpecificationVersion 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 | + return TosaSpecificationVersion(1, 1); |
| 46 | + case Extension::none: |
| 47 | + return TosaSpecificationVersion(0, 0); |
| 48 | + } |
| 49 | + llvm_unreachable("Unknown TOSA extension"); |
| 50 | +} |
| 51 | + |
| 52 | +TosaSpecificationVersion getMinVersion(const Level &level) { |
| 53 | + switch (level) { |
| 54 | + case Level::eightK: |
| 55 | + case Level::none: |
| 56 | + return TosaSpecificationVersion(1, 0); |
| 57 | + } |
| 58 | + llvm_unreachable("Unknown TOSA level"); |
| 59 | +} |
| 60 | + |
| 61 | +FailureOr<TargetEnv> |
| 62 | +TargetEnv::createTargetEnvFromAttr(TargetEnvAttr targetAttr, |
| 63 | + Location targetEnvAttrLoc) { |
| 64 | + if (failed(verifyTargetInformation(targetAttr, targetEnvAttrLoc))) |
| 65 | + return failure(); |
| 66 | + |
| 67 | + return TargetEnv(targetAttr.getSpecificationVersion(), targetAttr.getLevel(), |
| 68 | + targetAttr.getProfiles(), targetAttr.getExtensions()); |
| 69 | +} |
| 70 | + |
| 71 | +LogicalResult TargetEnv::verifyTargetInformation(TargetEnvAttr targetAttr, |
| 72 | + Location targetAttrLoc) { |
| 73 | + TosaSpecificationVersion targetVersion(targetAttr.getSpecificationVersion()); |
| 74 | + |
| 75 | + const auto isCompatibleWithTargetVersion = |
| 76 | + [&](const auto &targetEnum, Location targetAttrLoc, |
| 77 | + StringRef enumName) -> LogicalResult { |
| 78 | + const TosaSpecificationVersion minRequiredVersion = |
| 79 | + getMinVersion(targetEnum); |
| 80 | + if (!targetVersion.isBackwardsCompatibleWith(minRequiredVersion)) |
| 81 | + return emitError(targetAttrLoc, enumName) |
| 82 | + << " '" << stringifyEnum(targetEnum) |
| 83 | + << "' is not compatible with the target version " |
| 84 | + << stringifyVersion(targetVersion) |
| 85 | + << ", minimum required version is " |
| 86 | + << stringifyVersion(minRequiredVersion); |
| 87 | + return success(); |
| 88 | + }; |
| 89 | + |
| 90 | + for (const auto &profile : targetAttr.getProfiles()) |
| 91 | + if (failed( |
| 92 | + isCompatibleWithTargetVersion(profile, targetAttrLoc, "profile"))) |
| 93 | + return failure(); |
| 94 | + for (const auto &extension : targetAttr.getExtensions()) |
| 95 | + if (failed(isCompatibleWithTargetVersion(extension, targetAttrLoc, |
| 96 | + "extension"))) |
| 97 | + return failure(); |
| 98 | + if (failed(isCompatibleWithTargetVersion(targetAttr.getLevel(), targetAttrLoc, |
| 99 | + "level"))) |
| 100 | + return failure(); |
| 101 | + |
| 102 | + return success(); |
| 103 | +} |
| 104 | + |
15 | 105 | TargetEnvAttr lookupTargetEnv(Operation *op) { |
16 | 106 | while (op) { |
17 | 107 | op = SymbolTable::getNearestSymbolTable(op); |
@@ -39,9 +129,5 @@ TargetEnvAttr lookupTargetEnvOrDefault(Operation *op) { |
39 | 129 | return getDefaultTargetEnv(op->getContext()); |
40 | 130 | } |
41 | 131 |
|
42 | | -llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version) { |
43 | | - return llvm::formatv("{0}.{1}", version.getMajor(), version.getMinor()); |
44 | | -} |
45 | | - |
46 | 132 | } // namespace tosa |
47 | 133 | } // namespace mlir |
0 commit comments