Add support for GT-Pin Callbacks [2/n]

Change-Id: Ibdb76361be2a0e48888b46e9ed6dfe6b0ed49862
This commit is contained in:
hjnapiat
2018-01-24 13:26:46 +01:00
parent 9477f03f34
commit 5b0ebe25d5
8 changed files with 171 additions and 0 deletions

View File

@@ -1323,6 +1323,9 @@ cl_kernel CL_API_CALL clCreateKernel(cl_program clProgram,
if (errcodeRet) {
*errcodeRet = retVal;
}
if (kernel != nullptr) {
gtpinNotifyKernelCreate(kernel);
}
return kernel;
}

View File

@@ -29,4 +29,7 @@ void gtpinNotifyContextCreate(cl_context context) {
void gtpinNotifyContextDestroy(cl_context context) {
}
void gtpinNotifyKernelCreate(cl_kernel kernel) {
}
}

View File

@@ -23,9 +23,11 @@
#include "config.h"
#include "gtpin_ocl_interface.h"
#include "CL/cl.h"
#include "runtime/context/context.h"
#include "runtime/device/device.h"
#include "runtime/device/device_info.h"
#include "runtime/gtpin/gtpin_hw_helper.h"
#include "runtime/kernel/kernel.h"
#include "runtime/platform/platform.h"
using namespace gtpin;
@@ -55,4 +57,27 @@ void gtpinNotifyContextDestroy(cl_context context) {
(*GTPinCallbacks.onContextDestroy)((context_handle_t)context);
}
}
void gtpinNotifyKernelCreate(cl_kernel kernel) {
if (isGTPinInitialized) {
auto pKernel = castToObject<Kernel>(kernel);
Context *pContext = &(pKernel->getContext());
cl_context context = (cl_context)pContext;
const KernelInfo &kInfo = pKernel->getKernelInfo();
instrument_params_in_t paramsIn;
paramsIn.kernel_type = GTPIN_KERNEL_TYPE_CS;
paramsIn.simd = (GTPIN_SIMD_WIDTH)kInfo.getMaxSimdSize();
paramsIn.orig_kernel_binary = (uint8_t *)pKernel->getKernelHeap();
paramsIn.orig_kernel_size = static_cast<uint32_t>(pKernel->getKernelHeapSize());
paramsIn.buffer_type = GTPIN_BUFFER_BINDFULL;
paramsIn.buffer_desc.BTI = kInfo.patchInfo.bindingTableState->Count;
paramsIn.igc_hash_id = kInfo.heapInfo.pKernelHeader->ShaderHashCode;
paramsIn.kernel_name = (char *)kInfo.name.c_str();
paramsIn.igc_info = nullptr;
instrument_params_out_t paramsOut = {0};
(*GTPinCallbacks.onKernelCreate)((context_handle_t)(cl_context)context, &paramsIn, &paramsOut);
pKernel->substituteKernelHeap(paramsOut.inst_kernel_binary, paramsOut.inst_kernel_size);
pKernel->setKernelId(paramsOut.kernel_id);
}
}
}

View File

@@ -25,4 +25,5 @@
namespace OCLRT {
void gtpinNotifyContextCreate(cl_context context);
void gtpinNotifyContextDestroy(cl_context context);
void gtpinNotifyKernelCreate(cl_kernel kernel);
}

View File

