Skip to content

Commit 45af04d

Browse files
[NFCI][SYCL RTC] Move createCommandline call to SYCLToolchain::run
After intel#20204 it's easy to stop using the translated command line to compute the hash (by doing that on user's options + format), which in turn allows us to perform this translation in a single place.
1 parent 4398638 commit 45af04d

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,55 @@ class SYCLToolchain {
117117
}
118118
};
119119

120+
std::vector<std::string> createCommandLine(const InputArgList &UserArgList,
121+
BinaryFormat Format,
122+
std::string_view SourceFilePath) {
123+
DerivedArgList DAL{UserArgList};
124+
const auto &OptTable = getDriverOptTable();
125+
DAL.AddFlagArg(nullptr, OptTable.getOption(OPT_fsycl_device_only));
126+
// User args may contain options not intended for the frontend, but we can't
127+
// claim them here to tell the driver they're used later. Hence, suppress
128+
// the unused argument warning.
129+
DAL.AddFlagArg(nullptr, OptTable.getOption(OPT_Qunused_arguments));
130+
131+
if (Format == BinaryFormat::PTX || Format == BinaryFormat::AMDGCN) {
132+
auto [CPU, Features] =
133+
Translator::getTargetCPUAndFeatureAttrs(nullptr, "", Format);
134+
(void)Features;
135+
StringRef OT = Format == BinaryFormat::PTX ? "nvptx64-nvidia-cuda"
136+
: "amdgcn-amd-amdhsa";
137+
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_fsycl_targets_EQ), OT);
138+
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_Xsycl_backend_EQ), OT);
139+
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_offload_arch_EQ), CPU);
140+
}
141+
142+
ArgStringList ASL;
143+
for_each(DAL, [&DAL, &ASL](Arg *A) { A->render(DAL, ASL); });
144+
for_each(UserArgList,
145+
[&UserArgList, &ASL](Arg *A) { A->render(UserArgList, ASL); });
146+
147+
std::vector<std::string> CommandLine;
148+
CommandLine.reserve(ASL.size() + 2);
149+
CommandLine.emplace_back(ClangXXExe);
150+
transform(ASL, std::back_inserter(CommandLine),
151+
[](const char *AS) { return std::string{AS}; });
152+
CommandLine.emplace_back(SourceFilePath);
153+
return CommandLine;
154+
}
155+
120156
public:
121157
static SYCLToolchain &instance() {
122158
static SYCLToolchain Instance;
123159
return Instance;
124160
}
125161

