Skip to content

Commit 3595525

Browse files
committed
Embed required fallback spv in sycl-post-link
Signed-off-by: jinge90 <[email protected]>
1 parent aff431a commit 3595525

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

llvm/include/llvm/SYCLLowerIR/SYCLRequiredDeviceLibs.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,21 @@ enum class DeviceLibExt : std::uint32_t {
4040
cl_intel_devicelib_bfloat16,
4141
};
4242

43+
struct SYCLDeviceLibSPVBinary {
44+
typedef uint8_t value_type;
45+
value_type *SPVRawBytes;
46+
size_t SPVBytesNum;
47+
SYCLDeviceLibSPVBinary(value_type *RawB, size_t BNum) {
48+
SPVRawBytes = RawB;
49+
SPVBytesNum = BNum;
50+
}
51+
value_type *data() const { return SPVRawBytes; }
52+
size_t size() const { return SPVBytesNum; }
53+
};
54+
4355
void getRequiredSYCLDeviceLibs(const Module &M,
44-
SmallVector<StringRef, 16> &ReqLibs);
56+
SmallVector<DeviceLibExt, 16> &ReqLibs);
57+
58+
const char *getDeviceLibFileName(DeviceLibExt RequiredDeviceLibExt);
4559

4660
} // namespace llvm

llvm/lib/SYCLLowerIR/ComputeModuleRuntimeInfo.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "llvm/SYCLLowerIR/SYCLRequiredDeviceLibs.h"
2222
#include "llvm/SYCLLowerIR/SYCLUtils.h"
2323
#include "llvm/SYCLLowerIR/SpecConstants.h"
24+
#include "llvm/Support/FileSystem.h"
25+
#include "llvm/Support/MemoryBuffer.h"
2426
#include <queue>
2527
#include <unordered_set>
2628

@@ -167,9 +169,29 @@ PropSetRegTy computeModuleProperties(const Module &M,
167169

168170
PropSetRegTy PropSet;
169171
{
170-
SmallVector<StringRef, 16> RequiredLibs;
172+
SmallVector<llvm::DeviceLibExt, 16> RequiredLibs;
171173
llvm::getRequiredSYCLDeviceLibs(M, RequiredLibs);
172-
for (auto RL : RequiredLibs) {
174+
for (auto Ext : RequiredLibs) {
175+
const char *SPVFileName = llvm::getDeviceLibFileName(Ext);
176+
std::string SPVPath =
177+
DeviceLibSPVLoc.str() + "/" + std::string(SPVFileName);
178+
if (!llvm::sys::fs::exists(SPVPath))
179+
continue;
180+
181+
auto SPVMB = llvm::MemoryBuffer::getFile(SPVPath);
182+
if (!SPVMB)
183+
continue;
184+
185+
size_t SPVSize = (*SPVMB)->getBufferSize();
186+
uint8_t *SPVBuffer = reinterpret_cast<uint8_t *>(
187+
std::aligned_alloc(alignof(uint32_t), SPVSize + sizeof(uint32_t)));
188+
*(reinterpret_cast<uint32_t *>(SPVBuffer)) = static_cast<uint32_t>(Ext);
189+
std::memcpy(SPVBuffer + 1, (*SPVMB)->getBufferStart(), SPVSize);
190+
llvm::SYCLDeviceLibSPVBinary SPVBinaryObj(SPVBuffer,
191+
SPVSize + sizeof(uint32_t));
192+
PropSet.add(PropSetRegTy::SYCL_DEVICELIB_REQ_BINS, SPVFileName,
193+
SPVBinaryObj);
194+
std::free(SPVBuffer);
173195
}
174196
}
175197

llvm/lib/SYCLLowerIR/SYCLRequiredDeviceLibs.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ namespace llvm {
760760
// 2. The function is declaration which means it doesn't have function body
761761
// And we don't expect non-spirv functions with "__devicelib_" prefix.
762762
void getRequiredSYCLDeviceLibs(
763-
const Module &M, llvm::SmallVector<llvm::StringRef, 16> &ReqDeviceLibs) {
763+
const Module &M, llvm::SmallVector<DeviceLibExt, 16> &ReqDeviceLibs) {
764764
// Device libraries will be enabled only for spir-v module.
765765
if (!Triple(M.getTargetTriple()).isSPIROrSPIRV())
766766
return;
@@ -776,9 +776,12 @@ void getRequiredSYCLDeviceLibs(
776776
continue;
777777

778778
DeviceLibUsed.insert(DeviceLibFuncIter->second);
779-
ReqDeviceLibs.push_back(DeviceLibSPVExtMap[DeviceLibFuncIter->second]);
779+
ReqDeviceLibs.push_back(DeviceLibFuncIter->second);
780780
}
781781
}
782782
}
783783

784+
const char *getDeviceLibFileName(DeviceLibExt RequiredDeviceLibExt) {
785+
return DeviceLibSPVExtMap[RequiredDeviceLibExt];
786+
}
784787
} // namespace llvm

0 commit comments

Comments
 (0)