diff --git a/hipamd/rocclr/hip_memory.cpp b/hipamd/rocclr/hip_memory.cpp index 54ac5bb1d4..55732a8ee1 100755 --- a/hipamd/rocclr/hip_memory.cpp +++ b/hipamd/rocclr/hip_memory.cpp @@ -1850,18 +1850,27 @@ hipError_t ihipMemset3D(hipPitchedPtr pitchedDevPtr, hipExtent extent, hipStream_t stream, bool isAsync = false) { - if (pitchedDevPtr.pitch == extent.width) { - return ihipMemset(pitchedDevPtr.ptr, value, sizeof(int8_t), extent.width * extent.height * extent.depth, stream, isAsync); - } - - // Workaround for cases when pitch > row untill fill kernel will be updated to support pitch. - // Fallback to filling one row at a time. - - amd::HostQueue* queue = hip::getQueue(stream); - size_t offset = 0; amd::Memory* memory = getMemoryObject(pitchedDevPtr.ptr, offset); + auto sizeBytes = extent.width * extent.height * extent.depth; + + if (memory == nullptr) { + return hipErrorInvalidValue; + } + if (sizeBytes > memory->getSize()) { + return hipErrorInvalidValue; + } + + if (pitchedDevPtr.pitch == extent.width) { + return ihipMemset(pitchedDevPtr.ptr, value, sizeof(int8_t), static_cast(sizeBytes), stream, isAsync); + } + + // Workaround for cases when pitch > row until fill kernel will be updated to support pitch. + // Fall back to filling one row at a time. + + amd::HostQueue* queue = hip::getQueue(stream); + amd::Coord3D origin(offset); amd::Coord3D region(pitchedDevPtr.xsize, pitchedDevPtr.ysize, extent.depth); amd::BufferRect rect; @@ -1870,34 +1879,26 @@ hipError_t ihipMemset3D(hipPitchedPtr pitchedDevPtr, return hipErrorInvalidValue; } - if (memory != nullptr) { - std::vector commands; + std::vector commands; - for (size_t slice = 0; slice < extent.depth; slice++) { - for (size_t row = 0; row < extent.height; row++) { - const size_t rowOffset = rect.offset(0, row, slice); - amd::FillMemoryCommand* command = new amd::FillMemoryCommand(*queue, - CL_COMMAND_FILL_BUFFER, - amd::Command::EventWaitList{}, - *memory->asBuffer(), - &value, - sizeof(int8_t), - amd::Coord3D{rowOffset, 0, 0}, - amd::Coord3D{extent.width, 1, 1}); + for (size_t slice = 0; slice < extent.depth; slice++) { + for (size_t row = 0; row < extent.height; row++) { + const size_t rowOffset = rect.offset(0, row, slice); + amd::FillMemoryCommand *command = new amd::FillMemoryCommand(*queue, + CL_COMMAND_FILL_BUFFER, amd::Command::EventWaitList { }, + *memory->asBuffer(), &value, sizeof(int8_t), amd::Coord3D { rowOffset, + 0, 0 }, amd::Coord3D { extent.width, 1, 1 }); - command->enqueue(); - commands.push_back(command); - } + command->enqueue(); + commands.push_back(command); } + } - for (auto &command: commands) { - if (!isAsync) { - command->awaitCompletion(); - } - command->release(); + for (auto &command : commands) { + if (!isAsync) { + command->awaitCompletion(); } - } else { - return hipErrorInvalidValue; + command->release(); } return hipSuccess;