MLIR 22.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
32 bool operator==(const TosaLevel &rhs) {
33 return MAX_RANK == rhs.MAX_RANK && MAX_KERNEL == rhs.MAX_KERNEL &&
34 MAX_STRIDE == rhs.MAX_STRIDE && MAX_SCALE == rhs.MAX_SCALE &&
35 MAX_LOG2_SIZE == rhs.MAX_LOG2_SIZE &&
36 MAX_NESTING == rhs.MAX_NESTING &&
37 MAX_TENSOR_LIST_SIZE == rhs.MAX_TENSOR_LIST_SIZE;
38 }
39};
40
41static constexpr TosaLevel TOSA_LEVEL_EIGHTK = {6, 8192, 8192, 256, 31, 6, 64};
42static constexpr TosaLevel TOSA_LEVEL_NONE = {32, 2147483647, 2147483647, 2048,
43 63, 256, 256};
44
45TargetEnvAttr lookupTargetEnv(Operation *op);
46TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
47
48/// Queries the target environment recursively from enclosing symbol table ops
49/// containing the given `op` or returns the default target environment as
50/// returned by getDefaultTargetEnv() if not provided.
51TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
52
53/// A thin wrapper around the SpecificationVersion enum to represent
54/// and provide utilities around the TOSA specification version.
56public:
58
59 TosaSpecificationVersion(uint32_t major, uint32_t minor)
60 : majorVersion(major), minorVersion(minor) {}
61 TosaSpecificationVersion(SpecificationVersion version)
62 : TosaSpecificationVersion(fromVersionEnum(version)) {}
63
65 return this->majorVersion == baseVersion.majorVersion &&
66 this->minorVersion >= baseVersion.minorVersion;
67 }
68
69 uint32_t getMajor() const { return majorVersion; }
70 uint32_t getMinor() const { return minorVersion; }
71
72private:
73 uint32_t majorVersion = 0;
74 uint32_t minorVersion = 0;
75
77 fromVersionEnum(SpecificationVersion version) {
78 switch (version) {
79 case SpecificationVersion::V_1_0:
80 return TosaSpecificationVersion(1, 0);
81 case SpecificationVersion::V_1_1_DRAFT:
82 return TosaSpecificationVersion(1, 1);
83 }
84 llvm_unreachable("Unknown TOSA version");
85 }
86};
87
88TosaSpecificationVersion getMinVersion(const Profile &profile);
89TosaSpecificationVersion getMinVersion(const Extension &extension);
90TosaSpecificationVersion getMinVersion(const Level &level);
91
92llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version);
93
94/// This class represents the capability enabled in the target implementation
95/// such as profile, extension, and level. It's a wrapper class around
96/// tosa::TargetEnvAttr.
97class TargetEnv {
98public:
100
101 static FailureOr<TargetEnv>
102 createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc);
103
104 static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr,
105 Location targetAttrLoc);
106
107 void addProfile(Profile p) { enabledProfiles.insert(p); }
108 void addExtension(Extension e) { enabledExtensions.insert(e); }
109
111 return specificationVersion;
112 }
113
115 if (level == Level::eightK)
116 return TOSA_LEVEL_EIGHTK;
117 else if (level == Level::none)
118 return TOSA_LEVEL_NONE;
119 else
120 llvm_unreachable("Unknown TOSA level");
121 };
122
123 // Returns true if the given profile is allowed.
124 bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }
125
126 bool allowsAnyOf(ArrayRef<Profile> profs) const {
127 return llvm::any_of(profs, [&](Profile prof) { return allows(prof); });
128 }
129
130 bool allowsAllOf(ArrayRef<Profile> profs) const {
131 return llvm::all_of(profs, [&](Profile prof) { return allows(prof); });
132 }
133
134 // Returns true if the given extension is allowed.
135 bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }
136
138 return llvm::any_of(exts, [&](Extension ext) { return allows(ext); });
139 }
140
142 return llvm::all_of(exts, [&](Extension ext) { return allows(ext); });
143 }
144
145private:
146 // Require target information is verified before constructing, via the use of
147 // `createTargetEnvFromAttr`.
148 explicit TargetEnv(SpecificationVersion specificationVersion, Level level,
149 const ArrayRef<Profile> &profiles,
150 const ArrayRef<Extension> &extensions)
151 : specificationVersion(specificationVersion), level(level) {
152 enabledProfiles.insert_range(profiles);
153 enabledExtensions.insert_range(extensions);
154 }
155
156 TosaSpecificationVersion specificationVersion;
157 Level level;
158 llvm::SmallSet<Profile, 3> enabledProfiles;
159 llvm::SmallSet<Extension, 13> enabledExtensions;
160};
161
162} // namespace tosa
163} // namespace mlir
164
165#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:114
void addExtension(Extension e)
Definition TargetEnv.h:108
static FailureOr< TargetEnv > createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc)
Definition TargetEnv.cpp:63
bool allowsAllOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:141
void addProfile(Profile p)
Definition TargetEnv.h:107
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:130
bool allows(Profile prof) const
Definition TargetEnv.h:124
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
Definition TargetEnv.cpp:72
bool allows(Extension ext) const
Definition TargetEnv.h:135
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:126
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:110
bool allowsAnyOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:137
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
TosaSpecificationVersion(SpecificationVersion version)
Definition TargetEnv.h:61
TosaSpecificationVersion(uint32_t major, uint32_t minor)
Definition TargetEnv.h:59
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:15
static constexpr TosaLevel TOSA_LEVEL_EIGHTK
Definition TargetEnv.h:41
static constexpr TosaLevel TOSA_LEVEL_NONE
Definition TargetEnv.h:42
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.
int32_t MAX_TENSOR_LIST_SIZE
Definition TargetEnv.h:30
bool operator==(const TosaLevel &rhs)
Definition TargetEnv.h:32