brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 4a1ca46 Raw
79 lines · cpp
1//===- TargetToTargetFeatures.cpp - extract features from TargetMachine ---===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Target/LLVMIR/Transforms/Passes.h"10#include "mlir/Target/LLVMIR/Transforms/TargetUtils.h"11 12#include "mlir/Dialect/DLTI/DLTI.h"13#include "mlir/Dialect/LLVMIR/LLVMDialect.h"14#include "mlir/Target/LLVMIR/Import.h"15 16#include "llvm/MC/MCSubtargetInfo.h"17 18namespace mlir {19namespace LLVM {20#define GEN_PASS_DEF_LLVMTARGETTOTARGETFEATURES21#include "mlir/Target/LLVMIR/Transforms/Passes.h.inc"22} // namespace LLVM23} // namespace mlir24 25using namespace mlir;26 27struct TargetToTargetFeaturesPass28    : public LLVM::impl::LLVMTargetToTargetFeaturesBase<29          TargetToTargetFeaturesPass> {30  using LLVM::impl::LLVMTargetToTargetFeaturesBase<31      TargetToTargetFeaturesPass>::LLVMTargetToTargetFeaturesBase;32 33  void runOnOperation() override {34    Operation *op = getOperation();35 36    if (initializeLLVMTargets)37      LLVM::detail::initializeBackendsOnce();38 39    auto targetAttr = op->getAttrOfType<LLVM::TargetAttr>(40        LLVM::LLVMDialect::getTargetAttrName());41    if (!targetAttr) {42      op->emitError() << "no LLVM::TargetAttr attribute at key \""43                      << LLVM::LLVMDialect::getTargetAttrName() << "\"";44      return signalPassFailure();45    }46 47    FailureOr<std::unique_ptr<llvm::TargetMachine>> targetMachine =48        LLVM::detail::getTargetMachine(targetAttr);49    if (failed(targetMachine)) {50      op->emitError() << "failed to obtain llvm::TargetMachine for "51                      << targetAttr;52      return signalPassFailure();53    }54 55    llvm::MCSubtargetInfo const *subTargetInfo =56        (*targetMachine)->getMCSubtargetInfo();57 58    const std::vector<llvm::SubtargetFeatureKV> enabledFeatures =59        subTargetInfo->getEnabledProcessorFeatures();60 61    auto plussedFeatures = llvm::to_vector(62        llvm::map_range(enabledFeatures, [](llvm::SubtargetFeatureKV feature) {63          return std::string("+") + feature.Key;64        }));65 66    auto plussedFeaturesRefs = llvm::to_vector(llvm::map_range(67        plussedFeatures, [](auto &it) { return StringRef(it.c_str()); }));68 69    auto fullTargetFeaturesAttr =70        LLVM::TargetFeaturesAttr::get(&getContext(), plussedFeaturesRefs);71 72    auto updatedTargetAttr =73        LLVM::TargetAttr::get(&getContext(), targetAttr.getTriple(),74                              targetAttr.getChip(), fullTargetFeaturesAttr);75 76    op->setAttr(LLVM::LLVMDialect::getTargetAttrName(), updatedTargetAttr);77  }78};79