MLIR  21.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_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
16 
20 #include "llvm/ADT/MapVector.h"
21 
22 namespace llvm {
23 class StringRef;
24 class DataLayout;
25 } // namespace llvm
26 
27 namespace mlir {
28 class FloatType;
29 class MLIRContext;
30 class Operation;
31 
32 namespace LLVM {
33 class LLVMFuncOp;
34 
35 namespace 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.
39 FloatType 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.
48 public:
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 
67 private:
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  std::string layoutStr = {};
112  StringRef lastToken = {};
113  SmallVector<StringRef> unhandledTokens;
114  llvm::MapVector<StringAttr, DataLayoutEntryInterface> keyEntries;
115  llvm::MapVector<TypeAttr, DataLayoutEntryInterface> typeEntries;
116  MLIRContext *context;
117  DataLayoutSpecInterface dataLayout;
118 };
119 
120 } // namespace detail
121 } // namespace LLVM
122 } // namespace mlir
123 
124 #endif // MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
Helper class that translates an LLVM data layout to an MLIR data layout specification.
StringRef getLastToken() const
Returns the last data layout token that has been processed before the data layout translation failed.
DataLayoutSpecInterface getDataLayout() const
Returns the MLIR data layout specification translated from the LLVM data layout.
DataLayoutImporter(MLIRContext *context, const llvm::DataLayout &llvmDataLayout)
ArrayRef< StringRef > getUnhandledTokens() const
Returns the data layout tokens that have not been handled during the data layout translation.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
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.