-
Notifications
You must be signed in to change notification settings - Fork 77
[Comgr] Add an unpackaging action #668
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
base: amd-staging
Are you sure you want to change the base?
Conversation
|
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 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. |
amd/comgr/src/comgr-compiler.cpp
Outdated
| 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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:
llvm-project/amd/comgr/src/comgr.cpp
Line 312 in 41f7de0
| DataKind = AMD_COMGR_DATA_KIND_UNDEF; |
There was a problem hiding this comment.
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.
jhuber6
left a comment
There was a problem hiding this 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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| // 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // 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
There was a problem hiding this comment.
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, "")) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
| StringRef OutputPrefix = Input->Name; | ||
| size_t Index = OutputPrefix.find_last_of("."); | ||
| OutputPrefix = OutputPrefix.substr(0, Index); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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?
| // 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 |
There was a problem hiding this comment.
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"?
Currently, clang uses the
clang-offload-bundlerto offload GPU binaries, but thellvm-offload-binarypackager is a possible replacement for GPU offloading. This PR adds support to Comgr for retrieving the files from anllvm-offload-binarypackage through an Unpackager class and anAMD_COMGR_ACTION_UNPACKAGEaction.