Skip to content

[mlir][gpu] Generate multiple rank-specializations for tensor map cre… #74082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,24 @@ extern "C" MLIR_CUDA_WRAPPERS_EXPORT void mgpuTensorMapEncodeTiled(
elementStrides[4], interleave, swizzle, l2Promotion, oobFill);
}

namespace {

template <int rank>
void mgpuGetMemRefDataAndShape(void *raw_descriptor, char **addr,
uint64_t *globalDim) {
auto descriptor =
reinterpret_cast<StridedMemRefType<char, rank> *>(raw_descriptor);
*addr = descriptor->data;
for (int i = 0; i < rank; ++i) {
globalDim[i] = static_cast<uint64_t>(descriptor->sizes[rank - i - 1]);
}
}

} // namespace

extern "C" MLIR_CUDA_WRAPPERS_EXPORT void *mgpuTensorMapEncodeTiledMemref(
int64_t tensorRank, // Dimensionality of tensor
StridedMemRefType<char, 1> *descriptor, // Starting address
void *ranked_descriptor, // Ranked MemRef descriptor
const CUtensorMapDataType tensorDataType, // Stride size (in bytes)
CUtensorMapInterleave interleave, // Type of interleaved layout
CUtensorMapSwizzle swizzle, // Bank swizzling pattern
Expand All @@ -435,17 +450,39 @@ extern "C" MLIR_CUDA_WRAPPERS_EXPORT void *mgpuTensorMapEncodeTiledMemref(
) {
CUtensorMap tensorMap;

auto *globalAddress = descriptor->data;
uint32_t boxDim[5] = {1, 1, 1, 1, 1}, elementStrides[5] = {1, 1, 1, 1, 1};
uint64_t globalDim[5] = {1, 1, 1, 1, 1}, globalStrides[5] = {0};
uint32_t tensorRank32 = uint32_t(tensorRank);

char *globalAddress = nullptr;
switch (tensorRank) {
case 1:
mgpuGetMemRefDataAndShape<1>(ranked_descriptor, &globalAddress, globalDim);
break;
case 2:
mgpuGetMemRefDataAndShape<2>(ranked_descriptor, &globalAddress, globalDim);
break;
case 3:
mgpuGetMemRefDataAndShape<3>(ranked_descriptor, &globalAddress, globalDim);
break;
case 4:
mgpuGetMemRefDataAndShape<4>(ranked_descriptor, &globalAddress, globalDim);
break;
case 5:
mgpuGetMemRefDataAndShape<5>(ranked_descriptor, &globalAddress, globalDim);
break;
default:
fprintf(
stderr,
"'mgpuTensorMapEncodeTiledMemref' failed with 'rank is too high'\n");
return NULL;
}

static const int elementSizeInBytes[] = {1, 2, 4, 4, 8, 8, 2,
4, 8, 2, 4, 4, 4};
for (int64_t r = 0; r < tensorRank; ++r) {
elementStrides[r] = uint32_t(1);
boxDim[r] = static_cast<uint32_t>(inputBoxDims[tensorRank - r - 1]);
globalDim[r] = static_cast<uint64_t>(descriptor->sizes[tensorRank - r - 1]);
}

globalStrides[0] = globalDim[0] * elementSizeInBytes[tensorDataType];
Expand Down