Skip to content

Conversation

@MixedMatched
Copy link

Currently, clang uses the clang-offload-bundler to offload GPU binaries, but the llvm-offload-binary packager is a possible replacement for GPU offloading. This PR adds support to Comgr for retrieving the files from an llvm-offload-binary package through an Unpackager class and an AMD_COMGR_ACTION_UNPACKAGE action.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@z1-cciauto
Copy link
Collaborator

Comment on lines 1498 to 1518
case llvm::object::IMG_Object:
FileExtension = "o";
break;
case llvm::object::IMG_Bitcode:
FileExtension = "bc";
break;
case llvm::object::IMG_Cubin:
FileExtension = "cubin";
break;
case llvm::object::IMG_Fatbinary:
FileExtension = "fatbin";
break;
case llvm::object::IMG_PTX:
FileExtension = "ptx";
break;
case llvm::object::IMG_SPIRV:
FileExtension = "spv";
break;
default:
FileExtension = "unknown";
break;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we ERROR_INVALID_ARGUMENT for unsupported ImageKinds? Cubin/PTX?

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking that we should imagine ImageKind as a non-exhaustive tag. All image data types are treated the exact same way (basically directly copying the byte data into a file descriptor) and I don't imagine that will need to change at any point, since text data would be treated the same way. I thought a .unknown fallback would be more justifiable than an error because, if it's a well-formed package, it shouldn't matter what type it is. OTOH, if packages do gain some sort of differentiated images in the future, our implementation might unexpectedly break. What do you think makes more sense?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm mostly thinking about it for the return AMD_COMGR_DATA_KIND

But maybe instead of erroring, we could set the output file DATA_KIND to AMD_COMGR_DATA_KIND_UNDEF (for the ImageKinds that don't have a corresponding DATA_KIND)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Though looking more, seems like DATA_KIND_UNDEF is used to signal destroyed/invalid data objects, so that probably won't work:

DataKind = AMD_COMGR_DATA_KIND_UNDEF;

Copy link
Author

Choose a reason for hiding this comment

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

Sure, that makes sense. Do you think we should simply return an error for "unsupported" filetypes or should there be a different data kind for valid but unknown files? I think my preference would probably be error for now, and then possibly add that data kind in the future.

@z1-cciauto
Copy link
Collaborator

@z1-cciauto
Copy link
Collaborator

@z1-cciauto
Copy link
Collaborator

@MixedMatched MixedMatched marked this pull request as ready for review December 12, 2025 17:25
@z1-cciauto
Copy link
Collaborator

@z1-cciauto
Copy link
Collaborator

@lamb-j lamb-j requested review from kzhuravl and yxsamliu December 12, 2025 20:10
@lamb-j
Copy link
Collaborator

lamb-j commented Dec 12, 2025

@jhuber6

Copy link

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Some initial comments, I don't know the COMGR style at all


std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(
llvm::Triple(Opts.Triple)));
std::unique_ptr<MCRegisterInfo> MRI(
Copy link

Choose a reason for hiding this comment

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

A lot of these seem like unrelated format changes

Copy link
Collaborator

Choose a reason for hiding this comment

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

@MixedMatched we could apply these formatting changes as a separate patch, land it, and rebase this one on top of that

Comment on lines +1454 to +1457
// if supplied file isn't a package, return an error
if (Input->DataKind != AMD_COMGR_DATA_KIND_PACKAGE) {
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
}
Copy link

Choose a reason for hiding this comment

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

Suggested change
// if supplied file isn't a package, return an error
if (Input->DataKind != AMD_COMGR_DATA_KIND_PACKAGE) {
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
}
// if supplied file isn't a package, return an error.
if (Input->DataKind != AMD_COMGR_DATA_KIND_PACKAGE)
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;

Does COMGR follow the LLVM coding style? Would do it like this and later if so

Copy link
Collaborator

Choose a reason for hiding this comment

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

Where possible we try to use the same style as LLVM

}

