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 
12 #include "mlir/Dialect/DLTI/DLTI.h"
15 
16 #include "llvm/MC/MCSubtargetInfo.h"
17 
18 namespace mlir {
19 namespace LLVM {
20 #define GEN_PASS_DEF_LLVMTARGETTOTARGETFEATURES
21 #include "mlir/Target/LLVMIR/Transforms/Passes.h.inc"
22 } // namespace LLVM
23 } // namespace mlir
24 
25 using namespace mlir;
26 
29  TargetToTargetFeaturesPass> {
31  TargetToTargetFeaturesPass>::LLVMTargetToTargetFeaturesBase;
32 
33  void runOnOperation() override {
34  Operation *op = getOperation();
35 
36  if (initializeLLVMTargets)
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 };
static MLIRContext * getContext(OpFoldResult val)
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...
Definition: Operation.cpp:267
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
FailureOr< std::unique_ptr< llvm::TargetMachine > > getTargetMachine(mlir::LLVM::TargetAttrInterface attr)
Helper to obtain the TargetMachine specified by the properties of the TargetAttrInterface-implementin...
Definition: TargetUtils.cpp:37
void initializeBackendsOnce()
Idempotent helper to register/initialize all backends that LLVM has been configured to support.
Definition: TargetUtils.cpp:25
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...