126-
bool run(const std::vector<std::string> &CommandLine,
127-
FrontendAction &FEAction,
162+
bool run(const InputArgList &UserArgList, BinaryFormat Format,
163+
const char *SourceFilePath, FrontendAction &FEAction,
128164
IntrusiveRefCntPtr<FileSystem> FSOverlay = nullptr,
129165
DiagnosticConsumer *DiagConsumer = nullptr) {
166+
std::vector<std::string> CommandLine =
167+
createCommandLine(UserArgList, Format, SourceFilePath);
168+
130169
auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
131170
llvm::vfs::getRealFileSystem());
132171
FS->pushOverlay(ToolchainFS);
@@ -226,42 +265,6 @@ class LLVMDiagnosticWrapper : public llvm::DiagnosticHandler {
226265

227266
} // anonymous namespace
228267

229-
static std::vector<std::string>
230-
createCommandLine(const InputArgList &UserArgList, BinaryFormat Format,
231-
std::string_view SourceFilePath) {
232-
DerivedArgList DAL{UserArgList};
233-
const auto &OptTable = getDriverOptTable();
234-
DAL.AddFlagArg(nullptr, OptTable.getOption(OPT_fsycl_device_only));
235-
// User args may contain options not intended for the frontend, but we can't
236-
// claim them here to tell the driver they're used later. Hence, suppress the
237-
// unused argument warning.
238-
DAL.AddFlagArg(nullptr, OptTable.getOption(OPT_Qunused_arguments));
239-
240-
if (Format == BinaryFormat::PTX || Format == BinaryFormat::AMDGCN) {
241-
auto [CPU, Features] =
242-
Translator::getTargetCPUAndFeatureAttrs(nullptr, "", Format);
243-
(void)Features;
244-
StringRef OT = Format == BinaryFormat::PTX ? "nvptx64-nvidia-cuda"
245-
: "amdgcn-amd-amdhsa";
246-
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_fsycl_targets_EQ), OT);
247-
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_Xsycl_backend_EQ), OT);
248-
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_offload_arch_EQ), CPU);
249-
}
250-
251-
ArgStringList ASL;
252-
for_each(DAL, [&DAL, &ASL](Arg *A) { A->render(DAL, ASL); });
253-
for_each(UserArgList,
254-
[&UserArgList, &ASL](Arg *A) { A->render(UserArgList, ASL); });
255-
256-
std::vector<std::string> CommandLine;
257-
CommandLine.reserve(ASL.size() + 2);
258-
CommandLine.emplace_back(SYCLToolchain::instance().getClangXXExe());
259-
transform(ASL, std::back_inserter(CommandLine),
260-
[](const char *AS) { return std::string{AS}; });
261-
CommandLine.emplace_back(SourceFilePath);
262-
return CommandLine;
263-
}
264-
265268
static llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>
266269
getInMemoryFS(InMemoryFile SourceFile, View<InMemoryFile> IncludeFiles) {
267270
auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
@@ -283,9 +286,6 @@ Expected<std::string> jit_compiler::calculateHash(
283286
const InputArgList &UserArgList, BinaryFormat Format) {
284287
TimeTraceScope TTS{"calculateHash"};
285288

286-
std::vector<std::string> CommandLine =
287-
createCommandLine(UserArgList, Format, SourceFile.Path);
288-
289289
class HashPreprocessedAction : public PreprocessorFrontendAction {
290290
protected:
291291
void ExecuteAction() override {
@@ -315,7 +315,8 @@ Expected<std::string> jit_compiler::calculateHash(
315315
BLAKE3 Hasher;
316316
HashPreprocessedAction HashAction{Hasher};
317317

318-
if (!SYCLToolchain::instance().run(CommandLine, HashAction,
318+
if (!SYCLToolchain::instance().run(UserArgList, Format, SourceFile.Path,
319+
HashAction,
319320
getInMemoryFS(SourceFile, IncludeFiles)))
320321
return createStringError("Calculating source hash failed");
321322

@@ -324,10 +325,11 @@ Expected<std::string> jit_compiler::calculateHash(
324325
ArrayRef<uint8_t>{reinterpret_cast<const uint8_t *>(&Format),
325326
reinterpret_cast<const uint8_t *>(&Format + 1)});
326327

327-
// Last argument is "rtc_N.cpp" source file name which is never the same,
328-
// ignore it:
329-
for (auto &Opt : drop_end(CommandLine, 1))
330-
Hasher.update(Opt);
328+
for (Arg *Opt : UserArgList) {
329+
Hasher.update(Opt->getSpelling());
330+
for (const char *Val : Opt->getValues())
331+
Hasher.update(Val);
332+
}
331333

332334
std::string EncodedHash = encodeBase64(Hasher.result());
333335

@@ -346,9 +348,9 @@ Expected<ModuleUPtr> jit_compiler::compileDeviceCode(
346348
DiagnosticOptions DiagOpts;
347349
ClangDiagnosticWrapper Wrapper(BuildLog, &DiagOpts);
348350

349-
if (SYCLToolchain::instance().run(
350-
createCommandLine(UserArgList, Format, SourceFile.Path), ELOA,
351-
getInMemoryFS(SourceFile, IncludeFiles), Wrapper.consumer())) {
351+
if (SYCLToolchain::instance().run(UserArgList, Format, SourceFile.Path, ELOA,
352+
getInMemoryFS(SourceFile, IncludeFiles),
353+
Wrapper.consumer())) {
352354
return ELOA.takeModule();
353355
} else {
354356
return createStringError(BuildLog);

0 commit comments

Comments
 (0)