How can i create memory alloc by memref.alloc for dynamic dimensions using c++

This is my c++ code.I know it will be wrong to write this way. The memory created will be 0x0x0x0x0xi8.
weightsShape is a dynamic dimension obtained through memref.dim.I want to be able to alloc memref<?x?x?x?xi8> memory.But I don’t know how to get the right memrefType,the first argument to MemRef::get is always an ArrayRef.I don’t know how to solve this problem.If anyone can help me, I would be grateful.Thanks!

MemRefType weightsMatType = MemRefType::get({0, 0, 0, 0}, rewriter.getI8Type());
Value weightsMat = rewriter.create<memref::AllocOp>(loc, weightsMatType, weightsShape);

You can try using ShapedType::kDynamic as done in llvm-project/Promotion.cpp at main · llvm/llvm-project · GitHub

  auto dynamicBufferType =
      MemRefType::get(ShapedType::kDynamic, b.getIntegerType(8));

Here it is 1D but I believe it will work if you just replace each of your 0s with it.

1 Like

Thank you for your reply!