Skip to content

Commit

Permalink
Fix identified struct to opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanradanov committed Oct 6, 2023
1 parent 5e519a4 commit fdbe260
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions lib/polygeist/Passes/ConvertToOpaquePtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "polygeist/Passes/Passes.h"
#include "polygeist/Passes/Utils.h"
#include "llvm/ADT/STLExtras.h"
#include <memory>

using namespace mlir;
using namespace polygeist;
Expand Down Expand Up @@ -170,7 +171,8 @@ struct AllocaConversion : public OpConversionPattern<LLVM::AllocaOp> {
Operation *rewritten;
auto resTy = op.getRes().getType();
assert(!resTy.isOpaque());
TypeAttr elty = TypeAttr::get(resTy.getElementType());
TypeAttr elty =
TypeAttr::get(getTypeConverter()->convertType(resTy.getElementType()));
if (convertPtrsToOpaque(op, rewritten, elty, adaptor.getOperands(),
rewriter, getTypeConverter())
.failed())
Expand All @@ -188,10 +190,11 @@ struct GEPConversion : public OpConversionPattern<LLVM::GEPOp> {
Operation *rewritten;
TypeAttr elty = nullptr;
if (!op->getAttr(kElemTypeAttrName))
elty = TypeAttr::get(op.getOperand(0)
.getType()
.dyn_cast<LLVM::LLVMPointerType>()
.getElementType());
elty = TypeAttr::get(
getTypeConverter()->convertType(op.getOperand(0)
.getType()
.dyn_cast<LLVM::LLVMPointerType>()
.getElementType()));
if (convertPtrsToOpaque(op, rewritten, elty, adaptor.getOperands(),
rewriter, getTypeConverter())
.failed())
Expand Down Expand Up @@ -219,6 +222,7 @@ struct ConvertToOpaquePtrPass
return isOpOpaque(op);
});

std::map<StringRef, LLVM::LLVMStructType> typeCache;
TypeConverter converter;
converter.addConversion([&](Type ty) -> Type {
if (auto pt = ty.dyn_cast<LLVM::LLVMPointerType>()) {
Expand All @@ -229,11 +233,30 @@ struct ConvertToOpaquePtrPass
converter.convertType(mt.getElementType()),
mt.getLayout(), mt.getMemorySpace());
} else if (auto st = ty.dyn_cast<LLVM::LLVMStructType>()) {
auto key = st.getName();
if (typeCache.find(key) != typeCache.end()) {
return typeCache[key];
}
SmallVector<Type> bodyTypes;
for (auto ty : st.getBody())
bodyTypes.push_back(converter.convertType(ty));
return LLVM::LLVMStructType::getLiteral(&getContext(), bodyTypes,
st.isPacked());
if (st.isIdentified()) {
typeCache[key] = LLVM::LLVMStructType::getIdentified(
&getContext(), "opaque@" + st.getName().str());
}
for (auto ty : st.getBody()) {
if (typeCache.find(key) != typeCache.end()) {
bodyTypes.push_back(typeCache[key]);
} else {
bodyTypes.push_back(converter.convertType(ty));
}
}
if (st.isIdentified()) {
auto res = typeCache[key].setBody(bodyTypes, st.isPacked());
assert(res.succeeded());
return typeCache[key];
} else {
return LLVM::LLVMStructType::getLiteral(&getContext(), bodyTypes,
st.isPacked());
}
} else if (auto at = ty.dyn_cast<LLVM::LLVMArrayType>()) {
return LLVM::LLVMArrayType::get(
converter.convertType(at.getElementType()), at.getNumElements());
Expand Down

0 comments on commit fdbe260

Please sign in to comment.