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"]>>} {
}
```
72 lines
2.4 KiB
C++
72 lines
2.4 KiB
C++
//===- TargetUtils.cpp - utils for obtaining generic target backend info --===//
|
|
//
|
|
// 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/Dialect/DLTI/DLTI.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
#include "mlir/Target/LLVMIR/Import.h"
|
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
|
#include "llvm/Support/DebugLog.h"
|
|
#include "llvm/Support/TargetSelect.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#define DEBUG_TYPE "mlir-llvm-target-utils"
|
|
|
|
namespace mlir {
|
|
namespace LLVM {
|
|
namespace detail {
|
|
void initializeBackendsOnce() {
|
|
static const auto initOnce = [] {
|
|
// Ensure that the targets, that LLVM has been configured to support,
|
|
// are loaded into the TargetRegistry.
|
|
llvm::InitializeAllTargets();
|
|
llvm::InitializeAllTargetMCs();
|
|
return true;
|
|
}();
|
|
(void)initOnce; // Dummy usage.
|
|
}
|
|
|
|
FailureOr<std::unique_ptr<llvm::TargetMachine>>
|
|
getTargetMachine(mlir::LLVM::TargetAttrInterface attr) {
|
|
StringRef triple = attr.getTriple();
|
|
StringRef chipAKAcpu = attr.getChip();
|
|
// NB: `TargetAttrInterface::getFeatures()` is coarsely typed to work around
|
|
// cyclic dependency issue in tablegen files.
|
|
auto featuresAttr =
|
|
llvm::cast_if_present<LLVM::TargetFeaturesAttr>(attr.getFeatures());
|
|
std::string features = featuresAttr ? featuresAttr.getFeaturesString() : "";
|
|
|
|
std::string error;
|
|
const llvm::Target *target =
|
|
llvm::TargetRegistry::lookupTarget(triple, error);
|
|
if (!target || !error.empty()) {
|
|
LDBG() << "Looking up target '" << triple << "' failed: " << error << "\n";
|
|
return failure();
|
|
}
|
|
|
|
return std::unique_ptr<llvm::TargetMachine>(target->createTargetMachine(
|
|
llvm::Triple(triple), chipAKAcpu, features, {}, {}));
|
|
}
|
|
|
|
FailureOr<llvm::DataLayout>
|
|
getDataLayout(mlir::LLVM::TargetAttrInterface attr) {
|
|
FailureOr<std::unique_ptr<llvm::TargetMachine>> targetMachine =
|
|
getTargetMachine(attr);
|
|
if (failed(targetMachine)) {
|
|
LDBG() << "Failed to retrieve the target machine for data layout.\n";
|
|
return failure();
|
|
}
|
|
return (targetMachine.value())->createDataLayout();
|
|
}
|
|
|
|
} // namespace detail
|
|
} // namespace LLVM
|
|
} // namespace mlir
|