Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit 60095f9

Browse files
committed
SWDEV-241902 - Changes to pass file descriptor and offset to load code object.
Change-Id: I0243cccdeaa533b2a56fde42f12d5424c3b63a3b
1 parent 5ab3531 commit 60095f9

File tree

14 files changed

+220
-45
lines changed

14 files changed

+220
-45
lines changed

device/device.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2008-present Advanced Micro Devices, Inc.
1+
/* Copyright (c) 2008-presenet Advanced Micro Devices, Inc.
22
33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal
@@ -759,14 +759,24 @@ bool ClBinary::createElfBinary(bool doencrypt, Program::type_t type) {
759759

760760
Program::binary_t ClBinary::data() const { return {binary_, size_}; }
761761

762-
bool ClBinary::setBinary(const char* theBinary, size_t theBinarySize, bool allocated) {
762+
Program::finfo_t ClBinary::Datafd() const { return {fdesc_, foffset_}; }
763+
764+
std::string ClBinary::DataURI() const { return uri_; }
765+
766+
bool ClBinary::setBinary(const char* theBinary, size_t theBinarySize, bool allocated,
767+
amd::Os::FileDesc fdesc, size_t foffset, std::string uri) {
763768
release();
764769

765770
size_ = theBinarySize;
766771
binary_ = theBinary;
767772
if (allocated) {
768773
flags_ |= BinaryAllocated;
769774
}
775+
776+
fdesc_ = fdesc;
777+
foffset_ = foffset;
778+
uri_ = uri;
779+
770780
return true;
771781
}
772782

device/device.hpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,9 @@ class ClBinary : public amd::HeapObject {
869869

870870
void init(amd::option::Options* optionsObj, bool amdilRequired = false);
871871

872-
/** called only in loading image routines,
873-
never called in storing routines */
874-
bool setBinary(const char* theBinary, size_t theBinarySize, bool allocated = false);
872+
/** called only in loading image routines, never storing routines */
873+
bool setBinary(const char* theBinary, size_t theBinarySize, bool allocated = false,
874+
amd::Os::FileDesc fd = -1, size_t foffset = 0, std::string uri = std::string());
875875

