From 475d4614a7bbc1c8e9c73089c2ba7137b2afa2cb Mon Sep 17 00:00:00 2001 From: Filip Hazubski Date: Thu, 12 Mar 2020 03:05:37 +0100 Subject: [PATCH] Update Program constructor Change-Id: Ic1420d5ffa6e82d0e8ebbc9e65a98805bda3cc6c Signed-off-by: Filip Hazubski --- opencl/source/program/program.cpp | 31 ++++++++++------- .../test/unit_test/program/program_tests.cpp | 33 +++++++++++++++++++ 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/opencl/source/program/program.cpp b/opencl/source/program/program.cpp index b1ee5a172d..349013f396 100644 --- a/opencl/source/program/program.cpp +++ b/opencl/source/program/program.cpp @@ -46,16 +46,25 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b this->context->incRefInternal(); } blockKernelManager = new BlockKernelManager(); - auto clDevice = context != nullptr ? context->getDevice(0) : (device != nullptr) ? device->getSpecializedDevice() : nullptr; - if (pDevice == nullptr && context != nullptr) { - pDevice = &context->getDevice(0)->getDevice(); + ClDevice *pClDevice = nullptr; + if (context != nullptr) { + pClDevice = context->getDevice(0); + if (pDevice == nullptr) { + pDevice = &pClDevice->getDevice(); + } + } else if (pDevice != nullptr) { + auto pSpecializedDevice = castToObject(pDevice->getSpecializedDevice()); + if (pSpecializedDevice != nullptr) { + pClDevice = pSpecializedDevice; + } } + numDevices = 1; char paramValue[32] = {}; bool force32BitAddressess = false; - if (clDevice) { - clDevice->getDeviceInfo(CL_DEVICE_VERSION, 32, paramValue, nullptr); + if (pClDevice) { + pClDevice->getDeviceInfo(CL_DEVICE_VERSION, 32, paramValue, nullptr); if (strstr(paramValue, "2.1")) { internalOptions = "-ocl-version=210 "; } else if (strstr(paramValue, "2.0")) { @@ -63,13 +72,13 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b } else if (strstr(paramValue, "1.2")) { internalOptions = "-ocl-version=120 "; } - force32BitAddressess = clDevice->getDeviceInfo().force32BitAddressess; + force32BitAddressess = pClDevice->getDeviceInfo().force32BitAddressess; if (force32BitAddressess) { CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::arch32bit); } - if (clDevice->areSharedSystemAllocationsAllowed() || + if (pClDevice->areSharedSystemAllocationsAllowed() || DebugManager.flags.DisableStatelessToStatefulOptimization.get()) { CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::greaterThan4gbBuffersRequired); } @@ -82,9 +91,9 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::bindlessImages); } - kernelDebugEnabled = clDevice->isDebuggerActive(); + kernelDebugEnabled = pClDevice->isDebuggerActive(); - auto enableStatelessToStatefullWithOffset = clDevice->getHardwareCapabilities().isStatelesToStatefullWithOffsetSupported; + auto enableStatelessToStatefullWithOffset = pClDevice->getHardwareCapabilities().isStatelesToStatefullWithOffsetSupported; if (DebugManager.flags.EnableStatelessToStatefulBufferOffsetOpt.get() != -1) { enableStatelessToStatefullWithOffset = DebugManager.flags.EnableStatelessToStatefulBufferOffsetOpt.get() != 0; } @@ -93,8 +102,8 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::hasBufferOffsetArg); } - auto &hwHelper = HwHelper::get(clDevice->getHardwareInfo().platform.eRenderCoreFamily); - if (hwHelper.isForceEmuInt32DivRemSPWARequired(clDevice->getHardwareInfo())) { + auto &hwHelper = HwHelper::get(pClDevice->getHardwareInfo().platform.eRenderCoreFamily); + if (hwHelper.isForceEmuInt32DivRemSPWARequired(pClDevice->getHardwareInfo())) { CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::forceEmuInt32DivRemSP); } } diff --git a/opencl/test/unit_test/program/program_tests.cpp b/opencl/test/unit_test/program/program_tests.cpp index b9e1cf8a6e..12b9dbcaa6 100644 --- a/opencl/test/unit_test/program/program_tests.cpp +++ b/opencl/test/unit_test/program/program_tests.cpp @@ -2628,6 +2628,39 @@ TEST_F(ProgramTests, givenProgramWhenBuiltThenAdditionalOptionsAreApplied) { EXPECT_EQ(1u, program.applyAdditionalOptionsCalled); } +TEST_F(ProgramTests, WhenProgramIsCreatedThenItsDeviceIsProperlySet) { + auto wasValidClDeviceUsed = [](MockProgram &program) -> bool { + return (program.getInternalOptions().find(CompilerOptions::arch32bit) != std::string::npos); + }; + + MockExecutionEnvironment executionEnvironment; + MockDevice mockDevice; + mockDevice.deviceInfo.force32BitAddressess = true; + auto pContextMockDevice = new MockDevice; + MockClDevice contextMockClDevice{pContextMockDevice}; + MockContext mockContext{&contextMockClDevice}; + + MockProgram programWithDeviceGiven{executionEnvironment, &mockContext, false, &mockDevice}; + EXPECT_EQ(&mockDevice, programWithDeviceGiven.pDevice); + + MockProgram programWithDeviceFromContext{executionEnvironment, &mockContext, false, nullptr}; + EXPECT_EQ(pContextMockDevice, programWithDeviceFromContext.pDevice); + + MockProgram programWithDeviceWithoutSpecializedDevice{executionEnvironment, nullptr, false, &mockDevice}; + EXPECT_FALSE(wasValidClDeviceUsed(programWithDeviceWithoutSpecializedDevice)); + + MockDevice invalidClDevice; + mockDevice.setSpecializedDevice(&invalidClDevice); + MockProgram programWithDeviceWithInvalidSpecializedDevice{executionEnvironment, nullptr, false, &mockDevice}; + EXPECT_FALSE(wasValidClDeviceUsed(programWithDeviceWithInvalidSpecializedDevice)); + + MockClDevice validClDevice{new MockDevice}; + validClDevice.deviceInfo.force32BitAddressess = true; + validClDevice.device.deviceInfo.force32BitAddressess = true; + MockProgram programWithDeviceWithValidSpecializedDevice{executionEnvironment, nullptr, false, &validClDevice.getDevice()}; + EXPECT_TRUE(wasValidClDeviceUsed(programWithDeviceWithValidSpecializedDevice)); +} + TEST(CreateProgramFromBinaryTests, givenBinaryProgramWhenKernelRebulildIsForcedThenDeviceBinaryIsNotUsed) { DebugManagerStateRestore dbgRestorer; DebugManager.flags.RebuildPrecompiledKernels.set(true);