MLIR 23.0.0git
TargetEnv.cpp
Go to the documentation of this file.
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
10#include "llvm/Support/FormatVariadic.h"
11
12namespace mlir {
13namespace tosa {
14
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 case Extension::mxfp_conv:
47 case Extension::shape:
48 return TosaSpecificationVersion(1, 1);
49 case Extension::none:
50 return TosaSpecificationVersion(0, 0);
51 }
52 llvm_unreachable("Unknown TOSA extension");
53}
54
56 switch (level) {
57 case Level::eightK:
58 case Level::none:
59 return TosaSpecificationVersion(1, 0);
60 }
61 llvm_unreachable("Unknown TOSA level");
62}
63
64FailureOr<TargetEnv>
65TargetEnv::createTargetEnvFromAttr(TargetEnvAttr targetAttr,
66 Location targetEnvAttrLoc) {
67 if (failed(verifyTargetInformation(targetAttr, targetEnvAttrLoc)))
68 return failure();
69
70 return TargetEnv(targetAttr.getSpecificationVersion(), targetAttr.getLevel(),
71 targetAttr.getProfiles(), targetAttr.getExtensions());
72}
73
74LogicalResult TargetEnv::verifyTargetInformation(TargetEnvAttr targetAttr,
75 Location targetAttrLoc) {
76 TosaSpecificationVersion targetVersion(targetAttr.getSpecificationVersion());
77
78 const auto isCompatibleWithTargetVersion =
79 [&](const auto &targetEnum, Location targetAttrLoc,
80 StringRef enumName) -> LogicalResult {
81 const TosaSpecificationVersion minRequiredVersion =
82 getMinVersion(targetEnum);
83 if (!targetVersion.isBackwardsCompatibleWith(minRequiredVersion))
84 return emitError(targetAttrLoc, enumName)
85 << " '" << stringifyEnum(targetEnum)
86 << "' is not compatible with the target version "
87 << stringifyVersion(targetVersion)
88 << ", minimum required version is "
89 << stringifyVersion(minRequiredVersion);
90 return success();
91 };
92
93 for (const auto &profile : targetAttr.getProfiles())
94 if (failed(
95 isCompatibleWithTargetVersion(profile, targetAttrLoc, "profile")))
96 return failure();
97 for (const auto &extension : targetAttr.getExtensions())
98 if (failed(isCompatibleWithTargetVersion(extension, targetAttrLoc,
99 "extension")))
100 return failure();
101 if (failed(isCompatibleWithTargetVersion(targetAttr.getLevel(), targetAttrLoc,
102 "level")))
103 return failure();
104
105 return success();
106}
107
108TargetEnvAttr lookupTargetEnv(Operation *op) {
109 while (op) {
111 if (!op)
112 break;
113
114 if (auto attr = op->getAttrOfType<TargetEnvAttr>(TargetEnvAttr::name))
115 return attr;
116
117 op = op->getParentOp();
118 }
119
120 return {};
121}
122
123TargetEnvAttr getDefaultTargetEnv(MLIRContext *context) {
124 return TargetEnvAttr::get(context, SpecificationVersion::V_1_0, Level::eightK,
125 {Profile::pro_int, Profile::pro_fp}, {});
126}
127
129 if (auto attr = lookupTargetEnv(op))
130 return attr;
131
132 return getDefaultTargetEnv(op->getContext());
133}
134
135} // namespace tosa
136} // namespace mlir
return success()
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
AttrClass getAttrOfType(StringAttr name)
Definition Operation.h:550
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:234
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:216
static Operation * getNearestSymbolTable(Operation *from)
Returns the nearest symbol table from a given operation from.
static FailureOr< TargetEnv > createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc)
Definition TargetEnv.cpp:65
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
Definition TargetEnv.cpp:74
A thin wrapper around the SpecificationVersion enum to represent and provide utilities around the TOS...
Definition TargetEnv.h:58
bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const
Definition TargetEnv.h:67
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:15
TargetEnvAttr getDefaultTargetEnv(MLIRContext *context)
TosaSpecificationVersion getMinVersion(const Profile &profile)
Definition TargetEnv.cpp:19
TargetEnvAttr lookupTargetEnv(Operation *op)
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.