Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[mlir] support recursive type conversion of named LLVM structs
A previous commit added support for converting elemental types contained in
LLVM dialect types in case they were not compatible with the LLVM dialect. It
was missing support for named structs as they could be recursive, which was not
supported by the conversion infra. Now that it is, add support for converting
such named structs.

Depends On D113579

Reviewed By: wsmoses

Differential Revision: https://reviews.llvm.org/D113580
  • Loading branch information
ftynse committed Dec 3, 2021
1 parent 5e1c038 commit 9dd1f8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
37 changes: 31 additions & 6 deletions mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
Expand Up @@ -53,18 +53,43 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx,
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;
addConversion([&](LLVM::LLVMStructType type, SmallVectorImpl<Type> &results,
ArrayRef<Type> callStack) -> llvm::Optional<LogicalResult> {
if (type.isIdentified()) {
auto convertedType = LLVM::LLVMStructType::getIdentified(
type.getContext(), ("_Converted_" + type.getName()).str());
unsigned counter = 1;
while (convertedType.isInitialized()) {
assert(counter != UINT_MAX &&
"about to overflow struct renaming counter in conversion");
convertedType = LLVM::LLVMStructType::getIdentified(
type.getContext(),
("_Converted_" + std::to_string(counter) + type.getName()).str());
}
if (llvm::count(callStack, type) > 1) {
results.push_back(convertedType);
return success();
}

SmallVector<Type> convertedElemTypes;
convertedElemTypes.reserve(type.getBody().size());
if (failed(convertTypes(type.getBody(), convertedElemTypes)))
return llvm::None;

if (failed(convertedType.setBody(convertedElemTypes, type.isPacked())))
return failure();
results.push_back(convertedType);
return success();
}

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());
results.push_back(LLVM::LLVMStructType::getLiteral(
type.getContext(), convertedSubtypes, type.isPacked()));
return success();
});
addConversion([&](LLVM::LLVMArrayType type) -> llvm::Optional<Type> {
if (auto element = convertType(type.getElementType()))
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Conversion/StandardToLLVM/convert-types.mlir
Expand Up @@ -13,7 +13,7 @@ func private @ptr_ptr() -> !llvm.ptr<!llvm.ptr<!test.smpla>>
func private @struct_ptr() -> !llvm.struct<(ptr<!test.smpla>)>

// CHECK-LABEL: @named_struct_ptr()
// CHECK: !llvm.struct<"named", (ptr<!test.smpla>)>
// CHECK: !llvm.struct<"_Converted_named", (ptr<i42>)>
func private @named_struct_ptr() -> !llvm.struct<"named", (ptr<!test.smpla>)>

// CHECK-LABEL: @array_ptr()
Expand All @@ -26,6 +26,6 @@ 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">>)>
// CHECK: !llvm.struct<"_Converted_recursive", (ptr<i42>, ptr<struct<"_Converted_recursive">>)>
func private @named_recursive() -> !llvm.struct<"recursive", (ptr<!test.smpla>, ptr<struct<"recursive">>)>

0 comments on commit 9dd1f8d

Please sign in to comment.