P4 to Git Change 1611775 by gandryey@gera-ocl-lc on 2018/09/27 18:02:54
SWDEV-79445 - OCL generic changes and code clean-up Program compilation clean-up. Step#6: - Move the second linkImpl() method to the abstraciton layer - Create the new setKernel virtual interface for the backend specific setup Affected files ... ... //depot/stg/opencl/drivers/opencl/runtime/device/device.hpp#320 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/devprogram.cpp#8 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/devprogram.hpp#6 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/pal/paldefs.hpp#41 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palprogram.cpp#79 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palprogram.hpp#35 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palsettings.cpp#57 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocprogram.cpp#91 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocprogram.hpp#40 edit
Tá an tiomantas seo le fáil i:
@@ -195,76 +195,15 @@ bool HSAILProgram::saveBinaryAndSetType(type_t type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HSAILProgram::linkImpl(amd::option::Options* options) {
|
||||
acl_error errorCode;
|
||||
aclType continueCompileFrom = ACL_TYPE_LLVMIR_BINARY;
|
||||
bool finalize = true;
|
||||
// If !binaryElf_ then program must have been created using clCreateProgramWithBinary
|
||||
if (!binaryElf_) {
|
||||
continueCompileFrom = getNextCompilationStageFromBinary(options);
|
||||
}
|
||||
switch (continueCompileFrom) {
|
||||
// Compilation from ACL_TYPE_LLVMIR_BINARY to ACL_TYPE_CG in cases:
|
||||
// 1. if the program is not created with binary;
|
||||
// 2. if the program is created with binary and contains only .llvmir & .comment
|
||||
// 3. if the program is created with binary, contains .llvmir, .comment, brig sections,
|
||||
// but the binary's compile & link options differ from current ones (recompilation);
|
||||
case ACL_TYPE_LLVMIR_BINARY:
|
||||
// Compilation from ACL_TYPE_HSAIL_BINARY to ACL_TYPE_CG in cases:
|
||||
// 1. if the program is created with binary and contains only brig sections
|
||||
case ACL_TYPE_HSAIL_BINARY:
|
||||
// Compilation from ACL_TYPE_HSAIL_TEXT to ACL_TYPE_CG in cases:
|
||||
// 1. if the program is created with binary and contains only hsail text
|
||||
case ACL_TYPE_HSAIL_TEXT: {
|
||||
std::string curOptions =
|
||||
options->origOptionStr + ProcessOptions(options);
|
||||
errorCode = aclCompile(device().compiler(), binaryElf_, curOptions.c_str(),
|
||||
continueCompileFrom, ACL_TYPE_CG, logFunction);
|
||||
buildLog_ += aclGetCompilerLog(device().compiler());
|
||||
if (errorCode != ACL_SUCCESS) {
|
||||
buildLog_ += "Error while BRIG Codegen phase: compilation error \n";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACL_TYPE_CG:
|
||||
break;
|
||||
case ACL_TYPE_ISA: {
|
||||
finalize = false;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
buildLog_ += "Error while BRIG Codegen phase: the binary is incomplete \n";
|
||||
return false;
|
||||
}
|
||||
bool HSAILProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize) {
|
||||
// Stop compilation if it is an offline device - HSA runtime does not
|
||||
// support ISA compiled offline
|
||||
if (!dev().isOnline()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (finalize) {
|
||||
std::string fin_options(options->origOptionStr);
|
||||
// Append an option so that we can selectively enable a SCOption on CZ
|
||||
// whenever IOMMUv2 is enabled.
|
||||
if (dev().isFineGrainedSystem(true)) {
|
||||
fin_options.append(" -sc-xnack-iommu");
|
||||
}
|
||||
errorCode = aclCompile(device().compiler(), binaryElf_, fin_options.c_str(), ACL_TYPE_CG,
|
||||
ACL_TYPE_ISA, logFunction);
|
||||
buildLog_ += aclGetCompilerLog(device().compiler());
|
||||
if (errorCode != ACL_SUCCESS) {
|
||||
buildLog_ += "Error: BRIG finalization to ISA failed.\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
size_t secSize;
|
||||
void* data =
|
||||
(void*)aclExtractSection(device().compiler(), binaryElf_, &secSize, aclTEXT, &errorCode);
|
||||
if (errorCode != ACL_SUCCESS) {
|
||||
buildLog_ += "Error: cannot extract ISA from compiled binary.\n";
|
||||
return false;
|
||||
}
|
||||
size_t secSize = binSize;
|
||||
void* data = binary;
|
||||
|
||||
// Create an executable.
|
||||
hsa_status_t status = hsa_executable_create_alt(
|
||||
@@ -349,7 +288,7 @@ bool HSAILProgram::linkImpl(amd::option::Options* options) {
|
||||
md.numHiddenKernelArgs = 0;
|
||||
|
||||
size_t sizeOfnumHiddenKernelArgs = sizeof(md.numHiddenKernelArgs);
|
||||
errorCode = aclQueryInfo(device().compiler(), binaryElf_,
|
||||
acl_error errorCode = aclQueryInfo(device().compiler(), binaryElf_,
|
||||
RT_NUM_KERNEL_HIDDEN_ARGS, openclKernelName.c_str(),
|
||||
&md.numHiddenKernelArgs, &sizeOfnumHiddenKernelArgs);
|
||||
if (errorCode != ACL_SUCCESS) {
|
||||
@@ -415,8 +354,6 @@ bool HSAILProgram::linkImpl(amd::option::Options* options) {
|
||||
std::string::npos);
|
||||
kernels()[kernelName] = aKernel;
|
||||
}
|
||||
saveBinaryAndSetType(TYPE_EXECUTABLE);
|
||||
buildLog_ += aclGetCompilerLog(device().compiler());
|
||||
return true;
|
||||
}
|
||||
#endif // defined(WITH_COMPILER_LIB)
|
||||
@@ -457,234 +394,12 @@ bool LightningProgram::saveBinaryAndSetType(type_t type, void* rawBinary, size_t
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LightningProgram::linkImpl(amd::option::Options* options) {
|
||||
acl_error errorCode;
|
||||
aclType continueCompileFrom = ACL_TYPE_LLVMIR_BINARY;
|
||||
using namespace amd::opencl_driver;
|
||||
std::vector<Data*> inputs;
|
||||
std::unique_ptr<Compiler> C(newCompilerInstance());
|
||||
bool bLinkLLVMBitcode = true;
|
||||
if (llvmBinary_.empty()) {
|
||||
continueCompileFrom = getNextCompilationStageFromBinary(options);
|
||||
}
|
||||
switch (continueCompileFrom) {
|
||||
case ACL_TYPE_CG:
|
||||
case ACL_TYPE_LLVMIR_BINARY: {
|
||||
break;
|
||||
}
|
||||
case ACL_TYPE_ASM_TEXT: {
|
||||
char* section;
|
||||
size_t sz;
|
||||
clBinary()->elfOut()->getSection(amd::OclElf::SOURCE, §ion, &sz);
|
||||
Data* input = C->NewBufferReference(DT_ASSEMBLY, section, sz);
|
||||
if (!input) {
|
||||
buildLog_ += "Error: Failed to open the assembler text.\n";
|
||||
return false;
|
||||
}
|
||||
inputs.push_back(input);
|
||||
bLinkLLVMBitcode = false;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ACL_TYPE_ISA: {
|
||||
binary_t isaBinary = binary();
|
||||
if ((isaBinary.first != nullptr) && (isaBinary.second > 0)) {
|
||||
return setKernels(options, (void*)isaBinary.first, isaBinary.second);
|
||||
} else {
|
||||
buildLog_ += "Error: code object is empty \n";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
buildLog_ += "Error while Codegen phase: the binary is incomplete \n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// call LinkLLVMBitcode
|
||||
if (bLinkLLVMBitcode) {
|
||||
// open the input IR source
|
||||
Data* input = C->NewBufferReference(DT_LLVM_BC, llvmBinary_.data(), llvmBinary_.size());
|
||||
|
||||
if (!input) {
|
||||
buildLog_ += "Error: Failed to open the compiled program.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
inputs.push_back(input); //< must be the first input
|
||||
|
||||
// open the bitcode libraries
|
||||
Data* opencl_bc =
|
||||
C->NewBufferReference(DT_LLVM_BC, (const char*)opencl_amdgcn, opencl_amdgcn_size);
|
||||
Data* ocml_bc = C->NewBufferReference(DT_LLVM_BC, (const char*)ocml_amdgcn, ocml_amdgcn_size);
|
||||
Data* ockl_bc = C->NewBufferReference(DT_LLVM_BC, (const char*)ockl_amdgcn, ockl_amdgcn_size);
|
||||
|
||||
if (!opencl_bc || !ocml_bc || !ockl_bc) {
|
||||
buildLog_ += "Error: Failed to open the bitcode library.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
inputs.push_back(opencl_bc); // depends on oclm & ockl
|
||||
inputs.push_back(ockl_bc);
|
||||
inputs.push_back(ocml_bc);
|
||||
|
||||
// open the control functions
|
||||
auto isa_version = get_oclc_isa_version(dev().deviceInfo().gfxipVersion_);
|
||||
if (!isa_version.first) {
|
||||
buildLog_ += "Error: Linking for this device is not supported\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
Data* isa_version_bc =
|
||||
C->NewBufferReference(DT_LLVM_BC, (const char*)isa_version.first, isa_version.second);
|
||||
|
||||
if (!isa_version_bc) {
|
||||
buildLog_ += "Error: Failed to open the control functions.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
inputs.push_back(isa_version_bc);
|
||||
|
||||
auto correctly_rounded_sqrt =
|
||||
get_oclc_correctly_rounded_sqrt(options->oVariables->FP32RoundDivideSqrt);
|
||||
Data* correctly_rounded_sqrt_bc = C->NewBufferReference(DT_LLVM_BC, correctly_rounded_sqrt.first,
|
||||
correctly_rounded_sqrt.second);
|
||||
|
||||
auto daz_opt = get_oclc_daz_opt(options->oVariables->DenormsAreZero ||
|
||||
AMD_GPU_FORCE_SINGLE_FP_DENORM == 0 ||
|
||||
(dev().deviceInfo().gfxipVersion_ < 900 &&
|
||||
AMD_GPU_FORCE_SINGLE_FP_DENORM < 0));
|
||||
Data* daz_opt_bc = C->NewBufferReference(DT_LLVM_BC, daz_opt.first, daz_opt.second);
|
||||
|
||||
auto finite_only = get_oclc_finite_only(options->oVariables->FiniteMathOnly ||
|
||||
options->oVariables->FastRelaxedMath);
|
||||
Data* finite_only_bc = C->NewBufferReference(DT_LLVM_BC, finite_only.first, finite_only.second);
|
||||
|
||||
auto unsafe_math = get_oclc_unsafe_math(options->oVariables->UnsafeMathOpt ||
|
||||
options->oVariables->FastRelaxedMath);
|
||||
Data* unsafe_math_bc = C->NewBufferReference(DT_LLVM_BC, unsafe_math.first, unsafe_math.second);
|
||||
|
||||
if (!correctly_rounded_sqrt_bc || !daz_opt_bc || !finite_only_bc || !unsafe_math_bc) {
|
||||
buildLog_ += "Error: Failed to open the control functions.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
inputs.push_back(correctly_rounded_sqrt_bc);
|
||||
inputs.push_back(daz_opt_bc);
|
||||
inputs.push_back(finite_only_bc);
|
||||
inputs.push_back(unsafe_math_bc);
|
||||
|
||||
// open the linked output
|
||||
std::vector<std::string> linkOptions;
|
||||
Buffer* linked_bc = C->NewBuffer(DT_LLVM_BC);
|
||||
|
||||
if (!linked_bc) {
|
||||
buildLog_ += "Error: Failed to open the linked program.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// NOTE: The linkOptions parameter is also used to identy cached code object. This parameter
|
||||
// should not contain any dyanamically generated filename.
|
||||
bool ret =
|
||||
dev().cacheCompilation()->linkLLVMBitcode(C.get(), inputs, linked_bc, linkOptions, buildLog_);
|
||||
buildLog_ += C->Output();
|
||||
if (!ret) {
|
||||
buildLog_ += "Error: Linking bitcode failed: linking source & IR libraries.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options->isDumpFlagSet(amd::option::DUMP_BC_LINKED)) {
|
||||
std::ofstream f(options->getDumpFileName("_linked.bc").c_str(), std::ios::trunc);
|
||||
if (f.is_open()) {
|
||||
f.write(linked_bc->Buf().data(), linked_bc->Size());
|
||||
}
|
||||
else {
|
||||
buildLog_ += "Warning: opening the file to dump the linked IR failed.\n";
|
||||
}
|
||||
}
|
||||
|
||||
inputs.clear();
|
||||
inputs.push_back(linked_bc);
|
||||
}
|
||||
|
||||
Buffer* out_exec = C->NewBuffer(DT_EXECUTABLE);
|
||||
if (!out_exec) {
|
||||
buildLog_ += "Error: Failed to create the linked executable.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string codegenOptions(options->llvmOptions);
|
||||
|
||||
// Set the machine target
|
||||
codegenOptions.append(" -mcpu=");
|
||||
codegenOptions.append(dev().deviceInfo().machineTarget_);
|
||||
|
||||
// Set xnack option if needed
|
||||
if (dev().deviceInfo().xnackEnabled_) {
|
||||
codegenOptions.append(" -mxnack");
|
||||
}
|
||||
|
||||
// Set the -O#
|
||||
std::ostringstream optLevel;
|
||||
optLevel << "-O" << options->oVariables->OptLevel;
|
||||
codegenOptions.append(" ").append(optLevel.str());
|
||||
|
||||
// Pass clang options
|
||||
std::ostringstream ostrstr;
|
||||
std::copy(options->clangOptions.begin(), options->clangOptions.end(),
|
||||
std::ostream_iterator<std::string>(ostrstr, " "));
|
||||
codegenOptions.append(" ").append(ostrstr.str());
|
||||
|
||||
// Set whole program mode
|
||||
codegenOptions.append(" -mllvm -amdgpu-internalize-symbols -mllvm -amdgpu-early-inline-all");
|
||||
|
||||
// Tokenize the options string into a vector of strings
|
||||
std::istringstream strstr(codegenOptions);
|
||||
std::istream_iterator<std::string> sit(strstr), end;
|
||||
std::vector<std::string> params(sit, end);
|
||||
|
||||
// NOTE: The params is also used to identy cached code object. This parameter
|
||||
// should not contain any dyanamically generated filename.
|
||||
bool ret = dev().cacheCompilation()->compileAndLinkExecutable(C.get(), inputs, out_exec, params,
|
||||
buildLog_);
|
||||
buildLog_ += C->Output();
|
||||
if (!ret) {
|
||||
if (continueCompileFrom == ACL_TYPE_ASM_TEXT) {
|
||||
buildLog_ += "Error: Creating the executable from ISA assembly text failed.\n";
|
||||
} else {
|
||||
buildLog_ += "Error: Creating the executable from LLVM IRs failed.\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options->isDumpFlagSet(amd::option::DUMP_O)) {
|
||||
std::ofstream f(options->getDumpFileName(".so").c_str(), std::ios::trunc);
|
||||
if (f.is_open()) {
|
||||
f.write(out_exec->Buf().data(), out_exec->Size());
|
||||
} else {
|
||||
buildLog_ += "Warning: opening the file to dump the code object failed.\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (options->isDumpFlagSet(amd::option::DUMP_ISA)) {
|
||||
std::string name = options->getDumpFileName(".s");
|
||||
File* dump = C->NewFile(DT_INTERNAL, name);
|
||||
if (!C->DumpExecutableAsText(out_exec, dump)) {
|
||||
buildLog_ += "Warning: failed to dump code object.\n";
|
||||
}
|
||||
}
|
||||
|
||||
return setKernels(options, out_exec->Buf().data(), out_exec->Size());
|
||||
}
|
||||
|
||||
bool LightningProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize) {
|
||||
// Find the size of global variables from the binary
|
||||
if (!FindGlobalVarSize(binary, binSize)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
saveBinaryAndSetType(TYPE_EXECUTABLE, binary, binSize);
|
||||
|
||||
//Load the stored copy of the ELF binary.
|
||||
binary_t stored_binary = this->binary();
|
||||
binary = const_cast<void*>(stored_binary.first);
|
||||
|
||||
Tagairt in Eagrán Nua
Cuir bac ar úsáideoir