Skip to content

[SYCL][Offload] Add SYCLBIN format and dump tool #16873

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

Merged

Conversation

steffenlarsen
Copy link
Contributor

This commit adds a new option --syclbin to the clang-linker-wrapper tool to produce SYCLBIN files from packaged device code binaries. The SYCLBIN format parsing added with these changes follow the format defined in the SYCLBIN design document.

To help both users and the testing framework, a tool (syclbin-dump) for printing information about a SYCLBIN file is added.

This commit adds a new option `--syclbin` to the clang-linker-wrapper
tool to produce SYCLBIN files from packaged device code binaries. The
SYCLBIN format parsing added with these changes follow the format
defined in the SYCLBIN design document.

To help both users and the testing framework, a tool (syclbin-dump) for
printing information about a SYCLBIN file is added.

Signed-off-by: Larsen, Steffen <[email protected]>
@steffenlarsen
Copy link
Contributor Author

Design document: #16872

Copy link
Contributor

@sarnex sarnex left a comment

Choose a reason for hiding this comment

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

just a quick first pass

@@ -18,6 +18,7 @@
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: do we need this instead of forward declaring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do. Unlike shared_ptr, unique_ptr insists on the contained type not being incomplete.

Copy link
Contributor

Choose a reason for hiding this comment

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

But why do we need this in a header file which is otherwise unchanged? It seems like either this #include should be moved straight into .cpp file, or into another .h file

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 seems to be a dependency bug that just wasn't noticed because Module.h was included before ModuleSplitter.h in all other cases.

Signed-off-by: Larsen, Steffen <[email protected]>
Signed-off-by: Larsen, Steffen <[email protected]>
Signed-off-by: Larsen, Steffen <[email protected]>
Signed-off-by: Larsen, Steffen <[email protected]>
Copy link
Contributor

@AlexeySachkov AlexeySachkov left a comment

Choose a reason for hiding this comment

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

Haven't yet looked at clang-linker-wrapper and SYCLBIN::read/write implementations

SmallVector<AbstractModule, 4> AbstractModules;

private:
SYCLBIN(const SYCLBIN &Other) = delete;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need it to be declared as private to delete it? If we explicitly deleting it, then I think it is better to put it next to other constructors so that they are all next to each other.

Comment on lines 163 to 164
for (const ModuleDesc &Desc : ModuleDescs) {
for (const module_split::SplitModule &SM : Desc.SplitModules) {
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 a very long for loop, I suggest that we outline some elements of it into smaller helper functions so that it is easier to grasp high-level logic and keep track of indentation levels

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've separated it a bit.

Comment on lines 43 to 45
static Expected<SmallString<0>> write(const SmallVector<ModuleDesc> &);

static Expected<std::unique_ptr<SYCLBIN>> read(MemoryBufferRef Source);
Copy link
Contributor

Choose a reason for hiding this comment

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

Implementation of read/write is quite complex, it has to be exhaustively tested. I'm not saying that all the testing should be here from day 1, but we at least need to lay some groundwork for it. For example what you can do is to have a unit-test which does write, then read and checks that result is the same as the initial input. See e471ba3 for inspiration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unittest has been added. Definitely could use some expanding on, but there are also a handful of TODOs in the current version, so it will be expanded upon over time.

Signed-off-by: Larsen, Steffen <[email protected]>
@steffenlarsen
Copy link
Contributor Author

@maksimsab & @AlexeySachkov & @asudarsa - I've refactored the implementation based on discussions with @maksimsab. It should reduce the memory relocation now by pre-calculating the size needed for the serialized SYCLBIN in memory. Let me know what you think!

if (!BlobOrError)
return BlobOrError.takeError();

std::string PropStr{BlobOrError->str()};
Copy link
Contributor

Choose a reason for hiding this comment

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

nit.
We could create here a MemoryBuffer right from the StringRef.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly it won't work. PropertySetRegistry::read expects null-termination and the binary blob doesn't have that. It gets it when we copy it to a string though.


namespace {

class SYCLBINBlockReader {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit.
These 3 classes could be replaced with simple functions making the maintenance easier for developers.

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 think the benefit of the class is that we can implicitly track how much memory is left. This lets us avoid reading outside the buffer, which is good for security. We could of course track that through functions as well, but it would require some additional variables we pass around. Personally I think this is cleaner, but if you think having free-functions for it is better, I won't die on this hill. 😉

@steffenlarsen
Copy link
Contributor Author

Friendly ping @KseniyaTikhomirova .

Copy link
Contributor

@KseniyaTikhomirova KseniyaTikhomirova left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@asudarsa asudarsa left a comment

Choose a reason for hiding this comment

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

LGTM.

@steffenlarsen steffenlarsen requested a review from mdtoguchi April 28, 2025 15:04
@steffenlarsen steffenlarsen merged commit c997f77 into intel:sycl Apr 29, 2025
38 of 39 checks passed
steffenlarsen added a commit to steffenlarsen/llvm that referenced this pull request Apr 29, 2025
This commit avoids a warning produced by
intel#16873 when building syclbin-dump
using compilers other than gcc and clang, where the -fno-rtti option
isn't available.

Signed-off-by: Larsen, Steffen <[email protected]>
@steffenlarsen
Copy link
Contributor Author

Windows post-commit failure should be addressed by #18233.

steffenlarsen added a commit that referenced this pull request Apr 29, 2025
This commit avoids a warning produced by
#16873 when building syclbin-dump
using compilers other than gcc and clang, where the -fno-rtti option
isn't available.

Signed-off-by: Larsen, Steffen <[email protected]>
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.

7 participants