MLIR  21.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 /// This class represents the capability enabled in the target implementation
24 /// such as profile, extension, and level.
25 class TargetEnv {
26 public:
27  TargetEnv() {}
28  explicit TargetEnv(const SmallVectorImpl<Profile> &profiles,
29  const SmallVectorImpl<Extension> &extensions) {
30  enabledProfiles.insert_range(profiles);
31 
32  enabledExtensions.insert_range(extensions);
33  }
34 
35  void addProfile(Profile p) { enabledProfiles.insert(p); }
36  void addExtension(Extension e) { enabledExtensions.insert(e); }
37 
38  // TODO implement the following utilities.
39  // Version getSpecVersion() const;
40  // TosaLevel getLevel() const;
41 
42  // Returns true if the given profile is allowed.
43  bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }
44 
45  bool allowsAnyOf(ArrayRef<Profile> profs) const {
46  const auto *chosen = llvm::find_if(
47  profs, [this](tosa::Profile prof) { return allows(prof); });
48  return chosen != profs.end() ? true : false;
49  }
50 
51  bool allowsAllOf(ArrayRef<Profile> profs) const {
52  bool is_allowed = true;
53  llvm::for_each(profs,
54  [&](tosa::Profile prof) { is_allowed &= allows(prof); });
55  return is_allowed;
56  }
57 
58  // Returns true if the given extension is allowed.
59  bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }
60 
61  bool allowsAnyOf(ArrayRef<Extension> exts) const {
62  const auto *chosen = llvm::find_if(
63  exts, [this](tosa::Extension ext) { return allows(ext); });
64  return chosen != exts.end() ? true : false;
65  }
66 
67  bool allowsAllOf(ArrayRef<Extension> exts) const {
68  bool is_allowed = true;
69  llvm::for_each(exts,
70  [&](tosa::Extension ext) { is_allowed &= allows(ext); });
71  return is_allowed;
72  }
73 
74 private:
75  llvm::SmallSet<Profile, 3> enabledProfiles;
76  llvm::SmallSet<Extension, 8> enabledExtensions;
77 };
78 
79 } // namespace tosa
80 } // namespace mlir
81 
82 #endif // MLIR_DIALECT_TOSA_IR_TARGETENV_H
This class represents the capability enabled in the target implementation such as profile,...
Definition: TargetEnv.h:25
void addExtension(Extension e)
Definition: TargetEnv.h:36
bool allowsAllOf(ArrayRef< Extension > exts) const
Definition: TargetEnv.h:67
TargetEnv(const SmallVectorImpl< Profile > &profiles, const SmallVectorImpl< Extension > &extensions)
Definition: TargetEnv.h:28
void addProfile(Profile p)
Definition: TargetEnv.h:35
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition: TargetEnv.h:51
bool allows(Profile prof) const
Definition: TargetEnv.h:43
bool allows(Extension ext) const
Definition: TargetEnv.h:59
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition: TargetEnv.h:45
bool allowsAnyOf(ArrayRef< Extension > exts) const
Definition: TargetEnv.h:61
Include the generated interface declarations.