MLIR 23.0.0git
TargetEnv.h
Go to the documentation of this file.
1//===- TargetEnv.h - Tosa target environment utilities ----------*- C++ -*-===//
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// This file declares utilities for Tosa target environment (implementation).
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_DIALECT_TOSA_IR_TARGETENV_H
14#define MLIR_DIALECT_TOSA_IR_TARGETENV_H
15
17#include "mlir/Support/LLVM.h"
18#include "llvm/ADT/SmallSet.h"
19
20namespace mlir {
21namespace tosa {
22
23struct TosaLevel {
24 int32_t MAX_RANK = 0;
25 int32_t MAX_KERNEL = 0;
26 int32_t MAX_STRIDE = 0;
27 int32_t MAX_SCALE = 0;
28 int32_t MAX_LOG2_SIZE = 0;
29 int32_t MAX_NESTING = 0;
31 int32_t MAX_SHAPE_LEN = 0;
32
33 bool operator==(const TosaLevel &rhs) {
34 return MAX_RANK == rhs.MAX_RANK && MAX_KERNEL == rhs.MAX_KERNEL &&
35 MAX_STRIDE == rhs.MAX_STRIDE && MAX_SCALE == rhs.MAX_SCALE &&
36 MAX_LOG2_SIZE == rhs.MAX_LOG2_SIZE &&
37 MAX_NESTING == rhs.MAX_NESTING &&
38 MAX_TENSOR_LIST_SIZE == rhs.MAX_TENSOR_LIST_SIZE &&
39 MAX_SHAPE_LEN == rhs.MAX_SHAPE_LEN;
40 }
41};
42
43static constexpr TosaLevel TOSA_LEVEL_EIGHTK = {6, 8192, 8192, 256,
44 31, 6, 64, 16};
45static constexpr TosaLevel TOSA_LEVEL_NONE = {32, 2147483647, 2147483647, 2048,
46 63, 256, 256, 64};
47
48TargetEnvAttr lookupTargetEnv(Operation *op);
49TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
50
51/// Queries the target environment recursively from enclosing symbol table ops
52/// containing the given `op` or returns the default target environment as
53/// returned by getDefaultTargetEnv() if not provided.
54TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
55
56/// A thin wrapper around the SpecificationVersion enum to represent
57/// and provide utilities around the TOSA specification version.
59public:
61
62 TosaSpecificationVersion(uint32_t major, uint32_t minor, bool draft = false)
63 : majorVersion(major), minorVersion(minor), draft(draft) {}
64 TosaSpecificationVersion(SpecificationVersion version)
65 : TosaSpecificationVersion(fromVersionEnum(version)) {}
66
68 if (this->majorVersion != baseVersion.majorVersion)
69 return false;
70 if (this->minorVersion < baseVersion.minorVersion)
71 return false;
72 // An unreleased version is not expected to be backwards compatible with
73 // a corresponding released version. However, an unreleased version is
74 // expected to be backwards compatible with all released versions prior to
75 // it.
76 //
77 // For example:
78 // - 1.1.draft is not expected to be backwards compatible with 1.1
79 // - 1.1.draft is expected to be backwards compatible with 1.0
80 // - 1.1.draft is not expected to be backwards compatible with 1.0.draft
81 if (this->draft && !baseVersion.draft &&
82 this->minorVersion == baseVersion.minorVersion)
83 return false;
84 return true;
85 }
86
87 uint32_t getMajor() const { return majorVersion; }
88 uint32_t getMinor() const { return minorVersion; }
89 bool isDraft() const { return draft; }
90
91private:
92 uint32_t majorVersion = 0;
93 uint32_t minorVersion = 0;
94 bool draft = false;
95
97 fromVersionEnum(SpecificationVersion version) {
98 switch (version) {
99 case SpecificationVersion::V_1_0:
100 return TosaSpecificationVersion(1, 0);
101 case SpecificationVersion::V_1_1_DRAFT:
102 return TosaSpecificationVersion(1, 1, true);
103 }
104 llvm_unreachable("Unknown TOSA version");
105 }
106};
107
108TosaSpecificationVersion getMinVersion(const Profile &profile);
109TosaSpecificationVersion getMinVersion(const Extension &extension);
110TosaSpecificationVersion getMinVersion(const Level &level);
111
112llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version);
113
114/// This class represents the capability enabled in the target implementation
115/// such as profile, extension, and level. It's a wrapper class around
116/// tosa::TargetEnvAttr.
118public:
120
121 static FailureOr<TargetEnv>
122 createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc);
123
124 static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr,
125 Location targetAttrLoc);
126
127 void addProfile(Profile p) { enabledProfiles.insert(p); }
128 void addExtension(Extension e) { enabledExtensions.insert(e); }
129
131 return specificationVersion;
132 }
133
135 if (level == Level::eightK)
136 return TOSA_LEVEL_EIGHTK;
137 else if (level == Level::none)
138 return TOSA_LEVEL_NONE;
139 else
140 llvm_unreachable("Unknown TOSA level");
141 };
142
143 // Returns true if the given profile is allowed.
144 bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }
145
146 bool allowsAnyOf(ArrayRef<Profile> profs) const {
147 return llvm::any_of(profs, [&](Profile prof) { return allows(prof); });
148 }
149
150 bool allowsAllOf(ArrayRef<Profile> profs) const {
151 return llvm::all_of(profs, [&](Profile prof) { return allows(prof); });
152 }
153
154 // Returns true if the given extension is allowed.
155 bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }
156
158 return llvm::any_of(exts, [&](Extension ext) { return allows(ext); });
159 }
160
162 return llvm::all_of(exts, [&](Extension ext) { return allows(ext); });
163 }
164
165private:
166 // Require target information is verified before constructing, via the use of
167 // `createTargetEnvFromAttr`.
168 explicit TargetEnv(SpecificationVersion specificationVersion, Level level,
169 const ArrayRef<Profile> &profiles,
170 const ArrayRef<Extension> &extensions)
171 : specificationVersion(specificationVersion), level(level) {
172 enabledProfiles.insert_range(profiles);
173 enabledExtensions.insert_range(extensions);
174 }
175
176 TosaSpecificationVersion specificationVersion;
177 Level level;
178 llvm::SmallSet<Profile, 3> enabledProfiles;
179 llvm::SmallSet<Extension, 13> enabledExtensions;
180};
181
182} // namespace tosa
183} // namespace mlir
184
185#endif // MLIR_DIALECT_TOSA_IR_TARGETENV_H
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
TosaLevel getLevel() const
Definition TargetEnv.h:134
void addExtension(Extension e)
Definition TargetEnv.h:128
static FailureOr< TargetEnv > createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc)
Definition TargetEnv.cpp:92
bool allowsAllOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:161
void addProfile(Profile p)
Definition TargetEnv.h:127
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:150
bool allows(Profile prof) const
Definition TargetEnv.h:144
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
bool allows(Extension ext) const
Definition TargetEnv.h:155
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:146
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:130
bool allowsAnyOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:157
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
TosaSpecificationVersion(uint32_t major, uint32_t minor, bool draft=false)
Definition TargetEnv.h:62
TosaSpecificationVersion(SpecificationVersion version)
Definition TargetEnv.h:64
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:15
static constexpr TosaLevel TOSA_LEVEL_EIGHTK
Definition TargetEnv.h:43
static constexpr TosaLevel TOSA_LEVEL_NONE
Definition TargetEnv.h:45
TargetEnvAttr getDefaultTargetEnv(MLIRContext *context)
TosaSpecificationVersion getMinVersion(const Profile &profile)
Definition TargetEnv.cpp:20
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.
int32_t MAX_TENSOR_LIST_SIZE
Definition TargetEnv.h:30
bool operator==(const TosaLevel &rhs)
Definition TargetEnv.h:33