Skip to content

Commit 2792fe2

Browse files
sjalanderhavedphate
authored
Support for structs in MLIR (phate#1205)
Co-authored-by: HKrogstie <krogstie.havard@gmail.com> Co-authored-by: Nico Reissmann <nico.reissmann@gmail.com>
1 parent c3fb20c commit 2792fe2

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

jlm/mlir/backend/JlmToMlirConverter.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,35 @@ JlmToMlirConverter::ConvertType(const rvsdg::Type & type)
10461046
{
10471047
return Builder_->getType<::mlir::NoneType>();
10481048
}
1049+
else if (auto structType = dynamic_cast<const llvm::StructType *>(&type))
1050+
{
1051+
std::vector<::mlir::Type> elements;
1052+
for (size_t i = 0; i < structType->numElements(); i++)
1053+
{
1054+
elements.push_back(ConvertType(*structType->getElementType(i)));
1055+
}
1056+
1057+
if (structType->IsLiteral())
1058+
{
1059+
return ::mlir::LLVM::LLVMStructType::getLiteral(
1060+
Builder_->getContext(),
1061+
elements,
1062+
structType->IsPacked());
1063+
}
1064+
else
1065+
{
1066+
auto mlirStructType = ::mlir::LLVM::LLVMStructType::getIdentified(
1067+
Builder_->getContext(),
1068+
structType->GetName());
1069+
if (mlirStructType.isInitialized())
1070+
return mlirStructType;
1071+
if (mlirStructType.setBody(elements, structType->IsPacked()).failed())
1072+
{
1073+
throw util::Error("Not able to set the body of struct in the MLIR backend.");
1074+
}
1075+
return mlirStructType;
1076+
}
1077+
}
10491078
else
10501079
{
10511080
auto message = util::strfmt("Type conversion not implemented: ", type.debug_string());

jlm/mlir/frontend/MlirToJlmConverter.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,35 @@ MlirToJlmConverter::ConvertType(const ::mlir::Type & type)
12021202
// RVSDG does not support indices, which are modeled as integers
12031203
return rvsdg::BitType::Create(MlirToJlmConverter::GetIndexBitWidth());
12041204
}
1205+
else if (auto structType = ::mlir::dyn_cast<::mlir::LLVM::LLVMStructType>(type))
1206+
{
1207+
if (StructTypeMap_.HasKey(&structType))
1208+
{
1209+
return StructTypeMap_.LookupKey(&structType);
1210+
}
1211+
1212+
std::vector<std::shared_ptr<const rvsdg::Type>> types;
1213+
for (auto element : structType.getBody())
1214+
{
1215+
types.push_back(ConvertType(element));
1216+
}
1217+
1218+
std::shared_ptr<const llvm::StructType> jlmStructType;
1219+
if (structType.isIdentified())
1220+
{
1221+
jlmStructType = jlm::llvm::StructType::CreateIdentified(
1222+
structType.getName().str(),
1223+
types,
1224+
structType.isPacked());
1225+
}
1226+
else
1227+
{
1228+
jlmStructType = jlm::llvm::StructType::CreateLiteral(types, structType.isPacked());
1229+
}
1230+
1231+
StructTypeMap_.Insert(&structType, jlmStructType);
1232+
return jlmStructType;
1233+
}
12051234
else
12061235
{
12071236
type.dump();

jlm/mlir/frontend/MlirToJlmConverter.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <jlm/rvsdg/bitstring/constant.hpp>
1515
#include <jlm/rvsdg/gamma.hpp>
1616
#include <jlm/rvsdg/theta.hpp>
17+
#include <jlm/util/BijectiveMap.hpp>
1718

1819
#include <JLM/JLMDialect.h>
1920
#include <JLM/JLMOps.h>
@@ -227,7 +228,7 @@ class MlirToJlmConverter final
227228
* \param type The MLIR type to be converted.
228229
* \result The converted RVSDG type.
229230
*/
230-
static std::shared_ptr<const rvsdg::Type>
231+
std::shared_ptr<const rvsdg::Type>
231232
ConvertType(const ::mlir::Type & type);
232233

233234
/**
@@ -247,6 +248,8 @@ class MlirToJlmConverter final
247248
}
248249

249250
std::unique_ptr<::mlir::MLIRContext> Context_;
251+
util::BijectiveMap<::mlir::LLVM::LLVMStructType *, std::shared_ptr<const llvm::StructType>>
252+
StructTypeMap_;
250253
};
251254

252255
} // namespace jlm::mlir

0 commit comments

Comments
 (0)