MLIR 23.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> {
33
34 void runOnOperation() override {
35 Operation *op = getOperation();
36
39
40 auto targetAttr = op->getAttrOfType<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<llvm::SubtargetFeatureKV> enabledFeatures =
60 subTargetInfo->getEnabledProcessorFeatures();
61
62 auto plussedFeatures = llvm::map_to_vector(
63 enabledFeatures, [](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->setAttr(LLVM::LLVMDialect::getTargetAttrName(), updatedTargetAttr);
78 }
79};
OpT getOperation()
Return the current operation being transformed.
Definition Pass.h:389
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:178
void signalPassFailure()
Signal that some invariant was broken when running.
Definition Pass.h:226
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.