876876
//! setin elfIn_
877877
bool setElfIn();
@@ -900,8 +900,10 @@ class ClBinary : public amd::HeapObject {
900900
bool decryptElf(const char* binaryIn, size_t size, char** decryptBin, size_t* decryptSize,
901901
int* encryptCode);
902902

903-
//! Returns the binary pair for the abstraction layer
903+
//! Returns the binary pair, fdesc pair, uri for the abstraction layer
904904
Program::binary_t data() const;
905+
Program::finfo_t Datafd() const;
906+
std::string DataURI() const;
905907

906908
//! Loads llvmir binary from OCL binary file
907909
bool loadLlvmBinary(
@@ -1016,6 +1018,10 @@ class ClBinary : public amd::HeapObject {
10161018
size_t size_; //!< binary size
10171019
uint flags_; //!< CL binary object flags
10181020

1021+
amd::Os::FileDesc fdesc_; //!< file descriptor
1022+
size_t foffset_; //!< file offset
1023+
std::string uri_; //!< memory URI
1024+
10191025
const char* origBinary_; //!< original binary data
10201026
size_t origSize_; //!< original binary size
10211027

@@ -1034,6 +1040,20 @@ inline const Program::binary_t Program::binary() const {
10341040
return clBinary()->data();
10351041
}
10361042

1043+
inline std::string Program::BinaryURI() const {
1044+
if (clBinary() == NULL) {
1045+
return std::string();
1046+
}
1047+
return clBinary()->DataURI();
1048+
}
1049+
1050+
inline Program::finfo_t Program::BinaryFd() const {
1051+
if (clBinary() == NULL) {
1052+
return {-1, 0};
1053+
}
1054+
return clBinary()->Datafd();
1055+
}
1056+
10371057
inline Program::binary_t Program::binary() {
10381058
if (clBinary() == NULL) {
10391059
return {(const void*)0, 0};

device/devprogram.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,10 +1089,12 @@ bool Program::linkImplLC(amd::option::Options* options) {
10891089
case ACL_TYPE_ISA: {
10901090
amd::Comgr::destroy_data_set(inputs);
10911091
binary_t isaBinary = binary();
1092+
finfo_t isaFdesc = BinaryFd();
10921093
if (GPU_DUMP_CODE_OBJECT) {
10931094
dumpCodeObject(std::string{(const char*)isaBinary.first, isaBinary.second});
10941095
}
1095-
return setKernels(options, const_cast<void *>(isaBinary.first), isaBinary.second);
1096+
return setKernels(options, const_cast<void *>(isaBinary.first), isaBinary.second,
1097+
isaFdesc.first, isaFdesc.second, BinaryURI());
10961098
break;
10971099
}
10981100
default:
@@ -1852,7 +1854,8 @@ bool isSPIRVMagicL(const void* Image, size_t Length) {
18521854
}
18531855

18541856
// ================================================================================================
1855-
bool Program::initClBinary(const char* binaryIn, size_t size) {
1857+
bool Program::initClBinary(const char* binaryIn, size_t size, amd::Os::FileDesc fdesc,
1858+
size_t foffset, std::string uri) {
18561859
if (!initClBinary()) {
18571860
DevLogError("Init CL Binary failed \n");
18581861
return false;
@@ -1947,12 +1950,13 @@ bool Program::initClBinary(const char* binaryIn, size_t size) {
19471950

19481951
clBinary()->setFlags(encryptCode);
19491952

1950-
return clBinary()->setBinary(bin, sz, (decryptedBin != nullptr));
1953+
return clBinary()->setBinary(bin, sz, (decryptedBin != nullptr), fdesc, foffset, uri);
19511954
}
19521955

19531956
// ================================================================================================
1954-
bool Program::setBinary(const char* binaryIn, size_t size, const device::Program* same_dev_prog) {
1955-
if (!initClBinary(binaryIn, size)) {
1957+
bool Program::setBinary(const char* binaryIn, size_t size, const device::Program* same_dev_prog,
1958+
amd::Os::FileDesc fdesc, size_t foffset, std::string uri) {
1959+
if (!initClBinary(binaryIn, size, fdesc, foffset, uri)) {
19561960
DevLogError("Init CL Binary failed \n");
19571961
return false;
19581962
}
@@ -2187,6 +2191,8 @@ aclType Program::getCompilationStagesFromBinary(std::vector<aclType>& completeSt
21872191
aclType Program::getNextCompilationStageFromBinary(amd::option::Options* options) {
21882192
aclType continueCompileFrom = ACL_TYPE_DEFAULT;
21892193
binary_t binary = this->binary();
2194+
finfo_t finfo = this->BinaryFd();
2195+
std::string uri = this->BinaryURI();
21902196
// If the binary already exists
21912197
if ((binary.first != nullptr) && (binary.second > 0)) {
21922198
#if defined(WITH_COMPILER_LIB)
@@ -2207,7 +2213,8 @@ aclType Program::getNextCompilationStageFromBinary(amd::option::Options* options
22072213

22082214
// Saving binary in the interface class,
22092215
// which also load compile & link options from binary
2210-
setBinary(static_cast<const char*>(binary.first), binary.second);
2216+
setBinary(static_cast<const char*>(binary.first), binary.second, nullptr,
2217+
finfo.first, finfo.second, uri);
22112218

22122219
// Calculate the next stage to compile from, based on sections in binaryElf_;
22132220
// No any validity checks here

device/devprogram.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ struct SymbolLoweredName {
7070
//! A program object for a specific device.
7171
class Program : public amd::HeapObject {
7272
public:
73-
typedef std::pair<const void*, size_t> binary_t;
73+
typedef std::pair<const void* /* binary_image */, size_t /* binary size */> binary_t;
74+
typedef std::pair<amd::Os::FileDesc /* file_desc */, size_t /* file_offset */> finfo_t;
7475
typedef std::unordered_map<std::string, Kernel*> kernels_t;
7576
// type of the program
7677
typedef enum {
@@ -186,12 +187,15 @@ class Program : public amd::HeapObject {
186187
//! Return the binary image.
187188
inline const binary_t binary() const;
188189
inline binary_t binary();
190+
inline finfo_t BinaryFd() const;
191+
inline std::string BinaryURI() const;
189192

190193
//! Returns the CL program binary file
191194
ClBinary* clBinary() { return clBinary_; }
192195
const ClBinary* clBinary() const { return clBinary_; }
193196

194-
bool setBinary(const char* binaryIn, size_t size, const device::Program* same_dev_prog = nullptr);
197+
bool setBinary(const char* binaryIn, size_t size, const device::Program* same_dev_prog = nullptr,
198+
amd::Os::FileDesc fdesc = -1, size_t foffset = 0, std::string uri = std::string());
195199

196200
type_t type() const { return type_; }
197201

@@ -279,7 +283,8 @@ class Program : public amd::HeapObject {
279283
virtual bool createBinary(amd::option::Options* options) = 0;
280284

281285
//! Initialize Binary (used only for clCreateProgramWithBinary()).
282-
bool initClBinary(const char* binaryIn, size_t size);
286+
bool initClBinary(const char* binaryIn, size_t size, amd::Os::FileDesc fdesc = -1,
287+
size_t foffset = 0, std::string uri = std::string());
283288

284289
//! Initialize Binary
285290
virtual bool initClBinary();
@@ -293,7 +298,9 @@ class Program : public amd::HeapObject {
293298
virtual const aclTargetInfo& info(const char* str = "") = 0;
294299

295300
virtual bool setKernels(
296-
amd::option::Options* options, void* binary, size_t binSize) { return true; }
301+
amd::option::Options* options, void* binary, size_t binSize,
302+
amd::Os::FileDesc fdesc = -1, size_t foffset = 0,
303+
std::string uri = std::string()) { return true; }
297304

298305
//! Returns all the options to be appended while passing to the compiler library
299306
std::vector<std::string> ProcessOptions(amd::option::Options* options);

device/pal/palprogram.cpp

100644100755
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ inline static std::vector<std::string> splitSpaceSeparatedString(char* str) {
238238
return vec;
239239
}
240240

241-
bool HSAILProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize) {
241+
bool HSAILProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize,
242+
amd::Os::FileDesc fdesc, size_t foffset, std::string uri) {
242243
#if defined(WITH_COMPILER_LIB)
243244
// ACL_TYPE_CG stage is not performed for offline compilation
244245
hsa_agent_t agent;
@@ -735,7 +736,8 @@ bool LightningProgram::createBinary(amd::option::Options* options) {
735736
return true;
736737
}
737738

738-
bool LightningProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize) {
739+
bool LightningProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize,
740+
amd::Os::FileDesc fdesc, size_t foffset, std::string uri) {
739741
#if defined(USE_COMGR_LIBRARY)
740742
hsa_agent_t agent;
741743
agent.handle = 1;

device/pal/palprogram.hpp

100644100755
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ class HSAILProgram : public device::Program {
195195

196196
virtual const aclTargetInfo& info(const char* str = "");
197197

198-
virtual bool setKernels(amd::option::Options* options, void* binary, size_t binSize) override;
198+
virtual bool setKernels(amd::option::Options* options, void* binary, size_t binSize,
199+
amd::Os::FileDesc fdesc = -1, size_t foffset = 0,
200+
std::string uri = std::string()) override;
199201

200202
//! Destroys CPU allocations in the code segment
201203
void DestroySegmentCpuAccess() const {

device/rocm/rocprogram.cpp

100644100755
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ bool HSAILProgram::saveBinaryAndSetType(type_t type) {
264264
return true;
265265
}
266266

267-
bool HSAILProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize) {
267+
bool HSAILProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize,
268+
amd::Os::FileDesc fdesc, size_t foffset, std::string uri) {
268269
#if defined(WITH_COMPILER_LIB)
269270
// Stop compilation if it is an offline device - HSA runtime does not
270271
// support ISA compiled offline
@@ -467,7 +468,8 @@ bool LightningProgram::saveBinaryAndSetType(type_t type, void* rawBinary, size_t
467468
return true;
468469
}
469470

470-
bool LightningProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize) {
471+
bool LightningProgram::setKernels(amd::option::Options* options, void* binary, size_t binSize,
472+
amd::Os::FileDesc fdesc, size_t foffset, std::string uri) {
471473
#if defined(USE_COMGR_LIBRARY)
472474
// Find the size of global variables from the binary
473475
if (!FindGlobalVarSize(binary, binSize)) {
@@ -488,7 +490,9 @@ bool LightningProgram::setKernels(amd::option::Options* options, void* binary, s
488490
return false;
489491
}
490492

491-
// Load the code object.
493+
// Load the code object, either with file descriptor and offset
494+
// or binary image and binary size with URI
495+
// or binary image and binary size
492496
status = hsa_code_object_reader_create_from_memory(binary, binSize, &hsaCodeObjectReader_);
493497
if (status != HSA_STATUS_SUCCESS) {
494498
buildLog_ += "Error: AMD HSA Code Object Reader create failed: ";

device/rocm/rocprogram.hpp

100644100755
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ class HSAILProgram : public roc::Program {
9191
protected:
9292
bool createBinary(amd::option::Options* options) override { return true; }
9393

94-
virtual bool setKernels(amd::option::Options* options, void* binary, size_t binSize) override;
94+
virtual bool setKernels(amd::option::Options* options, void* binary, size_t binSize,
95+
amd::Os::FileDesc fdesc = -1, size_t foffset = 0,
96+
std::string uri = std::string()) override;
9597

9698
private:
9799
std::string codegenOptions(amd::option::Options* options);
@@ -112,7 +114,9 @@ class LightningProgram final : public roc::Program {
112114
private:
113115
bool saveBinaryAndSetType(type_t type, void* rawBinary, size_t size);
114116

115-
bool setKernels(amd::option::Options* options, void* binary, size_t binSize) final;
117+
bool setKernels(amd::option::Options* options, void* binary, size_t binSize,
118+
amd::Os::FileDesc fdesc = -1, size_t foffset = 0,
119+
std::string uri = std::string()) final;
116120
};
117121

118122
/*@}*/} // namespace roc

os/os.cpp

100644100755
File mode changed.

os/os.hpp

100644100755
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ class Thread; // For Os::createOsThread()
5252

5353
class Os : AllStatic {
5454
public:
55+
56+
// File Desc abstraction between OS
57+
#if defined(_WIN32)
58+
typedef HANDLE FileDesc;
59+
#else
60+
typedef int FileDesc;
61+
#endif
62+
5563
enum MemProt { MEM_PROT_NONE = 0, MEM_PROT_READ, MEM_PROT_RW, MEM_PROT_RWX };
5664

5765
class ThreadAffinityMask {
@@ -91,7 +99,20 @@ class Os : AllStatic {
9199
#endif
92100
};
93101

102+
// Returns unique resource indicator for a particular memory
103+
static bool GetURIFromMemory(const void* image, size_t image_size, std::string& uri);
104+
105+
// Closes the file Handle
106+
static bool CloseFileHandle(FileDesc fdesc);
107+
// Given a valid file name, returns file descriptor and file size
108+
static bool GetFileHandle(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr);
109+
110+
// Given a valid file descriptor returns mmaped memory for size and offset
111+
static bool MemoryMapFileDesc(FileDesc fdesc, size_t fsize, size_t foffset,
112+
const void** mmap_ptr);
113+
// Given a valid file name, returns mmapped memory with the mapped size.
94114
static bool MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size);
115+
// Given a valid mmaped ptr with correct size, unmaps the ptr from memory
95116
static bool MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size);
96117

97118
private:

0 commit comments

Comments
 (0)