55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
77// ===----------------------------------------------------------------------===//
8+
89#include " mlir/Target/LLVMIR/Transforms/Passes.h"
910
1011#include " mlir/Dialect/DLTI/DLTI.h"
1112#include " mlir/Dialect/LLVMIR/LLVMDialect.h"
1213#include " mlir/Target/LLVMIR/Import.h"
1314
14- #include " llvm/MC/TargetRegistry.h"
15- #include " llvm/Support/Debug.h"
16- #include " llvm/Support/TargetSelect.h"
17- #include " llvm/Target/TargetMachine.h"
18-
19- #define DEBUG_TYPE " mlir-llvm-target-to-data-layout"
20- #define DBGS () (llvm::dbgs() << ' [' << DEBUG_TYPE << " ] " )
21- #define LDBG (X ) LLVM_DEBUG(DBGS() << X << " \n " )
22-
2315namespace mlir {
2416namespace LLVM {
2517#define GEN_PASS_DEF_LLVMTARGETTODATALAYOUT
@@ -29,36 +21,6 @@ namespace LLVM {
2921
3022using namespace mlir ;
3123
32- static FailureOr<std::unique_ptr<llvm::TargetMachine>>
33- getTargetMachine (LLVM::TargetAttrInterface attr) {
34- StringRef triple = attr.getTriple ();
35- StringRef chipAKAcpu = attr.getChip ();
36- std::string features =
37- attr.getFeatures () ? attr.getFeatures ().getFeaturesString () : " " ;
38-
39- std::string error;
40- const llvm::Target *target =
41- llvm::TargetRegistry::lookupTarget (triple, error);
42- if (!target || !error.empty ()) {
43- LDBG (" Looking up target '" << triple << " ' failed: " << error << " \n " );
44- return failure ();
45- }
46-
47- return std::unique_ptr<llvm::TargetMachine>(target->createTargetMachine (
48- llvm::Triple (triple), chipAKAcpu, features, {}, {}));
49- }
50-
51- static FailureOr<llvm::DataLayout>
52- getDataLayout (LLVM::TargetAttrInterface attr) {
53- FailureOr<std::unique_ptr<llvm::TargetMachine>> targetMachine =
54- getTargetMachine (attr);
55- if (failed (targetMachine)) {
56- LDBG (" Failed to retrieve the target machine for data layout.\n " );
57- return failure ();
58- }
59- return (targetMachine.value ())->createDataLayout ();
60- }
61-
6224struct TargetToDataLayoutPass
6325 : public LLVM::impl::LLVMTargetToDataLayoutBase<TargetToDataLayoutPass> {
6426 using LLVM::impl::LLVMTargetToDataLayoutBase<
@@ -67,15 +29,8 @@ struct TargetToDataLayoutPass
6729 void runOnOperation () override {
6830 Operation *op = getOperation ();
6931
70- if (initializeLLVMTargets) {
71- static llvm::once_flag initializeBackendsOnce;
72- llvm::call_once (initializeBackendsOnce, []() {
73- // Ensure that the targets, that LLVM has been configured to support,
74- // are loaded into the TargetRegistry.
75- llvm::InitializeAllTargets ();
76- llvm::InitializeAllTargetMCs ();
77- });
78- }
32+ if (initializeLLVMTargets)
33+ LLVM::detail::initializeBackendsOnce ();
7934
8035 auto targetAttr = op->getAttrOfType <LLVM::TargetAttrInterface>(
8136 LLVM::LLVMDialect::getTargetAttrName ());
@@ -86,7 +41,8 @@ struct TargetToDataLayoutPass
8641 return signalPassFailure ();
8742 }
8843
89- FailureOr<llvm::DataLayout> dataLayout = getDataLayout (targetAttr);
44+ FailureOr<llvm::DataLayout> dataLayout =
45+ LLVM::detail::getDataLayout (targetAttr);
9046 if (failed (dataLayout)) {
9147 op->emitError () << " failed to obtain llvm::DataLayout for " << targetAttr;
9248 return signalPassFailure ();
0 commit comments