Move CompilerInterface to ExecutionEnvironment

Change-Id: I14afdd8fc41ecd4ac8c8fcbeecda2539bc847288
Signed-off-by: Maciej Dziuban <maciej.dziuban@intel.com>
This commit is contained in:
Maciej Dziuban
2018-08-01 09:06:46 +02:00
committed by sys_ocldev
parent 931b462ee1
commit d2759a0629
18 changed files with 114 additions and 190 deletions

View File

@@ -1715,11 +1715,11 @@ TEST_F(BuiltInTests, createBuiltInProgramForInvalidBuiltinKernelName) {
TEST_F(BuiltInTests, getSipKernelReturnsProgramCreatedOutOfIsaAcquiredFromCompilerInterface) {
MockBuiltins mockBuiltins;
MockCompilerInterface mockCompilerInterface;
mockCompilerInterface.overrideGlobalCompilerInterface();
mockCompilerInterface.sipKernelBinaryOverride = mockCompilerInterface.getDummyGenBinary();
auto mockCompilerInterface = new MockCompilerInterface();
pDevice->getExecutionEnvironment()->compilerInterface.reset(mockCompilerInterface);
mockCompilerInterface->sipKernelBinaryOverride = mockCompilerInterface->getDummyGenBinary();
cl_int errCode = CL_BUILD_PROGRAM_FAILURE;
auto p = Program::createFromGenBinary(*pDevice->getExecutionEnvironment(), pContext, mockCompilerInterface.sipKernelBinaryOverride.data(), mockCompilerInterface.sipKernelBinaryOverride.size(),
auto p = Program::createFromGenBinary(*pDevice->getExecutionEnvironment(), pContext, mockCompilerInterface->sipKernelBinaryOverride.data(), mockCompilerInterface->sipKernelBinaryOverride.size(),
false, &errCode);
ASSERT_EQ(CL_SUCCESS, errCode);
errCode = p->processGenBinary();
@@ -1735,7 +1735,7 @@ TEST_F(BuiltInTests, getSipKernelReturnsProgramCreatedOutOfIsaAcquiredFromCompil
auto expectedMem = reinterpret_cast<const char *>(sipKernelInfo->heapInfo.pKernelHeap) + sipOffset;
ASSERT_EQ(compbinedKernelHeapSize - sipOffset, sipKern.getBinarySize());
EXPECT_EQ(0, memcmp(expectedMem, sipKern.getBinary(), sipKern.getBinarySize()));
EXPECT_EQ(SipKernelType::Csr, mockCompilerInterface.requestedSipKernel);
EXPECT_EQ(SipKernelType::Csr, mockCompilerInterface->requestedSipKernel);
p->release();
}

View File

@@ -54,32 +54,6 @@ class BinaryCacheFixture
BinaryCache *cache;
};
class TestedCompilerInterface : public CompilerInterface {
public:
static TestedCompilerInterface *getInstance() {
if (pInstance == nullptr) {
auto instance = new TestedCompilerInterface();
if (!instance->initialize()) {
delete instance;
instance = nullptr;
}
pInstance = instance;
}
return pInstance;
}
static void shutdown() {
if (pInstance) {
delete pInstance;
pInstance = nullptr;
}
}
static TestedCompilerInterface *pInstance;
};
TestedCompilerInterface *TestedCompilerInterface::pInstance = nullptr;
class BinaryCacheMock : public BinaryCache {
public:
BinaryCacheMock() {
@@ -103,12 +77,11 @@ class CompilerInterfaceCachedFixture : public DeviceFixture {
public:
void SetUp() {
DeviceFixture::SetUp();
pCompilerInterface = TestedCompilerInterface::getInstance();
pCompilerInterface = pDevice->getExecutionEnvironment()->getCompilerInterface();
ASSERT_NE(pCompilerInterface, nullptr);
}
void TearDown() {
TestedCompilerInterface::shutdown();
DeviceFixture::TearDown();
}

View File

@@ -58,10 +58,10 @@ class CompilerInterfaceTest : public DeviceFixture,
retVal = CL_SUCCESS;
// create the compiler interface
pCompilerInterface.reset(new MockCompilerInterface());
ASSERT_NE(nullptr, pCompilerInterface);
this->pCompilerInterface = new MockCompilerInterface();
bool initRet = pCompilerInterface->initialize();
ASSERT_TRUE(initRet);
pDevice->getExecutionEnvironment()->compilerInterface.reset(pCompilerInterface);
std::string testFile;
@@ -94,12 +94,11 @@ class CompilerInterfaceTest : public DeviceFixture,
delete pContext;
deleteDataReadFromFile(pSource);
pCompilerInterface.reset();
DeviceFixture::TearDown();
}
std::unique_ptr<MockCompilerInterface> pCompilerInterface = nullptr;
MockCompilerInterface *pCompilerInterface;
TranslationArgs inputArgs;
Program *pProgram = nullptr;
MockContext *pContext = nullptr;
@@ -205,12 +204,6 @@ TEST_F(CompilerInterfaceTest, BuildWithDebugData) {
delete cip;
}
TEST_F(CompilerInterfaceTest, GetInstance_Shutdown) {
auto *pCompilerInterface = CompilerInterface::getInstance();
EXPECT_NE(nullptr, pCompilerInterface);
CompilerInterface::shutdown();
}
TEST_F(CompilerInterfaceTest, CompileClToIsa) {
// build from .cl to gen ISA
retVal = pCompilerInterface->build(*pProgram, inputArgs, false);

View File

@@ -29,6 +29,7 @@
#include "runtime/platform/platform.h"
#include "test.h"
#include "unit_tests/mocks/mock_csr.h"
#include "runtime/compiler_interface/compiler_interface.h"
#include "runtime/source_level_debugger/source_level_debugger.h"
using namespace OCLRT;
@@ -113,33 +114,42 @@ TEST(ExecutionEnvironment, givenExecutionEnvironmentWhenInitializeMemoryManagerI
}
auto destructorId = 0u;
static_assert(sizeof(ExecutionEnvironment) == (is64bit ? 64 : 36), "New members detected in ExecutionEnvironment, please ensure that destruction sequence of objects is correct");
static_assert(sizeof(ExecutionEnvironment) == sizeof(std::mutex) + (is64bit ? 72 : 40), "New members detected in ExecutionEnvironment, please ensure that destruction sequence of objects is correct");
TEST(ExecutionEnvironment, givenExecutionEnvironmentWithVariousMembersWhenItIsDestroyedThenDeleteSequenceIsSpecified) {
destructorId = 0u;
struct OsInterfaceMock : public OSInterface {
~OsInterfaceMock() override {
EXPECT_EQ(destructorId, 3u);
destructorId++;
}
};
struct GmmHelperMock : public GmmHelper {
using GmmHelper::GmmHelper;
~GmmHelperMock() override {
EXPECT_EQ(destructorId, 5u);
destructorId++;
}
};
struct OsInterfaceMock : public OSInterface {
~OsInterfaceMock() override {
EXPECT_EQ(destructorId, 4u);
destructorId++;
}
};
struct MemoryMangerMock : public OsAgnosticMemoryManager {
~MemoryMangerMock() override {
EXPECT_EQ(destructorId, 2u);
EXPECT_EQ(destructorId, 3u);
destructorId++;
}
};
struct CommandStreamReceiverMock : public MockCommandStreamReceiver {
~CommandStreamReceiverMock() override {
EXPECT_EQ(destructorId, 2u);
destructorId++;
};
};
struct CompilerInterfaceMock : public CompilerInterface {
~CompilerInterfaceMock() override {
EXPECT_EQ(destructorId, 1u);
destructorId++;
};
@@ -161,11 +171,12 @@ TEST(ExecutionEnvironment, givenExecutionEnvironmentWithVariousMembersWhenItIsDe
executionEnvironment->gmmHelper.reset(new GmmHelperMock(platformDevices[0]));
executionEnvironment->memoryManager.reset(new MemoryMangerMock);
executionEnvironment->commandStreamReceiver.reset(new CommandStreamReceiverMock);
executionEnvironment->compilerInterface.reset(new CompilerInterfaceMock());
executionEnvironment->sourceLevelDebugger.reset(new SourceLevelDebuggerMock);
executionEnvironment->osInterface.reset(new OsInterfaceMock);
executionEnvironment.reset(nullptr);
EXPECT_EQ(5u, destructorId);
EXPECT_EQ(6u, destructorId);
}
TEST(ExecutionEnvironment, givenMultipleDevicesWhenTheyAreCreatedTheyAllReuseTheSameMemoryManagerAndCommandStreamReceiver) {

View File

@@ -32,7 +32,6 @@ BuiltInFixture::BuiltInFixture() : pBuiltIns(nullptr) {
}
void BuiltInFixture::SetUp(Device *pDevice) {
CompilerInterface::getInstance();
// create an instance of the builtins
pBuiltIns = &BuiltIns::getInstance();
pBuiltIns->setCacheingEnableState(false);

View File

@@ -26,9 +26,12 @@
#include "unit_tests/mocks/mock_program.h"
namespace OCLRT {
const SipKernel &initSipKernel(SipKernelType type, Device &device) {
std::unique_ptr<MockCompilerInterface> mockCompilerInterface(new MockCompilerInterface());
mockCompilerInterface->overrideGlobalCompilerInterface();
auto mockCompilerInterface = new MockCompilerInterface();
mockCompilerInterface->initialize();
device.getExecutionEnvironment()->compilerInterface.reset(mockCompilerInterface);
mockCompilerInterface->sipKernelBinaryOverride = mockCompilerInterface->getDummyGenBinary();
return BuiltIns::getInstance().getSipKernel(type, device);
}

View File

@@ -23,6 +23,7 @@
#pragma once
#include "runtime/compiler_interface/compiler_interface.h"
#include "runtime/execution_environment/execution_environment.h"
#include "unit_tests/mocks/mock_cif.h"
#include "ocl_igc_interface/fcl_ocl_device_ctx.h"
@@ -251,10 +252,6 @@ struct MockFclOclDeviceCtx : MockCIF<IGC::FclOclDeviceCtxTagOCL> {
class MockCompilerInterface : public CompilerInterface {
public:
~MockCompilerInterface() {
CompilerInterface::pInstance = originalGlobalCompilerInterface;
}
bool isCompilerAvailable() const {
return CompilerInterface::isCompilerAvailable();
}
@@ -342,11 +339,6 @@ class MockCompilerInterface : public CompilerInterface {
return this->fclBaseTranslationCtx.get();
}
void overrideGlobalCompilerInterface() {
originalGlobalCompilerInterface = CompilerInterface::pInstance;
CompilerInterface::pInstance = this;
}
cl_int getSipKernelBinary(SipKernelType type, const Device &device, std::vector<char> &retBinary) override {
if (this->sipKernelBinaryOverride.size() > 0) {
retBinary = this->sipKernelBinaryOverride;
@@ -359,8 +351,6 @@ class MockCompilerInterface : public CompilerInterface {
static std::vector<char> getDummyGenBinary();
CompilerInterface *originalGlobalCompilerInterface = nullptr;
void (*lockListener)(MockCompilerInterface &compInt) = nullptr;
void *lockListenerData = nullptr;
bool failCreateFclTranslationCtx = false;

View File

@@ -90,10 +90,16 @@ std::vector<const char *> KernelNames{
"CopyBuffer",
};
class NoCompilerInterfaceProgram : public MockProgram {
class MockExecutionEnvironment : public ExecutionEnvironment {
public:
NoCompilerInterfaceProgram(ExecutionEnvironment &executionEnvironment) : MockProgram(executionEnvironment) {}
CompilerInterface *getCompilerInterface() const override { return nullptr; }
MockExecutionEnvironment(CompilerInterface *compilerInterface) : compilerInterface(compilerInterface) {}
CompilerInterface *getCompilerInterface() override {
return compilerInterface;
}
protected:
CompilerInterface *compilerInterface;
};
class FailingGenBinaryProgram : public MockProgram {
@@ -102,6 +108,12 @@ class FailingGenBinaryProgram : public MockProgram {
cl_int processGenBinary() override { return CL_INVALID_BINARY; }
};
class SucceedingGenBinaryProgram : public MockProgram {
public:
SucceedingGenBinaryProgram(ExecutionEnvironment &executionEnvironment) : MockProgram(executionEnvironment) {}
cl_int processGenBinary() override { return CL_SUCCESS; }
};
////////////////////////////////////////////////////////////////////////////////
// Program::createProgramWithBinary
////////////////////////////////////////////////////////////////////////////////
@@ -716,10 +728,12 @@ TEST_P(ProgramFromSourceTest, CreateWithSource_Build) {
pMockProgram->SetBuildStatus(CL_BUILD_NONE);
// fail build - CompilerInterface cannot be obtained
auto p2 = std::make_unique<NoCompilerInterfaceProgram>(*pPlatform->getDevice(0)->getExecutionEnvironment());
auto noCompilerInterfaceExecutionEnvironment = std::make_unique<MockExecutionEnvironment>(nullptr);
auto p2 = std::make_unique<MockProgram>(*noCompilerInterfaceExecutionEnvironment);
retVal = p2->build(0, nullptr, nullptr, nullptr, nullptr, false);
EXPECT_EQ(CL_OUT_OF_HOST_MEMORY, retVal);
p2.reset(nullptr);
noCompilerInterfaceExecutionEnvironment.reset();
// fail build - any build error (here caused by specifying unrecognized option)
retVal = pProgram->build(0, nullptr, "-invalid-option", nullptr, nullptr, false);
@@ -992,10 +1006,12 @@ TEST_P(ProgramFromSourceTest, CreateWithSource_Compile) {
delete p3;
// fail compilation - CompilerInterface cannot be obtained
auto p2 = std::make_unique<NoCompilerInterfaceProgram>(*pPlatform->getDevice(0)->getExecutionEnvironment());
auto noCompilerInterfaceExecutionEnvironment = std::make_unique<MockExecutionEnvironment>(nullptr);
auto p2 = std::make_unique<MockProgram>(*noCompilerInterfaceExecutionEnvironment);
retVal = p2->compile(0, nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr);
EXPECT_EQ(CL_OUT_OF_HOST_MEMORY, retVal);
p2.reset(nullptr);
noCompilerInterfaceExecutionEnvironment.reset();
// fail compilation - any compilation error (here caused by specifying unrecognized option)
retVal = pProgram->compile(0, nullptr, "-invalid-option", 0, nullptr, nullptr, nullptr, nullptr);
@@ -1036,33 +1052,12 @@ TEST_P(ProgramFromSourceTest, CompileProgramWithReraFlag) {
std::string buildInternalOptions;
};
class MyProgram : public Program {
public:
MyProgram(ExecutionEnvironment &executionEnvironment) : Program(executionEnvironment), cip(nullptr) {}
~MyProgram() override {
delete cip;
}
cl_int processGenBinary() override { return CL_SUCCESS; }
MyCompilerInterface *getCompilerInterface() const override {
if (cip == nullptr) {
cip = new MyCompilerInterface();
}
return cip;
}
protected:
mutable MyCompilerInterface *cip;
};
auto program = std::make_unique<MyProgram>(*pPlatform->getDevice(0)->getExecutionEnvironment());
EXPECT_NE(nullptr, program);
auto cip = std::make_unique<MyCompilerInterface>();
MockExecutionEnvironment executionEnvironment(cip.get());
auto program = std::make_unique<SucceedingGenBinaryProgram>(executionEnvironment);
cl_device_id deviceId = pContext->getDevice(0);
Device *pDevice = castToObject<Device>(deviceId);
program->setDevice(pDevice);
MyCompilerInterface *cip = program->getCompilerInterface();
EXPECT_NE(nullptr, cip);
program->setSource((char *)"__kernel mock() {}");
// Check default build options
@@ -1266,7 +1261,8 @@ TEST_P(ProgramFromSourceTest, CreateWithSource_Link) {
// Program::Link (create library)
////////////////////////////////////////////////////////////////////////////////
TEST_P(ProgramFromSourceTest, CreateWithSource_CreateLibrary) {
auto p = std::make_unique<NoCompilerInterfaceProgram>(*pPlatform->getDevice(0)->getExecutionEnvironment());
auto noCompilerInterfaceExecutionEnvironment = std::make_unique<MockExecutionEnvironment>(nullptr);
auto p = std::make_unique<MockProgram>(*noCompilerInterfaceExecutionEnvironment);
cl_program program = pProgram;
// Order of following microtests is important - do not change.
@@ -2149,7 +2145,6 @@ TEST_F(ProgramTests, GetGenBinaryReturnsBinaryStoreInProgram) {
TEST_F(ProgramTests, ValidBinaryWithIGCVersionEqual0) {
cl_int retVal;
CompilerInterface::getInstance();
auto program = std::make_unique<MockProgram>(*pDevice->getExecutionEnvironment());
EXPECT_NE(nullptr, program);
@@ -2204,7 +2199,8 @@ TEST_F(ProgramTests, ValidBinaryWithIGCVersionEqual0) {
}
TEST_F(ProgramTests, RebuildBinaryButNoCompilerInterface) {
auto program = std::make_unique<NoCompilerInterfaceProgram>(*pDevice->getExecutionEnvironment());
auto noCompilerInterfaceExecutionEnvironment = std::make_unique<MockExecutionEnvironment>(nullptr);
auto program = std::make_unique<MockProgram>(*noCompilerInterfaceExecutionEnvironment);
EXPECT_NE(nullptr, program);
cl_device_id deviceId = pContext->getDevice(0);
Device *pDevice = castToObject<Device>(deviceId);
@@ -2238,23 +2234,9 @@ TEST_F(ProgramTests, RebuildBinaryWithRebuildError) {
cl_int link(Program &program, const TranslationArgs &pInputArgs) override { return CL_LINK_PROGRAM_FAILURE; }
};
class MyProgram : public MockProgram {
public:
MyProgram(ExecutionEnvironment &executionEnvironment) : MockProgram(executionEnvironment), cip(nullptr) {}
~MyProgram() override {
delete cip;
}
protected:
CompilerInterface *getCompilerInterface() const override {
cip = new MyCompilerInterface();
return cip;
}
mutable MyCompilerInterface *cip;
};
auto program = std::make_unique<MyProgram>(*pDevice->getExecutionEnvironment());
EXPECT_NE(nullptr, program);
auto cip = std::make_unique<MyCompilerInterface>();
MockExecutionEnvironment executionEnvironment(cip.get());
auto program = std::make_unique<MockProgram>(executionEnvironment);
cl_device_id deviceId = pContext->getDevice(0);
Device *pDevice = castToObject<Device>(deviceId);
program->setDevice(pDevice);
@@ -2297,33 +2279,12 @@ TEST_F(ProgramTests, BuildProgramWithReraFlag) {
char buildInternalOptions[1024];
};
class MyProgram : public Program {
public:
MyProgram(ExecutionEnvironment &executionEnvironment) : Program(executionEnvironment), cip(nullptr) {}
~MyProgram() override {
delete cip;
}
cl_int processGenBinary() override { return CL_SUCCESS; }
MyCompilerInterface *getCompilerInterface() const override {
if (cip == nullptr) {
cip = new MyCompilerInterface;
}
return cip;
}
protected:
mutable MyCompilerInterface *cip;
};
auto program = std::make_unique<MyProgram>(*pDevice->getExecutionEnvironment());
EXPECT_NE(nullptr, program);
auto cip = std::make_unique<MyCompilerInterface>();
MockExecutionEnvironment executionEnvironment(cip.get());
auto program = std::make_unique<SucceedingGenBinaryProgram>(executionEnvironment);
cl_device_id deviceId = pContext->getDevice(0);
Device *pDevice = castToObject<Device>(deviceId);
program->setDevice(pDevice);
MyCompilerInterface *cip = program->getCompilerInterface();
EXPECT_NE(nullptr, cip);
program->setSource((char *)"__kernel mock() {}");
// Check default build options
@@ -2370,7 +2331,6 @@ TEST_F(ProgramTests, BuildProgramWithReraFlag) {
TEST_F(ProgramTests, RebuildBinaryWithProcessGenBinaryError) {
cl_int retVal;
CompilerInterface::getInstance();
auto program = std::make_unique<FailingGenBinaryProgram>(*pDevice->getExecutionEnvironment());
EXPECT_NE(nullptr, program);
@@ -2891,47 +2851,47 @@ TEST_F(ProgramTests, givenCompilerInterfaceWhenCompileIsCalledThenProperIntermed
input.pInput = &inputData;
input.InputSize = 1;
SmallMockCompilerInterface compilerInterface;
auto compilerInterface = new SmallMockCompilerInterface();
auto compilerMain = new MockCIFMain();
compilerInterface.overrideGlobalCompilerInterface();
compilerInterface.SetFclMain(compilerMain);
compilerInterface->SetFclMain(compilerMain);
compilerMain->Retain();
compilerInterface.SetIgcMain(compilerMain);
compilerInterface->SetIgcMain(compilerMain);
compilerMain->setDefaultCreatorFunc<OCLRT::MockIgcOclDeviceCtx>(OCLRT::MockIgcOclDeviceCtx::Create);
compilerMain->setDefaultCreatorFunc<OCLRT::MockFclOclDeviceCtx>(OCLRT::MockFclOclDeviceCtx::Create);
pDevice->getExecutionEnvironment()->compilerInterface.reset(compilerInterface);
compilerInterface.useLlvmText = true;
compilerInterface->useLlvmText = true;
auto programLlvmText = wrapReleasableObjectWithUniquePtr(new MockProgram(*pDevice->getExecutionEnvironment()));
programLlvmText->setDevice(device);
compilerInterface.intermediateRepresentation = IGC::CodeType::spirV;
compilerInterface.compile(*programLlvmText, input);
compilerInterface->intermediateRepresentation = IGC::CodeType::spirV;
compilerInterface->compile(*programLlvmText, input);
EXPECT_FALSE(programLlvmText->getIsSpirV());
compilerInterface.useLlvmText = false;
compilerInterface->useLlvmText = false;
auto programSpirV = wrapReleasableObjectWithUniquePtr(new MockProgram(*pDevice->getExecutionEnvironment()));
programSpirV->setDevice(device);
compilerInterface.intermediateRepresentation = IGC::CodeType::spirV;
compilerInterface.compile(*programSpirV, input);
compilerInterface->intermediateRepresentation = IGC::CodeType::spirV;
compilerInterface->compile(*programSpirV, input);
EXPECT_TRUE(programSpirV->getIsSpirV());
auto programLlvmBc = wrapReleasableObjectWithUniquePtr(new MockProgram(*pDevice->getExecutionEnvironment()));
programLlvmBc->setDevice(device);
compilerInterface.intermediateRepresentation = IGC::CodeType::llvmBc;
compilerInterface.compile(*programLlvmBc, input);
compilerInterface->intermediateRepresentation = IGC::CodeType::llvmBc;
compilerInterface->compile(*programLlvmBc, input);
EXPECT_FALSE(programLlvmBc->getIsSpirV());
}
TEST_F(ProgramTests, givenProgramWithSpirvWhenRebuildProgramIsCalledThenSpirvPathIsTaken) {
auto device = castToObject<Device>(pContext->getDevice(0));
MockCompilerInterface compilerInterface;
auto compilerInterface = new MockCompilerInterface();
auto compilerMain = new MockCIFMain();
compilerInterface.overrideGlobalCompilerInterface();
compilerInterface.SetFclMain(compilerMain);
compilerInterface->SetFclMain(compilerMain);
compilerMain->Retain();
compilerInterface.SetIgcMain(compilerMain);
compilerInterface->SetIgcMain(compilerMain);
compilerMain->setDefaultCreatorFunc<OCLRT::MockIgcOclDeviceCtx>(OCLRT::MockIgcOclDeviceCtx::Create);
compilerMain->setDefaultCreatorFunc<OCLRT::MockFclOclDeviceCtx>(OCLRT::MockFclOclDeviceCtx::Create);
pDevice->getExecutionEnvironment()->compilerInterface.reset(compilerInterface);
std::string receivedInput;
MockCompilerDebugVars debugVars = {};