MLIR  22.0.0git
TargetUtils.cpp
Go to the documentation of this file.
1 //===- TargetUtils.cpp - utils for obtaining generic target backend info --===//
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 
10 
11 #include "mlir/Dialect/DLTI/DLTI.h"
14 
15 #include "llvm/MC/TargetRegistry.h"
16 #include "llvm/Support/DebugLog.h"
17 #include "llvm/Support/TargetSelect.h"
18 #include "llvm/Target/TargetMachine.h"
19 
20 #define DEBUG_TYPE "mlir-llvm-target-utils"
21 
22 namespace mlir {
23 namespace LLVM {
24 namespace detail {
26  static const auto initOnce = [] {
27  // Ensure that the targets, that LLVM has been configured to support,
28  // are loaded into the TargetRegistry.
29  llvm::InitializeAllTargets();
30  llvm::InitializeAllTargetMCs();
31  return true;
32  }();
33  (void)initOnce; // Dummy usage.
34 }
35 
36 FailureOr<std::unique_ptr<llvm::TargetMachine>>
37 getTargetMachine(mlir::LLVM::TargetAttrInterface attr) {
38  StringRef triple = attr.getTriple();
39  StringRef chipAKAcpu = attr.getChip();
40  // NB: `TargetAttrInterface::getFeatures()` is coarsely typed to work around
41  // cyclic dependency issue in tablegen files.
42  auto featuresAttr =
43  llvm::cast_if_present<LLVM::TargetFeaturesAttr>(attr.getFeatures());
44  std::string features = featuresAttr ? featuresAttr.getFeaturesString() : "";
45 
46  std::string error;
47  const llvm::Target *target =
48  llvm::TargetRegistry::lookupTarget(triple, error);
49  if (!target || !error.empty()) {
50  LDBG() << "Looking up target '" << triple << "' failed: " << error << "\n";
51  return failure();
52  }
53 
54  return std::unique_ptr<llvm::TargetMachine>(target->createTargetMachine(
55  llvm::Triple(triple), chipAKAcpu, features, {}, {}));
56 }
57 
58 FailureOr<llvm::DataLayout>
59 getDataLayout(mlir::LLVM::TargetAttrInterface attr) {
60  FailureOr<std::unique_ptr<llvm::TargetMachine>> targetMachine =
61  getTargetMachine(attr);
62  if (failed(targetMachine)) {
63  LDBG() << "Failed to retrieve the target machine for data layout.\n";
64  return failure();
65  }
66  return (targetMachine.value())->createDataLayout();
67 }
68 
69 } // namespace detail
70 } // namespace LLVM
71 } // namespace mlir
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
FailureOr< llvm::DataLayout > getDataLayout(mlir::LLVM::TargetAttrInterface attr)
Helper to obtain the DataLayout of the target specified by the properties of the TargetAttrInterface-...
Definition: TargetUtils.cpp:59
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.