MLIR 24.0.0git
TargetToDataLayout.cpp
Go to the documentation of this file.
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
11
15
16namespace mlir {
17namespace LLVM {
18#define GEN_PASS_DEF_LLVMTARGETTODATALAYOUT
19#include "mlir/Target/LLVMIR/Transforms/Passes.h.inc"
20} // namespace LLVM
21} // namespace mlir
22
23using namespace mlir;
24
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)
35
36 auto targetAttr = op->getDiscardableAttrOfType<LLVM::TargetAttrInterface>(
37 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 =
47 if (failed(dataLayout)) {
48 op->emitError() << "failed to obtain llvm::DataLayout for " << targetAttr;
49 return signalPassFailure();
50 }
51
52 DataLayoutSpecInterface dataLayoutSpec =
53 mlir::translateDataLayout(dataLayout.value(), &getContext());
54
55 if (auto existingDlSpec =
56 op->getDiscardableAttrOfType<DataLayoutSpecInterface>(
57 DLTIDialect::kDataLayoutAttrName)) {
58 dataLayoutSpec = existingDlSpec.combineWith({dataLayoutSpec});
59 }
60
61 op->setDiscardableAttr(DLTIDialect::kDataLayoutAttrName, dataLayoutSpec);
62 }
63};
b getContext())
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
void setDiscardableAttr(StringAttr name, Attribute value)
Set a discardable attribute by name.
Definition Operation.h:512
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
AttrClass getDiscardableAttrOfType(StringRef name)
Access a discardable attribute by name and cast it to AttrClass.
Definition Operation.h:493
FailureOr< llvm::DataLayout > getDataLayout(mlir::LLVM::TargetAttrInterface attr)
Helper to obtain the DataLayout of the target specified by the properties of the TargetAttrInterface-...
void initializeBackendsOnce()
Idempotent helper to register/initialize all backends that LLVM has been configured to support.
Include the generated interface declarations.
DataLayoutSpecInterface translateDataLayout(const llvm::DataLayout &dataLayout, MLIRContext *context)
Translate the given LLVM data layout into an MLIR equivalent using the DLTI dialect.