Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] PoC implementation of kernel compiler extension with libtooling and sycl-jit #15701

Open
wants to merge 14 commits into
base: sycl
Choose a base branch
from

Conversation

jopperm
Copy link
Contributor

@jopperm jopperm commented Oct 15, 2024

This PR sets up in-memory compilation for runtime-defined SYCL kernels, via clang's libtooling interface and reusing LLVM-to-SPRIV-translation infrastructure in sycl-jit. I introduced a new, undocumented source language sycljit, which shall be removed again when the proposed approach is ready to replace the current process/file-based implementation for the sycl source language.

Missing features:

  • Compiler warnings/errors -> build log / exception message
  • Instantiation of template kernels
  • Name mangling beyond removing the __sycl_kernel prefix when requesting a kernel from the bundle
  • Linking of device libs, and sycl-post-link phase, so only very simple kernels are supported
  • Property handling and integration into program_manager

…oling and sycl-jit

Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
@@ -40,6 +49,8 @@ target_include_directories(sycl-jit
SYSTEM PRIVATE
${LLVM_MAIN_INCLUDE_DIR}
${LLVM_SPIRV_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/../clang/include
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does LLVM CMake not define a variable for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to use ${LLVM_EXTERNAL_CLANG_SOURCE_DIR}.

Unfortunately there doesn't seem to be an equivalent for the build directory, from which an .inc file that defines CLANG_VERSION_MAJOR is included.


auto *LLVMCtx = &Module->getContext();
Module.reset();
delete LLVMCtx;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How expensive is it to set up and destroy the LLVMContext on every call to RTC? Would it be an alternative to use the context from JITContext?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, certain things like constants and metadata are stored within LLVMContext and won't be deallocated even if a module which references them is deallocated. Therefore, keeping LLVMContext between RTC call invocations could result in some memory build-up.

At least that is the behavior we discovered a few years ago when we were debugging exceptionally huge memory footprint of sycl-post-link where we had a huge codebase compiled with per-kernel device code split. I don't know if anything has changed since that in the upstream LLVM, but we hadn't proposed any patches back then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's certainly possible to pass an existing context into the ToolAction, but that also raises questions of thread safety.

For the performance implications, a) yes, looks like setting up a context does involve a non-trivial amount of work, and b) still seems true that types, constants and metadata are allocated in the context and not freed when the module is destroyed. I'd propose to keep the simple implementation for now, and will look out for the context setup overhead once we start benchmarking the RTC.

Comment on lines +99 to +101
NewArgs.push_back((Twine("-resource-dir=") + DPCPPRoot + "/lib/clang/" +
Twine(CLANG_VERSION_MAJOR))
.str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these paths also apply in a packaged release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a release icpx follows the same path structure (checked with -print-resource-dir).

Comment on lines 347 to 367
namespace sycl {
inline namespace _V1 {
namespace ext::oneapi::experimental {
namespace detail {

bool SYCLJIT_Compilation_Available() { return false; }

spirv_vec_t
SYCLJIT_to_SPIRV(const std::string &SYCLSource, include_pairs_t IncludePairs,
const std::vector<std::string> &UserArgs, std::string *LogPtr,
const std::vector<std::string> &RegisteredKernelNames) {
(void)SYCLSource;
(void)IncludePairs;
(void)UserArgs;
(void)LogPtr;
(void)RegisteredKernelNames;

throw sycl::exception(sycl::errc::build,
"kernel_compiler via sycl-jit is not available");
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With [[maybe_unused]], we should be able to avoid the double declaration and can just ifdef the body of the function. In any case, we don't need to duplicate the namespace declarations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's neat.

Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
opencl = 0,
spirv = 1,
sycl = 2 /* cuda */,
sycljit = 99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of a public interface. Can we document it somewhere? Also, I don't think you need to make a jump, as long as we don't change it after it's merged.

Side note, I personally prefer sycl_jit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only temporary until we complete functionality in follow-up PRs, so not intended to ever be exposed users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the enum value and added a comment that this is temporary.


// TODO: Handle situation.
assert(RegisteredKernelNames.empty() &&
"Instantiation of kernel templates NYI");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we throw instead? In cases where asserts are disabled what would happen if execution continues from here?

Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
extern "C" JITResult compileSYCL(const char *SYCLSource,
View<IncludePair> IncludePairs,
View<const char *> UserArgs,
const char *DPCPPRoot) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DPCPPRoot is not like the other arguments. Doesn't it seem like the routine should be able to figure that out itself, rather than being provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense; it's detected from inside the JIT library now.

CommandLine.append(UserArgs.begin(), UserArgs.end());
clang::tooling::FixedCompilationDatabase DB{"./", CommandLine};

constexpr auto SourcePath = "rtc.cpp";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than hard-code "rtc.cpp" in, can this 'fantasy name' for the file be an argument to the API? It might show up in debug information, so it might be useful to users to be able to disambiguate, rather than having every dynamic device compiled kernel originate with the same fictional file name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will do 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I'm passing in a semi-random ID, same as the file-based implementation. There's no property yet in the extension to specify a file name or prefix, correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the reason that a user might want to change this name? Why is it better to have a semi-random ID as a default rather than a fixed string?

I can imagine that the name might show up in error / log messages, for example, when there is a syntax error in the source string. If that's the only case the name is visible, it seems like having a fixed string like rtc.cpp would be fine, and probably preferable to a name with a random number.

I'm not opposed to adding a property which allows the user to set this name, but I think it should be an optional property because I think many people will not care what the name is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can imagine that the name might show up in error / log messages, for example, when there is a syntax error in the source string. If that's the only case the name is visible, [...]

Yes, that's the only case. I agree that the ID doesn't add much value here because we don't materialise anything on the actual filesystem. I'll keep the plumbing to pass the filename down to the JIT, but will set it rtc.cpp until there's a need and a means to modify it from the extension.

sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr, "compileSYCL"));
if (!this->CompileSYCLHandle) {
printPerformanceWarning(
"Cannot resolve JIT library function entry point");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds more serious than a mere performance warning :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printPerformanceWarning is the generic error message helper in sycl-jit, but yes, I agree it's a bit of a misnomer when used here (and while attempting to set-up the other entrypoints before).

Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants