• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- TypeTranslation.h - Translate types between MLIR & LLVM --*- 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 declares the type translation function going from MLIR LLVM dialect
10 // to LLVM IR and back.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TARGET_LLVMIR_TYPETRANSLATION_H
15 #define MLIR_TARGET_LLVMIR_TYPETRANSLATION_H
16 
17 #include <memory>
18 
19 namespace llvm {
20 class DataLayout;
21 class LLVMContext;
22 class Type;
23 } // namespace llvm
24 
25 namespace mlir {
26 
27 class MLIRContext;
28 
29 namespace LLVM {
30 
31 class LLVMType;
32 
33 namespace detail {
34 class TypeToLLVMIRTranslatorImpl;
35 class TypeFromLLVMIRTranslatorImpl;
36 } // namespace detail
37 
38 /// Utility class to translate MLIR LLVM dialect types to LLVM IR. Stores the
39 /// translation state, in particular any identified structure types that can be
40 /// reused in further translation.
41 class TypeToLLVMIRTranslator {
42 public:
43   TypeToLLVMIRTranslator(llvm::LLVMContext &context);
44   ~TypeToLLVMIRTranslator();
45 
46   /// Returns the perferred alignment for the type given the data layout. Note
47   /// that this will perform type conversion and store its results for future
48   /// uses.
49   // TODO: this should be removed when MLIR has proper data layout.
50   unsigned getPreferredAlignment(LLVM::LLVMType type,
51                                  const llvm::DataLayout &layout);
52 
53   /// Translates the given MLIR LLVM dialect type to LLVM IR.
54   llvm::Type *translateType(LLVM::LLVMType type);
55 
56 private:
57   /// Private implementation.
58   std::unique_ptr<detail::TypeToLLVMIRTranslatorImpl> impl;
59 };
60 
61 /// Utility class to translate LLVM IR types to the MLIR LLVM dialect. Stores
62 /// the translation state, in particular any identified structure types that are
63 /// reused across translations.
64 class TypeFromLLVMIRTranslator {
65 public:
66   TypeFromLLVMIRTranslator(MLIRContext &context);
67   ~TypeFromLLVMIRTranslator();
68 
69   /// Translates the given LLVM IR type to the MLIR LLVM dialect.
70   LLVM::LLVMType translateType(llvm::Type *type);
71 
72 private:
73   /// Private implementation.
74   std::unique_ptr<detail::TypeFromLLVMIRTranslatorImpl> impl;
75 };
76 
77 } // namespace LLVM
78 } // namespace mlir
79 
80 #endif // MLIR_TARGET_LLVMIR_TYPETRANSLATION_H
81