MemRefDescriptor for ranked memref is not good

Recently I want to use cpu-runner to run the model which has been lower to LLVM dialect, but I found that there is something unreasonable here for MemRefDescriptor. we should create input args based MemRefDescriptor struct and run the LLVM dialect with cpu-runner and these input args. MemRefDescriptor as shown below

template<typename T, size_t N>
struct MemRefDescriptor {
  T *allocated;
  T *aligned;
  intptr_t offset;
  intptr_t sizes[N];
  intptr_t strides[N];
};

N must to be a const value. but is not very good because we always get the shape and stride in running time when run a model. Am I right and any one can give me some advice,thank you!

What is your question here?

You need to specify sizes of your data somewhere, it is not unreasonable. One does the same for strings in C++ (not in C) by storing their length.

Now this data structure for MemRefDescriptor can cause redundant code or redundant memory. For example, add op can operate on any shape tensor and I don’t know the exactly shape in compilation time. so I need give the as larger as possible N for MemRefDescriptor or I writer many switch case to create different MemRefDescriptor for different shape.
So maybe we can use pointer and length of array, not array. As follows

template<typename T, size_t N>
struct MemRefDescriptor {
T allocated;
T aligned;
intptr_t offset;
intptr_t N;
intptr_t
sizes;
intptr_t
strides;
};

Thank you very much

Well, if you are using a ranked descriptor, the rank (N) is known at compile time by definition. If you need operations that work on unranked memrefs, there is the unranked descriptor that contains the rank and a pointer to the appropriately-ranked descriptor: LLVM IR Target - MLIR.

Note that tensors and memrefs are different types. The add operation you are using as example does not work on memrefs. The descriptors are only for memrefs.

1 Like

Yes, this proposed unification of ranked and unranked descriptors has been discussed a few times.
I think the latest person to touch things related to this was @tpopp ?