xref: /llvm-project/mlir/lib/Target/LLVMIR/DataLayoutImporter.h (revision c1ed45a271145acbfad81d87706aeebf361809c3)
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 
21 namespace llvm {
22 class StringRef;
23 class DataLayout;
24 } // namespace llvm
25 
26 namespace mlir {
27 class FloatType;
28 class MLIRContext;
29 class Operation;
30 
31 namespace LLVM {
32 class LLVMFuncOp;
33 
34 namespace detail {
35 
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);
39 
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 {
47 public:
DataLayoutImporter(MLIRContext * context,const llvm::DataLayout & llvmDataLayout)48   DataLayoutImporter(MLIRContext *context,
49                      const llvm::DataLayout &llvmDataLayout)
50       : context(context) {
51     translateDataLayout(llvmDataLayout);
52   }
53 
54   /// Returns the MLIR data layout specification translated from the LLVM
55   /// data layout.
getDataLayout()56   DataLayoutSpecInterface getDataLayout() const { return dataLayout; }
57 
58   /// Returns the last data layout token that has been processed before
59   /// the data layout translation failed.
getLastToken()60   StringRef getLastToken() const { return lastToken; }
61 
62   /// Returns the data layout tokens that have not been handled during the
63   /// data layout translation.
getUnhandledTokens()64   ArrayRef<StringRef> getUnhandledTokens() const { return unhandledTokens; }
65 
66 private:
67   /// Translates the LLVM `dataLayout` to an MLIR data layout specification.
68   void translateDataLayout(const llvm::DataLayout &llvmDataLayout);
69 
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;
73 
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;
77 
78   /// Tries to parse an integer parameter array.
79   FailureOr<SmallVector<uint64_t>> tryToParseIntList(StringRef token) const;
80 
81   /// Tries to parse the parameters of a type alignment entry.
82   FailureOr<DenseIntElementsAttr> tryToParseAlignment(StringRef token) const;
83 
84   /// Tries to parse the parameters of a pointer alignment entry.
85   FailureOr<DenseIntElementsAttr>
86   tryToParsePointerAlignment(StringRef token) const;
87 
88   /// Adds a type alignment entry if there is none yet.
89   LogicalResult tryToEmplaceAlignmentEntry(Type type, StringRef token);
90 
91   /// Adds a pointer alignment entry if there is none yet.
92   LogicalResult tryToEmplacePointerAlignmentEntry(LLVMPointerType type,
93                                                   StringRef token);
94 
95   /// Adds an endianness entry if there is none yet.
96   LogicalResult tryToEmplaceEndiannessEntry(StringRef endianness,
97                                             StringRef token);
98 
99   /// Adds an alloca address space entry if there is none yet.
100   LogicalResult tryToEmplaceAddrSpaceEntry(StringRef token,
101                                            llvm::StringLiteral spaceKey);
102 
103   /// Adds a stack alignment entry if there is none yet.
104   LogicalResult tryToEmplaceStackAlignmentEntry(StringRef token);
105 
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;
113 };
114 
115 } // namespace detail
116 } // namespace LLVM
117 } // namespace mlir
118 
119 #endif // MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
120