mozilla / mozilla/sccache

Clang compilation with --offload-compress failing

Open
#2,762 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
7.7k
Forks
748
Avg merge
4d 7h
Merged PRs (30d)
21

Description

Clang added the --offload-compress flag recently-ish. The intention here is that the output clang offload bundle is compressed to reduce the executable binary size if there are a lot of kernels. It has the interesting side effect that clang++ -E test.hip --offload-compress produces binary output on the stdout, which causes process_preprocessed_file to not find any includes in the best case and crash in the worst case. --offload-compress is now enabled by default in many ROCm projects. This has been the case for a while actually, and from what I can tell this never worked properly.

I'm not sure why I've never ran into this crash before. It seems to just be bad luck, because sccache only crashes if the binary output just so happens to contains a sequence that hits the bail in process_prepdocessor_line, meaning something like # \d+.*" (even though the particular input that I'm observing the crash on doesn't match that regex).
https://github.com/mozilla/sccache/blob/3a85b1a704241c97fd1116b8409249d726e79055/src/compiler/c.rs#L877-L879

I don't think this is a bug in clang; clang++ -E test.hip --offload-arch=gfx1200 --offload-arch=gfx1100 also outputs multiple preprocessed source files. This makes sense, Clang compiles the source separately for each architecture including the host and devices and they are also preprocessed separately. You can verify this by running clang++ -E test.hip --offload-arch=gfx1200 --offload-arch=gfx1100 | grep BUNDLE. It will output something like.

// __CLANG_OFFLOAD_BUNDLE____START__ hip-amdgcn-amd-amdhsa--gfx1100
// __CLANG_OFFLOAD_BUNDLE____END__ hip-amdgcn-amd-amdhsa--gfx1100
// __CLANG_OFFLOAD_BUNDLE____START__ hip-amdgcn-amd-amdhsa--gfx1200
// __CLANG_OFFLOAD_BUNDLE____END__ hip-amdgcn-amd-amdhsa--gfx1200
// __CLANG_OFFLOAD_BUNDLE____START__ host-x86_64-unknown-linux-gnu-
// __CLANG_OFFLOAD_BUNDLE____END__ host-x86_64-unknown-linux-gnu-

(Compressed) offload bundles support source files as well in addition to the more typical binary files, so that also kinda makes sense.

Ultimately I think its the right choice to process that combined output for directives, sccache should get all include statements for all architectures combined to properly cache the source. Otherwise, something like the following may not get properly cached:

#ifdef __gfx942__
#include "something.h"
#endif

I think the easiest way to solve this is to simply strip out any --offload-compress calls when preprocessing the source. It shouldn't affect the compilation in any other way than that the output is not compressed. The alternative is of course to unbundle the output, but that is more effort since it would require calling the unbundler/reimplementing the unblundler.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/compiler/c.rs around process_preprocessed_file and process_prepdocessor_line, then reproduce the issue with clang++ -E test.hip --offload-compress. Verify that preprocessing with compressed offload output no longer crashes and that include directives from the combined output are still discovered.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.