@@ -684,6 +684,23 @@ size_t Kernel::getKernelHeapSize() const {
return kernelInfo.heapInfo.pKernelHeader->KernelHeapSize;
}
void Kernel::substituteKernelHeap(void *newKernelHeap, size_t newKernelHeapSize) {
KernelInfo *pKernelInfo = const_cast<KernelInfo *>(&kernelInfo);
void **pKernelHeap = const_cast<void **>(&pKernelInfo->heapInfo.pKernelHeap);
*pKernelHeap = newKernelHeap;
SKernelBinaryHeaderCommon *pHeader = const_cast<SKernelBinaryHeaderCommon *>(pKernelInfo->heapInfo.pKernelHeader);
pHeader->KernelHeapSize = static_cast<uint32_t>(newKernelHeapSize);
}
uint64_t Kernel::getKernelId() const {
return kernelInfo.kernelId;
}
void Kernel::setKernelId(uint64_t newKernelId) {
KernelInfo *pKernelInfo = const_cast<KernelInfo *>(&kernelInfo);
pKernelInfo->kernelId = newKernelId;
}
const void *Kernel::getSurfaceStateHeap() const {
return kernelInfo.usesSsh
? pSshLocal

View File

@@ -157,6 +157,10 @@ class Kernel : public BaseObject<_cl_kernel> {
size_t getDynamicStateHeapSize() const;
size_t getNumberOfSurfaceStates() const;
void substituteKernelHeap(void *newKernelHeap, size_t newKernelHeapSize);
uint64_t getKernelId() const;
void setKernelId(uint64_t newKernelId);
const std::vector<SimpleKernelArgInfo> &getKernelArguments() const {
return kernelArguments;
}

View File

@@ -235,5 +235,6 @@ struct KernelInfo {
const BuiltinDispatchInfoBuilder *builtinDispatchBuilder = nullptr;
uint32_t argumentsToPatchNum = 0;
uint32_t systemKernelOffset = 0;
uint64_t kernelId = 0;
};
} // namespace OCLRT

View File

@@ -26,11 +26,15 @@
#include "runtime/gtpin/gtpin_init.h"
#include "runtime/gtpin/gtpin_helpers.h"
#include "runtime/helpers/basic_math.h"
#include "runtime/helpers/file_io.h"
#include "runtime/helpers/options.h"
#include "runtime/kernel/kernel.h"
#include "runtime/mem_obj/buffer.h"
#include "unit_tests/fixtures/context_fixture.h"
#include "unit_tests/fixtures/memory_management_fixture.h"
#include "unit_tests/fixtures/platform_fixture.h"
#include "unit_tests/helpers/kernel_binary_helper.h"
#include "unit_tests/helpers/test_files.h"
#include "test.h"
#include "gtest/gtest.h"
@@ -45,6 +49,7 @@ namespace ULT {
int ContextCreateCallbackCount = 0;
int ContextDestroyCallbackCount = 0;
int KernelCreateCallbackCount = 0;
void OnContextCreate(context_handle_t context, platform_info_t *platformInfo, igc_init_t **igcInit) {
ContextCreateCallbackCount++;
@@ -55,6 +60,10 @@ void OnContextDestroy(context_handle_t context) {
}
void OnKernelCreate(context_handle_t context, const instrument_params_in_t *paramsIn, instrument_params_out_t *paramsOut) {
paramsOut->inst_kernel_binary = const_cast<uint8_t *>(paramsIn->orig_kernel_binary);
paramsOut->inst_kernel_size = paramsIn->orig_kernel_size;
paramsOut->kernel_id = paramsIn->igc_hash_id;
KernelCreateCallbackCount++;
}
void OnKernelSubmit(command_buffer_handle_t cb, uint64_t kernelId, uint32_t *entryOffset, resource_handle_t *resource) {
@@ -527,4 +536,112 @@ TEST_F(GTPinTests, givenInitializedGTPinInterfaceThenGTPinContextCallbackIsCalle
EXPECT_EQ(ContextDestroyCallbackCount, prevCount + 1);
}
TEST_F(GTPinTests, givenUninitializedGTPinInterfaceThenGTPinKernelCreateCallbackIsNotCalled) {
cl_kernel kernel = nullptr;
cl_program pProgram = nullptr;
cl_device_id device = (cl_device_id)pDevice;
void *pSource = nullptr;
size_t sourceSize = 0;
std::string testFile;
KernelBinaryHelper kbHelper("CopyBuffer_simd8", false);
testFile.append(clFiles);
testFile.append("CopyBuffer_simd8.cl");
sourceSize = loadDataFromFile(testFile.c_str(), pSource);
EXPECT_NE(0u, sourceSize);
EXPECT_NE(nullptr, pSource);
pProgram = clCreateProgramWithSource(
(cl_context)((Context *)pContext),
1,
(const char **)&pSource,
&sourceSize,
&retVal);
ASSERT_NE(nullptr, pProgram);
retVal = clBuildProgram(
pProgram,
1,
&device,
nullptr,
nullptr,
nullptr);
EXPECT_EQ(CL_SUCCESS, retVal);
int prevCount = KernelCreateCallbackCount;
kernel = clCreateKernel(pProgram, "CopyBuffer", &retVal);
EXPECT_NE(nullptr, kernel);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(prevCount, KernelCreateCallbackCount);
retVal = clReleaseKernel(kernel);
EXPECT_EQ(CL_SUCCESS, retVal);
retVal = clReleaseProgram(pProgram);
EXPECT_EQ(CL_SUCCESS, retVal);
deleteDataReadFromFile(pSource);
}
TEST_F(GTPinTests, givenInitializedGTPinInterfaceThenGTPinKernelCreateCallbackIsCalled) {
gtpinCallbacks.onContextCreate = OnContextCreate;
gtpinCallbacks.onContextDestroy = OnContextDestroy;
gtpinCallbacks.onKernelCreate = OnKernelCreate;
gtpinCallbacks.onKernelSubmit = OnKernelSubmit;
gtpinCallbacks.onCommandBufferCreate = OnCommandBufferCreate;
gtpinCallbacks.onCommandBufferComplete = OnCommandBufferComplete;
retFromGtPin = GTPin_Init(&gtpinCallbacks, &driverServices, nullptr);
EXPECT_EQ(GTPIN_DI_SUCCESS, retFromGtPin);
cl_kernel kernel = nullptr;
cl_program pProgram = nullptr;
cl_device_id device = (cl_device_id)pDevice;
void *pSource = nullptr;
size_t sourceSize = 0;
std::string testFile;
KernelBinaryHelper kbHelper("CopyBuffer_simd8", false);
testFile.append(clFiles);
testFile.append("CopyBuffer_simd8.cl");
sourceSize = loadDataFromFile(testFile.c_str(), pSource);
EXPECT_NE(0u, sourceSize);
EXPECT_NE(nullptr, pSource);
pProgram = clCreateProgramWithSource(
(cl_context)((Context *)pContext),
1,
(const char **)&pSource,
&sourceSize,
&retVal);
ASSERT_NE(nullptr, pProgram);
retVal = clBuildProgram(
pProgram,
1,
&device,
nullptr,
nullptr,
nullptr);
EXPECT_EQ(CL_SUCCESS, retVal);
int prevCount = KernelCreateCallbackCount;
kernel = clCreateKernel(pProgram, "CopyBuffer", &retVal);
EXPECT_NE(nullptr, kernel);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(prevCount + 1, KernelCreateCallbackCount);
Kernel *pKernel = (Kernel *)kernel;
const KernelInfo &kInfo = pKernel->getKernelInfo();
uint64_t gtpinKernelId = pKernel->getKernelId();
EXPECT_EQ(kInfo.heapInfo.pKernelHeader->ShaderHashCode, gtpinKernelId);
retVal = clReleaseKernel(kernel);
EXPECT_EQ(CL_SUCCESS, retVal);
retVal = clReleaseProgram(pProgram);
EXPECT_EQ(CL_SUCCESS, retVal);
deleteDataReadFromFile(pSource);
}
} // namespace ULT