Skip to content

Commit 475d461

Browse files
Update Program constructor
Change-Id: Ic1420d5ffa6e82d0e8ebbc9e65a98805bda3cc6c Signed-off-by: Filip Hazubski <[email protected]>
1 parent 7bc4b57 commit 475d461

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

opencl/source/program/program.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,39 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b
4646
this->context->incRefInternal();
4747
}
4848
blockKernelManager = new BlockKernelManager();
49-
auto clDevice = context != nullptr ? context->getDevice(0) : (device != nullptr) ? device->getSpecializedDevice<ClDevice>() : nullptr;
50-
if (pDevice == nullptr && context != nullptr) {
51-
pDevice = &context->getDevice(0)->getDevice();
49+
ClDevice *pClDevice = nullptr;
50+
if (context != nullptr) {
51+
pClDevice = context->getDevice(0);
52+
if (pDevice == nullptr) {
53+
pDevice = &pClDevice->getDevice();
54+
}
55+
} else if (pDevice != nullptr) {
56+
auto pSpecializedDevice = castToObject<ClDevice>(pDevice->getSpecializedDevice<ClDevice>());
57+
if (pSpecializedDevice != nullptr) {
58+
pClDevice = pSpecializedDevice;
59+
}
5260
}
61+
5362
numDevices = 1;
5463
char paramValue[32] = {};
5564
bool force32BitAddressess = false;
5665

57-
if (clDevice) {
58-
clDevice->getDeviceInfo(CL_DEVICE_VERSION, 32, paramValue, nullptr);
66+
if (pClDevice) {
67+
pClDevice->getDeviceInfo(CL_DEVICE_VERSION, 32, paramValue, nullptr);
5968
if (strstr(paramValue, "2.1")) {
6069
internalOptions = "-ocl-version=210 ";
6170
} else if (strstr(paramValue, "2.0")) {
6271
internalOptions = "-ocl-version=200 ";
6372
} else if (strstr(paramValue, "1.2")) {
6473
internalOptions = "-ocl-version=120 ";
6574
}
66-
force32BitAddressess = clDevice->getDeviceInfo().force32BitAddressess;
75+
force32BitAddressess = pClDevice->getDeviceInfo().force32BitAddressess;
6776

6877
if (force32BitAddressess) {
6978
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::arch32bit);
7079
}
7180

72-
if (clDevice->areSharedSystemAllocationsAllowed() ||
81+
if (pClDevice->areSharedSystemAllocationsAllowed() ||
7382
DebugManager.flags.DisableStatelessToStatefulOptimization.get()) {
7483
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::greaterThan4gbBuffersRequired);
7584
}
@@ -82,9 +91,9 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b
8291
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::bindlessImages);
8392
}
8493

85-
kernelDebugEnabled = clDevice->isDebuggerActive();
94+
kernelDebugEnabled = pClDevice->isDebuggerActive();
8695

87-
auto enableStatelessToStatefullWithOffset = clDevice->getHardwareCapabilities().isStatelesToStatefullWithOffsetSupported;
96+
auto enableStatelessToStatefullWithOffset = pClDevice->getHardwareCapabilities().isStatelesToStatefullWithOffsetSupported;
8897
if (DebugManager.flags.EnableStatelessToStatefulBufferOffsetOpt.get() != -1) {
8998
enableStatelessToStatefullWithOffset = DebugManager.flags.EnableStatelessToStatefulBufferOffsetOpt.get() != 0;
9099
}
@@ -93,8 +102,8 @@ Program::Program(ExecutionEnvironment &executionEnvironment, Context *context, b
93102
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::hasBufferOffsetArg);
94103
}
95104

96-
auto &hwHelper = HwHelper::get(clDevice->getHardwareInfo().platform.eRenderCoreFamily);
97-
if (hwHelper.isForceEmuInt32DivRemSPWARequired(clDevice->getHardwareInfo())) {
105+
auto &hwHelper = HwHelper::get(pClDevice->getHardwareInfo().platform.eRenderCoreFamily);
106+
if (hwHelper.isForceEmuInt32DivRemSPWARequired(pClDevice->getHardwareInfo())) {
98107
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::forceEmuInt32DivRemSP);
99108
}
100109
}

opencl/test/unit_test/program/program_tests.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,39 @@ TEST_F(ProgramTests, givenProgramWhenBuiltThenAdditionalOptionsAreApplied) {
26282628
EXPECT_EQ(1u, program.applyAdditionalOptionsCalled);
26292629
}
26302630

2631+
TEST_F(ProgramTests, WhenProgramIsCreatedThenItsDeviceIsProperlySet) {
2632+
auto wasValidClDeviceUsed = [](MockProgram &program) -> bool {
2633+
return (program.getInternalOptions().find(CompilerOptions::arch32bit) != std::string::npos);
2634+
};
2635+
2636+
MockExecutionEnvironment executionEnvironment;
2637+
MockDevice mockDevice;
2638+
mockDevice.deviceInfo.force32BitAddressess = true;
2639+
auto pContextMockDevice = new MockDevice;
2640+
MockClDevice contextMockClDevice{pContextMockDevice};
2641+
MockContext mockContext{&contextMockClDevice};
2642+
2643+
MockProgram programWithDeviceGiven{executionEnvironment, &mockContext, false, &mockDevice};
2644+
EXPECT_EQ(&mockDevice, programWithDeviceGiven.pDevice);
2645+
2646+
MockProgram programWithDeviceFromContext{executionEnvironment, &mockContext, false, nullptr};
2647+
EXPECT_EQ(pContextMockDevice, programWithDeviceFromContext.pDevice);
2648+
2649+
MockProgram programWithDeviceWithoutSpecializedDevice{executionEnvironment, nullptr, false, &mockDevice};
2650+
EXPECT_FALSE(wasValidClDeviceUsed(programWithDeviceWithoutSpecializedDevice));
2651+
2652+
MockDevice invalidClDevice;
2653+
mockDevice.setSpecializedDevice(&invalidClDevice);
2654+
MockProgram programWithDeviceWithInvalidSpecializedDevice{executionEnvironment, nullptr, false, &mockDevice};
2655+
EXPECT_FALSE(wasValidClDeviceUsed(programWithDeviceWithInvalidSpecializedDevice));
2656+
2657+
MockClDevice validClDevice{new MockDevice};
2658+
validClDevice.deviceInfo.force32BitAddressess = true;
2659+
validClDevice.device.deviceInfo.force32BitAddressess = true;
2660+
MockProgram programWithDeviceWithValidSpecializedDevice{executionEnvironment, nullptr, false, &validClDevice.getDevice()};
2661+
EXPECT_TRUE(wasValidClDeviceUsed(programWithDeviceWithValidSpecializedDevice));
2662+
}
2663+
26312664
TEST(CreateProgramFromBinaryTests, givenBinaryProgramWhenKernelRebulildIsForcedThenDeviceBinaryIsNotUsed) {
26322665
DebugManagerStateRestore dbgRestorer;
26332666
DebugManager.flags.RebuildPrecompiledKernels.set(true);

0 commit comments

Comments
 (0)