Skip to content

Commit 1001d6a

Browse files
authored
[MLIR][LLVM] Add import-structs-as-literals flag to the IR import (#140098)
This commit introduces the `import-structs-as-literals` option to the MLIR import. This ensures that all struct types are imported as literal structs, even when they are named in LLVM IR.
1 parent a83668c commit 1001d6a

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

mlir/include/mlir/Target/LLVMIR/Import.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ class ModuleOp;
4646
/// registered an explicit intrinsic operation. Warning: passes that rely on
4747
/// matching explicit intrinsic operations may not work properly if this flag is
4848
/// enabled.
49+
/// The `importStructsAsLiterals` flag (default off) ensures that all structs
50+
/// are imported as literal structs, even when they are named in the LLVM
51+
/// module.
4952
OwningOpRef<ModuleOp> translateLLVMIRToModule(
5053
std::unique_ptr<llvm::Module> llvmModule, MLIRContext *context,
5154
bool emitExpensiveWarnings = true, bool dropDICompositeTypeElements = false,
52-
bool loadAllDialects = true, bool preferUnregisteredIntrinsics = false);
55+
bool loadAllDialects = true, bool preferUnregisteredIntrinsics = false,
56+
bool importStructsAsLiterals = false);
5357

5458
/// Translate the given LLVM data layout into an MLIR equivalent using the DLTI
5559
/// dialect.

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ModuleImport {
4848
public:
4949
ModuleImport(ModuleOp mlirModule, std::unique_ptr<llvm::Module> llvmModule,
5050
bool emitExpensiveWarnings, bool importEmptyDICompositeTypes,
51-
bool preferUnregisteredIntrinsics);
51+
bool preferUnregisteredIntrinsics, bool importStructsAsLiterals);
5252

5353
/// Calls the LLVMImportInterface initialization that queries the registered
5454
/// dialect interfaces for the supported LLVM IR intrinsics and metadata kinds

mlir/include/mlir/Target/LLVMIR/TypeFromLLVM.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include <memory>
1818

1919
namespace llvm {
20-
class DataLayout;
21-
class LLVMContext;
2220
class Type;
2321
} // namespace llvm
2422

@@ -38,7 +36,8 @@ class TypeFromLLVMIRTranslatorImpl;
3836
/// reused across translations.
3937
class TypeFromLLVMIRTranslator {
4038
public:
41-
TypeFromLLVMIRTranslator(MLIRContext &context);
39+
TypeFromLLVMIRTranslator(MLIRContext &context,
40+
bool importStructsAsLiterals = false);
4241
~TypeFromLLVMIRTranslator();
4342

4443
/// Translates the given LLVM IR type to the MLIR LLVM dialect.

mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ void registerFromLLVMIRTranslation() {
4444
"of using dialect supported intrinsics"),
4545
llvm::cl::init(false));
4646

