Skip to content

Commit 929189a

Browse files
committed
[MLIR][LLVM] Expose type translator from LLVM to MLIR Type
This commit moves the type translator from LLVM to MLIR to a public header for use by external projects or other code. Unlike a previous attempt (https://reviews.llvm.org/D104726), this patch moves the type conversion into separate files which remedies the linker error which was only caught by CI. Differential Revision: https://reviews.llvm.org/D104834
1 parent da2e614 commit 929189a

File tree

9 files changed

+234
-172
lines changed

9 files changed

+234
-172
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "mlir/IR/Value.h"
1919
#include "mlir/Target/LLVMIR/Export.h"
2020
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
21-
#include "mlir/Target/LLVMIR/TypeTranslation.h"
21+
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
2222

2323
#include "llvm/ADT/SetVector.h"
2424
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===- TypeFromLLVM.h - Translate types from LLVM to MLIR --*- 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_TYPEFROMLLVM_H
15+
#define MLIR_TARGET_LLVMIR_TYPEFROMLLVM_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 Type;
28+
class MLIRContext;
29+
30+
namespace LLVM {
31+
32+
namespace detail {
33+
class TypeFromLLVMIRTranslatorImpl;
34+
} // namespace detail
35+
36+
/// Utility class to translate LLVM IR types to the MLIR LLVM dialect. Stores
37+
/// the translation state, in particular any identified structure types that are
38+
/// reused across translations.
39+
class TypeFromLLVMIRTranslator {
40+
public:
41+
TypeFromLLVMIRTranslator(MLIRContext &context);
42+
~TypeFromLLVMIRTranslator();
43+
44+
/// Translates the given LLVM IR type to the MLIR LLVM dialect.
45+
Type translateType(llvm::Type *type);
46+
47+
private:
48+
/// Private implementation.
49+
std::unique_ptr<detail::TypeFromLLVMIRTranslatorImpl> impl;
50+
};
51+
52+
} // namespace LLVM
53+
} // namespace mlir
54+
55+
#endif // MLIR_TARGET_LLVMIR_TYPEFROMLLVM_H

mlir/include/mlir/Target/LLVMIR/TypeTranslation.h renamed to mlir/include/mlir/Target/LLVMIR/TypeToLLVM.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- TypeTranslation.h - Translate types between MLIR & LLVM --*- C++ -*-===//
1+
//===- TypeToLLVM.h - Translate types from MLIR to LLVM --*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -11,8 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#ifndef MLIR_TARGET_LLVMIR_TYPETRANSLATION_H
15-
#define MLIR_TARGET_LLVMIR_TYPETRANSLATION_H
14+
#ifndef MLIR_TARGET_LLVMIR_TYPETOLLVM_H
15+
#define MLIR_TARGET_LLVMIR_TYPETOLLVM_H
1616

1717
#include <memory>
1818

@@ -58,4 +58,4 @@ class TypeToLLVMIRTranslator {
5858
} // namespace LLVM
5959
} // namespace mlir
6060

61-
#endif // MLIR_TARGET_LLVMIR_TYPETRANSLATION_H
61+
#endif // MLIR_TARGET_LLVMIR_TYPETOLLVM_H

mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "mlir/Dialect/Vector/VectorOps.h"
1818
#include "mlir/IR/BuiltinTypes.h"
1919
#include "mlir/Support/MathExtras.h"
20-
#include "mlir/Target/LLVMIR/TypeTranslation.h"
20+
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
2121
#include "mlir/Transforms/DialectConversion.h"
2222

2323
using namespace mlir;

mlir/lib/Target/LLVMIR/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ set(LLVM_OPTIONAL_SOURCES
55
ConvertToLLVMIR.cpp
66
DebugTranslation.cpp
77
ModuleTranslation.cpp
8-
TypeTranslation.cpp
8+
TypeToLLVM.cpp
9+
TypeFromLLVM.cpp
910
)
1011

1112

