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 
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(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 =
65  TargetEnvAttr::get(ctx, level, selectedProfiles, selectedExtensions);
66  mod->setAttr(TargetEnvAttr::name, targetEnvAttr);
67  }
68 
69 private:
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
static MLIRContext * getContext(OpFoldResult val)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...