Skip to content

Commit 7c533bd

Browse files
author
Jakub Chlanda
authored
[SYCL-JIT] Store env variables in configuration (#14860)
By storing the env variables in the config helper we eliminate the need for extra function parameters while also making it accessible throughout the codebase.
1 parent 4c7d2f8 commit 7c533bd

File tree

8 files changed

+64
-42
lines changed

8 files changed

+64
-42
lines changed

sycl-jit/common/include/DynArray.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define SYCL_FUSION_COMMON_DYNARRAY_H
1111

1212
#include <algorithm>
13+
#include <cstring>
1314

1415
namespace jit_compiler {
1516

@@ -22,6 +23,11 @@ template <typename T> class DynArray {
2223

2324
explicit DynArray(size_t Size) { init(Size); }
2425

26+
template <typename InputIt> DynArray(InputIt Begin, InputIt End) {
27+
init(End - Begin);
28+
std::copy(Begin, End, this->begin());
29+
}
30+
2531
~DynArray() { deinit(); }
2632

2733
DynArray(const DynArray &Other) {

sycl-jit/jit-compiler/include/KernelFusion.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ JITResult fuseKernels(View<SYCLKernelInfo> KernelInformation,
6464

6565
JITResult materializeSpecConstants(const char *KernelName,
6666
jit_compiler::SYCLKernelBinaryInfo &BinInfo,
67-
View<unsigned char> SpecConstBlob,
68-
const char *TargetCPU,
69-
const char *TargetFeatures);
67+
View<unsigned char> SpecConstBlob);
7068

7169
/// Clear all previously set options.
7270
void resetJITConfiguration();

sycl-jit/jit-compiler/include/Options.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313

1414
namespace jit_compiler {
1515

16-
enum OptionID { VerboseOutput, EnableCaching, TargetDeviceInfo };
16+
enum OptionID {
17+
VerboseOutput,
18+
EnableCaching,
19+
TargetDeviceInfo,
20+
TargetCPU,
21+
TargetFeatures
22+
};
1723

1824
class OptionPtrBase {
1925
protected:
@@ -90,6 +96,17 @@ struct JITTargetInfo
9096
using OptionBase::OptionBase;
9197
};
9298

99+
struct JITTargetCPU
100+
: public OptionBase<JITTargetCPU, OptionID::TargetCPU, DynArray<char>> {
101+
using OptionBase::OptionBase;
102+
};
103+
104+
struct JITTargetFeatures
105+
: public OptionBase<JITTargetFeatures, OptionID::TargetFeatures,
106+
DynArray<char>> {
107+
using OptionBase::OptionBase;
108+
};
109+
93110
} // namespace option
94111
} // namespace jit_compiler
95112

sycl-jit/jit-compiler/lib/KernelFusion.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ static bool isTargetFormatSupported(BinaryFormat TargetFormat) {
7373
extern "C" JITResult
7474
materializeSpecConstants(const char *KernelName,
7575
jit_compiler::SYCLKernelBinaryInfo &BinInfo,
76-
View<unsigned char> SpecConstBlob,
77-
const char *TargetCPU, const char *TargetFeatures) {
76+
View<unsigned char> SpecConstBlob) {
7877
auto &JITCtx = JITContext::getInstance();
7978

8079
TargetInfo TargetInfo = ConfigHelper::get<option::JITTargetInfo>();
@@ -107,8 +106,7 @@ materializeSpecConstants(const char *KernelName,
107106

108107
SYCLKernelInfo &MaterializerKernelInfo = *ModuleInfo.getKernelFor(KernelName);
109108
if (auto Error = translation::KernelTranslator::translateKernel(
110-
MaterializerKernelInfo, *NewMod, JITCtx, TargetFormat, TargetCPU,
111-
TargetFeatures)) {
109+
MaterializerKernelInfo, *NewMod, JITCtx, TargetFormat)) {
112110
return errorToFusionResult(std::move(Error),
113111
"Translation to output format failed");
114112
}

sycl-jit/jit-compiler/lib/translation/KernelTranslation.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "KernelTranslation.h"
10+
#include "helper/ConfigHelper.h"
1011

1112
#include "SPIRVLLVMTranslation.h"
1213
#include "llvm/Bitcode/BitcodeReader.h"
@@ -168,11 +169,10 @@ KernelTranslator::loadSPIRVKernel(llvm::LLVMContext &LLVMCtx,
168169
return SPIRVLLVMTranslator::loadSPIRVKernel(LLVMCtx, Kernel);
169170
}
170171

171-
llvm::Error
172-
KernelTranslator::translateKernel(SYCLKernelInfo &Kernel, llvm::Module &Mod,
173-
JITContext &JITCtx, BinaryFormat Format,
174-
const std::string &TargetCPU,
175-
const std::string &TargetFeatures) {
172+
llvm::Error KernelTranslator::translateKernel(SYCLKernelInfo &Kernel,
173+
llvm::Module &Mod,
174+
JITContext &JITCtx,
175+
BinaryFormat Format) {
176176

177177
KernelBinary *KernelBin = nullptr;
178178
switch (Format) {
@@ -187,7 +187,7 @@ KernelTranslator::translateKernel(SYCLKernelInfo &Kernel, llvm::Module &Mod,
187187
}
188188
case BinaryFormat::PTX: {
189189
llvm::Expected<KernelBinary *> BinaryOrError =
190-
translateToPTX(Kernel, Mod, JITCtx, TargetCPU, TargetFeatures);
190+
translateToPTX(Kernel, Mod, JITCtx);
191191
if (auto Error = BinaryOrError.takeError()) {
192192
return Error;
193193
}
@@ -196,7 +196,7 @@ KernelTranslator::translateKernel(SYCLKernelInfo &Kernel, llvm::Module &Mod,
196196
}
197197
case BinaryFormat::AMDGCN: {
198198
llvm::Expected<KernelBinary *> BinaryOrError =
199-
translateToAMDGCN(Kernel, Mod, JITCtx, TargetCPU, TargetFeatures);
199+
translateToAMDGCN(Kernel, Mod, JITCtx);
200200
if (auto Error = BinaryOrError.takeError())
201201
return Error;
202202
KernelBin = *BinaryOrError;
@@ -227,10 +227,9 @@ KernelTranslator::translateToSPIRV(llvm::Module &Mod, JITContext &JITCtx) {
227227
return SPIRVLLVMTranslator::translateLLVMtoSPIRV(Mod, JITCtx);
228228
}
229229

230-
llvm::Expected<KernelBinary *> KernelTranslator::translateToPTX(
231-
SYCLKernelInfo &KernelInfo, llvm::Module &Mod, JITContext &JITCtx,
232-
[[maybe_unused]] const std::string &TargetCPU,
233-
[[maybe_unused]] const std::string &TargetFeatures) {
230+
llvm::Expected<KernelBinary *>
231+
KernelTranslator::translateToPTX(SYCLKernelInfo &KernelInfo, llvm::Module &Mod,
232+
JITContext &JITCtx) {
234233
#ifndef JIT_SUPPORT_PTX
235234
(void)KernelInfo;
236235
(void)Mod;
@@ -261,8 +260,10 @@ llvm::Expected<KernelBinary *> KernelTranslator::translateToPTX(
261260

262261
// Give priority to user specified values (through environment variables:
263262
// SYCL_JIT_AMDGCN_PTX_TARGET_CPU and SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES).
264-
llvm::StringRef CPU{TargetCPU};
265-
llvm::StringRef Features{TargetFeatures};
263+
auto CPUVal = ConfigHelper::get<option::JITTargetCPU>();
264+
auto FeaturesVal = ConfigHelper::get<option::JITTargetFeatures>();
265+
llvm::StringRef CPU = CPUVal.begin();
266+
llvm::StringRef Features = FeaturesVal.begin();
266267

267268
auto *KernelFunc = Mod.getFunction(KernelInfo.Name.c_str());
268269
// If they were not set, use default and consult the module for alternatives
@@ -309,10 +310,9 @@ llvm::Expected<KernelBinary *> KernelTranslator::translateToPTX(
309310
#endif // JIT_SUPPORT_PTX
310311
}
311312

312-
llvm::Expected<KernelBinary *> KernelTranslator::translateToAMDGCN(
313-
SYCLKernelInfo &KernelInfo, llvm::Module &Mod, JITContext &JITCtx,
314-
[[maybe_unused]] const std::string &TargetCPU,
315-
[[maybe_unused]] const std::string &TargetFeatures) {
313+
llvm::Expected<KernelBinary *>
314+
KernelTranslator::translateToAMDGCN(SYCLKernelInfo &KernelInfo,
315+
llvm::Module &Mod, JITContext &JITCtx) {
316316
#ifndef JIT_SUPPORT_AMDGCN
317317
(void)KernelInfo;
318318
(void)Mod;
@@ -341,8 +341,10 @@ llvm::Expected<KernelBinary *> KernelTranslator::translateToAMDGCN(
341341
"Failed to load and translate AMDGCN LLVM IR module with error %s",
342342
ErrorMessage.c_str());
343343

344-
llvm::StringRef CPU{TargetCPU};
345-
llvm::StringRef Features{TargetFeatures};
344+
auto CPUVal = ConfigHelper::get<option::JITTargetCPU>();
345+
auto FeaturesVal = ConfigHelper::get<option::JITTargetFeatures>();
346+
llvm::StringRef CPU = CPUVal.begin();
347+
llvm::StringRef Features = FeaturesVal.begin();
346348

347349
auto *KernelFunc = Mod.getFunction(KernelInfo.Name.c_str());
348350
if (CPU.empty()) {

sycl-jit/jit-compiler/lib/translation/KernelTranslation.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class KernelTranslator {
2525
loadKernels(llvm::LLVMContext &LLVMCtx, std::vector<SYCLKernelInfo> &Kernels);
2626

2727
static llvm::Error translateKernel(SYCLKernelInfo &Kernel, llvm::Module &Mod,
28-
JITContext &JITCtx, BinaryFormat Format,
29-
const std::string &TargetCPU = {},
30-
const std::string &TargetFeatures = {});
28+
JITContext &JITCtx, BinaryFormat Format);
3129

3230
private:
3331
///
@@ -44,14 +42,11 @@ class KernelTranslator {
4442
JITContext &JITCtx);
4543

4644
static llvm::Expected<KernelBinary *>
47-
translateToPTX(SYCLKernelInfo &Kernel, llvm::Module &Mod, JITContext &JITCtx,
48-
const std::string &TargetCPU = {},
49-
const std::string &TargetFeatures = {});
45+
translateToPTX(SYCLKernelInfo &Kernel, llvm::Module &Mod, JITContext &JITCtx);
5046

5147
static llvm::Expected<KernelBinary *>
5248
translateToAMDGCN(SYCLKernelInfo &KernelInfo, llvm::Module &Mod,
53-
JITContext &JITCtx, const std::string &TargetCPU = {},
54-
const std::string &TargetFeatures = {});
49+
JITContext &JITCtx);
5550
};
5651
} // namespace translation
5752
} // namespace jit_compiler

sycl/source/detail/jit_compiler.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -663,15 +663,20 @@ ur_kernel_handle_t jit_compiler::materializeSpecConstants(
663663
detail::SYCLConfig<detail::SYCL_RT_WARNING_LEVEL>::get() > 0;
664664
AddToConfigHandle(
665665
::jit_compiler::option::JITEnableVerbose::set(DebugEnabled));
666-
667-
std::string TargetCPU =
668-
detail::SYCLConfig<detail::SYCL_JIT_AMDGCN_PTX_TARGET_CPU>::get();
669-
std::string TargetFeatures =
670-
detail::SYCLConfig<detail::SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES>::get();
666+
auto SetUpOption = [](const std::string &Value) {
667+
::jit_compiler::JITEnvVar Option(Value.begin(), Value.end());
668+
return Option;
669+
};
670+
::jit_compiler::JITEnvVar TargetCPUOpt = SetUpOption(
671+
detail::SYCLConfig<detail::SYCL_JIT_AMDGCN_PTX_TARGET_CPU>::get());
672+
AddToConfigHandle(::jit_compiler::option::JITTargetCPU::set(TargetCPUOpt));
673+
::jit_compiler::JITEnvVar TargetFeaturesOpt = SetUpOption(
674+
detail::SYCLConfig<detail::SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES>::get());
675+
AddToConfigHandle(
676+
::jit_compiler::option::JITTargetFeatures::set(TargetFeaturesOpt));
671677

672678
auto MaterializerResult =
673-
MaterializeSpecConstHandle(KernelName.c_str(), BinInfo, SpecConstBlob,
674-
TargetCPU.c_str(), TargetFeatures.c_str());
679+
MaterializeSpecConstHandle(KernelName.c_str(), BinInfo, SpecConstBlob);
675680
if (MaterializerResult.failed()) {
676681
std::string Message{"Compilation for kernel failed with message:\n"};
677682
Message.append(MaterializerResult.getErrorMessage());

sycl/source/detail/jit_compiler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct SYCLKernelInfo;
2525
struct SYCLKernelAttribute;
2626
template <typename T> class DynArray;
2727
using ArgUsageMask = DynArray<uint8_t>;
28+
using JITEnvVar = DynArray<char>;
2829
} // namespace jit_compiler
2930

3031
namespace sycl {

0 commit comments

Comments
 (0)