// Generate random name if none provided
if (!strcmp(Input->Name, "")) {
Copy link

Choose a reason for hiding this comment

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

This should be a StringRef that checks if it's empty, that will correctly handle nullptr.

// Generate random name if none provided
if (!strcmp(Input->Name, "")) {
const size_t BufSize = sizeof(char) * 30;
char *Buf = (char *)malloc(BufSize);
Copy link

Choose a reason for hiding this comment

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

Does this ever get freed? Much better to use a unique pointer or a SmallVector than to use manual memory management.

Also, if you're just creating a random filename I would suggest using llvm::sys::fs::createUniquePath or similar.

Comment on lines +1483 to +1485
StringRef OutputPrefix = Input->Name;
size_t Index = OutputPrefix.find_last_of(".");
OutputPrefix = OutputPrefix.substr(0, Index);
Copy link

Choose a reason for hiding this comment

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

Use llvm::sys::path::extension if that's what you're after.


for (StringRef Entry : ActionInfo->PackageEntryIDs) {
// TODO: this should probably check compatability, not strict equivalence
if (Entry == Target) {
Copy link

Choose a reason for hiding this comment

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

Could we use llvm::object::areTargetsCompatible?

}

auto *DataKind = DataKinds.begin();
for (StringRef OutputFilePath : OutputFileNames) {
Copy link

Choose a reason for hiding this comment

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

Could we use llvm::zip_equal and C++17 structured bindings to make this cleaner?

amd_comgr_status_t UnpackageCommand::execute(raw_ostream &LogS) {
StringMap<StringRef> Worklist;
const auto *Output = OutputFileNames.begin();
for (auto &Target : TargetNames) {
Copy link

Choose a reason for hiding this comment

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

Can probably zip_equal here as well

OutputBuffer.resize_for_overwrite(OutputSize + sizeof(OneOutputFileSize) +
OneOutputFileSize);

memcpy(OutputBuffer.data() + OutputSize, &OneOutputFileSize,
Copy link

Choose a reason for hiding this comment

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

Do we need raw memcpy here? Seems a little weird but I don't know the underlying format we're writing to

const llvm::object::OffloadBinary *Binary = File.getBinary();
StringRef Triple = Binary->getTriple();
StringRef Arch = Binary->getArch();
std::string Target = (Triple + "-" + Arch).str();
Copy link

Choose a reason for hiding this comment

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

I preferred to handle this as an ordered pair, that's what TargetID's type alias is. But I guess it's handled as a string here so we need compatibility?

@lamb-j lamb-j requested a review from jmmartinez December 12, 2025 23:36
Comment on lines +3 to +18
// RUN: clang -c -x hip --offload-arch=gfx900 \
// RUN: -nogpulib -nogpuinc -emit-llvm \
// RUN: --offload-device-only %s -o %t.gfx900.bc
//
// RUN: clang -c -x hip --offload-arch=gfx1030 \
// RUN: -nogpulib -nogpuinc -emit-llvm \
// RUN: --offload-device-only %s -o %t.gfx1030.bc
//
// RUN: llvm-offload-binary \
// RUN: --image=file=%t.gfx900.bc,triple=amdgcn-amd-amdhsa,arch=gfx900,kind=hip \
// RUN: --image=file=%t.gfx1030.bc,triple=amdgcn-amd-amdhsa,arch=gfx1030,kind=hip \
// RUN: -o %t.package.bc
//
// COM: extract using Comgr
// RUN: unpackage %t.package.bc amdgcn-amd-amdhsa-gfx900 %t.gfx900.output.bc
// RUN: llvm-dis %t.gfx900.output.bc -o - | FileCheck --check-prefixes=BOTH,GFX9 %s
Copy link
Collaborator

Choose a reason for hiding this comment

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

@jhuber6 is there a way to generate a package for this test via a single clang command?

Something analogous to "clang -c -emit-llvm --offload-device-only --gpu-bundle-output"?

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.

5 participants