| 1 | //===- TosaAttachTarget.cpp |
| 2 | //------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Attach target information to a TOSA module. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 15 | #include "mlir/Dialect/Tosa/IR/TargetEnv.h" |
| 16 | #include "mlir/Dialect/Tosa/Transforms/Passes.h" |
| 17 | #include "mlir/Pass/Pass.h" |
| 18 | |
| 19 | namespace mlir { |
| 20 | namespace tosa { |
| 21 | |
| 22 | #define GEN_PASS_DEF_TOSAATTACHTARGET |
| 23 | #include "mlir/Dialect/Tosa/Transforms/Passes.h.inc" |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class TosaAttachTarget |
| 28 | : public tosa::impl::TosaAttachTargetBase<TosaAttachTarget> { |
| 29 | using Base::Base; |
| 30 | |
| 31 | public: |
| 32 | void runOnOperation() override { |
| 33 | llvm::SmallVector<Profile, 2> selectedProfiles; |
| 34 | if (!profiles.empty()) { |
| 35 | for (const std::string &prof : profiles) { |
| 36 | std::optional<Profile> profSymbol = symbolizeProfile(prof); |
| 37 | if (!profSymbol) { |
| 38 | llvm::SmallVector<Profile> allProfiles = ProfileAttr::getAllValues(); |
| 39 | llvm::errs() << buildUnkownParameterErrorMessage(enumValues&: allProfiles, |
| 40 | enumName: "profile" , unknownArgument: prof); |
| 41 | return signalPassFailure(); |
| 42 | } |
| 43 | selectedProfiles.push_back(Elt: profSymbol.value()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | llvm::SmallVector<Extension, 10> selectedExtensions; |
| 48 | if (!extensions.empty()) { |
| 49 | for (const std::string &ext : extensions) { |
| 50 | std::optional<Extension> extSymbol = symbolizeExtension(ext); |
| 51 | if (!extSymbol) { |
| 52 | llvm::SmallVector<Extension> allExtensions = |
| 53 | ExtensionAttr::getAllValues(); |
| 54 | llvm::errs() << buildUnkownParameterErrorMessage(enumValues&: allExtensions, |
| 55 | enumName: "extension" , unknownArgument: ext); |
| 56 | return signalPassFailure(); |
| 57 | } |
| 58 | selectedExtensions.push_back(Elt: extSymbol.value()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | ModuleOp mod = getOperation(); |
| 63 | MLIRContext *ctx = &getContext(); |
| 64 | const auto targetEnvAttr = TargetEnvAttr::get( |
| 65 | context: ctx, specification_version: specificationVersion, level, profiles: selectedProfiles, extensions: selectedExtensions); |
| 66 | |
| 67 | if (failed(Result: TargetEnv::verifyTargetInformation(targetAttr: targetEnvAttr, targetAttrLoc: mod.getLoc()))) |
| 68 | return signalPassFailure(); |
| 69 | |
| 70 | mod->setDiscardableAttr(name: TargetEnvAttr::name, value: targetEnvAttr); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | template <typename T> |
| 75 | std::string buildUnkownParameterErrorMessage(llvm::SmallVector<T> &enumValues, |
| 76 | std::string enumName, |
| 77 | std::string unknownArgument) { |
| 78 | std::string message; |
| 79 | llvm::raw_string_ostream os(message); |
| 80 | os << "Unknown TOSA " << enumName << " name passed in '" << unknownArgument |
| 81 | << "', supported " << enumName << "s are: " ; |
| 82 | llvm::interleaveComma(enumValues, os); |
| 83 | os << "\n" ; |
| 84 | return message; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | } // namespace tosa |
| 91 | } // namespace mlir |
| 92 | |