blob: f1d36228bef1ffa3bac24c20f0842f68b130ff52 (
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
|
//===- 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
|