Skip to content

Commit b490a13

Browse files
jchodorCompute-Runtime-Automation
authored andcommitted
Refactoring/preparation for preferred IR from FCL
NEO will be querying FCL for preferrerd IR (intermediate representation) instead of using llvm bc. Change-Id: I98316b5623557a9651da2a4c5e610ab26421c491
1 parent 3077727 commit b490a13

File tree

17 files changed

+371
-158
lines changed

17 files changed

+371
-158
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!groovy
22
neoDependenciesRev='782940-1037'
33
strategy='EQUAL'
4-
allowedCD=298
4+
allowedCD=297

offline_compiler/offline_compiler.cpp

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ OfflineCompiler::OfflineCompiler() = default;
9696
// dtor
9797
////////////////////////////////////////////////////////////////////////////////
9898
OfflineCompiler::~OfflineCompiler() {
99-
delete[] llvmBinary;
99+
delete[] irBinary;
100100
delete[] genBinary;
101101
delete[] elfBinary;
102102
}
@@ -137,7 +137,7 @@ int OfflineCompiler::buildSourceCode() {
137137
CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> igcOutput;
138138

139139
if (!inputFileLlvm) {
140-
IGC::CodeType::CodeType_t intermediateRepresentation = useLlvmText ? IGC::CodeType::llvmLl : IGC::CodeType::llvmBc;
140+
IGC::CodeType::CodeType_t intermediateRepresentation = useLlvmText ? IGC::CodeType::llvmLl : preferredIntermediateRepresentation;
141141
// sourceCode.size() returns the number of characters without null terminated char
142142
auto fclSrc = CIF::Builtins::CreateConstBuffer(fclMain.get(), sourceCode.c_str(), sourceCode.size() + 1);
143143
auto fclOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), options.c_str(), options.size());
@@ -169,7 +169,8 @@ int OfflineCompiler::buildSourceCode() {
169169
break;
170170
}
171171

172-
storeBinary(llvmBinary, llvmBinarySize, fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw());
172+
storeBinary(irBinary, irBinarySize, fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw());
173+
isSpirV = intermediateRepresentation == IGC::CodeType::spirV;
173174
updateBuildLog(fclOutput->GetBuildLog()->GetMemory<char>(), fclOutput->GetBuildLog()->GetSizeRaw());
174175