47+
static llvm::cl::opt<bool> importStructsAsLiterals(
48+
"import-structs-as-literals",
49+
llvm::cl::desc("Controls if structs should be imported as literal "
50+
"structs, i.e., nameless structs."),
51+
llvm::cl::init(false));
52+
4753
TranslateToMLIRRegistration registration(
4854
"import-llvm", "Translate LLVMIR to MLIR",
4955
[](llvm::SourceMgr &sourceMgr,
@@ -70,7 +76,7 @@ void registerFromLLVMIRTranslation() {
7076
return translateLLVMIRToModule(
7177
std::move(llvmModule), context, emitExpensiveWarnings,
7278
dropDICompositeTypeElements, /*loadAllDialects=*/true,
73-
preferUnregisteredIntrinsics);
79+
preferUnregisteredIntrinsics, importStructsAsLiterals);
7480
},
7581
[](DialectRegistry &registry) {
7682
// Register the DLTI dialect used to express the data layout

mlir/lib/Target/LLVMIR/ModuleImport.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ ModuleImport::ModuleImport(ModuleOp mlirModule,
164164
std::unique_ptr<llvm::Module> llvmModule,
165165
bool emitExpensiveWarnings,
166166
bool importEmptyDICompositeTypes,
167-
bool preferUnregisteredIntrinsics)
167+
bool preferUnregisteredIntrinsics,
168+
bool importStructsAsLiterals)
168169
: builder(mlirModule->getContext()), context(mlirModule->getContext()),
169170
mlirModule(mlirModule), llvmModule(std::move(llvmModule)),
170171
iface(mlirModule->getContext()),
171-
typeTranslator(*mlirModule->getContext()),
172+
typeTranslator(*mlirModule->getContext(), importStructsAsLiterals),
172173
debugImporter(std::make_unique<DebugImporter>(
173174
mlirModule, importEmptyDICompositeTypes)),
174175
loopAnnotationImporter(
@@ -3118,7 +3119,8 @@ ModuleImport::translateDereferenceableAttr(const llvm::MDNode *node,
31183119
OwningOpRef<ModuleOp> mlir::translateLLVMIRToModule(
31193120
std::unique_ptr<llvm::Module> llvmModule, MLIRContext *context,
31203121
bool emitExpensiveWarnings, bool dropDICompositeTypeElements,
3121-
bool loadAllDialects, bool preferUnregisteredIntrinsics) {
3122+
bool loadAllDialects, bool preferUnregisteredIntrinsics,
3123+
bool importStructsAsLiterals) {
31223124
// Preload all registered dialects to allow the import to iterate the
31233125
// registered LLVMImportDialectInterface implementations and query the
31243126
// supported LLVM IR constructs before starting the translation. Assumes the
@@ -3136,7 +3138,8 @@ OwningOpRef<ModuleOp> mlir::translateLLVMIRToModule(
31363138

31373139
ModuleImport moduleImport(module.get(), std::move(llvmModule),
31383140
emitExpensiveWarnings, dropDICompositeTypeElements,
3139-
preferUnregisteredIntrinsics);
3141+
preferUnregisteredIntrinsics,
3142+
importStructsAsLiterals);
31403143
if (failed(moduleImport.initializeImportInterface()))
31413144
return {};
31423145
if (failed(moduleImport.convertDataLayout()))

mlir/lib/Target/LLVMIR/TypeFromLLVM.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "mlir/IR/MLIRContext.h"
1313

1414
#include "llvm/ADT/TypeSwitch.h"
15-
#include "llvm/IR/DataLayout.h"
1615
#include "llvm/IR/DerivedTypes.h"
1716
#include "llvm/IR/Type.h"
1817

@@ -25,7 +24,9 @@ namespace detail {
2524
class TypeFromLLVMIRTranslatorImpl {
2625
public:
2726
/// Constructs a class creating types in the given MLIR context.
28-
TypeFromLLVMIRTranslatorImpl(MLIRContext &context) : context(context) {}
27+
TypeFromLLVMIRTranslatorImpl(MLIRContext &context,
28+
bool importStructsAsLiterals)
29+
: context(context), importStructsAsLiterals(importStructsAsLiterals) {}
2930

3031
/// Translates the given type.
3132
Type translateType(llvm::Type *type) {
@@ -103,7 +104,7 @@ class TypeFromLLVMIRTranslatorImpl {
103104
/// Translates the given structure type.
104105
Type translate(llvm::StructType *type) {
105106
SmallVector<Type, 8> subtypes;
106-
if (type->isLiteral()) {
107+
if (type->isLiteral() || importStructsAsLiterals) {
107108
translateTypes(type->subtypes(), subtypes);
108109
return LLVM::LLVMStructType::getLiteral(&context, subtypes,
109110
type->isPacked());
@@ -132,7 +133,7 @@ class TypeFromLLVMIRTranslatorImpl {
132133
Type translate(llvm::ScalableVectorType *type) {
133134
return VectorType::get(type->getMinNumElements(),
134135
translateType(type->getElementType()),
135-
/*scalable=*/true);
136+
/*scalableDims=*/true);
136137
}
137138

138139
/// Translates the given target extension type.
@@ -158,14 +159,20 @@ class TypeFromLLVMIRTranslatorImpl {
158159

159160
/// The context in which MLIR types are created.
160161
MLIRContext &context;
162+
163+
/// Controls if structs should be imported as literal structs, i.e., nameless
164+
/// structs.
165+
bool importStructsAsLiterals;
161166
};
162167

163168
} // namespace detail
164169
} // namespace LLVM
165170
} // namespace mlir
166171

167-
LLVM::TypeFromLLVMIRTranslator::TypeFromLLVMIRTranslator(MLIRContext &context)
168-
: impl(new detail::TypeFromLLVMIRTranslatorImpl(context)) {}
172+
LLVM::TypeFromLLVMIRTranslator::TypeFromLLVMIRTranslator(
173+
MLIRContext &context, bool importStructsAsLiterals)
174+
: impl(std::make_unique<detail::TypeFromLLVMIRTranslatorImpl>(
175+
context, importStructsAsLiterals)) {}
169176

170177
LLVM::TypeFromLLVMIRTranslator::~TypeFromLLVMIRTranslator() = default;
171178

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: mlir-translate -import-llvm -import-structs-as-literals -split-input-file %s | FileCheck %s
2+
3+
%named = type {i32, i8, i16, i32}
4+
5+
; CHECK: @named
6+
; CHECK-SAME: !llvm.struct<(i32, i8, i16, i32)>
7+
@named = external global %named
8+
9+
%opaque = type opaque
10+
11+
; CHECK: @opaque
12+
; CHECK-SAME: !llvm.struct<()>
13+
@opaque = external global %opaque

0 commit comments

Comments
 (0)