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)
63 : majorVersion(major), minorVersion(minor) {}
64 TosaSpecificationVersion(SpecificationVersion version)
65 : TosaSpecificationVersion(fromVersionEnum(version)) {}
66
68 return this->majorVersion == baseVersion.majorVersion &&
69 this->minorVersion >= baseVersion.minorVersion;
70 }
71
72 uint32_t getMajor() const { return majorVersion; }
73 uint32_t getMinor() const { return minorVersion; }
74
75private:
76 uint32_t majorVersion = 0;
77 uint32_t minorVersion = 0;
78
80 fromVersionEnum(SpecificationVersion version) {
81 switch (version) {
82 case SpecificationVersion::V_1_0:
83 return TosaSpecificationVersion(1, 0);
84 case SpecificationVersion::V_1_1_DRAFT:
85 return TosaSpecificationVersion(1, 1);
86 }
87 llvm_unreachable("Unknown TOSA version");
88 }
89};
90
91TosaSpecificationVersion getMinVersion(const Profile &profile);
92TosaSpecificationVersion getMinVersion(const Extension &extension);
93TosaSpecificationVersion getMinVersion(const Level &level);
94
95llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version);
96
97/// This class represents the capability enabled in the target implementation
98/// such as profile, extension, and level. It's a wrapper class around
99/// tosa::TargetEnvAttr.
101public:
103
104 static FailureOr<TargetEnv>
105 createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc);
106
107 static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr,
108 Location targetAttrLoc);
109
110 void addProfile(Profile p) { enabledProfiles.insert(p); }
111 void addExtension(Extension e) { enabledExtensions.insert(e); }
112
114 return specificationVersion;
115 }
116
118 if (level == Level::eightK)
119 return TOSA_LEVEL_EIGHTK;
120 else if (level == Level::none)
121 return TOSA_LEVEL_NONE;
122 else
123 llvm_unreachable("Unknown TOSA level");
124 };
125
126 // Returns true if the given profile is allowed.
127 bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }
128
129 bool allowsAnyOf(ArrayRef<Profile> profs) const {
130 return llvm::any_of(profs, [&](Profile prof) { return allows(prof); });
131 }
132
133 bool allowsAllOf(ArrayRef<Profile> profs) const {
134 return llvm::all_of(profs, [&](Profile prof) { return allows(prof); });
135 }
136
137 // Returns true if the given extension is allowed.
138 bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }
139
141 return llvm::any_of(exts, [&](Extension ext) { return allows(ext); });
142 }
143
145 return llvm::all_of(exts, [&](Extension ext) { return allows(ext); });
146 }
147
148private:
149 // Require target information is verified before constructing, via the use of
150 // `createTargetEnvFromAttr`.
151 explicit TargetEnv(SpecificationVersion specificationVersion, Level level,
152 const ArrayRef<Profile> &profiles,
153 const ArrayRef<Extension> &extensions)
154 : specificationVersion(specificationVersion), level(level) {
155 enabledProfiles.insert_range(profiles);
156 enabledExtensions.insert_range(extensions);
157 }
158
159 TosaSpecificationVersion specificationVersion;
160 Level level;
161 llvm::SmallSet<Profile, 3> enabledProfiles;
162 llvm::SmallSet<Extension, 13> enabledExtensions;
163};
164
165} // namespace tosa
166} // namespace mlir
167
168#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:117
void addExtension(Extension e)
Definition TargetEnv.h:111
static FailureOr< TargetEnv > createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc)
Definition TargetEnv.cpp:65
bool allowsAllOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:144
void addProfile(Profile p)
Definition TargetEnv.h:110
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:133
bool allows(Profile prof) const
Definition TargetEnv.h:127
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
Definition TargetEnv.cpp:74
bool allows(Extension ext) const
Definition TargetEnv.h:138
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:129
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:113
bool allowsAnyOf(ArrayRef< Extension > exts) const
Definition TargetEnv.h:140
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(SpecificationVersion version)
Definition TargetEnv.h:64
TosaSpecificationVersion(uint32_t major, uint32_t minor)
Definition TargetEnv.h:62
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: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:33