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