Modifies `#llvm.target<..., features = $FEATURES>` so that `$FEATURES`
is now an `#llvm.target_features<[...]>` attribute (rather than a
`StringAttr`). This enables the attribute to respond to DLTI queries for
the different target features.
The pass updates the `$FEATURES` attribute of the target attr at name
`llvm.target` in accordance with the (Sub)Target's features that the
relevant LLVM backend knows about.
---
DEMO:
```mlir
module attributes {llvm.target = #llvm.target<triple = "x86_64-unknown-linux",
chip = "skylake"> } {
}
```
by way of `-llvm-target-to-target-features` turns into:
```mlir
module attributes {llvm.target = #llvm.target<triple = "x86_64-unknown-linux",
chip = "skylake",
features = <["+64bit", "+64bit-mode", "+adx", "+aes", "+allow-light-256-bit", "+avx", "+avx2", "+bmi", "+bmi2", "+clflushopt", "+cmov", "+crc32", "+cx16", "+cx8", "+ermsb", "+f16c", "+false-deps-popcnt", "+fast-15bytenop", "+fast-gather", "+fast-scalar-fsqrt", "+fast-shld-rotate", "+fast-variable-crosslane-shuffle", "+fast-variable-perlane-shuffle", "+fast-vector-fsqrt", "+fma", "+fsgsbase", "+fxsr", "+idivq-to-divl", "+invpcid", "+lzcnt", "+macrofusion", "+mmx", "+movbe", "+no-bypass-delay-blend", "+no-bypass-delay-mov", "+no-bypass-delay-shuffle", "+nopl", "+pclmul", "+popcnt", "+prfchw", "+rdrnd", "+rdseed", "+sahf", "+slow-3ops-lea", "+sse", "+sse2", "+sse3", "+sse4.1", "+sse4.2", "+ssse3", "+vzeroupper", "+x87", "+xsave", "+xsavec", "+xsaveopt", "+xsaves"]>>} {
}
```
79 lines
2.7 KiB
C++
79 lines
2.7 KiB
C++
//===- 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);
|
|
}
|
|
};
|