MLIR 22.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
15
16#include "llvm/MC/MCSubtargetInfo.h"
17
18namespace mlir {
19namespace LLVM {
20#define GEN_PASS_DEF_LLVMTARGETTOTARGETFEATURES
21#include "mlir/Target/LLVMIR/Transforms/Passes.h.inc"
22} // namespace LLVM
23} // namespace mlir
24
25using namespace mlir;
26
29 TargetToTargetFeaturesPass> {
32
33 void runOnOperation() override {
34 Operation *op = getOperation();
35
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 =
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};
OpT getOperation()
Return the current operation being transformed.
Definition Pass.h:378
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
AttrClass getAttrOfType(StringAttr name)
Definition Operation.h:550
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
void setAttr(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
Definition Operation.h:582
MLIRContext & getContext()
Return the MLIR context for the current operation being transformed.
Definition Pass.h:177
void signalPassFailure()
Signal that some invariant was broken when running.
Definition Pass.h:218
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.
void runOnOperation() override
The polymorphic API that runs the pass over the currently held operation.