1//===- DataLayoutImporter.h - LLVM to MLIR data layout conversion -*- C++ -*-=//
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// This file implements the translation between the LLVMIR data layout and the
10// corresponding MLIR representation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
15#define MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
16
17#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
18#include "mlir/IR/BuiltinAttributes.h"
19#include "mlir/Interfaces/DataLayoutInterfaces.h"
20#include "llvm/ADT/MapVector.h"
21
22namespace llvm {
23class StringRef;
24class DataLayout;
25} // namespace llvm
26
27namespace mlir {
28class FloatType;
29class MLIRContext;
30class Operation;
31
32namespace LLVM {
33class LLVMFuncOp;
34
35namespace detail {
36
37/// Returns a supported MLIR floating point type of the given bit width or
38/// null if the bit width is not supported.
39FloatType getFloatType(MLIRContext *context, unsigned width);
40
41/// Helper class that translates an LLVM data layout to an MLIR data layout
42/// specification. Only integer, float, pointer, alloca memory space, stack
43/// alignment, and endianness entries are translated. The class also returns all
44/// entries from the default data layout specification found in the language
45/// reference (https://llvm.org/docs/LangRef.html#data-layout) if they are not
46/// overwritten by the provided data layout.
47class DataLayoutImporter {
48public:
49 DataLayoutImporter(MLIRContext *context,
50 const llvm::DataLayout &llvmDataLayout)
51 : context(context) {
52 translateDataLayout(llvmDataLayout);
53 }
54
55 /// Returns the MLIR data layout specification translated from the LLVM
56 /// data layout.
57 DataLayoutSpecInterface getDataLayout() const { return dataLayout; }
58
59 /// Returns the last data layout token that has been processed before
60 /// the data layout translation failed.
61 StringRef getLastToken() const { return lastToken; }
62
63 /// Returns the data layout tokens that have not been handled during the
64 /// data layout translation.
65 ArrayRef<StringRef> getUnhandledTokens() const { return unhandledTokens; }
66
67private:
68 /// Translates the LLVM `dataLayout` to an MLIR data layout specification.
69 void translateDataLayout(const llvm::DataLayout &llvmDataLayout);
70
71 /// Tries to parse the letter only prefix that identifies the specification
72 /// and removes the consumed characters from the beginning of the string.
73 FailureOr<StringRef> tryToParseAlphaPrefix(StringRef &token) const;
74
75 /// Tries to parse an integer parameter and removes the integer from the
76 /// beginning of the string.
77 FailureOr<uint64_t> tryToParseInt(StringRef &token) const;
78
79 /// Tries to parse an integer parameter array.
80 FailureOr<SmallVector<uint64_t>> tryToParseIntList(StringRef token) const;
81
82 /// Tries to parse the parameters of a type alignment entry.
83 FailureOr<DenseIntElementsAttr> tryToParseAlignment(StringRef token) const;
84
85 /// Tries to parse the parameters of a pointer alignment entry.
86 FailureOr<DenseIntElementsAttr>
87 tryToParsePointerAlignment(StringRef token) const;
88
89 /// Adds a type alignment entry if there is none yet.
90 LogicalResult tryToEmplaceAlignmentEntry(Type type, StringRef token);
91
92 /// Adds a pointer alignment entry if there is none yet.
93 LogicalResult tryToEmplacePointerAlignmentEntry(LLVMPointerType type,
94 StringRef token);
95
96 /// Adds an endianness entry if there is none yet.
97 LogicalResult tryToEmplaceEndiannessEntry(StringRef endianness,
98 StringRef token);
99
100 /// Adds an alloca address space entry if there is none yet.
101 LogicalResult tryToEmplaceAddrSpaceEntry(StringRef token,
102 llvm::StringLiteral spaceKey);
103
104 /// Adds an mangling mode entry if there is none yet.
105 LogicalResult tryToEmplaceManglingModeEntry(StringRef token,
106 llvm::StringLiteral manglingKey);
107
108 /// Adds a stack alignment entry if there is none yet.
109 LogicalResult tryToEmplaceStackAlignmentEntry(StringRef token);
110
111 /// Adds a function pointer alignment entry if there is none yet.
112 LogicalResult
113 tryToEmplaceFunctionPointerAlignmentEntry(StringRef fnPtrAlignEntry,
114 StringRef token);
115
116 /// Adds legal int widths entry if there is none yet.
117 LogicalResult tryToEmplaceLegalIntWidthsEntry(StringRef token);
118
119 std::string layoutStr = {};
120 StringRef lastToken = {};
121 SmallVector<StringRef> unhandledTokens;
122 llvm::MapVector<StringAttr, DataLayoutEntryInterface> keyEntries;
123 llvm::MapVector<TypeAttr, DataLayoutEntryInterface> typeEntries;
124 MLIRContext *context;
125 DataLayoutSpecInterface dataLayout;
126};
127
128} // namespace detail
129} // namespace LLVM
130} // namespace mlir
131
132#endif // MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
133

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of mlir/lib/Target/LLVMIR/DataLayoutImporter.h