Repository navigation
Expand file tree
/
Copy pathRTC.cpp
More file actions
161 lines (137 loc) · 6.07 KB
/
Copy pathRTC.cpp
File metadata and controls
161 lines (137 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//===- RTC.cpp ------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "RTC.h"
#include "JITBinaryInfo.h"
#include "helper/ErrorHelper.h"
#include "rtc/DeviceCompilation.h"
#include "translation/SPIRVLLVMTranslation.h"
#include "translation/Translation.h"
#include <llvm/ADT/StringExtras.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/TimeProfiler.h>
#include <clang/Driver/Options.h>
#include <chrono>
using namespace jit_compiler;
JIT_EXPORT_SYMBOL RTCHashResult calculateHash(InMemoryFile SourceFile,
View<InMemoryFile> IncludeFiles,
View<const char *> UserArgs,
BinaryFormat Format) {
llvm::opt::InputArgList UserArgList;
if (auto Error = parseUserArgs(UserArgs).moveInto(UserArgList)) {
return errorTo<RTCHashResult>(std::move(Error),
"Parsing of user arguments failed",
/*IsHash=*/false);
}
auto Start = std::chrono::high_resolution_clock::now();
std::string Hash;
if (auto Error = calculateHash(SourceFile, IncludeFiles, UserArgList, Format)
.moveInto(Hash)) {
return errorTo<RTCHashResult>(std::move(Error), "Hashing failed",
/*IsHash=*/false);
}
auto Stop = std::chrono::high_resolution_clock::now();
if (UserArgList.hasArg(clang::driver::options::OPT_ftime_trace_EQ)) {
std::chrono::duration<double, std::milli> HashTime = Stop - Start;
llvm::dbgs() << "Hashing of " << SourceFile.Path << " took "
<< int(HashTime.count()) << " ms\n";
}
return RTCHashResult{Hash.c_str()};
}
JIT_EXPORT_SYMBOL RTCResult compileSYCL(InMemoryFile SourceFile,
View<InMemoryFile> IncludeFiles,
View<const char *> UserArgs,
View<char> CachedIR, bool SaveIR,
BinaryFormat Format) {
llvm::LLVMContext Context;
std::string BuildLog;
configureDiagnostics(Context, BuildLog);
llvm::opt::InputArgList UserArgList;
if (auto Error = parseUserArgs(UserArgs).moveInto(UserArgList)) {
return errorTo<RTCResult>(std::move(Error),
"Parsing of user arguments failed",
RTCResult::RTCErrorCode::INVALID);
}
llvm::StringRef TraceFileName;
if (auto *Arg =
UserArgList.getLastArg(clang::driver::options::OPT_ftime_trace_EQ)) {
TraceFileName = Arg->getValue();
unsigned Granularity =
500; // microseconds. Same default as in `clang::FrontendOptions`.
if (auto *Arg = UserArgList.getLastArg(
clang::driver::options::OPT_ftime_trace_granularity_EQ)) {
if (!llvm::to_integer(Arg->getValue(), Granularity)) {
BuildLog += "warning: ignoring malformed argument: '" +
Arg->getAsString(UserArgList) + "'\n";
}
}
bool Verbose =
UserArgList.hasArg(clang::driver::options::OPT_ftime_trace_verbose);
llvm::timeTraceProfilerInitialize(Granularity, /*ProcName=*/"sycl-rtc",
Verbose);
}
std::unique_ptr<llvm::Module> Module;
if (CachedIR.size() > 0) {
llvm::StringRef IRStr{CachedIR.begin(), CachedIR.size()};
std::unique_ptr<llvm::MemoryBuffer> IRBuf =
llvm::MemoryBuffer::getMemBuffer(IRStr, /*BufferName=*/"",
/*RequiresNullTerminator=*/false);
if (auto Error = llvm::parseBitcodeFile(*IRBuf, Context).moveInto(Module)) {
// Not a fatal error, we'll just compile the source string normally.
BuildLog.append(formatError(std::move(Error),
"Loading of cached device code failed"));
}
}
bool FromSource = !Module;
if (FromSource) {
if (auto Error = compileDeviceCode(SourceFile, IncludeFiles, UserArgList,
BuildLog, Context, Format)
.moveInto(Module)) {
return errorTo<RTCResult>(std::move(Error), "Device compilation failed");
}
}
RTCDeviceCodeIR IR;
if (SaveIR && FromSource) {
std::string BCString;
llvm::raw_string_ostream BCStream{BCString};
llvm::WriteBitcodeToFile(*Module, BCStream);
IR = RTCDeviceCodeIR{BCString.data(), BCString.data() + BCString.size()};
}
if (auto Error =
linkDeviceLibraries(*Module, UserArgList, BuildLog, Format)) {
return errorTo<RTCResult>(std::move(Error), "Device linking failed");
}
auto PostLinkResultOrError = performPostLink(std::move(Module), UserArgList);
if (!PostLinkResultOrError) {
return errorTo<RTCResult>(PostLinkResultOrError.takeError(),
"Post-link phase failed");
}
auto [BundleInfo, Modules] = std::move(*PostLinkResultOrError);
for (auto [DevImgInfo, Module] :
llvm::zip_equal(BundleInfo.DevImgInfos, Modules)) {
if (auto Error =
Translator::translate(*Module, JITContext::getInstance(), Format)
.moveInto(DevImgInfo.BinaryInfo)) {
return errorTo<RTCResult>(std::move(Error), "SPIR-V translation failed");
}
}
encodeBuildOptions(BundleInfo, UserArgList);
if (llvm::timeTraceProfilerEnabled()) {
auto Error = llvm::timeTraceProfilerWrite(
TraceFileName, /*FallbackFileName=*/"trace.json");
llvm::timeTraceProfilerCleanup();
if (Error) {
return errorTo<RTCResult>(std::move(Error), "Trace file writing failed");
}
}
return RTCResult{std::move(BundleInfo), std::move(IR), BuildLog.c_str()};
}
JIT_EXPORT_SYMBOL void destroyBinary(BinaryAddress Address) {
JITContext::getInstance().destroyBinary(Address);
}