File: DataLayoutImporter.h

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (118 lines) | stat: -rw-r--r-- 4,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//===- DataLayoutImporter.h - LLVM to MLIR data layout conversion -*- C++ -*-=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the translation between the LLVMIR data layout and the
// corresponding MLIR representation.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
#define MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_

#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"

namespace llvm {
class StringRef;
class DataLayout;
} // namespace llvm

namespace mlir {
class FloatType;
class MLIRContext;
class Operation;

namespace LLVM {
class LLVMFuncOp;

namespace detail {

/// Returns a supported MLIR floating point type of the given bit width or
/// null if the bit width is not supported.
FloatType getFloatType(MLIRContext *context, unsigned width);

/// Helper class that translates an LLVM data layout to an MLIR data layout
/// specification. Only integer, float, pointer, alloca memory space, stack
/// alignment, and endianness entries are translated. The class also returns all
/// entries from the default data layout specification found in the language
/// reference (https://llvm.org/docs/LangRef.html#data-layout) if they are not
/// overwritten by the provided data layout.
class DataLayoutImporter {
public:
  DataLayoutImporter(MLIRContext *context,
                     const llvm::DataLayout &llvmDataLayout)
      : context(context) {
    translateDataLayout(llvmDataLayout);
  }

  /// Returns the MLIR data layout specification translated from the LLVM
  /// data layout.
  DataLayoutSpecInterface getDataLayout() const { return dataLayout; }

  /// Returns the last data layout token that has been processed before
  /// the data layout translation failed.
  StringRef getLastToken() const { return lastToken; }

  /// Returns the data layout tokens that have not been handled during the
  /// data layout translation.
  ArrayRef<StringRef> getUnhandledTokens() const { return unhandledTokens; }

private:
  /// Translates the LLVM `dataLayout` to an MLIR data layout specification.
  void translateDataLayout(const llvm::DataLayout &llvmDataLayout);

  /// Tries to parse the letter only prefix that identifies the specification
  /// and removes the consumed characters from the beginning of the string.
  FailureOr<StringRef> tryToParseAlphaPrefix(StringRef &token) const;

  /// Tries to parse an integer parameter and removes the integer from the
  /// beginning of the string.
  FailureOr<unsigned> tryToParseInt(StringRef &token) const;

  /// Tries to parse an integer parameter array.
  FailureOr<SmallVector<unsigned>> tryToParseIntList(StringRef token) const;

  /// Tries to parse the parameters of a type alignment entry.
  FailureOr<DenseIntElementsAttr> tryToParseAlignment(StringRef token) const;

  /// Tries to parse the parameters of a pointer alignment entry.
  FailureOr<DenseIntElementsAttr>
  tryToParsePointerAlignment(StringRef token) const;

  /// Adds a type alignment entry if there is none yet.
  LogicalResult tryToEmplaceAlignmentEntry(Type type, StringRef token);

  /// Adds a pointer alignment entry if there is none yet.
  LogicalResult tryToEmplacePointerAlignmentEntry(LLVMPointerType type,
                                                  StringRef token);

  /// Adds an endianness entry if there is none yet.
  LogicalResult tryToEmplaceEndiannessEntry(StringRef endianness,
                                            StringRef token);

  /// Adds an alloca address space entry if there is none yet.
  LogicalResult tryToEmplaceAllocaAddrSpaceEntry(StringRef token);

  /// Adds a stack alignment entry if there is none yet.
  LogicalResult tryToEmplaceStackAlignmentEntry(StringRef token);

  std::string layoutStr = {};
  StringRef lastToken = {};
  SmallVector<StringRef> unhandledTokens;
  DenseMap<StringAttr, DataLayoutEntryInterface> keyEntries;
  DenseMap<TypeAttr, DataLayoutEntryInterface> typeEntries;
  MLIRContext *context;
  DataLayoutSpecInterface dataLayout;
};

} // namespace detail
} // namespace LLVM
} // namespace mlir

#endif // MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_