Hi All,
I am trying to read data using an external cpp file. The cpp file works fine, however, while trying to print the data using MLIR, it gives 0 for the array.
External cpp file -
extern "C" int readData(std::vector<int> vect)
{
std::string myText;
// Read from the text file
std::ifstream MyReadFile("filename.txt");
while (getline(MyReadFile, myText))
{
vect.push_back(stoi(myText));
}
MyReadFile.close();
return 0;
}
MLIR code to read the file and print -
int generateMLIRForQ6(mlir::MLIRContext &context, mlir::OwningOpRef<mlir::ModuleOp> &module, Location loc){
OpBuilder builder(&context);
auto funcType = builder.getFunctionType(std::nullopt, std::nullopt);
auto funcn = builder.create<func::FuncOp>(loc, "main", funcType);
Type elementType = builder.getI64Type();
auto memTp = MemRefType::get({10}, elementType);
auto loadFuncnType = builder.getFunctionType(memTp, elementType);
auto loadFunc = builder.create<func::FuncOp>(loc, "readData", loadFuncnType);
loadFunc.setPrivate();
// auto memTp2 = MemRefType::get({10, 3}, elementType);
UnrankedMemRefType castMemrefType = UnrankedMemRefType::get(memTp.getElementType(), /*memorySpace=*/0);
auto printMemRefFuncnType = builder.getFunctionType(castMemrefType, std::nullopt);
auto printMemRefFunc = builder.create<func::FuncOp>(loc, "printMemrefI64", printMemRefFuncnType);
printMemRefFunc.setPrivate();
Block *entryBlock = funcn.addEntryBlock();
// Region *funcBody = entryBlock->getParent();
builder.setInsertionPointToEnd(entryBlock);
Value mem = builder.create<memref::AllocOp>(loc, memTp);
auto x = builder.create<func::CallOp>(loc, loadFunc, ValueRange{mem});
Value castedMem = builder.create<memref::CastOp>(loc, castMemrefType, mem);
auto printFuncCall = builder.create<func::CallOp>(loc, printMemRefFunc, castedMem);
builder.create<func::ReturnOp>(loc);
module->push_back(funcn);
module->push_back(loadFunc);
module->push_back(printMemRefFunc);
return 0;
}
Output it produced -
Unranked Memref base@ = 0x55b92baa0eb0 rank = 1 offset = 0 sizes = [10] strides = [1] data =
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I could not understand where it is failing. Would you please help to figure out the issue.
Thanks in advance.
Regards,
Sudip