88 lines · cpp
1//===- TosaAttachTarget.cpp2//------------------------------------------------===//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-exception7//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 19namespace mlir {20namespace tosa {21 22#define GEN_PASS_DEF_TOSAATTACHTARGET23#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"24 25namespace {26 27class TosaAttachTarget28 : 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 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 '" << unknownArgument77 << "', supported " << enumName << "s are: ";78 llvm::interleaveComma(enumValues, os);79 os << "\n";80 return message;81 }82};83 84} // namespace85 86} // namespace tosa87} // namespace mlir88