|
| 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 | + |
| 16 | +#include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 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 | + for (Profile prof : profiles) |
| 31 | + enabledProfiles.insert(prof); |
| 32 | + |
| 33 | + for (Extension ext : extensions) |
| 34 | + enabledExtensions.insert(ext); |
| 35 | + } |
| 36 | + |
| 37 | + void addProfile(Profile p) { enabledProfiles.insert(p); } |
| 38 | + void addExtension(Extension e) { enabledExtensions.insert(e); } |
| 39 | + |
| 40 | + // TODO implement the following utilities. |
| 41 | + // Version getSpecVersion() const; |
| 42 | + // TosaLevel getLevel() const; |
| 43 | + |
| 44 | + // Returns true if the given profile is allowed. |
| 45 | + bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; } |
| 46 | + |
| 47 | + bool allowsAnyOf(ArrayRef<Profile> profs) const { |
| 48 | + const auto *chosen = llvm::find_if( |
| 49 | + profs, [this](tosa::Profile prof) { return allows(prof); }); |
| 50 | + return chosen != profs.end() ? true : false; |
| 51 | + } |
| 52 | + |
| 53 | + bool allowsAllOf(ArrayRef<Profile> profs) const { |
| 54 | + bool is_allowed = true; |
| 55 | + llvm::for_each(profs, |
| 56 | + [&](tosa::Profile prof) { is_allowed &= allows(prof); }); |
| 57 | + return is_allowed; |
| 58 | + } |
| 59 | + |
| 60 | + // Returns true if the given extension is allowed. |
| 61 | + bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; } |
| 62 | + |
| 63 | + bool allowsAnyOf(ArrayRef<Extension> exts) const { |
| 64 | + const auto *chosen = llvm::find_if( |
| 65 | + exts, [this](tosa::Extension ext) { return allows(ext); }); |
| 66 | + return chosen != exts.end() ? true : false; |
| 67 | + } |
| 68 | + |
| 69 | + bool allowsAllOf(ArrayRef<Extension> exts) const { |
| 70 | + bool is_allowed = true; |
| 71 | + llvm::for_each(exts, |
| 72 | + [&](tosa::Extension ext) { is_allowed &= allows(ext); }); |
| 73 | + return is_allowed; |
| 74 | + } |
| 75 | + |
| 76 | +private: |
| 77 | + llvm::SmallSet<Profile, 3> enabledProfiles; |
| 78 | + llvm::SmallSet<Extension, 8> enabledExtensions; |
| 79 | +}; |
| 80 | + |
| 81 | +} // namespace tosa |
| 82 | +} // namespace mlir |
| 83 | + |
| 84 | +#endif // MLIR_DIALECT_TOSA_IR_TARGETENV_H |
0 commit comments