/* * Copyright (c) 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #include "cif/common/cif_main.h" #include "cif/helpers/error.h" #include "cif/import/library_api.h" #include "ocl_igc_interface/code_type.h" #include "ocl_igc_interface/fcl_ocl_device_ctx.h" #include "ocl_igc_interface/igc_ocl_device_ctx.h" #include "ocl_igc_interface/platform_helper.h" #include "offline_compiler.h" #include "igfxfmid.h" #include "runtime/helpers/file_io.h" #include "runtime/os_interface/debug_settings_manager.h" #include "runtime/os_interface/os_inc_base.h" #include "runtime/os_interface/os_library.h" #include "runtime/helpers/string.h" #include "runtime/helpers/debug_helpers.h" #include "runtime/helpers/hw_info.h" #include "runtime/helpers/validators.h" #include "runtime/platform/extensions.h" #include "elf/writer.h" #include #include #include #include #ifdef _WIN32 #include #define MakeDirectory _mkdir #define GetCurrentWorkingDirectory _getcwd #else #include #define MakeDirectory(dir) mkdir(dir, 0777) #define GetCurrentWorkingDirectory getcwd #endif namespace OCLRT { CIF::CIFMain *createMainNoSanitize(CIF::CreateCIFMainFunc_t createFunc); //////////////////////////////////////////////////////////////////////////////// // StringsAreEqual //////////////////////////////////////////////////////////////////////////////// bool stringsAreEqual(const char *string1, const char *string2) { if (string2 == nullptr) return false; return (strcmp(string1, string2) == 0); } //////////////////////////////////////////////////////////////////////////////// // convertToPascalCase //////////////////////////////////////////////////////////////////////////////// std::string convertToPascalCase(const std::string &inString) { std::string outString; bool capitalize = true; for (unsigned int i = 0; i < inString.length(); i++) { if (isalpha(inString[i]) && capitalize == true) { outString += toupper(inString[i]); capitalize = false; } else if (inString[i] == '_') { capitalize = true; } else { outString += inString[i]; } } return outString; } //////////////////////////////////////////////////////////////////////////////// // ctor //////////////////////////////////////////////////////////////////////////////// OfflineCompiler::OfflineCompiler() = default; //////////////////////////////////////////////////////////////////////////////// // dtor //////////////////////////////////////////////////////////////////////////////// OfflineCompiler::~OfflineCompiler() { delete[] llvmBinary; delete[] genBinary; delete[] elfBinary; } //////////////////////////////////////////////////////////////////////////////// // Create //////////////////////////////////////////////////////////////////////////////// OfflineCompiler *OfflineCompiler::create(uint32_t numArgs, const char **argv, int &retVal) { retVal = CL_SUCCESS; auto pOffCompiler = new OfflineCompiler(); if (pOffCompiler) { retVal = pOffCompiler->initialize(numArgs, argv); } if (retVal != CL_SUCCESS) { delete pOffCompiler; pOffCompiler = nullptr; } return pOffCompiler; } //////////////////////////////////////////////////////////////////////////////// // buildSourceCode //////////////////////////////////////////////////////////////////////////////// int OfflineCompiler::buildSourceCode() { int retVal = CL_SUCCESS; do { if (strcmp(sourceCode.c_str(), "") == 0) { retVal = CL_INVALID_PROGRAM; break; } UNRECOVERABLE_IF(fclDeviceCtx == nullptr); UNRECOVERABLE_IF(igcDeviceCtx == nullptr); CIF::RAII::UPtr_t igcOutput; if (!inputFileLlvm) { IGC::CodeType::CodeType_t intermediateRepresentation = useLlvmText ? IGC::CodeType::llvmLl : IGC::CodeType::llvmBc; auto fclSrc = CIF::Builtins::CreateConstBuffer(fclMain.get(), sourceCode.c_str(), sourceCode.size()); auto fclOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), options.c_str(), options.size()); auto fclInternalOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), internalOptions.c_str(), internalOptions.size()); auto fclTranslationCtx = fclDeviceCtx->CreateTranslationCtx(IGC::CodeType::oclC, intermediateRepresentation); auto igcTranslationCtx = igcDeviceCtx->CreateTranslationCtx(intermediateRepresentation, IGC::CodeType::oclGenBin); if (false == OCLRT::areNotNullptr(fclSrc.get(), fclOptions.get(), fclInternalOptions.get(), fclTranslationCtx.get(), igcTranslationCtx.get())) { retVal = CL_OUT_OF_HOST_MEMORY; break; } auto fclOutput = fclTranslationCtx->Translate(fclSrc.get(), fclOptions.get(), fclInternalOptions.get(), nullptr, 0); if (fclOutput == nullptr) { retVal = CL_OUT_OF_HOST_MEMORY; break; } UNRECOVERABLE_IF(fclOutput->GetBuildLog() == nullptr); UNRECOVERABLE_IF(fclOutput->GetOutput() == nullptr); if (fclOutput->Successful() == false) { updateBuildLog(fclOutput->GetBuildLog()->GetMemory(), fclOutput->GetBuildLog()->GetSizeRaw()); retVal = CL_BUILD_PROGRAM_FAILURE; break; } storeBinary(llvmBinary, llvmBinarySize, fclOutput->GetOutput()->GetMemory(), fclOutput->GetOutput()->GetSizeRaw()); updateBuildLog(fclOutput->GetBuildLog()->GetMemory(), fclOutput->GetBuildLog()->GetSizeRaw()); igcOutput = igcTranslationCtx->Translate(fclOutput->GetOutput(), fclOptions.get(), fclInternalOptions.get(), nullptr, 0); } else { auto igcSrc = CIF::Builtins::CreateConstBuffer(igcMain.get(), sourceCode.c_str(), sourceCode.size()); auto igcOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), nullptr, 0); auto igcInternalOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), internalOptions.c_str(), internalOptions.size()); auto igcTranslationCtx = igcDeviceCtx->CreateTranslationCtx(IGC::CodeType::llvmLl, IGC::CodeType::oclGenBin); igcOutput = igcTranslationCtx->Translate(igcSrc.get(), igcOptions.get(), igcInternalOptions.get(), nullptr, 0); } if (igcOutput == nullptr) { retVal = CL_OUT_OF_HOST_MEMORY; break; } UNRECOVERABLE_IF(igcOutput->GetBuildLog() == nullptr); UNRECOVERABLE_IF(igcOutput->GetOutput() == nullptr); storeBinary(genBinary, genBinarySize, igcOutput->GetOutput()->GetMemory(), igcOutput->GetOutput()->GetSizeRaw()); updateBuildLog(igcOutput->GetBuildLog()->GetMemory(), igcOutput->GetBuildLog()->GetSizeRaw()); retVal = igcOutput->Successful() ? CL_SUCCESS : CL_BUILD_PROGRAM_FAILURE; } while (0); return retVal; } //////////////////////////////////////////////////////////////////////////////// // build //////////////////////////////////////////////////////////////////////////////// int OfflineCompiler::build() { int retVal = CL_SUCCESS; retVal = buildSourceCode(); if (retVal == CL_SUCCESS) { generateElfBinary(); writeOutAllFiles(); } return retVal; } //////////////////////////////////////////////////////////////////////////////// // updateBuildLog //////////////////////////////////////////////////////////////////////////////// void OfflineCompiler::updateBuildLog(const char *pErrorString, const size_t errorStringSize) { std::string errorString = (errorStringSize && pErrorString) ? std::string(pErrorString, pErrorString + errorStringSize) : ""; if (errorString[0] != '\0') { if (buildLog.empty()) { buildLog.assign(errorString); } else { buildLog.append("\n" + errorString); } } } //////////////////////////////////////////////////////////////////////////////// // getBuildLog //////////////////////////////////////////////////////////////////////////////// std::string &OfflineCompiler::getBuildLog() { return buildLog; } //////////////////////////////////////////////////////////////////////////////// // getHardwareInfo //////////////////////////////////////////////////////////////////////////////// int OfflineCompiler::getHardwareInfo(const char *pDeviceName) { int retVal = CL_INVALID_DEVICE; for (unsigned int productId = 0; productId < IGFX_MAX_PRODUCT; ++productId) { if (stringsAreEqual(pDeviceName, hardwarePrefix[productId])) { if (hardwareInfoTable[productId]) { hwInfo = hardwareInfoTable[productId]; retVal = CL_SUCCESS; break; } } } return retVal; } //////////////////////////////////////////////////////////////////////////////// // getStringWithinDelimiters //////////////////////////////////////////////////////////////////////////////// std::string OfflineCompiler::getStringWithinDelimiters(const std::string &src) { size_t start = src.find("R\"===("); size_t stop = src.find(")===\""); DEBUG_BREAK_IF(std::string::npos == start); DEBUG_BREAK_IF(std::string::npos == stop); start += strlen("R\"===("); size_t size = stop - start; std::string dst(src, start, size); return dst; } //////////////////////////////////////////////////////////////////////////////// // Initialize //////////////////////////////////////////////////////////////////////////////// int OfflineCompiler::initialize(uint32_t numArgs, const char **argv) { int retVal = CL_SUCCESS; const char *pSource = nullptr; void *pSourceFromFile = nullptr; size_t sourceFromFileSize = 0; retVal = parseCommandLine(numArgs, argv); if (retVal != CL_SUCCESS) { return retVal; } parseDebugSettings(); if (options.empty()) { // try to read options from file if not provided by commandline std::string optionsFileName = inputFile; size_t ext_start = optionsFileName.find(".cl"); if (ext_start != std::string::npos) { optionsFileName.replace(ext_start, strlen(".cl"), "_options.txt"); void *pOptions = nullptr; size_t optionsSize = loadDataFromFile(optionsFileName.c_str(), pOptions); if (optionsSize > 0) { options = (char *)pOptions; // Remove comment containing copyright header size_t commentBegin = options.find_first_of("/*"); size_t commentEnd = options.find_last_of("*/"); if (commentBegin != std::string::npos && commentEnd != std::string::npos) { options = options.replace(commentBegin, commentEnd - commentBegin + 1, ""); size_t optionsBegin = options.find_first_not_of(" \t\n\r"); if (optionsBegin != std::string::npos) { options = options.substr(optionsBegin, options.length()); } } auto trimPos = options.find_last_not_of(" \n\r"); options = options.substr(0, trimPos + 1); if (!isQuiet()) printf("Building with options:\n%s\n", options.c_str()); } deleteDataReadFromFile(pOptions); } } // set up the device inside the program sourceFromFileSize = loadDataFromFile(inputFile.c_str(), pSourceFromFile); struct Helper { static void deleter(void *ptr) { deleteDataReadFromFile(ptr); } }; auto sourceRaii = std::unique_ptr{pSourceFromFile, Helper::deleter}; if (sourceFromFileSize == 0) { retVal = INVALID_FILE; return retVal; } // we also accept files used as runtime builtins pSource = strstr((const char *)pSourceFromFile, "R\"===("); sourceCode = (pSource != nullptr) ? getStringWithinDelimiters((char *)pSourceFromFile) : (char *)pSourceFromFile; this->fclLib.reset(OsLibrary::load(Os::frontEndDllName)); if (this->fclLib == nullptr) { return CL_OUT_OF_HOST_MEMORY; } auto fclCreateMain = reinterpret_cast(this->fclLib->getProcAddress(CIF::CreateCIFMainFuncName)); if (fclCreateMain == nullptr) { return CL_OUT_OF_HOST_MEMORY; } this->fclMain = CIF::RAII::UPtr(createMainNoSanitize(fclCreateMain)); if (this->fclMain == nullptr) { return CL_OUT_OF_HOST_MEMORY; } if (false == this->fclMain->IsCompatible()) { // given FCL is not compatible DEBUG_BREAK_IF(true); return CL_OUT_OF_HOST_MEMORY; } this->fclDeviceCtx = this->fclMain->CreateInterface(); if (this->fclDeviceCtx == nullptr) { return CL_OUT_OF_HOST_MEMORY; } fclDeviceCtx->SetOclApiVersion(hwInfo->capabilityTable.clVersionSupport * 10); this->igcLib.reset(OsLibrary::load(Os::igcDllName)); if (this->igcLib == nullptr) { return CL_OUT_OF_HOST_MEMORY; } auto igcCreateMain = reinterpret_cast(this->igcLib->getProcAddress(CIF::CreateCIFMainFuncName)); if (igcCreateMain == nullptr) { return CL_OUT_OF_HOST_MEMORY; } this->igcMain = CIF::RAII::UPtr(createMainNoSanitize(igcCreateMain)); if (this->igcMain == nullptr) { return CL_OUT_OF_HOST_MEMORY; } if (false == this->igcMain->IsCompatible()) { // given IGC is not compatible DEBUG_BREAK_IF(true); return CL_OUT_OF_HOST_MEMORY; } this->igcDeviceCtx = this->igcMain->CreateInterface(); if (this->igcDeviceCtx == nullptr) { return CL_OUT_OF_HOST_MEMORY; } this->igcDeviceCtx->SetProfilingTimerResolution(static_cast(hwInfo->capabilityTable.defaultProfilingTimerResolution)); auto igcPlatform = this->igcDeviceCtx->GetPlatformHandle(); auto igcGtSystemInfo = this->igcDeviceCtx->GetGTSystemInfoHandle(); auto igcFeWa = this->igcDeviceCtx->GetIgcFeaturesAndWorkaroundsHandle(); if ((igcPlatform == nullptr) || (igcGtSystemInfo == nullptr) || (igcFeWa == nullptr)) { return CL_OUT_OF_HOST_MEMORY; } IGC::PlatformHelper::PopulateInterfaceWith(*igcPlatform.get(), *hwInfo->pPlatform); IGC::GtSysInfoHelper::PopulateInterfaceWith(*igcGtSystemInfo.get(), *hwInfo->pSysInfo); // populate with features igcFeWa.get()->SetFtrDesktop(hwInfo->pSkuTable->ftrDesktop); igcFeWa.get()->SetFtrChannelSwizzlingXOREnabled(hwInfo->pSkuTable->ftrChannelSwizzlingXOREnabled); igcFeWa.get()->SetFtrGtBigDie(hwInfo->pSkuTable->ftrGtBigDie); igcFeWa.get()->SetFtrGtMediumDie(hwInfo->pSkuTable->ftrGtMediumDie); igcFeWa.get()->SetFtrGtSmallDie(hwInfo->pSkuTable->ftrGtSmallDie); igcFeWa.get()->SetFtrGT1(hwInfo->pSkuTable->ftrGT1); igcFeWa.get()->SetFtrGT1_5(hwInfo->pSkuTable->ftrGT1_5); igcFeWa.get()->SetFtrGT2(hwInfo->pSkuTable->ftrGT2); igcFeWa.get()->SetFtrGT3(hwInfo->pSkuTable->ftrGT3); igcFeWa.get()->SetFtrGT4(hwInfo->pSkuTable->ftrGT4); igcFeWa.get()->SetFtrIVBM0M1Platform(hwInfo->pSkuTable->ftrIVBM0M1Platform); igcFeWa.get()->SetFtrGTL(hwInfo->pSkuTable->ftrGT1); igcFeWa.get()->SetFtrGTM(hwInfo->pSkuTable->ftrGT2); igcFeWa.get()->SetFtrGTH(hwInfo->pSkuTable->ftrGT3); igcFeWa.get()->SetFtrSGTPVSKUStrapPresent(hwInfo->pSkuTable->ftrSGTPVSKUStrapPresent); igcFeWa.get()->SetFtrGTA(hwInfo->pSkuTable->ftrGTA); igcFeWa.get()->SetFtrGTC(hwInfo->pSkuTable->ftrGTC); igcFeWa.get()->SetFtrGTX(hwInfo->pSkuTable->ftrGTX); igcFeWa.get()->SetFtr5Slice(hwInfo->pSkuTable->ftr5Slice); igcFeWa.get()->SetFtrGpGpuMidThreadLevelPreempt(hwInfo->pSkuTable->ftrGpGpuMidThreadLevelPreempt); igcFeWa.get()->SetFtrIoMmuPageFaulting(hwInfo->pSkuTable->ftrIoMmuPageFaulting); igcFeWa.get()->SetFtrWddm2Svm(hwInfo->pSkuTable->ftrWddm2Svm); igcFeWa.get()->SetFtrPooledEuEnabled(hwInfo->pSkuTable->ftrPooledEuEnabled); igcFeWa.get()->SetFtrResourceStreamer(hwInfo->pSkuTable->ftrResourceStreamer); return retVal; } //////////////////////////////////////////////////////////////////////////////// // ParseCommandLine //////////////////////////////////////////////////////////////////////////////// int OfflineCompiler::parseCommandLine(uint32_t numArgs, const char **argv) { int retVal = CL_SUCCESS; bool compile32 = false; bool compile64 = false; if (numArgs < 2) { printUsage(); retVal = PRINT_USAGE; } for (uint32_t argIndex = 1; argIndex < numArgs; argIndex++) { if ((stringsAreEqual(argv[argIndex], "-file")) && (argIndex + 1 < numArgs)) { inputFile = argv[argIndex + 1]; argIndex++; } else if (stringsAreEqual(argv[argIndex], "-32")) { compile32 = true; internalOptions.append(" -m32 "); } else if (stringsAreEqual(argv[argIndex], "-64")) { compile64 = true; internalOptions.append(" -m64 "); } else if (stringsAreEqual(argv[argIndex], "-cl-intel-greater-than-4GB-buffer-required")) { internalOptions.append(" -cl-intel-greater-than-4GB-buffer-required "); } else if ((stringsAreEqual(argv[argIndex], "-device")) && (argIndex + 1 < numArgs)) { deviceName = argv[argIndex + 1]; argIndex++; } else if (stringsAreEqual(argv[argIndex], "-llvm_text")) { useLlvmText = true; } else if (stringsAreEqual(argv[argIndex], "-llvm_input")) { inputFileLlvm = true; } else if (stringsAreEqual(argv[argIndex], "-cpp_file")) { useCppFile = true; } else if ((stringsAreEqual(argv[argIndex], "-options")) && (argIndex + 1 < numArgs)) { options = argv[argIndex + 1]; argIndex++; } else if (stringsAreEqual(argv[argIndex], "-options_name")) { useOptionsSuffix = true; } else if ((stringsAreEqual(argv[argIndex], "-out_dir")) && (argIndex + 1 < numArgs)) { outputDirectory = argv[argIndex + 1]; argIndex++; } else if (stringsAreEqual(argv[argIndex], "-q")) { quiet = true; } else if (stringsAreEqual(argv[argIndex], "-?")) { printUsage(); retVal = PRINT_USAGE; } else { printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex]); retVal = INVALID_COMMAND_LINE; break; } } if (retVal == CL_SUCCESS) { if (compile32 && compile64) { printf("Error: Cannot compile for 32-bit and 64-bit, please choose one.\n"); retVal = INVALID_COMMAND_LINE; } else if (inputFile.empty()) { printf("Error: Input file name missing.\n"); retVal = INVALID_COMMAND_LINE; } else if (deviceName.empty()) { printf("Error: Device name missing.\n"); retVal = INVALID_COMMAND_LINE; } else if (!fileExists(inputFile)) { printf("Error: Input file %s missing.\n", inputFile.c_str()); retVal = INVALID_FILE; } else { retVal = getHardwareInfo(deviceName.c_str()); if (retVal != CL_SUCCESS) { printf("Error: Cannot get HW Info for device %s.\n", deviceName.c_str()); } std::string extensionsList = getExtensionsList(*hwInfo); internalOptions.append(convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str())); } } return retVal; } //////////////////////////////////////////////////////////////////////////////// // ParseCommandLine //////////////////////////////////////////////////////////////////////////////// void OfflineCompiler::parseDebugSettings() { if (DebugManager.flags.EnableStatelessToStatefulBufferOffsetOpt.get()) { internalOptions += "-cl-intel-has-buffer-offset-arg "; } } //////////////////////////////////////////////////////////////////////////////// // ParseBinAsCharArray //////////////////////////////////////////////////////////////////////////////// std::string OfflineCompiler::parseBinAsCharArray(uint8_t *binary, size_t size, std::string &deviceName, std::string &fileName) { std::string builtinName = convertToPascalCase(fileName); std::ostringstream out; // Convert binary to cpp out << "#include \n"; out << "#include \n\n"; out << "size_t " << builtinName << "BinarySize_" << deviceName << " = " << size << ";\n"; out << "uint32_t " << builtinName << "Binary_" << deviceName << "[" << (size + 3) / 4 << "] = {" << std::endl << " "; uint32_t *binaryUint = (uint32_t *)binary; for (size_t i = 0; i < (size + 3) / 4; i++) { if (i != 0) { out << ", "; if (i % 8 == 0) { out << std::endl << " "; } } if (i < size / 4) { out << "0x" << std::hex << std::setw(8) << std::setfill('0') << binaryUint[i]; } else { uint32_t lastBytes = size & 0x3; uint32_t lastUint = 0; uint8_t *pLastUint = (uint8_t *)&lastUint; for (uint32_t j = 0; j < lastBytes; j++) { pLastUint[sizeof(uint32_t) - 1 - j] = binary[i * 4 + j]; } out << "0x" << std::hex << std::setw(8) << std::setfill('0') << lastUint; } } out << "};" << std::endl; out << std::endl << "#include \"runtime/built_ins/registry/built_ins_registry.h\"\n" << std::endl; out << "namespace OCLRT {" << std::endl; out << "static RegisterEmbeddedResource register" << builtinName << "Bin(" << std::endl; out << " createBuiltinResourceName(" << std::endl; out << " EBuiltInOps::" << builtinName << "," << std::endl; out << " BuiltinCode::getExtension(BuiltinCode::ECodeType::Binary), \"" << deviceName << "\", 0)" << std::endl; out << " .c_str()," << std::endl; out << " (const char *)" << builtinName << "Binary" << "_" << deviceName << "," << std::endl; out << " " << builtinName << "BinarySize_" << deviceName << ");" << std::endl; out << "}" << std::endl; return out.str(); } //////////////////////////////////////////////////////////////////////////////// // GetFileNameTrunk //////////////////////////////////////////////////////////////////////////////// std::string OfflineCompiler::getFileNameTrunk(std::string &filePath) { size_t slashPos = filePath.find_last_of("\\/", filePath.size()) + 1; size_t extPos = filePath.find_last_of(".", filePath.size()); if (extPos == std::string::npos) { extPos = filePath.size(); } std::string fileName; std::string fileTrunk = filePath.substr(slashPos, (extPos - slashPos)); return fileTrunk; } // std::string getDevicesTypes() { std::list prefixes; for (int j = 0; j < IGFX_MAX_PRODUCT; j++) { if (hardwarePrefix[j] == nullptr) continue; prefixes.push_back(hardwarePrefix[j]); } ostringstream os; for (auto it = prefixes.begin(); it != prefixes.end(); it++) { if (it != prefixes.begin()) os << ","; os << *it; } return os.str(); } //////////////////////////////////////////////////////////////////////////////// // PrintUsage //////////////////////////////////////////////////////////////////////////////// void OfflineCompiler::printUsage() { printf("Compiles CL files into llvm (.bc or .ll), gen isa (.gen), and binary files (.bin)\n\n"); printf("cloc -file -device [-outdir ]\n\n"); printf(" -file Indicates the CL kernel file to be compiled.\n"); printf(" -device Indicates which device for which we will compile.\n"); printf(" can be: %s\n", getDevicesTypes().c_str()); printf(" -out_dir Indicates the directory into which the compiled files\n"); printf(" will be placed.\n"); printf(" -llvm_text Readable LLVM text will be output in a .ll file instead of\n"); printf(" through the default lllvm binary (.bc) file.\n"); printf(" -llvm_input Indicates input file is llvm source\n"); printf(" -cpp_file Cpp file with scheduler program binary will be generated."); printf(" -options Compiler options.\n"); printf(" -options_name Add suffix with compile options to filename\n"); printf(" -32 Force compile to 32-bit binary.\n"); printf(" -64 Force compile to 64-bit binary.\n"); printf(" -q Be more quiet. print only warnings and errors.\n"); printf(" -? Print this usage message.\n"); } //////////////////////////////////////////////////////////////////////////////// // StoreBinary //////////////////////////////////////////////////////////////////////////////// void OfflineCompiler::storeBinary( char *&pDst, size_t &dstSize, const void *pSrc, const size_t srcSize) { dstSize = 0; DEBUG_BREAK_IF(!(pSrc && srcSize > 0)); delete[] pDst; pDst = new char[srcSize]; dstSize = (cl_uint)srcSize; memcpy_s(pDst, dstSize, pSrc, srcSize); } //////////////////////////////////////////////////////////////////////////////// // GenerateElfBinary //////////////////////////////////////////////////////////////////////////////// bool OfflineCompiler::generateElfBinary() { bool retVal = true; CLElfLib::CElfWriter *pElfWriter = nullptr; if (!genBinary || !genBinarySize) { retVal = false; } if (retVal) { pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::EH_TYPE_OPENCL_EXECUTABLE, CLElfLib::EH_MACHINE_NONE, 0); if (pElfWriter) { CLElfLib::SSectionNode sectionNode; // Always add the options string sectionNode.Name = "BuildOptions"; sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_OPTIONS; sectionNode.pData = (char *)options.c_str(); sectionNode.DataSize = (uint32_t)(strlen(options.c_str()) + 1); retVal = pElfWriter->addSection(§ionNode); if (retVal) { sectionNode.Name = "Intel(R) OpenCL LLVM Object"; sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY; sectionNode.pData = llvmBinary; sectionNode.DataSize = (uint32_t)llvmBinarySize; retVal = pElfWriter->addSection(§ionNode); } // Add the device binary if it exists if (retVal && genBinary) { sectionNode.Name = "Intel(R) OpenCL Device Binary"; sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_DEV_BINARY; sectionNode.pData = genBinary; sectionNode.DataSize = (uint32_t)genBinarySize; retVal = pElfWriter->addSection(§ionNode); } if (retVal) { // get the size retVal = pElfWriter->resolveBinary(elfBinary, elfBinarySize); } if (retVal) { // allocate the binary elfBinary = new char[elfBinarySize]; retVal = pElfWriter->resolveBinary(elfBinary, elfBinarySize); } } else { retVal = false; } CLElfLib::CElfWriter::destroy(pElfWriter); } return retVal; } //////////////////////////////////////////////////////////////////////////////// // WriteOutAllFiles //////////////////////////////////////////////////////////////////////////////// void OfflineCompiler::writeOutAllFiles() { std::string fileTrunk = getFileNameTrunk(inputFile); std::string fileBase = fileTrunk + "_" + deviceName; if (outputDirectory != "") { std::list dirList; std::string tmp = outputDirectory; size_t pos = outputDirectory.size() + 1; do { dirList.push_back(tmp); pos = tmp.find_last_of("/\\", pos); tmp = tmp.substr(0, pos); } while (pos != std::string::npos); while (!dirList.empty()) { MakeDirectory(dirList.back().c_str()); dirList.pop_back(); } } if (llvmBinary) { std::string llvmOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/"; (useLlvmText == true) ? llvmOutputFile.append(fileBase + ".ll") : llvmOutputFile.append(fileBase + ".bc"); if (useOptionsSuffix) { std::string opts(options.c_str()); std::replace(opts.begin(), opts.end(), ' ', '_'); llvmOutputFile.append(opts); } writeDataToFile( llvmOutputFile.c_str(), llvmBinary, llvmBinarySize); } if (genBinary) { std::string genOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/"; genOutputFile.append(fileBase + ".gen"); if (useOptionsSuffix) { std::string opts(options.c_str()); std::replace(opts.begin(), opts.end(), ' ', '_'); genOutputFile.append(opts); } writeDataToFile( genOutputFile.c_str(), genBinary, genBinarySize); if (useCppFile) { std::string cppOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/"; cppOutputFile.append(fileBase + ".cpp"); std::string cpp = parseBinAsCharArray((uint8_t *)genBinary, genBinarySize, deviceName, fileTrunk); writeDataToFile(cppOutputFile.c_str(), cpp.c_str(), cpp.size()); } } if (elfBinary) { std::string elfOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/"; elfOutputFile.append(fileBase + ".bin"); if (useOptionsSuffix) { std::string opts(options.c_str()); std::replace(opts.begin(), opts.end(), ' ', '_'); elfOutputFile.append(opts); } writeDataToFile( elfOutputFile.c_str(), elfBinary, elfBinarySize); } } } // namespace OCLRT