MLIR 24.0.0git
TargetToTargetFeatures.cpp
Go to the documentation of this file.
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-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "llvm/ADT/SmallVectorExtras.h"
12
16
17#include "llvm/MC/MCSubtargetInfo.h"
18
19namespace mlir {
20namespace LLVM {
21#define GEN_PASS_DEF_LLVMTARGETTOTARGETFEATURES
22#include "mlir/Target/LLVMIR/Transforms/Passes.h.inc"
23} // namespace LLVM
24} // namespace mlir
25
26using namespace mlir;
27
30 TargetToTargetFeaturesPass> {
32 TargetToTargetFeaturesPass>::LLVMTargetToTargetFeaturesBase;
33
34 void runOnOperation() override {
35 Operation *op = getOperation();
36
37 if (initializeLLVMTargets)
39
40 auto targetAttr = op->getDiscardableAttrOfType<LLVM::TargetAttr>(
41 LLVM::LLVMDialect::getTargetAttrName());
42 if (!targetAttr) {
43 op->emitError() << "no LLVM::TargetAttr attribute at key \""
44 << LLVM::LLVMDialect::getTargetAttrName() << "\"";
45 return signalPassFailure();
46 }
47
48 FailureOr<std::unique_ptr<llvm::TargetMachine>> targetMachine =
50 if (failed(targetMachine)) {
51 op->emitError() << "failed to obtain llvm::TargetMachine for "
52 << targetAttr;
53 return signalPassFailure();
54 }
55
56 llvm::MCSubtargetInfo const &subTargetInfo =
57 (*targetMachine)->getMCSubtargetInfo();
58
59 const std::vector<const llvm::SubtargetFeatureKV *> enabledFeatures =
60 subTargetInfo.getEnabledProcessorFeatures();
61
62 auto plussedFeatures = llvm::map_to_vector(
63 enabledFeatures, [](const llvm::SubtargetFeatureKV *feature) {
64 return std::string("+") + feature->key();
65 });
66
67 auto plussedFeaturesRefs = llvm::map_to_vector(
68 plussedFeatures, [](auto &it) { return StringRef(it.c_str()); });
69
70 auto fullTargetFeaturesAttr =
71 LLVM::TargetFeaturesAttr::get(&getContext(), plussedFeaturesRefs);
72
73 auto updatedTargetAttr =
74 LLVM::TargetAttr::get(&getContext(), targetAttr.getTriple(),
75 targetAttr.getChip(), fullTargetFeaturesAttr);
76
77 op->setDiscardableAttr(LLVM::LLVMDialect::getTargetAttrName(),
78 updatedTargetAttr);
79 }
80};
b getContext())
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
void setDiscardableAttr(StringAttr name, Attribute value)
Set a discardable attribute by name.
Definition Operation.h:512
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
AttrClass getDiscardableAttrOfType(StringRef name)
Access a discardable attribute by name and cast it to AttrClass.
Definition Operation.h:493
FailureOr< std::unique_ptr< llvm::TargetMachine > > getTargetMachine(mlir::LLVM::TargetAttrInterface attr)
Helper to obtain the TargetMachine specified by the properties of the TargetAttrInterface-implementin...
void initializeBackendsOnce()
Idempotent helper to register/initialize all backends that LLVM has been configured to support.
Include the generated interface declarations.