1213
add_mlir_translation_library(MLIRTargetLLVMIRExport
1314
DebugTranslation.cpp
1415
ModuleTranslation.cpp
15-
TypeTranslation.cpp
16+
TypeToLLVM.cpp
1617

1718
ADDITIONAL_HEADER_DIRS
1819
${MLIR_MAIN_INCLUDE_DIR}/mlir/Target/LLVMIR
@@ -35,6 +36,7 @@ add_mlir_translation_library(MLIRTargetLLVMIRExport
3536

3637
add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
3738
ConvertToLLVMIR.cpp
39+
TypeToLLVM.cpp
3840

3941
LINK_LIBS PUBLIC
4042
MLIRArmNeonToLLVMIRTranslation
@@ -50,6 +52,7 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
5052

5153
add_mlir_translation_library(MLIRTargetLLVMIRImport
5254
ConvertFromLLVMIR.cpp
55+
TypeFromLLVM.cpp
5356

5457
ADDITIONAL_HEADER_DIRS
5558
${MLIR_MAIN_INCLUDE_DIR}/mlir/Target/LLVMIR

mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp

Lines changed: 1 addition & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/IR/BuiltinTypes.h"
1717
#include "mlir/IR/MLIRContext.h"
1818
#include "mlir/Target/LLVMIR/Import.h"
19+
#include "mlir/Target/LLVMIR/TypeFromLLVM.h"
1920
#include "mlir/Translation.h"
2021

2122
#include "llvm/ADT/TypeSwitch.h"
@@ -45,167 +46,6 @@ static std::string diag(llvm::Value &v) {
4546
return os.str();
4647
}
4748

48-
namespace mlir {
49-
namespace LLVM {
50-
namespace detail {
51-
/// Support for translating LLVM IR types to MLIR LLVM dialect types.
52-
class TypeFromLLVMIRTranslatorImpl {
53-
public:
54-
/// Constructs a class creating types in the given MLIR context.
55-
TypeFromLLVMIRTranslatorImpl(MLIRContext &context) : context(context) {}
56-
57-
/// Translates the given type.
58-
Type translateType(llvm::Type *type) {
59-
if (knownTranslations.count(type))
60-
return knownTranslations.lookup(type);
61-
62-
Type translated =
63-
llvm::TypeSwitch<llvm::Type *, Type>(type)
64-
.Case<llvm::ArrayType, llvm::FunctionType, llvm::IntegerType,
65-
llvm::PointerType, llvm::StructType, llvm::FixedVectorType,
66-
llvm::ScalableVectorType>(
67-
[this](auto *type) { return this->translate(type); })
68-
.Default([this](llvm::Type *type) {
69-
return translatePrimitiveType(type);
70-
});
71-
knownTranslations.try_emplace(type, translated);
72-
return translated;
73-
}
74-
75-
private:
76-
/// Translates the given primitive, i.e. non-parametric in MLIR nomenclature,
77-
/// type.
78-
Type translatePrimitiveType(llvm::Type *type) {
79-
if (type->isVoidTy())
80-
return LLVM::LLVMVoidType::get(&context);
81-
if (type->isHalfTy())
82-
return Float16Type::get(&context);
83-
if (type->isBFloatTy())
84-
return BFloat16Type::get(&context);
85-
if (type->isFloatTy())
86-
return Float32Type::get(&context);
87-
if (type->isDoubleTy())
88-
return Float64Type::get(&context);
89-
if (type->isFP128Ty())
90-
return Float128Type::get(&context);
91-
if (type->isX86_FP80Ty())
92-
return Float80Type::get(&context);
93-
if (type->isPPC_FP128Ty())
94-
return LLVM::LLVMPPCFP128Type::get(&context);
95-
if (type->isX86_MMXTy())
96-
return LLVM::LLVMX86MMXType::get(&context);
97-
if (type->isLabelTy())
98-
return LLVM::LLVMLabelType::get(&context);
99-
if (type->isMetadataTy())
100-
return LLVM::LLVMMetadataType::get(&context);
101-
llvm_unreachable("not a primitive type");
102-
}
103-
104-
/// Translates the given array type.
105-
Type translate(llvm::ArrayType *type) {
106-
return LLVM::LLVMArrayType::get(translateType(type->getElementType()),
107-
type->getNumElements());
108-
}
109-
110-
/// Translates the given function type.
111-
Type translate(llvm::FunctionType *type) {
112-
SmallVector<Type, 8> paramTypes;
113-
translateTypes(type->params(), paramTypes);
114-
return LLVM::LLVMFunctionType::get(translateType(type->getReturnType()),
115-
paramTypes, type->isVarArg());
116-
}
117-
118-
/// Translates the given integer type.
119-
Type translate(llvm::IntegerType *type) {
120-
return IntegerType::get(&context, type->getBitWidth());
121-
}
122-
123-
/// Translates the given pointer type.
124-
Type translate(llvm::PointerType *type) {
125-
return LLVM::LLVMPointerType::get(translateType(type->getElementType()),
126-
type->getAddressSpace());
127-
}
128-
129-
/// Translates the given structure type.
130-
Type translate(llvm::StructType *type) {
131-
SmallVector<Type, 8> subtypes;
132-
if (type->isLiteral()) {
133-
translateTypes(type->subtypes(), subtypes);
134-
return LLVM::LLVMStructType::getLiteral(&context, subtypes,
135-
type->isPacked());
136-
}
137-
138-
if (type->isOpaque())
139-
return LLVM::LLVMStructType::getOpaque(type->getName(), &context);
140-
141-
LLVM::LLVMStructType translated =
142-
LLVM::LLVMStructType::getIdentified(&context, type->getName());
143-
knownTranslations.try_emplace(type, translated);
144-
translateTypes(type->subtypes(), subtypes);
145-
LogicalResult bodySet = translated.setBody(subtypes, type->isPacked());
146-
assert(succeeded(bodySet) &&
147-
"could not set the body of an identified struct");
148-
(void)bodySet;
149-
return translated;
150-
}
151-
152-
/// Translates the given fixed-vector type.
153-
Type translate(llvm::FixedVectorType *type) {
154-
return LLVM::getFixedVectorType(translateType(type->getElementType()),
155-
type->getNumElements());
156-
}
157-
158-
/// Translates the given scalable-vector type.
159-
Type translate(llvm::ScalableVectorType *type) {
160-
return LLVM::LLVMScalableVectorType::get(
161-
translateType(type->getElementType()), type->getMinNumElements());
162-
}
163-
164-
/// Translates a list of types.
165-
void translateTypes(ArrayRef<llvm::Type *> types,
166-
SmallVectorImpl<Type> &result) {
167-
result.reserve(result.size() + types.size());
168-
for (llvm::Type *type : types)
169-
result.push_back(translateType(type));
170-
}
171-
172-
/// Map of known translations. Serves as a cache and as recursion stopper for
173-
/// translating recursive structs.
174-
llvm::DenseMap<llvm::Type *, Type> knownTranslations;
175-
176-
/// The context in which MLIR types are created.
177-
MLIRContext &context;
178-
};
179-
} // end namespace detail
180-
181-
/// Utility class to translate LLVM IR types to the MLIR LLVM dialect. Stores
182-
/// the translation state, in particular any identified structure types that are
183-
/// reused across translations.
184-
class TypeFromLLVMIRTranslator {
185-
public:
186-
TypeFromLLVMIRTranslator(MLIRContext &context);
187-
~TypeFromLLVMIRTranslator();
188-
189-
/// Translates the given LLVM IR type to the MLIR LLVM dialect.
190-
Type translateType(llvm::Type *type);
191-
192-
private:
193-
/// Private implementation.
194-
std::unique_ptr<detail::TypeFromLLVMIRTranslatorImpl> impl;
195-
};
196-
197-
} // end namespace LLVM
198-
} // end namespace mlir
199-
200-
LLVM::TypeFromLLVMIRTranslator::TypeFromLLVMIRTranslator(MLIRContext &context)
201-
: impl(new detail::TypeFromLLVMIRTranslatorImpl(context)) {}
202-
203-
LLVM::TypeFromLLVMIRTranslator::~TypeFromLLVMIRTranslator() {}
204-
205-
Type LLVM::TypeFromLLVMIRTranslator::translateType(llvm::Type *type) {
206-
return impl->translateType(type);
207-
}
208-
20949
// Handles importing globals and functions from an LLVM module.
21050
namespace {
21151
class Importer {

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "mlir/IR/RegionGraphTraits.h"
2424
#include "mlir/Support/LLVM.h"
2525
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
26-
#include "mlir/Target/LLVMIR/TypeTranslation.h"
26+
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
2727
#include "llvm/ADT/TypeSwitch.h"
2828

2929
#include "llvm/ADT/PostOrderIterator.h"

0 commit comments

Comments
 (0)