| 1 | //===- TargetToDataLayout.cpp - extract data layout from TargetMachine ----===// |
|---|---|
| 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 | |
| 9 | #include "mlir/Target/LLVMIR/Transforms/Passes.h" |
| 10 | #include "mlir/Target/LLVMIR/Transforms/TargetUtils.h" |
| 11 | |
| 12 | #include "mlir/Dialect/DLTI/DLTI.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 14 | #include "mlir/Target/LLVMIR/Import.h" |
| 15 | |
| 16 | namespace mlir { |
| 17 | namespace LLVM { |
| 18 | #define GEN_PASS_DEF_LLVMTARGETTODATALAYOUT |
| 19 | #include "mlir/Target/LLVMIR/Transforms/Passes.h.inc" |
| 20 | } // namespace LLVM |
| 21 | } // namespace mlir |
| 22 | |
| 23 | using namespace mlir; |
| 24 | |
| 25 | struct TargetToDataLayoutPass |
| 26 | : public LLVM::impl::LLVMTargetToDataLayoutBase<TargetToDataLayoutPass> { |
| 27 | using LLVM::impl::LLVMTargetToDataLayoutBase< |
| 28 | TargetToDataLayoutPass>::LLVMTargetToDataLayoutBase; |
| 29 | |
| 30 | void runOnOperation() override { |
| 31 | Operation *op = getOperation(); |
| 32 | |
| 33 | if (initializeLLVMTargets) |
| 34 | LLVM::detail::initializeBackendsOnce(); |
| 35 | |
| 36 | auto targetAttr = op->getDiscardableAttrOfType<LLVM::TargetAttrInterface>( |
| 37 | name: LLVM::LLVMDialect::getTargetAttrName()); |
| 38 | if (!targetAttr) { |
| 39 | op->emitError() |
| 40 | << "no TargetAttrInterface-implementing attribute at key \"" |
| 41 | << LLVM::LLVMDialect::getTargetAttrName() << "\""; |
| 42 | return signalPassFailure(); |
| 43 | } |
| 44 | |
| 45 | FailureOr<llvm::DataLayout> dataLayout = |
| 46 | LLVM::detail::getDataLayout(attr: targetAttr); |
| 47 | if (failed(Result: dataLayout)) { |
| 48 | op->emitError() << "failed to obtain llvm::DataLayout for "<< targetAttr; |
| 49 | return signalPassFailure(); |
| 50 | } |
| 51 | |
| 52 | DataLayoutSpecInterface dataLayoutSpec = |
| 53 | mlir::translateDataLayout(dataLayout: dataLayout.value(), context: &getContext()); |
| 54 | |
| 55 | if (auto existingDlSpec = |
| 56 | op->getDiscardableAttrOfType<DataLayoutSpecInterface>( |
| 57 | name: DLTIDialect::kDataLayoutAttrName)) { |
| 58 | dataLayoutSpec = existingDlSpec.combineWith(specs: {dataLayoutSpec}); |
| 59 | } |
| 60 | |
| 61 | op->setDiscardableAttr(name: DLTIDialect::kDataLayoutAttrName, value: dataLayoutSpec); |
| 62 | } |
| 63 | }; |
| 64 |