1 //===- DataLayoutImporter.h - LLVM to MLIR data layout conversion -*- C++ -*-=//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the translation between the LLVMIR data layout and the
10 // corresponding MLIR representation.
12 //===----------------------------------------------------------------------===//
14 #ifndef MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
17 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
18 #include "mlir/IR/BuiltinAttributes.h"
19 #include "mlir/Interfaces/DataLayoutInterfaces.h"
36 /// Returns a supported MLIR floating point type of the given bit width or
37 /// null if the bit width is not supported.
38 FloatType
getFloatType(MLIRContext
*context
, unsigned width
);
40 /// Helper class that translates an LLVM data layout to an MLIR data layout
41 /// specification. Only integer, float, pointer, alloca memory space, stack
42 /// alignment, and endianness entries are translated. The class also returns all
43 /// entries from the default data layout specification found in the language
44 /// reference (https://llvm.org/docs/LangRef.html#data-layout) if they are not
45 /// overwritten by the provided data layout.
46 class DataLayoutImporter
{
48 DataLayoutImporter(MLIRContext
*context
,
49 const llvm::DataLayout
&llvmDataLayout
)
51 translateDataLayout(llvmDataLayout
);
54 /// Returns the MLIR data layout specification translated from the LLVM
56 DataLayoutSpecInterface
getDataLayout() const { return dataLayout
; }
58 /// Returns the last data layout token that has been processed before
59 /// the data layout translation failed.
60 StringRef
getLastToken() const { return lastToken
; }
62 /// Returns the data layout tokens that have not been handled during the
63 /// data layout translation.
64 ArrayRef
<StringRef
> getUnhandledTokens() const { return unhandledTokens
; }
67 /// Translates the LLVM `dataLayout` to an MLIR data layout specification.
68 void translateDataLayout(const llvm::DataLayout
&llvmDataLayout
);
70 /// Tries to parse the letter only prefix that identifies the specification
71 /// and removes the consumed characters from the beginning of the string.
72 FailureOr
<StringRef
> tryToParseAlphaPrefix(StringRef
&token
) const;
74 /// Tries to parse an integer parameter and removes the integer from the
75 /// beginning of the string.
76 FailureOr
<uint64_t> tryToParseInt(StringRef
&token
) const;
78 /// Tries to parse an integer parameter array.
79 FailureOr
<SmallVector
<uint64_t>> tryToParseIntList(StringRef token
) const;
81 /// Tries to parse the parameters of a type alignment entry.
82 FailureOr
<DenseIntElementsAttr
> tryToParseAlignment(StringRef token
) const;
84 /// Tries to parse the parameters of a pointer alignment entry.
85 FailureOr
<DenseIntElementsAttr
>
86 tryToParsePointerAlignment(StringRef token
) const;
88 /// Adds a type alignment entry if there is none yet.
89 LogicalResult
tryToEmplaceAlignmentEntry(Type type
, StringRef token
);
91 /// Adds a pointer alignment entry if there is none yet.
92 LogicalResult
tryToEmplacePointerAlignmentEntry(LLVMPointerType type
,
95 /// Adds an endianness entry if there is none yet.
96 LogicalResult
tryToEmplaceEndiannessEntry(StringRef endianness
,
99 /// Adds an alloca address space entry if there is none yet.
100 LogicalResult
tryToEmplaceAddrSpaceEntry(StringRef token
,
101 llvm::StringLiteral spaceKey
);
103 /// Adds a stack alignment entry if there is none yet.
104 LogicalResult
tryToEmplaceStackAlignmentEntry(StringRef token
);
106 std::string layoutStr
= {};
107 StringRef lastToken
= {};
108 SmallVector
<StringRef
> unhandledTokens
;
109 DenseMap
<StringAttr
, DataLayoutEntryInterface
> keyEntries
;
110 DenseMap
<TypeAttr
, DataLayoutEntryInterface
> typeEntries
;
111 MLIRContext
*context
;
112 DataLayoutSpecInterface dataLayout
;
115 } // namespace detail
119 #endif // MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_