MLIR 22.0.0git
TosaAttachTarget.cpp
Go to the documentation of this file.
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
17#include "mlir/Pass/Pass.h"
18
19namespace mlir {
20namespace tosa {
21
22#define GEN_PASS_DEF_TOSAATTACHTARGET
23#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
24
25namespace {
26
27class TosaAttachTarget
28 : public tosa::impl::TosaAttachTargetBase<TosaAttachTarget> {
29 using Base::Base;
30
31public:
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(allProfiles,
40 "profile", prof);
41 return signalPassFailure();
42 }
43 selectedProfiles.push_back(profSymbol.value());
44 }
45 }
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) {
53 ExtensionAttr::getAllValues();
54 llvm::errs() << buildUnkownParameterErrorMessage(allExtensions,
55 "extension", ext);
57 }
58 selectedExtensions.push_back(extSymbol.value());
59 }
60 }
62 ModuleOp mod = getOperation();
63 MLIRContext *ctx = &getContext();
64 const auto targetEnvAttr = TargetEnvAttr::get(
65 ctx, specificationVersion, level, selectedProfiles, selectedExtensions);
66 mod->setAttr(TargetEnvAttr::name, targetEnvAttr);
67 }
68
69private:
70 template <typename T>
71 std::string buildUnkownParameterErrorMessage(llvm::SmallVector<T> &enumValues,
72 std::string enumName,
73 std::string unknownArgument) {
74 std::string message;
75 llvm::raw_string_ostream os(message);
76 os << "Unknown TOSA " << enumName << " name passed in '" << unknownArgument
77 << "', supported " << enumName << "s are: ";
78 llvm::interleaveComma(enumValues, os);
79 os << "\n";
80 return message;
81 }
82};
83
84} // namespace
85
86} // namespace tosa
87} // namespace mlir
b getContext())
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
void signalPassFailure()
Signal that some invariant was broken when running.
Definition Pass.h:218
::mlir::Pass::Option< mlir::tosa::Level > level
::mlir::Pass::Option< mlir::tosa::SpecificationVersion > specificationVersion
::mlir::Pass::ListOption< std::string > extensions
Include the generated interface declarations.