175176
igcOutput = igcTranslationCtx->Translate(fclOutput->GetOutput(), fclOptions.get(),
@@ -359,6 +360,7 @@ int OfflineCompiler::initialize(uint32_t numArgs, const char **argv) {
359360
}
360361

361362
fclDeviceCtx->SetOclApiVersion(hwInfo->capabilityTable.clVersionSupport * 10);
363+
preferredIntermediateRepresentation = IGC::CodeType::llvmBc;
362364

363365
this->igcLib.reset(OsLibrary::load(Os::igcDllName));
364366
if (this->igcLib == nullptr) {
@@ -692,9 +694,9 @@ bool OfflineCompiler::generateElfBinary() {
692694

693695
if (retVal) {
694696
sectionNode.Name = "Intel(R) OpenCL LLVM Object";
695-
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
696-
sectionNode.pData = llvmBinary;
697-
sectionNode.DataSize = (uint32_t)llvmBinarySize;
697+
sectionNode.Type = isSpirV ? CLElfLib::SH_TYPE_SPIRV : CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
698+
sectionNode.pData = irBinary;
699+
sectionNode.DataSize = (uint32_t)irBinarySize;
698700
retVal = pElfWriter->addSection(&sectionNode);
699701
}
700702

@@ -758,55 +760,32 @@ void OfflineCompiler::writeOutAllFiles() {
758760
}
759761
}
760762

761-
if (llvmBinary) {
762-
std::string llvmOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
763-
(useLlvmText == true) ? llvmOutputFile.append(fileBase + ".ll") : llvmOutputFile.append(fileBase + ".bc");
764-
765-
if (useOptionsSuffix) {
766-
std::string opts(options.c_str());
767-
std::replace(opts.begin(), opts.end(), ' ', '_');
768-
llvmOutputFile.append(opts);
769-
}
763+
if (irBinary) {
764+
std::string irOutputFileName = generateFilePathForIr(fileBase) + generateOptsSuffix();
770765

771766
writeDataToFile(
772-
llvmOutputFile.c_str(),
773-
llvmBinary,
774-
llvmBinarySize);
767+
irOutputFileName.c_str(),
768+
irBinary,
769+
irBinarySize);
775770
}
776771

777772
if (genBinary) {
778-
std::string genOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
779-
genOutputFile.append(fileBase + ".gen");
780-
781-
if (useOptionsSuffix) {
782-
std::string opts(options.c_str());
783-
std::replace(opts.begin(), opts.end(), ' ', '_');
784-
genOutputFile.append(opts);
785-
}
773+
std::string genOutputFile = generateFilePath(outputDirectory, fileBase, ".gen") + generateOptsSuffix();
786774

787775
writeDataToFile(
788776
genOutputFile.c_str(),
789777
genBinary,
790778
genBinarySize);
791779

792780
if (useCppFile) {
793-
std::string cppOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
794-
cppOutputFile.append(fileBase + ".cpp");
781+
std::string cppOutputFile = generateFilePath(outputDirectory, fileBase, ".cpp");
795782
std::string cpp = parseBinAsCharArray((uint8_t *)genBinary, genBinarySize, fileTrunk);
796783
writeDataToFile(cppOutputFile.c_str(), cpp.c_str(), cpp.size());
797784
}
798785
}
799786

800787
if (elfBinary) {
801-
std::string elfOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
802-
803-
elfOutputFile.append(fileBase + ".bin");
804-
805-
if (useOptionsSuffix) {
806-
std::string opts(options.c_str());
807-
std::replace(opts.begin(), opts.end(), ' ', '_');
808-
elfOutputFile.append(opts);
809-
}
788+
std::string elfOutputFile = generateFilePath(outputDirectory, fileBase, ".bin") + generateOptsSuffix();
810789

811790
writeDataToFile(
812791
elfOutputFile.c_str(),
@@ -815,14 +794,7 @@ void OfflineCompiler::writeOutAllFiles() {
815794
}
816795

817796
if (debugDataBinary) {
818-
std::string debugOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
819-
debugOutputFile.append(fileBase + ".dbg");
820-
821-
if (useOptionsSuffix) {
822-
std::string opts(options.c_str());
823-
std::replace(opts.begin(), opts.end(), ' ', '_');
824-
debugOutputFile.append(opts);
825-
}
797+
std::string debugOutputFile = generateFilePath(outputDirectory, fileBase, ".dbg") + generateOptsSuffix();
826798

827799
writeDataToFile(
828800
debugOutputFile.c_str(),
@@ -856,4 +828,24 @@ bool OfflineCompiler::readOptionsFromFile(std::string &options, const std::strin
856828
return true;
857829
}
858830

831+
std::string generateFilePath(const std::string &directory, const std::string &fileNameBase, const char *extension) {
832+
UNRECOVERABLE_IF(extension == nullptr);
833+
834+
if (directory.empty()) {
835+
return fileNameBase + extension;
836+
}
837+
838+
bool hasTrailingSlash = (*directory.rbegin() == '/');
839+
std::string ret;
840+
ret.reserve(directory.size() + (hasTrailingSlash ? 0 : 1) + fileNameBase.size() + strlen(extension) + 1);
841+
ret.append(directory);
842+
if (false == hasTrailingSlash) {
843+
ret.append("/", 1);
844+
}
845+
ret.append(fileNameBase);
846+
ret.append(extension);
847+
848+
return ret;
849+
}
850+
859851
} // namespace OCLRT

offline_compiler/offline_compiler.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum ErrorCode {
4141
PRINT_USAGE = -5152,
4242
};
4343

44+
std::string generateFilePath(const std::string &directory, const std::string &fileNameBase, const char *extension);
45+
4446
class OfflineCompiler {
4547
public:
4648
static OfflineCompiler *create(uint32_t numArgs, const char **argv, int &retVal);
@@ -72,6 +74,16 @@ class OfflineCompiler {
7274
int buildSourceCode();
7375
void updateBuildLog(const char *pErrorString, const size_t errorStringSize);
7476
bool generateElfBinary();
77+
std::string generateFilePathForIr(const std::string &fileNameBase) {
78+
const char *ext = (isSpirV) ? ".spv" : ".bc";
79+
return generateFilePath(outputDirectory, fileNameBase, useLlvmText ? ".ll" : ext);
80+
}
81+
82+
std::string generateOptsSuffix() {
83+
std::string suffix{useOptionsSuffix ? options : ""};
84+
std::replace(suffix.begin(), suffix.end(), ' ', '_');
85+
return suffix;
86+
}
7587
void writeOutAllFiles();
7688
const HardwareInfo *hwInfo = nullptr;
7789

@@ -95,8 +107,9 @@ class OfflineCompiler {
95107
size_t elfBinarySize = 0;
96108
char *genBinary = nullptr;
97109
size_t genBinarySize = 0;
98-
char *llvmBinary = nullptr;
99-
size_t llvmBinarySize = 0;
110+
char *irBinary = nullptr;
111+
size_t irBinarySize = 0;
112+
bool isSpirV = false;
100113
char *debugDataBinary = nullptr;
101114
size_t debugDataBinarySize = 0;
102115

@@ -107,5 +120,6 @@ class OfflineCompiler {
107120
std::unique_ptr<OsLibrary> fclLib = nullptr;
108121
CIF::RAII::UPtr_t<CIF::CIFMain> fclMain = nullptr;
109122
CIF::RAII::UPtr_t<IGC::FclOclDeviceCtxTagOCL> fclDeviceCtx = nullptr;
123+
IGC::CodeType::CodeType_t preferredIntermediateRepresentation;
110124
};
111125
} // namespace OCLRT

runtime/compiler_interface/compiler_interface.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ cl_int CompilerInterface::build(
7272
highLevelCodeType = IGC::CodeType::oclC;
7373
if (useLlvmText == true) {
7474
intermediateCodeType = IGC::CodeType::llvmLl;
75-
} else {
76-
intermediateCodeType = IGC::CodeType::llvmBc;
7775
}
7876
}
7977

@@ -90,7 +88,10 @@ cl_int CompilerInterface::build(
9088
uint32_t numDevices = static_cast<uint32_t>(program.getNumDevices());
9189
for (uint32_t i = 0; i < numDevices; i++) {
9290
const auto &device = program.getDevice(i);
93-
UNRECOVERABLE_IF(intermediateCodeType == IGC::CodeType::undefined);
91+
if (intermediateCodeType == IGC::CodeType::undefined) {
92+
UNRECOVERABLE_IF(highLevelCodeType != IGC::CodeType::oclC);
93+
intermediateCodeType = getPreferredIntermediateRepresentation(device);
94+
}
9495

9596
bool binaryLoaded = false;
9697
std::string kernelFileHash;
@@ -124,7 +125,7 @@ cl_int CompilerInterface::build(
124125
return CL_BUILD_PROGRAM_FAILURE;
125126
}
126127

127-
program.storeLlvmBinary(fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw());
128+
program.storeIrBinary(fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw(), intermediateCodeType == IGC::CodeType::spirV);
128129
program.updateBuildLog(&device, fclOutput->GetBuildLog()->GetMemory<char>(), fclOutput->GetBuildLog()->GetSizeRaw());
129130

130131
fclOutput->GetOutput()->Retain(); // will be used as input to compiler
@@ -186,14 +187,15 @@ cl_int CompilerInterface::compile(
186187
inType = IGC::CodeType::elf;
187188
if (useLlvmText == true) {
188189
outType = IGC::CodeType::llvmLl;
189-
} else {
190-
outType = IGC::CodeType::llvmBc;
191190
}
192191
}
193192

194193
uint32_t numDevices = static_cast<uint32_t>(program.getNumDevices());
195194
for (uint32_t i = 0; i < numDevices; i++) {
196195
const auto &device = program.getDevice(i);
196+
if (outType == IGC::CodeType::undefined) {
197+
outType = getPreferredIntermediateRepresentation(device);
198+
}
197199

198200
if (fromIntermediate == false) {
199201
auto fclSrc = CIF::Builtins::CreateConstBuffer(fclMain.get(), inputArgs.pInput, inputArgs.InputSize);
@@ -214,13 +216,13 @@ cl_int CompilerInterface::compile(
214216
return CL_COMPILE_PROGRAM_FAILURE;
215217
}
216218

217-
program.storeLlvmBinary(fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw());
219+
program.storeIrBinary(fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw(), outType == IGC::CodeType::spirV);
218220
program.updateBuildLog(&device, fclOutput->GetBuildLog()->GetMemory<char>(), fclOutput->GetBuildLog()->GetSizeRaw());
219221
} else {
220222
char *pOutput;
221223
uint32_t OutputSize;
222224
program.getSource(pOutput, OutputSize);
223-
program.storeLlvmBinary(pOutput, OutputSize);
225+
program.storeIrBinary(pOutput, OutputSize, outType == IGC::CodeType::spirV);
224226
}
225227
}
226228

@@ -249,8 +251,8 @@ cl_int CompilerInterface::link(
249251
CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> currOut;
250252
inSrc->Retain(); // shared with currSrc
251253
CIF::RAII::UPtr_t<CIF::Builtins::BufferSimple> currSrc(inSrc.get());
252-
253-
IGC::CodeType::CodeType_t translationChain[] = {IGC::CodeType::elf, IGC::CodeType::llvmBc, IGC::CodeType::oclGenBin};
254+
auto intermediateRepresentation = getPreferredIntermediateRepresentation(device);
255+
IGC::CodeType::CodeType_t translationChain[] = {IGC::CodeType::elf, intermediateRepresentation, IGC::CodeType::oclGenBin};
254256
constexpr size_t numTranslations = sizeof(translationChain) / sizeof(translationChain[0]);
255257
for (size_t ti = 1; ti < numTranslations; ti++) {
256258
IGC::CodeType::CodeType_t inType = translationChain[ti - 1];
@@ -295,7 +297,8 @@ cl_int CompilerInterface::createLibrary(
295297
auto igcOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), inputArgs.pOptions, inputArgs.OptionsSize);
296298
auto igcInternalOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), inputArgs.pInternalOptions, inputArgs.InternalOptionsSize);
297299

298-
auto igcTranslationCtx = createIgcTranslationCtx(device, IGC::CodeType::elf, IGC::CodeType::llvmBc);
300+
auto intermediateRepresentation = getPreferredIntermediateRepresentation(device);
301+
auto igcTranslationCtx = createIgcTranslationCtx(device, IGC::CodeType::elf, intermediateRepresentation);
299302

300303
auto igcOutput = translate(igcTranslationCtx.get(), igcSrc.get(),
301304
igcOptions.get(), igcInternalOptions.get());
@@ -309,7 +312,7 @@ cl_int CompilerInterface::createLibrary(
309312
return CL_BUILD_PROGRAM_FAILURE;
310313
}
311314

312-
program.storeLlvmBinary(igcOutput->GetOutput()->GetMemory<char>(), igcOutput->GetOutput()->GetSizeRaw());
315+
program.storeIrBinary(igcOutput->GetOutput()->GetMemory<char>(), igcOutput->GetOutput()->GetSizeRaw(), intermediateRepresentation == IGC::CodeType::spirV);
313316
program.updateBuildLog(&device, igcOutput->GetBuildLog()->GetMemory<char>(), igcOutput->GetBuildLog()->GetSizeRaw());
314317
}
315318

@@ -362,17 +365,17 @@ BinaryCache *CompilerInterface::replaceBinaryCache(BinaryCache *newCache) {
362365
return res;
363366
}
364367

365-
CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> CompilerInterface::createFclTranslationCtx(const Device &device, IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType) {
368+
IGC::FclOclDeviceCtxTagOCL *CompilerInterface::getFclDeviceCtx(const Device &device) {
366369
auto it = fclDeviceContexts.find(&device);
367370
if (it != fclDeviceContexts.end()) {
368-
return it->second->CreateTranslationCtx(inType, outType);
371+
return it->second.get();
369372
}
370373

371374
{
372375
auto ulock = this->lock();
373376
it = fclDeviceContexts.find(&device);
374377
if (it != fclDeviceContexts.end()) {
375-
return it->second->CreateTranslationCtx(inType, outType);
378+
return it->second.get();
376379
}
377380

378381
if (fclMain == nullptr) {
@@ -388,12 +391,27 @@ CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> CompilerInterface::createFclT
388391
newDeviceCtx->SetOclApiVersion(device.getHardwareInfo().capabilityTable.clVersionSupport * 10);
389392
fclDeviceContexts[&device] = std::move(newDeviceCtx);
390393

391-
if (fclBaseTranslationCtx == nullptr) {
392-
fclBaseTranslationCtx = fclDeviceContexts[&device]->CreateTranslationCtx(inType, outType);
393-
}
394+
return fclDeviceContexts[&device].get();
395+
}
396+
}
394397

395-
return fclDeviceContexts[&device]->CreateTranslationCtx(inType, outType);
398+
IGC::CodeType::CodeType_t CompilerInterface::getPreferredIntermediateRepresentation(const Device &device) {
399+
return IGC::CodeType::llvmBc;
400+
}
401+
402+
CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> CompilerInterface::createFclTranslationCtx(const Device &device, IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType) {
403+
404+
auto deviceCtx = getFclDeviceCtx(device);
405+
if (deviceCtx == nullptr) {
406+
DEBUG_BREAK_IF(true); // could not create device context
407+
return nullptr;
408+
}
409+
410+
if (fclBaseTranslationCtx == nullptr) {
411+
fclBaseTranslationCtx = fclDeviceContexts[&device]->CreateTranslationCtx(inType, outType);
396412
}
413+
414+
return deviceCtx->CreateTranslationCtx(inType, outType);
397415
}
398416

399417
CIF::RAII::UPtr_t<IGC::IgcOclTranslationCtxTagOCL> CompilerInterface::createIgcTranslationCtx(const Device &device, IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType) {

runtime/compiler_interface/compiler_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ class CompilerInterface {
117117
std::map<const Device *, fclDevCtxUptr> fclDeviceContexts;
118118
CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> fclBaseTranslationCtx = nullptr;
119119

120+
MOCKABLE_VIRTUAL IGC::FclOclDeviceCtxTagOCL *getFclDeviceCtx(const Device &device);
121+
MOCKABLE_VIRTUAL IGC::CodeType::CodeType_t getPreferredIntermediateRepresentation(const Device &device);
122+
120123
MOCKABLE_VIRTUAL CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> createFclTranslationCtx(const Device &device,
121124
IGC::CodeType::CodeType_t inType,
122125
IGC::CodeType::CodeType_t outType);

runtime/program/link.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ cl_int Program::link(
9696
break;
9797
}
9898
inputProgramsInternal.push_back(pInputProgObj);
99-
if ((pInputProgObj->llvmBinary == nullptr) || (pInputProgObj->llvmBinarySize == 0)) {
99+
if ((pInputProgObj->irBinary == nullptr) || (pInputProgObj->irBinarySize == 0)) {
100100
retVal = CL_INVALID_PROGRAM;
101101
break;
102102
}
@@ -107,8 +107,8 @@ cl_int Program::link(
107107
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
108108
}
109109
sectionNode.Flags = 0;
110-
sectionNode.pData = pInputProgObj->llvmBinary;
111-
sectionNode.DataSize = static_cast<unsigned int>(pInputProgObj->llvmBinarySize);
110+
sectionNode.pData = pInputProgObj->irBinary;
111+
sectionNode.DataSize = static_cast<unsigned int>(pInputProgObj->irBinarySize);
112112

113113
pElfWriter->addSection(&sectionNode);
114114
}

0 commit comments

Comments
 (0)