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