/* * Copyright (C) 2017-2020 Intel Corporation * * SPDX-License-Identifier: MIT * */ #include "printf_handler.h" #include "shared/source/helpers/aligned_memory.h" #include "shared/source/helpers/blit_commands_helper.h" #include "shared/source/helpers/hw_helper.h" #include "shared/source/helpers/ptr_math.h" #include "shared/source/memory_manager/memory_manager.h" #include "shared/source/program/print_formatter.h" #include "opencl/source/cl_device/cl_device.h" #include "opencl/source/helpers/dispatch_info.h" #include "opencl/source/kernel/kernel.h" #include "opencl/source/mem_obj/buffer.h" namespace NEO { const uint32_t PrintfHandler::printfSurfaceInitialDataSize; PrintfHandler::PrintfHandler(ClDevice &deviceArg) : device(deviceArg) {} PrintfHandler::~PrintfHandler() { device.getMemoryManager()->freeGraphicsMemory(printfSurface); } PrintfHandler *PrintfHandler::create(const MultiDispatchInfo &multiDispatchInfo, ClDevice &device) { if (multiDispatchInfo.usesStatelessPrintfSurface()) { return new PrintfHandler(device); } auto mainKernel = multiDispatchInfo.peekMainKernel(); if ((mainKernel != nullptr) && mainKernel->checkIfIsParentKernelAndBlocksUsesPrintf()) { return new PrintfHandler(device); } return nullptr; } void PrintfHandler::prepareDispatch(const MultiDispatchInfo &multiDispatchInfo) { auto printfSurfaceSize = device.getSharedDeviceInfo().printfBufferSize; if (printfSurfaceSize == 0) { return; } kernel = multiDispatchInfo.peekMainKernel(); printfSurface = device.getMemoryManager()->allocateGraphicsMemoryWithProperties({device.getRootDeviceIndex(), printfSurfaceSize, GraphicsAllocation::AllocationType::PRINTF_SURFACE, device.getDeviceBitfield()}); auto &hwInfo = device.getHardwareInfo(); auto &helper = HwHelper::get(hwInfo.platform.eRenderCoreFamily); if (printfSurface->isAllocatedInLocalMemoryPool() && helper.isBlitCopyRequiredForLocalMemory(hwInfo)) { BlitHelperFunctions::blitMemoryToAllocation(device.getDevice(), printfSurface, 0, &printfSurfaceInitialDataSize, {sizeof(printfSurfaceInitialDataSize), 1, 1}); } else { *reinterpret_cast(printfSurface->getUnderlyingBuffer()) = printfSurfaceInitialDataSize; } auto printfPatchAddress = ptrOffset(reinterpret_cast(kernel->getCrossThreadData()), kernel->getKernelInfo().patchInfo.pAllocateStatelessPrintfSurface->DataParamOffset); patchWithRequiredSize(printfPatchAddress, kernel->getKernelInfo().patchInfo.pAllocateStatelessPrintfSurface->DataParamSize, (uintptr_t)printfSurface->getGpuAddressToPatch()); if (kernel->requiresSshForBuffers()) { auto surfaceState = ptrOffset(reinterpret_cast(kernel->getSurfaceStateHeap()), kernel->getKernelInfo().patchInfo.pAllocateStatelessPrintfSurface->SurfaceStateHeapOffset); void *addressToPatch = printfSurface->getUnderlyingBuffer(); size_t sizeToPatch = printfSurface->getUnderlyingBufferSize(); Buffer::setSurfaceState(&device.getDevice(), surfaceState, sizeToPatch, addressToPatch, 0, printfSurface, 0, 0); } } void PrintfHandler::makeResident(CommandStreamReceiver &commandStreamReceiver) { commandStreamReceiver.makeResident(*printfSurface); } void PrintfHandler::printEnqueueOutput() { PrintFormatter printFormatter(reinterpret_cast(printfSurface->getUnderlyingBuffer()), static_cast(printfSurface->getUnderlyingBufferSize()), kernel->is32Bit(), kernel->getKernelInfo().patchInfo.stringDataMap); printFormatter.printKernelOutput(); } } // namespace NEO