Skip to content

Conversation

mdtoguchi
Copy link
Contributor

When generating preprocessed files for SYCL offloading, create a fully packaged file that contains both the HOST and DEVICE binaries. This will allow for consumption of these binary preprocessed files to be more useful, as opposed to only being able to preprocess and keep the host side of the offloading compilation

When the driver encounters preprocessed (file.ii) files on the command line, these are processed in the following way:

  • Determines if the file is a packaged file (offload binary)
  • Extracts device side
  • Compiles device side, packages generated device into offload binary
  • Extracts host side
  • Compiles host side, embedding device binary

Offload binary determination is performed by checking the magic number associated with the input file. The extraction is done via the clang-offload-packager using a new JobAction.

When no output file is given, we will not package the preprocessed files but will just perform the host preprocessing. When output to a file (with an output file option), we will perform the host and device compilation, package and output to that file.

When generating preprocessed files for SYCL offloading, create a fully
packaged file that contains both the HOST and DEVICE binaries.  This
will allow for consumption of these binary preprocessed files to be more
useful, as opposed to only being able to preprocess and keep the host
side of the offloading compilation

When the driver encounters preprocessed (file.ii) files on the command
line, these are processed in the following way:
 - Determines if the file is a packaged file (offload binary)
 - Extracts device side
 - Compiles device side, packages generated device into offload binary
 - Extracts host side
 - Compiles host side, embedding device binary

Offload binary determination is performed by checking the magic number
associated with the input file. The extraction is done via the
clang-offload-packager using a new JobAction.

When no output file is given, we will not package the preprocessed files
but will just perform the host preprocessing.  When output to a file
(with an output file option), we will perform the host and device
compilation, package and output to that file.
@mdtoguchi mdtoguchi marked this pull request as ready for review August 22, 2025 14:24
@mdtoguchi mdtoguchi requested review from a team as code owners August 22, 2025 14:24
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.

New test LGTM

@@ -10389,6 +10391,50 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs, Inputs, Output));
}

// Use the clang-offload-packager to extract binaries from an packaged
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Use the clang-offload-packager to extract binaries from an packaged
// Use the clang-offload-packager to extract device binaries from a packaged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The binaries within a packaged file are not always device binaries in our usage, as they can be either host or device binaries. I'll update, but not add 'device'.

@srividya-sundaram
Copy link
Contributor

  • Determines if the file is a packaged file (offload binary)

Earlier description says : packaged file that contains **both** the HOST and DEVICE binaries.

@@ -86,6 +88,13 @@ void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch,
// Deps job uses the host kinds.
if (Kind == OffloadDepsJobClass)
return;
// Packaging actions can use host kinds for preprocessing.
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure I follow this comment. Can you add a more descriptive comment.

/// When generating preprocessed files, verify the compilation phases.
// RUN: %clangxx --target=x86_64-unknown-linux-gnu --offload-new-driver -fsycl -E %s -o %t.ii -ccc-print-phases 2>&1 \
// RUN: | FileCheck %s -check-prefix PREPROC_PHASES
// RUN: %clang_cl --target=x86_64-unknown-linux-gnu --offload-new-driver -fsycl -P %s -Fi%t.ii -ccc-print-phases 2>&1 \
Copy link
Contributor

@srividya-sundaram srividya-sundaram Aug 26, 2025

Choose a reason for hiding this comment

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

Why is the target a Linux for clang_cl executable?

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 more for lining up the host target with the check strings. I can update to use a Windows based target instead, expanding the test accordingly.

/// output file name.
// RUN: %clangxx --offload-new-driver -fsycl -E %s -o sycl-preprocess.ii -### 2>&1 \
// RUN: | FileCheck %s -check-prefix PREPROC_TOOLS
// RUN: %clang_cl --offload-new-driver -fsycl -P %s -Fisycl-preprocess.ii -### 2>&1 \
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we passing -P here?
-P description reads: Disable linemarker output in -E mode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use of -P enables preprocessing information. It's just another way of covering enabling preprocessing only with the clang-cl driver.

/// output file name.
// RUN: %clangxx --offload-new-driver -fsycl -E %s -o sycl-preprocess.ii -### 2>&1 \
// RUN: | FileCheck %s -check-prefix PREPROC_TOOLS
// RUN: %clang_cl --offload-new-driver -fsycl -P %s -Fisycl-preprocess.ii -### 2>&1 \
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this some special file format - -Fisycl-preprocess.ii ?

Copy link
Contributor Author

@mdtoguchi mdtoguchi Aug 26, 2025

Choose a reason for hiding this comment

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

It's an MSVC compatible way of designating the output file from the preprocessing step.

(from the MSVC help output)
/Fi[file] name preprocessed file

@mdtoguchi
Copy link
Contributor Author

  • Determines if the file is a packaged file (offload binary)

Earlier description says : packaged file that contains **both** the HOST and DEVICE binaries.

Right. The file is still a packaged file that contains both the host and device binaries. But the representation of the file is an 'offload binary' that has a specific magic number that can be queried against. Akin to say an object or library file that have special magic number representations.

 - Update clang-cl based test to use Windows triple
 - Update some comments
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.

3 participants