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
48TosaLevel getTosaLevelFromEnum(const Level level);
49
50TargetEnvAttr lookupTargetEnv(Operation *op);
51TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
52
53/// Queries the target environment recursively from enclosing symbol table ops
54/// containing the given `op` or returns the default target environment as
55/// returned by getDefaultTargetEnv() if not provided.
56TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
57
58/// A thin wrapper around the SpecificationVersion enum to represent
59/// and provide utilities around the TOSA specification version.
61public:
63
64 TosaSpecificationVersion(uint32_t major, uint32_t minor, bool draft = false)
65 : majorVersion(major), minorVersion(minor), draft(draft) {}
66 TosaSpecificationVersion(SpecificationVersion version)
67 : TosaSpecificationVersion(fromVersionEnum(version)) {}
68
70 if (this->majorVersion != baseVersion.majorVersion)
71 return false;
72 if (this->minorVersion < baseVersion.minorVersion)
73 return false;
74 // An unreleased version is not expected to be backwards compatible with
75 // a corresponding released version. However, an unreleased version is
76 // expected to be backwards compatible with all released versions prior to
77 // it.
78 //
79 // For example:
80 // - 1.1.draft is not expected to be backwards compatible with 1.1
81 // - 1.1.draft is expected to be backwards compatible with 1.0
82 // - 1.1.draft is not expected to be backwards compatible with 1.0.draft
83 if (this->draft && !baseVersion.draft &&
84 this->minorVersion == baseVersion.minorVersion)
85 return false;
86 return true;
87 }
88
89 uint32_t getMajor() const { return majorVersion; }
90 uint32_t getMinor() const { return minorVersion; }
91 bool isDraft() const { return draft; }
92
93private:
94 uint32_t majorVersion = 0;
95 uint32_t minorVersion = 0;
96 bool draft = false;
97
99 fromVersionEnum(SpecificationVersion version) {
100 switch (version) {
101 case SpecificationVersion::V_1_0:
102 return TosaSpecificationVersion(1, 0);
103 case SpecificationVersion::V_1_1_DRAFT:
104 return TosaSpecificationVersion(1, 1, true);
105 }
106 llvm_unreachable("Unknown TOSA version");
107 }
108};
109
110TosaSpecificationVersion getMinVersion(const Profile &profile);
111TosaSpecificationVersion getMinVersion(const Extension &extension);
112TosaSpecificationVersion getMinVersion(const Level &level);
113
114llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version);
115
116/// This class represents the capability enabled in the target implementation
117/// such as profile, extension, and level. It's a wrapper class around
118/// tosa::TargetEnvAttr.
120public:
122
123 static FailureOr<TargetEnv>
124 createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc);
125
126 static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr,
127 Location targetAttrLoc);
128
129 void addProfile(Profile p) { enabledProfiles.insert(p); }
130 void addExtension(Extension e) { enabledExtensions.insert(e); }
131
133 return specificationVersion;
134 }
135
136 TosaLevel getLevel() const { return level; };
137
138 // Returns true if the given profile is allowed.
139 bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }
140
141 bool allowsAnyOf(ArrayRef<Profile> profs) const {
142 return llvm::any_of(profs, [&](Profile prof) { return allows(prof); });
143 }
144
145 bool allowsAllOf(ArrayRef<Profile> profs) const {
146 return llvm::all_of(profs, [&](Profile prof) { return allows(prof); });
147 }
148
149 // Returns true if the given extension is allowed.
150 bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }
151
153 return llvm::any_of(exts, [&](Extension ext) { return allows(ext); });
154 }
155
157 return llvm::all_of(exts, [&](Extension ext) { return allows(ext); });
158 }
159
160private:
161 // Require target information is verified before constructing, via the use of
162 // `createTargetEnvFromAttr`.
163 explicit TargetEnv(SpecificationVersion specificationVersion, Level level,
164 const ArrayRef<Profile> &profiles,
165 const ArrayRef<Extension> &extensions)
166 : specificationVersion(specificationVersion),
167 level(getTosaLevelFromEnum(level)) {
168 enabledProfiles.insert_range(profiles);
169 enabledExtensions.insert_range(extensions);
170 }
171
172 TosaSpecificationVersion specificationVersion;
173 TosaLevel level;
174 llvm::SmallSet<Profile, 3> enabledProfiles;
175 llvm::SmallSet<Extension, 13> enabledExtensions;
176};
177
178} // namespace tosa
179} // namespace mlir
180
181#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:87
TosaLevel getLevel() const
Definition TargetEnv.h:136
void addExtension(Extension e)
Definition TargetEnv.h:130
static FailureOr< TargetEnv > createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc)
bool allowsAllOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:156
void addProfile(Profile p)
Definition TargetEnv.h:129
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:145
bool allows(Profile prof) const
Definition TargetEnv.h:139
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
bool allows(Extension ext) const
Definition TargetEnv.h:150
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:141
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:132
bool allowsAnyOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:152
A thin wrapper around the SpecificationVersion enum to represent and provide utilities around the TOS...
Definition TargetEnv.h:60
bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const
Definition TargetEnv.h:69
TosaSpecificationVersion(uint32_t major, uint32_t minor, bool draft=false)
Definition TargetEnv.h:64
TosaSpecificationVersion(SpecificationVersion version)
Definition TargetEnv.h:66
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:25
TosaLevel getTosaLevelFromEnum(const Level level)
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:30
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