MLIR 23.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 }
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(allExtensions,
55 "extension", ext);
56 return signalPassFailure();
57 }
58 selectedExtensions.push_back(extSymbol.value());
59 }
60 }
61
62 ModuleOp mod = getOperation();
63 MLIRContext *ctx = &getContext();
64 const auto targetEnvAttr = TargetEnvAttr::get(
65 ctx, specificationVersion, level, selectedProfiles, selectedExtensions);
66
67 if (failed(TargetEnv::verifyTargetInformation(targetEnvAttr, mod.getLoc())))
68 return signalPassFailure();
69
70 mod->setAttr(TargetEnvAttr::name, targetEnvAttr);
71 }
72
73private:
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
b getContext())
static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr, Location targetAttrLoc)
Include the generated interface declarations.