MLIR  22.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 
12 namespace mlir {
13 namespace tosa {
14 
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  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 
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 
62 FailureOr<TargetEnv>
63 TargetEnv::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 
72 LogicalResult 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 
106 TargetEnvAttr lookupTargetEnv(Operation *op) {
107  while (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 
121 TargetEnvAttr getDefaultTargetEnv(MLIRContext *context) {
122  return TargetEnvAttr::get(context, SpecificationVersion::V_1_0, Level::eightK,
123  {Profile::pro_int, Profile::pro_fp}, {});
124 }
125 
127  if (auto attr = lookupTargetEnv(op))
128  return attr;
129 
130  return getDefaultTargetEnv(op->getContext());
131 }
132 
133 } // namespace tosa
134 } // namespace mlir
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
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition: Operation.h:234
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:63
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
Definition: TargetEnv.cpp:72
A thin wrapper around the SpecificationVersion enum to represent and provide utilities around the TOS...
Definition: TargetEnv.h:55
bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const
Definition: TargetEnv.h:64
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:561
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition: TargetEnv.cpp:15
TargetEnvAttr getDefaultTargetEnv(MLIRContext *context)
Definition: TargetEnv.cpp:121
TosaSpecificationVersion getMinVersion(const Profile &profile)
Definition: TargetEnv.cpp:19
TargetEnvAttr lookupTargetEnv(Operation *op)
Definition: TargetEnv.cpp:106
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
Definition: TargetEnv.cpp:126
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...