1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/*
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/command_stream/wait_status.h"
#include "shared/source/helpers/local_memory_access_modes.h"
#include "shared/source/kernel/implicit_args_helper.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/libult/ult_command_stream_receiver.h"
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/test_macros/hw_test.h"
#include "shared/test/common/test_macros/test_checks_shared.h"
#include "opencl/source/command_queue/command_queue_hw.h"
#include "opencl/source/event/event.h"
#include "opencl/source/event/user_event.h"
#include "opencl/source/program/printf_handler.h"
#include "opencl/test/unit_test/fixtures/multi_root_device_fixture.h"
#include "opencl/test/unit_test/mocks/mock_cl_device.h"
#include "opencl/test/unit_test/mocks/mock_context.h"
#include "opencl/test/unit_test/mocks/mock_kernel.h"
#include "opencl/test/unit_test/mocks/mock_mdi.h"
#include "opencl/test/unit_test/mocks/mock_printf_handler.h"
#include "opencl/test/unit_test/mocks/mock_program.h"
using namespace NEO;
using PrintfHandlerTests = ::testing::Test;
TEST_F(PrintfHandlerTests, givenPrintfHandlerWhenBeingConstructedThenStorePrintfSurfaceInitialDataSize) {
auto device = std::unique_ptr<MockDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
MockPrintfHandler printfHandler(*device);
EXPECT_NE(nullptr, printfHandler.printfSurfaceInitialDataSizePtr);
EXPECT_EQ(sizeof(uint32_t), *printfHandler.printfSurfaceInitialDataSizePtr);
}
TEST_F(PrintfHandlerTests, givenNotPreparedPrintfHandlerWhenGetSurfaceIsCalledThenResultIsNullptr) {
MockClDevice *device = new MockClDevice{MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr)};
MockContext context;
auto pKernelInfo = std::make_unique<MockKernelInfo>();
pKernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
MockProgram *pProgram = new MockProgram(&context, false, toClDeviceVector(*device));
MockKernel *pKernel = new MockKernel(pProgram, *pKernelInfo, *device);
MockMultiDispatchInfo multiDispatchInfo(device, pKernel);
PrintfHandler *printfHandler = PrintfHandler::create(multiDispatchInfo, device->getDevice());
EXPECT_EQ(nullptr, printfHandler->getSurface());
delete printfHandler;
delete pKernel;
delete pProgram;
delete device;
}
TEST_F(PrintfHandlerTests, givenPreparedPrintfHandlerWithUndefinedSshOffsetWhenGetSurfaceIsCalledThenResultIsNotNullptr) {
MockClDevice *device = new MockClDevice{MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr)};
MockContext context;
auto pKernelInfo = std::make_unique<MockKernelInfo>();
pKernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
MockProgram *pProgram = new MockProgram(&context, false, toClDeviceVector(*device));
uint64_t crossThread[10];
MockKernel *pKernel = new MockKernel(pProgram, *pKernelInfo, *device);
pKernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
MockMultiDispatchInfo multiDispatchInfo(device, pKernel);
PrintfHandler *printfHandler = PrintfHandler::create(multiDispatchInfo, device->getDevice());
printfHandler->prepareDispatch(multiDispatchInfo);
EXPECT_NE(nullptr, printfHandler->getSurface());
delete printfHandler;
delete pKernel;
delete pProgram;
delete device;
}
TEST_F(PrintfHandlerTests, givenKernelWithImplicitArgsWhenPreparingPrintfHandlerThenProperAddressIsPatchedInImplicitArgsStruct) {
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
MockContext context(device.get());
auto pKernelInfo = std::make_unique<MockKernelInfo>();
pKernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
pKernelInfo->kernelDescriptor.kernelAttributes.simdSize = device->getGfxCoreHelper().getMinimalSIMDSize();
pKernelInfo->kernelDescriptor.kernelAttributes.flags.requiresImplicitArgs = true;
MockProgram program{&context, false, toClDeviceVector(*device)};
uint64_t crossThread[10] = {};
MockKernel kernel{&program, *pKernelInfo, *device};
kernel.setCrossThreadData(crossThread, sizeof(uint64_t) * 10);
kernel.initialize();
MockMultiDispatchInfo multiDispatchInfo(device.get(), &kernel);
auto printfHandler = std::unique_ptr<PrintfHandler>(PrintfHandler::create(multiDispatchInfo, device->getDevice()));
printfHandler->prepareDispatch(multiDispatchInfo);
auto printfSurface = printfHandler->getSurface();
ASSERT_NE(nullptr, printfSurface);
auto pImplicitArgs = kernel.getImplicitArgs();
ASSERT_NE(nullptr, pImplicitArgs);
EXPECT_EQ(printfSurface->getGpuAddress(), pImplicitArgs->v0.printfBufferPtr);
}
HWTEST_F(PrintfHandlerTests, givenEnabledStatelessCompressionWhenPrintEnqueueOutputIsCalledThenBCSEngineIsUsedToDecompressPrintfOutput) {
HardwareInfo hwInfo = *defaultHwInfo;
hwInfo.capabilityTable.blitterOperationsSupported = true;
DebugManagerStateRestore restore;
for (auto enable : {-1, 0, 1}) {
debugManager.flags.EnableStatelessCompression.set(enable);
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(&hwInfo));
REQUIRE_BLITTER_OR_SKIP(device->getRootDeviceEnvironment());
MockContext context(device.get());
auto kernelInfo = std::make_unique<MockKernelInfo>();
kernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
auto program = std::make_unique<MockProgram>(&context, false, toClDeviceVector(*device));
uint64_t crossThread[10];
auto kernel = std::make_unique<MockKernel>(program.get(), *kernelInfo, *device);
kernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
MockMultiDispatchInfo multiDispatchInfo(device.get(), kernel.get());
std::unique_ptr<PrintfHandler> printfHandler(PrintfHandler::create(multiDispatchInfo, device->getDevice()));
printfHandler->prepareDispatch(multiDispatchInfo);
EXPECT_NE(nullptr, printfHandler->getSurface());
EXPECT_TRUE(printfHandler->printEnqueueOutput());
auto &bcsEngine = device->getEngine(EngineHelpers::getBcsEngineType(device->getRootDeviceEnvironment(), device->getDeviceBitfield(), device->getSelectorCopyEngine(), true), EngineUsage::regular);
auto bcsCsr = static_cast<UltCommandStreamReceiver<FamilyType> *>(bcsEngine.commandStreamReceiver);
if (enable > 0) {
EXPECT_EQ(1u, bcsCsr->blitBufferCalled);
EXPECT_EQ(BlitterConstants::BlitDirection::bufferToHostPtr, bcsCsr->receivedBlitProperties[0].blitDirection);
} else {
EXPECT_EQ(0u, bcsCsr->blitBufferCalled);
}
}
}
HWTEST_F(PrintfHandlerTests, givenGpuHangOnFlushBcsStreamAndEnabledStatelessCompressionWhenPrintEnqueueOutputIsCalledThenBCSEngineIsUsedToDecompressPrintfOutputAndFalseIsReturned) {
DebugManagerStateRestore restore;
debugManager.flags.EnableStatelessCompression.set(1);
HardwareInfo hwInfo = *defaultHwInfo;
hwInfo.capabilityTable.blitterOperationsSupported = true;
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(&hwInfo));
REQUIRE_BLITTER_OR_SKIP(device->getRootDeviceEnvironment());
MockContext context(device.get());
auto kernelInfo = std::make_unique<MockKernelInfo>();
kernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
auto program = std::make_unique<MockProgram>(&context, false, toClDeviceVector(*device));
uint64_t crossThread[10];
auto kernel = std::make_unique<MockKernel>(program.get(), *kernelInfo, *device);
kernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
MockMultiDispatchInfo multiDispatchInfo(device.get(), kernel.get());
std::unique_ptr<PrintfHandler> printfHandler(PrintfHandler::create(multiDispatchInfo, device->getDevice()));
printfHandler->prepareDispatch(multiDispatchInfo);
EXPECT_NE(nullptr, printfHandler->getSurface());
auto &bcsEngine = device->getEngine(EngineHelpers::getBcsEngineType(device->getRootDeviceEnvironment(), device->getDeviceBitfield(), device->getSelectorCopyEngine(), true), EngineUsage::regular);
auto bcsCsr = static_cast<UltCommandStreamReceiver<FamilyType> *>(bcsEngine.commandStreamReceiver);
bcsCsr->callBaseFlushBcsTask = false;
bcsCsr->flushBcsTaskReturnValue = CompletionStamp::gpuHang;
EXPECT_FALSE(printfHandler->printEnqueueOutput());
EXPECT_EQ(1u, bcsCsr->blitBufferCalled);
EXPECT_EQ(BlitterConstants::BlitDirection::bufferToHostPtr, bcsCsr->receivedBlitProperties[0].blitDirection);
EXPECT_EQ(1u, bcsCsr->receivedBlitProperties[0].dstAllocation->hostPtrTaskCountAssignment.load());
bcsCsr->receivedBlitProperties[0].dstAllocation->hostPtrTaskCountAssignment--;
}
HWTEST_F(PrintfHandlerTests, givenDisallowedLocalMemoryCpuAccessWhenPrintEnqueueOutputIsCalledThenBCSEngineIsUsedToCopyPrintfOutput) {
HardwareInfo hwInfo = *defaultHwInfo;
hwInfo.capabilityTable.blitterOperationsSupported = true;
DebugManagerStateRestore restore;
debugManager.flags.ForceLocalMemoryAccessMode.set(static_cast<int32_t>(LocalMemoryAccessMode::cpuAccessDisallowed));
debugManager.flags.EnableLocalMemory.set(1);
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(&hwInfo));
REQUIRE_BLITTER_OR_SKIP(device->getRootDeviceEnvironment());
MockContext context(device.get());
auto kernelInfo = std::make_unique<MockKernelInfo>();
kernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
auto program = std::make_unique<MockProgram>(&context, false, toClDeviceVector(*device));
uint64_t crossThread[10]{};
auto kernel = std::make_unique<MockKernel>(program.get(), *kernelInfo, *device);
kernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
MockMultiDispatchInfo multiDispatchInfo(device.get(), kernel.get());
auto printfHandler = std::make_unique<MockPrintfHandler>(device->getDevice());
printfHandler->callBasePrintEnqueueOutput = true;
printfHandler->prepareDispatch(multiDispatchInfo);
EXPECT_NE(nullptr, printfHandler->getSurface());
device->getMemoryManager()->freeGraphicsMemory(printfHandler->printfSurface);
auto allocation = new MockGraphicsAllocation(reinterpret_cast<void *>(0x1000), 0x1000);
allocation->memoryPool = MemoryPool::localMemory;
printfHandler->printfSurface = allocation;
printfHandler->printEnqueueOutput();
auto &bcsEngine = device->getEngine(EngineHelpers::getBcsEngineType(device->getRootDeviceEnvironment(), device->getDeviceBitfield(), device->getSelectorCopyEngine(), true), EngineUsage::regular);
auto bcsCsr = static_cast<UltCommandStreamReceiver<FamilyType> *>(bcsEngine.commandStreamReceiver);
EXPECT_TRUE(bcsCsr->blitBufferCalled >= 1);
EXPECT_EQ(BlitterConstants::BlitDirection::bufferToHostPtr, bcsCsr->receivedBlitProperties[0].blitDirection);
}
HWTEST_F(PrintfHandlerTests, givenPrintfHandlerWhenEnqueueIsBlockedThenDontUsePrintfObjectAfterMove) {
DebugManagerStateRestore restore;
debugManager.flags.MakeEachEnqueueBlocking.set(true);
class MyMockCommandQueueHw : public CommandQueueHw<FamilyType> {
public:
using CommandQueueHw<FamilyType>::CommandQueueHw;
WaitStatus waitForAllEngines(bool blockedQueue, PrintfHandler *printfHandler, bool cleanTemporaryAllocationsList, bool waitForTaskCountRequired) override {
waitCalled = true;
printfHandlerUsedForWait = printfHandler;
return waitForAllEnginesReturnValue;
}
bool waitCalled = false;
PrintfHandler *printfHandlerUsedForWait = nullptr;
WaitStatus waitForAllEnginesReturnValue = WaitStatus::ready;
};
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
MockContext context;
MyMockCommandQueueHw cmdQ(&context, device.get(), nullptr, false);
auto kernelInfo = std::make_unique<MockKernelInfo>();
kernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
uint64_t crossThread[10];
auto program = std::make_unique<MockProgram>(&context, false, toClDeviceVector(*device));
auto kernel = std::make_unique<MockKernel>(program.get(), *kernelInfo, *device);
kernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
kernel->incRefInternal();
UserEvent userEvent;
cl_event waitlist[] = {&userEvent};
size_t gws[] = {1, 1, 1};
cmdQ.enqueueKernel(kernel.get(), 1, nullptr, gws, nullptr, 1, waitlist, nullptr);
EXPECT_TRUE(cmdQ.waitCalled);
EXPECT_EQ(nullptr, cmdQ.printfHandlerUsedForWait);
userEvent.setStatus(CL_COMPLETE);
EXPECT_FALSE(cmdQ.isQueueBlocked());
}
TEST_F(PrintfHandlerTests, givenMultiDispatchInfoWithMultipleKernelsWhenCreatingAndDispatchingPrintfHandlerThenPickMainKernel) {
MockContext context;
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
auto program = std::make_unique<MockProgram>(&context, false, toClDeviceVector(*device));
auto pMainKernelInfo = std::make_unique<MockKernelInfo>();
pMainKernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
auto pKernelInfo = std::make_unique<MockKernelInfo>();
auto mainKernel = std::make_unique<MockKernel>(program.get(), *pMainKernelInfo, *device);
auto kernel1 = std::make_unique<MockKernel>(program.get(), *pKernelInfo, *device);
auto kernel2 = std::make_unique<MockKernel>(program.get(), *pKernelInfo, *device);
uint64_t crossThread[8];
mainKernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
DispatchInfo mainDispatchInfo(device.get(), mainKernel.get(), 1, {1, 1, 1}, {1, 1, 1}, {1, 1, 1});
DispatchInfo dispatchInfo1(device.get(), kernel1.get(), 1, {1, 1, 1}, {1, 1, 1}, {1, 1, 1});
DispatchInfo dispatchInfo2(device.get(), kernel2.get(), 1, {1, 1, 1}, {1, 1, 1}, {1, 1, 1});
MultiDispatchInfo multiDispatchInfo(mainKernel.get());
multiDispatchInfo.push(dispatchInfo1);
multiDispatchInfo.push(mainDispatchInfo);
multiDispatchInfo.push(dispatchInfo2);
std::unique_ptr<PrintfHandler> printfHandler(PrintfHandler::create(multiDispatchInfo, device->getDevice()));
ASSERT_NE(nullptr, printfHandler.get());
printfHandler->prepareDispatch(multiDispatchInfo);
EXPECT_NE(nullptr, printfHandler->getSurface());
}
TEST_F(PrintfHandlerTests, GivenEmptyMultiDispatchInfoWhenCreatingPrintfHandlerThenPrintfHandlerIsNotCreated) {
MockClDevice device{new MockDevice};
MockKernelWithInternals mockKernelWithInternals{device};
MockMultiDispatchInfo multiDispatchInfo{&device, mockKernelWithInternals.mockKernel};
multiDispatchInfo.dispatchInfos.resize(0);
EXPECT_EQ(nullptr, multiDispatchInfo.peekMainKernel());
auto printfHandler = PrintfHandler::create(multiDispatchInfo, device.getDevice());
EXPECT_EQ(nullptr, printfHandler);
}
TEST_F(PrintfHandlerTests, GivenAllocationInLocalMemoryWhichRequiresBlitterWhenPreparingPrintfSurfaceDispatchThenBlitterIsUsed) {
MockExecutionEnvironment mockExecutionEnvironment{};
REQUIRE_BLITTER_OR_SKIP(*mockExecutionEnvironment.rootDeviceEnvironments[0].get());
DebugManagerStateRestore restorer;
auto hwInfo = *defaultHwInfo;
hwInfo.capabilityTable.blitterOperationsSupported = true;
uint32_t blitsCounter = 0;
uint32_t expectedBlitsCount = 0;
auto mockBlitMemoryToAllocation = [&blitsCounter](const Device &device, GraphicsAllocation *memory, size_t offset, const void *hostPtr,
Vec3<size_t> size) -> BlitOperationResult {
blitsCounter++;
return BlitOperationResult::success;
};
VariableBackup<BlitHelperFunctions::BlitMemoryToAllocationFunc> blitMemoryToAllocationFuncBackup{
&BlitHelperFunctions::blitMemoryToAllocation, mockBlitMemoryToAllocation};
LocalMemoryAccessMode localMemoryAccessModes[] = {
LocalMemoryAccessMode::defaultMode,
LocalMemoryAccessMode::cpuAccessAllowed,
LocalMemoryAccessMode::cpuAccessDisallowed};
for (auto localMemoryAccessMode : localMemoryAccessModes) {
debugManager.flags.ForceLocalMemoryAccessMode.set(static_cast<int32_t>(localMemoryAccessMode));
for (auto isLocalMemorySupported : ::testing::Bool()) {
debugManager.flags.EnableLocalMemory.set(isLocalMemorySupported);
auto pClDevice = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(&hwInfo));
MockContext context{pClDevice.get()};
auto pKernelInfo = std::make_unique<MockKernelInfo>();
pKernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
auto program = std::make_unique<MockProgram>(&context, false, toClDeviceVector(*pClDevice));
uint64_t crossThread[10];
auto kernel = std::make_unique<MockKernel>(program.get(), *pKernelInfo, *pClDevice);
kernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
MockMultiDispatchInfo multiDispatchInfo(pClDevice.get(), kernel.get());
std::unique_ptr<PrintfHandler> printfHandler(PrintfHandler::create(multiDispatchInfo, pClDevice->getDevice()));
printfHandler->prepareDispatch(multiDispatchInfo);
if (printfHandler->getSurface()->isAllocatedInLocalMemoryPool() &&
(localMemoryAccessMode == LocalMemoryAccessMode::cpuAccessDisallowed)) {
expectedBlitsCount++;
}
EXPECT_EQ(expectedBlitsCount, blitsCounter);
}
}
}
using PrintfHandlerMultiRootDeviceTests = MultiRootDeviceFixture;
TEST_F(PrintfHandlerMultiRootDeviceTests, GivenPrintfSurfaceThenItHasCorrectRootDeviceIndex) {
auto pKernelInfo = std::make_unique<MockKernelInfo>();
pKernelInfo->setPrintfSurface(sizeof(uintptr_t), 0);
auto program = std::make_unique<MockProgram>(context.get(), false, toClDeviceVector(*device1));
auto kernel = std::make_unique<MockKernel>(program.get(), *pKernelInfo, *device1);
uint64_t crossThread[10];
kernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
MockMultiDispatchInfo multiDispatchInfo(device1, kernel.get());
std::unique_ptr<PrintfHandler> printfHandler(PrintfHandler::create(multiDispatchInfo, device1->getDevice()));
printfHandler->prepareDispatch(multiDispatchInfo);
auto surface = printfHandler->getSurface();
ASSERT_NE(nullptr, surface);
EXPECT_EQ(expectedRootDeviceIndex, surface->getRootDeviceIndex());
}
|