[mlir] recursively convert builtin types to LLVM when possible

Given that LLVM dialect types may now optionally contain types from other
dialects, which itself is motivated by dialect interoperability and progressive
lowering, the conversion should no longer assume that the outermost LLVM
dialect type can be left as is. Instead, it should inspect the types it
contains and attempt to convert them to the LLVM dialect. Introduce this
capability for LLVM array, pointer and structure types. Only literal structures
are currently supported as handling identified structures requires the
converison infrastructure to have a mechanism for avoiding infite recursion in
case of recursive types.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D112550
This commit is contained in:
Alex Zinenko 2021-10-27 10:28:29 +02:00
parent d96656ca90
commit e64c76672f
3 changed files with 76 additions and 1 deletions

View File

@ -38,12 +38,53 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx,
[&](UnrankedMemRefType type) { return convertUnrankedMemRefType(type); });
addConversion([&](VectorType type) { return convertVectorType(type); });
// LLVM-compatible types are legal, so add a pass-through conversion.
// LLVM-compatible types are legal, so add a pass-through conversion. Do this
// before the conversions below since conversions are attempted in reverse
// order and those should take priority.
addConversion([](Type type) {
return LLVM::isCompatibleType(type) ? llvm::Optional<Type>(type)
: llvm::None;
});
// LLVM container types may (recursively) contain other types that must be
// converted even when the outer type is compatible.
addConversion([&](LLVM::LLVMPointerType type) -> llvm::Optional<Type> {
if (auto pointee = convertType(type.getElementType()))
return LLVM::LLVMPointerType::get(pointee, type.getAddressSpace());
return llvm::None;
});
addConversion([&](LLVM::LLVMStructType type) -> llvm::Optional<Type> {
// TODO: handle conversion of identified structs, which may be recursive.
if (type.isIdentified())
return type;
SmallVector<Type> convertedSubtypes;
convertedSubtypes.reserve(type.getBody().size());
if (failed(convertTypes(type.getBody(), convertedSubtypes)))
return llvm::None;
return LLVM::LLVMStructType::getLiteral(type.getContext(),
convertedSubtypes, type.isPacked());
});
addConversion([&](LLVM::LLVMArrayType type) -> llvm::Optional<Type> {
if (auto element = convertType(type.getElementType()))
return LLVM::LLVMArrayType::get(element, type.getNumElements());
return llvm::None;
});
addConversion([&](LLVM::LLVMFunctionType type) -> llvm::Optional<Type> {
Type convertedResType = convertType(type.getReturnType());
if (!convertedResType)
return llvm::None;
SmallVector<Type> convertedArgTypes;
convertedArgTypes.reserve(type.getNumParams());
if (failed(convertTypes(type.getParams(), convertedArgTypes)))
return llvm::None;
return LLVM::LLVMFunctionType::get(convertedResType, convertedArgTypes,
type.isVarArg());
});
// Materialization for memrefs creates descriptor structs from individual
// values constituting them, when descriptors are used, i.e. more than one
// value represents a memref.

View File

@ -0,0 +1,31 @@
// RUN: mlir-opt -test-convert-call-op %s | FileCheck %s
// CHECK-LABEL: @ptr
// CHECK: !llvm.ptr<i42>
func private @ptr() -> !llvm.ptr<!test.smpla>
// CHECK-LABEL: @ptr_ptr()
// CHECK: !llvm.ptr<ptr<i42>>
func private @ptr_ptr() -> !llvm.ptr<!llvm.ptr<!test.smpla>>
// CHECK-LABEL: @struct_ptr()
// CHECK: !llvm.struct<(ptr<i42>)>
func private @struct_ptr() -> !llvm.struct<(ptr<!test.smpla>)>
// CHECK-LABEL: @named_struct_ptr()
// CHECK: !llvm.struct<"named", (ptr<!test.smpla>)>
func private @named_struct_ptr() -> !llvm.struct<"named", (ptr<!test.smpla>)>
// CHECK-LABEL: @array_ptr()
// CHECK: !llvm.array<10 x ptr<i42>>
func private @array_ptr() -> !llvm.array<10 x ptr<!test.smpla>>
// CHECK-LABEL: @func()
// CHECK: !llvm.ptr<func<i42 (i42)>>
func private @func() -> !llvm.ptr<!llvm.func<!test.smpla (!test.smpla)>>
// TODO: support conversion of recursive types in the conversion infra.
// CHECK-LABEL: @named_recursive()
// CHECK: !llvm.struct<"recursive", (ptr<!test.smpla>, ptr<struct<"recursive">>)>
func private @named_recursive() -> !llvm.struct<"recursive", (ptr<!test.smpla>, ptr<struct<"recursive">>)>

View File

@ -52,6 +52,9 @@ public:
typeConverter.addConversion([&](test::TestType type) {
return LLVM::LLVMPointerType::get(IntegerType::get(m.getContext(), 8));
});
typeConverter.addConversion([&](test::SimpleAType type) {
return IntegerType::get(type.getContext(), 42);
});
// Populate patterns.
RewritePatternSet patterns(m.getContext());