P4 to Git Change 1182078 by yaxunl@yaxunl_stg_win50 on 2015/08/19 07:12:32

ECR #354633 - SPIR-V: Add consumption of SPIR-V to HSAIL path.

Affected files ...

... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/build/Makefile.api#120 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_program.cpp#36 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/khronos/icd/OpenCL.def.in#11 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/khronos/icd/icd_exports.map.in#7 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/Makefile#35 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/api/v0_8/acl.cpp#34 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/api/v0_8/aclValidation.cpp#6 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/backends/common/v0_8/if_acl.cpp#73 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/build/Makefile.complib#90 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/include/v0_8/acl.h#10 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/include/v0_8/aclEnums.h#21 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/include/v0_8/aclTypes.h#7 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/loaders/bif/bifinternal.hpp#11 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/spirv/Makefile#1 add
... //depot/stg/opencl/drivers/opencl/compiler/lib/spirv/build/Makefile#1 add
... //depot/stg/opencl/drivers/opencl/compiler/lib/spirv/build/Makefile.spirv#1 add
... //depot/stg/opencl/drivers/opencl/compiler/lib/spirv/spirvUtils.cpp#1 add
... //depot/stg/opencl/drivers/opencl/compiler/lib/spirv/spirvUtils.h#1 add
... //depot/stg/opencl/drivers/opencl/compiler/lib/utils/options.hpp#16 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/utils/v0_8/libUtils.h#22 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/device.cpp#182 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/device.hpp#250 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuprogram.cpp#200 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/program.cpp#64 edit
... //depot/stg/opencl/drivers/opencl/tests/conformance/devel/2.0/test_conformance/spirv/select.zip#1 add
... //depot/stg/opencl/drivers/opencl/tests/ocltst/module/complib/CLEnumCheck.cpp#46 edit
... //depot/stg/opencl/drivers/opencl/tests/ocltst/module/complib/CLEnumCheck.h#4 edit
... //depot/stg/opencl/drivers/opencl/tests/ocltst/module/complib/aclAPI.cpp#20 edit
Cette révision appartient à :
foreman
2015-08-19 07:28:56 -04:00
Parent bd71fc8775
révision 773ffef7d2
12 fichiers modifiés avec 170 ajouts et 52 suppressions
+79 -4
Voir le fichier
@@ -45,6 +45,7 @@
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/IRReader.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ExecutionEngine/ObjectImage.h"
#include "llvm/ExecutionEngine/ObjectBuffer.h"
@@ -52,9 +53,11 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/SPIRV.h"
#endif
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h"
@@ -378,6 +381,59 @@ OCLFEToModule(
}
return module;
}
aclModule* ACL_API_ENTRY
SPIRVToModule(
aclLoaderData *ald,
const char *image,
size_t length,
aclContext *ctx,
acl_error *error)
{
auto compiler = reinterpret_cast<amdcl::LLVMCompilerStage*>(ald);
#ifdef LEGACY_COMPLIB
llvm::report_fatal_error("SPIR-V not supported on legacy compiler lib");
appendLogToCL(compiler->CL(), "SPIR-V not supported on legacy compiler lib");
if (error != nullptr) (*error) = ACL_SPIRV_LOAD_FAIL;
return nullptr;
#else
std::string spvImg(image, length);
auto opt = compiler->Options();
if (opt->isDumpFlagSet(amd::option::DUMP_SPIRV)) {
std::ofstream ofs(opt->getDumpFileName(".spv"), std::ios::binary);
ofs << spvImg;
ofs.close();
}
std::stringstream ss(spvImg);
std::string errMsg;
auto llCtx = reinterpret_cast<llvm::LLVMContext*>(ctx);
llvm::Module *llMod = nullptr;
bool success = llvm::ReadSPRV(*llCtx, ss, llMod, errMsg);
if (success && llMod && opt->isDumpFlagSet(amd::option::DUMP_BC_SPIRV)) {
auto bcDump = opt->getDumpFileName("_spirv.bc");
std::error_code ec;
llvm::raw_fd_ostream outS(bcDump.c_str(), ec, llvm::sys::fs::F_None);
if (!ec)
WriteBitcodeToFile(llMod, outS);
else
errMsg = ec.message();
}
if (!errMsg.empty()) {
appendLogToCL(compiler->CL(), errMsg);
}
if (!success || llMod == nullptr) {
if (error != nullptr) (*error) = ACL_SPIRV_LOAD_FAIL;
return nullptr;
}
if (error != nullptr) (*error) = ACL_SUCCESS;
return reinterpret_cast<aclModule*>(llMod);
#endif // LEGACY_COMPLIB
}
acl_error ACL_API_ENTRY
AMDILFEToISA(
aclLoaderData *ald,
@@ -1369,8 +1425,10 @@ if_aclCompile(aclCompiler *cl,
(from == ACL_TYPE_HSAIL_BINARY && to != ACL_TYPE_ISA && to != ACL_TYPE_CG)) {
return ACL_INVALID_ARG;
}
uint8_t sectable[ACL_TYPE_LAST] = {0, 0, 1, 1, 1, 1, 0, 6, 0, 3, 4, 4, 4, 0, 5, 0, 1};
aclSections d_section[7] = {aclSOURCE, aclLLVMIR, aclSPIR, aclSOURCE, aclCODEGEN, aclTEXT, aclINTERNAL};
uint8_t sectable[ACL_TYPE_LAST] = {0, 0, 1, 1, 1, 1, 0, 6, 0, 3, 4, 4, 4, 0,
5, 0, 1, 1};
aclSections d_section[7] = {aclSOURCE, aclLLVMIR, aclSPIR, aclSOURCE,
aclCODEGEN, aclTEXT, aclINTERNAL};
uint8_t start = sectable[from];
uint8_t stop = sectable[to];
const void* data = NULL;
@@ -1393,6 +1451,9 @@ if_aclCompile(aclCompiler *cl,
}
break;
}
case ACL_TYPE_SPIRV_BINARY:
data = cl->clAPI.extSec(cl, bin, &data_size, aclSPIRV, &error_code);
break;
case ACL_TYPE_SPIR_BINARY:
case ACL_TYPE_SPIR_TEXT:
data = cl->clAPI.extSec(cl, bin, &data_size, aclSPIR, &error_code);
@@ -1486,15 +1547,19 @@ if_aclCompile(aclCompiler *cl,
CONDITIONAL_CMP_ASSIGN(cl->feAPI.toModule, &OCLFEToModule, &SPIRToModule);
} else if (from == ACL_TYPE_LLVMIR_BINARY || from == ACL_TYPE_LLVMIR_TEXT ||
from == ACL_TYPE_SPIR_BINARY || from == ACL_TYPE_SPIR_TEXT ||
from == ACL_TYPE_RSLLVMIR_BINARY) {
from == ACL_TYPE_RSLLVMIR_BINARY || from == ACL_TYPE_SPIRV_BINARY) {
CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &SPIRInit, &OCLInit);
CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &AMDILInit, &OCLInit);
CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &HSAILFEInit, &OCLInit);
CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &SPIRFini, &OCLFini);
CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &AMDILFini, &OCLFini);
CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &HSAILFEFini, &OCLFini);
if (from == ACL_TYPE_RSLLVMIR_BINARY) {
if (from == ACL_TYPE_SPIRV_BINARY) {
cl->feAPI.toModule = &SPIRVToModule;
} else if (from == ACL_TYPE_RSLLVMIR_BINARY) {
cl->feAPI.toModule = &RSLLVMIRToModule;
} else {
cl->feAPI.toModule = &OCLFEToModule;
}
}
}
@@ -2284,6 +2349,16 @@ if_aclQueryInfo(aclCompiler *cl,
return ACL_SUCCESS;
}
return ACL_ERROR;
case RT_CONTAINS_SPIRV:
if (!ptr) {
*size = sizeof(bool);
return ACL_SUCCESS;
} else if (*size >= sizeof(bool)) {
bool contains = elfBin->isSection(aclSPIRV);
memcpy(ptr, &contains, sizeof(bool));
return ACL_SUCCESS;
}
return ACL_ERROR;
case RT_CONTAINS_OPTIONS:
if (!ptr) {
*size = sizeof(bool);
+6 -1
Voir le fichier
@@ -229,7 +229,12 @@ const void* ACL_API_ENTRY
const char *kernel,
size_t *size,
acl_error *error_code) ACL_API_0_8;
//!--------------------------------------------------------------------------!//
// Functions that deal with binary image.
//!--------------------------------------------------------------------------!//
bool ACL_API_ENTRY
aclValidateBinaryImage(const void* binary,
size_t length, unsigned) ACL_API_0_8;
//!--------------------------------------------------------------------------!//
// Functions that deal with aclJITObjectImage objects.
//!--------------------------------------------------------------------------!//
+15 -4
Voir le fichier
@@ -32,7 +32,8 @@ typedef enum _acl_error_enum_0_8 {
ACL_INVALID_SPIR = 24,
ACL_LWVERIFY_FAIL = 25,
ACL_HWVERIFY_FAIL = 26,
ACL_LAST_ERROR = 27
ACL_SPIRV_LOAD_FAIL = 27,
ACL_LAST_ERROR = 28
} acl_error_0_8;
typedef enum _comp_device_caps_enum_0_8 {
@@ -111,7 +112,8 @@ typedef enum _acl_type_enum_0_8 {
ACL_TYPE_ISA = 14,
ACL_TYPE_HEADER = 15,
ACL_TYPE_RSLLVMIR_BINARY = 16,
ACL_TYPE_LAST = 17
ACL_TYPE_SPIRV_BINARY = 17,
ACL_TYPE_LAST = 18
} aclType_0_8;
//! Enum of the various loader types that are supported.
@@ -184,7 +186,8 @@ typedef enum _bif_sections_enum_0_8 {
aclBRIGxxx3 = 33,
aclHSADEBUG = 34,
aclKSTATS = 35, // For storing kernel statistics
aclLAST = 36
aclSPIRV = 36,
aclLAST = 37
} aclSections_0_8;
//! An enumeration that defines what are valid queries for aclQueryInfo.
@@ -212,7 +215,8 @@ typedef enum _rt_query_types_enum_0_8 {
RT_CONTAINS_LOADER_MAP = 20,
RT_CONTAINS_SPIR = 21,
RT_NUM_KERNEL_HIDDEN_ARGS = 22,
RT_LAST_TYPE = 23
RT_CONTAINS_SPIRV = 23,
RT_LAST_TYPE = 24
} aclQueryType_0_8;
//! An enumeration for the various GPU capabilities
@@ -328,4 +332,11 @@ typedef enum _acl_access_type_enum_0_8 {
ACCESS_TYPE_LAST = 4
} aclAccessType_0_8;
// Enumeration that specifies the binary types.
typedef enum _acl_binary_image_type_enum_0_8 {
BINARY_TYPE_ELF = 1,
BINARY_TYPE_LLVM = 2,
BINARY_TYPE_SPIRV = 4,
} aclBinaryImageType_0_8;
#endif // _ACL_ENUMS_0_8_H_
+1
Voir le fichier
@@ -51,6 +51,7 @@ typedef enum _bif_version_enum_0_8 aclBIFVersion;
typedef enum _bif_platform_enum_0_8 aclPlatform;
typedef enum _bif_sections_enum_0_8 aclSections;
typedef enum _acl_loader_type_enum_0_8 aclLoaderType;
typedef enum _acl_binary_image_type_enum_0_8 aclBinaryImageType;
#include "aclFunctors.h"
// Typedefs for function pointers
+18
Voir le fichier
@@ -0,0 +1,18 @@
#include "spirvUtils.h"
const unsigned SPRVMagicNumber = 0x07230203;
bool isSPIRVMagic(const void* Image, size_t Length) {
if (Image == nullptr || Length < sizeof(unsigned))
return false;
auto Magic = static_cast<const unsigned*>(Image);
return *Magic == SPRVMagicNumber;
}
// ToDo: replace this with SPIR-V validator when it is available.
bool
validateSPIRV(const void *Image, size_t Length) {
return isSPIRVMagic(Image, Length);
}
+9
Voir le fichier
@@ -0,0 +1,9 @@
#ifndef _COMPLIB_SPIRV_UTILS_H
#define _COMPLIB_SPIRV_UTILS_H
#include <cstddef>
bool validateSPIRV(const void *image, size_t length);
bool isSPIRVMagic(const void* image, size_t length);
#endif
+3 -1
Voir le fichier
@@ -165,6 +165,8 @@ enum DumpFlags {
DUMP_BC_OPTIMIZED = 0x00000400, // bitcode after optimization (llvm opt's output)
DUMP_CGIL = 0x00000800, // output il generated by Codegen (llvm llc's output)
DUMP_DEBUGIL = 0x00001000, // debug il (from MDParser, dwarf refers to)
DUMP_SPIRV = 0x00002000, // SPIR-V binary
DUMP_BC_SPIRV = 0x00004000, // bitcode translated from SPIR-V
// For a binary is encrypted, can only dump the following
DUMP_ENCRYPT = (DUMP_DLL | DUMP_BIF),
@@ -174,7 +176,7 @@ enum DumpFlags {
DUMP_IL |DUMP_CGIL | DUMP_DEBUGIL |
DUMP_ISA | DUMP_BIF),
DUMP_ALL = 0x00001FFF // Everything
DUMP_ALL = 0x00007FFF // Everything
};
enum OptLevelFlags {
+15
Voir le fichier
@@ -376,4 +376,19 @@ inline bool writeFile(std::string source_filename, const char *source, size_t si
return EXIT_SUCCESS;
}
#if !defined(BCMAG)
#define BCMAG "BC"
#define SBCMAG 2
#endif
// Helper predicate returns true if p starts with bit code signature.
// TODO: Move it into Compiler Lib back in new 1_0 API
inline static bool
isBcMagic(const char* p)
{
if (p==NULL || strncmp(p, BCMAG, SBCMAG) != 0) {
return false;
}
return true;
}
#endif // _CL_LIB_UTILS_0_8_H_
+7 -37
Voir le fichier
@@ -32,6 +32,8 @@ extern void DeviceUnload();
#endif
#include "utils/bif_section_labels.hpp"
#include "utils/libUtils.h"
#include "spirv/spirvUtils.h"
#include <vector>
#include <string>
@@ -41,20 +43,7 @@ extern void DeviceUnload();
#include <fstream>
#include <set>
#if !defined(BCMAG)
#define BCMAG "BC"
#define SBCMAG 2
#endif
// Helper predicate returns true if p starts with bit code signature.
// TODO: Move it into Compiler Lib back in new 1_0 API
inline static bool
isBcMagic(const char* p)
{
if (p==NULL || strncmp(p, BCMAG, SBCMAG) != 0) {
return false;
}
return true;
}
namespace device {
extern const char* BlitSourceCode;
@@ -252,27 +241,6 @@ Device::~Device()
}
}
// TODO: Move it into Compiler Lib in new 1_0 API
bool
Device::verifyBinaryImage( const void* image, size_t size) const
{
const char* p = static_cast<const char*>(image);
#if defined(HAVE_BLOWFISH_H)
int outBufSize;
if (amd::isEncryptedBIF(p, (int)size, &outBufSize)) {
// For encrypted image, check it later and simply return true here.
return true;
}
#endif
if (amd::isElfMagic(p)) {
return true;
}
if (isBcMagic(p)) {
return true;
}
return false;
}
bool
Device::isAncestor(const Device* sub) const
{
@@ -1069,7 +1037,8 @@ Program::initClBinary(char* binaryIn, size_t size)
int encryptCode = 0;
char* decryptedBin = NULL;
if (isBcMagic(binaryIn))
bool isSPIRV = isSPIRVMagic(binaryIn, size);
if (isSPIRV || isBcMagic(binaryIn))
{
acl_error err = ACL_SUCCESS;
aclBinaryOptions binOpts = {0};
@@ -1087,7 +1056,8 @@ Program::initClBinary(char* binaryIn, size_t size)
aclBinaryFini(aclbin_v30);
return false;
}
err = aclInsertSection(device().compiler(), aclbin_v30, binaryIn, size, aclSPIR);
err = aclInsertSection(device().compiler(), aclbin_v30, binaryIn, size,
isSPIRV?aclSPIRV:aclSPIR);
if (ACL_SUCCESS != err) {
LogWarning("aclInsertSection failed");
aclBinaryFini(aclbin_v30);
-3
Voir le fichier
@@ -1648,9 +1648,6 @@ public:
const amd::Kernel& kernel,
const device::VirtualDevice* vdev) { return true; };
//! Returns true if the given binary image is valid for this device.
bool verifyBinaryImage( const void* image, size_t size) const;
//! Returns TRUE if the device is available for computations
bool isOnline() const { return online_; }
//! Returns TRUE if the device is a root device (as opposed to sub-device)
+13 -1
Voir le fichier
@@ -1840,6 +1840,16 @@ HSAILProgram::getCompilationStagesFromBinary(std::vector<aclType>& completeStage
size_t boolSize = sizeof(bool);
//! @todo Should we also check for ACL_TYPE_OPENCL & ACL_TYPE_LLVMIR_TEXT?
// Checking llvmir in .llvmir section
bool containsSpirv = true;
errorCode = aclQueryInfo(dev().hsaCompiler(), binaryElf_,
RT_CONTAINS_SPIRV, NULL, &containsSpirv, &boolSize);
if (errorCode != ACL_SUCCESS) {
containsSpirv = false;
}
if (containsSpirv) {
completeStages.push_back(from);
from = ACL_TYPE_SPIRV_BINARY;
}
bool containsSpirText = true;
errorCode = aclQueryInfo(dev().hsaCompiler(), binaryElf_, RT_CONTAINS_SPIR, NULL, &containsSpirText, &boolSize);
if (errorCode != ACL_SUCCESS) {
@@ -1998,7 +2008,8 @@ HSAILProgram::getNextCompilationStageFromBinary(amd::option::Options* options) {
if (recompile) {
while (!completeStages.empty()) {
continueCompileFrom = completeStages.back();
if (continueCompileFrom == ACL_TYPE_LLVMIR_BINARY ||
if (continueCompileFrom == ACL_TYPE_SPIRV_BINARY ||
continueCompileFrom == ACL_TYPE_LLVMIR_BINARY ||
continueCompileFrom == ACL_TYPE_SPIR_BINARY ||
continueCompileFrom == ACL_TYPE_DEFAULT) {
break;
@@ -2032,6 +2043,7 @@ HSAILProgram::linkImpl(amd::option::Options* options)
continueCompileFrom = getNextCompilationStageFromBinary(options);
}
switch (continueCompileFrom) {
case ACL_TYPE_SPIRV_BINARY:
case ACL_TYPE_SPIR_BINARY:
// Compilation from ACL_TYPE_LLVMIR_BINARY to ACL_TYPE_CG in cases:
// 1. if the program is not created with binary;
+4 -1
Voir le fichier
@@ -6,6 +6,7 @@
#include "platform/program.hpp"
#include "platform/context.hpp"
#include "utils/options.hpp"
#include "acl.h"
#include <cstdlib> // for malloc
#include <cstring> // for strcmp
@@ -48,7 +49,9 @@ Program::findSymbol(const char* kernelName) const
cl_int
Program::addDeviceProgram(Device& device, const void* image, size_t length, int oclVer)
{
if (image != NULL && !device.verifyBinaryImage(image, length)) {
if (image != NULL &&
!aclValidateBinaryImage(image, length,
isIL_?BINARY_TYPE_SPIRV:BINARY_TYPE_ELF|BINARY_TYPE_LLVM)) {
return CL_INVALID_BINARY;
}