MLIR 22.0.0git
DataLayoutImporter.h
Go to the documentation of this file.
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_TARGET_LLVMIR_DATALAYOUTIMPORTER_H
15#define MLIR_TARGET_LLVMIR_DATALAYOUTIMPORTER_H
16
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 string to an MLIR data
42/// layout specification. Only integer, float, pointer, alloca memory space,
43/// stack alignment, and endianness entries are translated. The class also
44/// returns all entries from the default data layout specification found in the
45/// language reference (https://llvm.org/docs/LangRef.html#data-layout) if they
46/// are not overwritten by the provided data layout.
48public:
49 DataLayoutImporter(MLIRContext *context, StringRef dataLayoutStr)
50 : dataLayoutStr(dataLayoutStr), context(context) {
51 // Translate the `dataLayoutStr`. First, append the default data layout
52 // string specified in the language reference
53 // (https://llvm.org/docs/LangRef.html#data-layout) to the supplied string.
54 // The translation then parses the string and ignores the default value if a
55 // specific kind occurs in both strings. Additionally, the following default
56 // values exist:
57 // - non-default address space pointer specifications default to the default
58 // address space pointer specification
59 // - the alloca address space defaults to the default address space.
60 dataLayoutSpec = dataLayoutSpecFromDataLayoutStr();
61 }
62
63 /// Returns the MLIR data layout specification translated from the LLVM
64 /// data layout.
65 DataLayoutSpecInterface getDataLayoutSpec() const { return dataLayoutSpec; }
66
67 /// Returns the last data layout token that has been processed before
68 /// the data layout translation failed.
69 StringRef getLastToken() const { return lastToken; }
70
71 /// Returns the data layout tokens that have not been handled during the
72 /// data layout translation.
73 ArrayRef<StringRef> getUnhandledTokens() const { return unhandledTokens; }
74
75private:
76 /// Translate the LLVM data layout string to an MLIR data layout
77 /// specification.
78 DataLayoutSpecInterface dataLayoutSpecFromDataLayoutStr();
79
80 /// Tries to parse the letter only prefix that identifies the specification
81 /// and removes the consumed characters from the beginning of the string.
82 FailureOr<StringRef> tryToParseAlphaPrefix(StringRef &token) const;
83
84 /// Tries to parse an integer parameter and removes the integer from the
85 /// beginning of the string.
86 FailureOr<uint64_t> tryToParseInt(StringRef &token) const;
87
88 /// Tries to parse an integer parameter array.
89 FailureOr<SmallVector<uint64_t>> tryToParseIntList(StringRef token) const;
90
91 /// Tries to parse the parameters of a type alignment entry.
92 FailureOr<DenseIntElementsAttr> tryToParseAlignment(StringRef token) const;
93
94 /// Tries to parse the parameters of a pointer alignment entry.
95 FailureOr<DenseIntElementsAttr>
96 tryToParsePointerAlignment(StringRef token) const;
97
98 /// Adds a type alignment entry if there is none yet.
99 LogicalResult tryToEmplaceAlignmentEntry(Type type, StringRef token);
100
101 /// Adds a pointer alignment entry if there is none yet.
102 LogicalResult tryToEmplacePointerAlignmentEntry(LLVMPointerType type,
103 StringRef token);
104
105 /// Adds an endianness entry if there is none yet.
106 LogicalResult tryToEmplaceEndiannessEntry(StringRef endianness,
107 StringRef token);
108
109 /// Adds an alloca address space entry if there is none yet.
110 LogicalResult tryToEmplaceAddrSpaceEntry(StringRef token,
111 llvm::StringLiteral spaceKey);
112
113 /// Adds an mangling mode entry if there is none yet.
114 LogicalResult tryToEmplaceManglingModeEntry(StringRef token,
115 llvm::StringLiteral manglingKey);
116
117 /// Adds a stack alignment entry if there is none yet.
118 LogicalResult tryToEmplaceStackAlignmentEntry(StringRef token);
119
120 /// Adds a function pointer alignment entry if there is none yet.
121 LogicalResult
122 tryToEmplaceFunctionPointerAlignmentEntry(StringRef fnPtrAlignEntry,
123 StringRef token);
124
125 /// Adds legal int widths entry if there is none yet.
126 LogicalResult tryToEmplaceLegalIntWidthsEntry(StringRef token);
127
128 std::string dataLayoutStr = {};
129 DataLayoutSpecInterface dataLayoutSpec;
130
131 StringRef lastToken = {};
132 SmallVector<StringRef> unhandledTokens;
133 llvm::MapVector<StringAttr, DataLayoutEntryInterface> keyEntries;
134 llvm::MapVector<TypeAttr, DataLayoutEntryInterface> typeEntries;
135 MLIRContext *context;
136};
137
138} // namespace detail
139} // namespace LLVM
140} // namespace mlir
141
142#endif // MLIR_TARGET_LLVMIR_DATALAYOUTIMPORTER_H
StringRef getLastToken() const
Returns the last data layout token that has been processed before the data layout translation failed.
ArrayRef< StringRef > getUnhandledTokens() const
Returns the data layout tokens that have not been handled during the data layout translation.
DataLayoutImporter(MLIRContext *context, StringRef dataLayoutStr)
DataLayoutSpecInterface getDataLayoutSpec() const
Returns the MLIR data layout specification translated from the LLVM data layout.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
FloatType getFloatType(MLIRContext *context, unsigned width)
Returns a supported MLIR floating point type of the given bit width or null if the bit width is not s...
Include the generated interface declarations.