@@ -7780,7 +7780,8 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
77807780 Action *HostAction) const {
77817781 // Don't build offloading actions if explicitly disabled or we do not have a
77827782 // valid source input.
7783- if (offloadHostOnly () || !types::isSrcFile (Input.first ))
7783+ if (offloadHostOnly () ||
7784+ !(types::isSrcFile (Input.first ) || Input.first == types::TY_PP_CXX))
77847785 return HostAction;
77857786
77867787 bool HIPNoRDC =
@@ -7845,6 +7846,25 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
78457846 for (const ToolChain *TC : ToolChains) {
78467847 for (StringRef Arch : OffloadArchs.lookup (TC)) {
78477848 TCAndArchs.push_back (std::make_pair (TC, Arch));
7849+ // Check if the InputArg is a preprocessed file that is created by the
7850+ // clang-offload-packager.
7851+ if (InputType == types::TY_PP_CXX &&
7852+ isOffloadBinaryFile (InputArg->getAsString (Args))) {
7853+ // Extract the specific preprocessed file given the current arch
7854+ // and triple. Add to DeviceActions if one was extracted.
7855+ ActionList PPActions;
7856+ OffloadAction::DeviceDependences DDep;
7857+ Action *IA = C.MakeAction <InputAction>(*InputArg, InputType, CUID);
7858+ PPActions.push_back (IA);
7859+ Action *PackagerAction =
7860+ C.MakeAction <OffloadPackagerExtractJobAction>(PPActions,
7861+ types::TY_PP_CXX);
7862+ DDep.add (*PackagerAction,
7863+ *C.getSingleOffloadToolChain <Action::OFK_Host>(), nullptr ,
7864+ C.getActiveOffloadKinds ());
7865+ DeviceActions.push_back (PackagerAction);
7866+ continue ;
7867+ }
78487868 DeviceActions.push_back (
78497869 C.MakeAction <InputAction>(*InputArg, InputType, CUID));
78507870 }
@@ -8002,6 +8022,37 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
80028022 DDep.add (*LinkAction, *C.getSingleOffloadToolChain <Action::OFK_Host>(),
80038023 nullptr , C.getActiveOffloadKinds ());
80048024 return C.MakeAction <OffloadAction>(DDep, types::TY_Nothing);
8025+ } else if (C.isOffloadingHostKind (Action::OFK_SYCL) &&
8026+ isa<PreprocessJobAction>(HostAction) &&
8027+ getFinalPhase (Args) == phases::Preprocess &&
8028+ Args.hasArg (options::OPT_o, options::OPT__SLASH_P,
8029+ options::OPT__SLASH_o)) {
8030+ // Performing preprocessing only. Take the host and device preprocessed
8031+ // files and package them together.
8032+ ActionList PackagerActions;
8033+ // Only add the preprocess actions from the device side. When one is
8034+ // found, add an additional compilation to generate the integration
8035+ // header/footer that is used for the host compile.
8036+ for (auto OA : OffloadActions) {
8037+ if (const OffloadAction *CurOA = dyn_cast<OffloadAction>(OA)) {
8038+ CurOA->doOnEachDependence (
8039+ [&](Action *A, const ToolChain *TC, const char *BoundArch) {
8040+ assert (TC && " Unknown toolchain" );
8041+ if (isa<PreprocessJobAction>(A)) {
8042+ PackagerActions.push_back (OA);
8043+ A->setCannotBeCollapsedWithNextDependentAction ();
8044+ Action *CompileAction =
8045+ C.MakeAction <CompileJobAction>(A, types::TY_Nothing);
8046+ DDeps.add (*CompileAction, *TC, BoundArch, Action::OFK_SYCL);
8047+ }
8048+ });
8049+ }
8050+ }
8051+ PackagerActions.push_back (HostAction);
8052+ Action *PackagerAction = C.MakeAction <OffloadPackagerJobAction>(
8053+ PackagerActions, types::TY_PP_CXX);
8054+ DDeps.add (*PackagerAction, *C.getSingleOffloadToolChain <Action::OFK_Host>(),
8055+ nullptr , C.getActiveOffloadKinds ());
80058056 } else if (C.isOffloadingHostKind (Action::OFK_SYCL) &&
80068057 Args.hasArg (options::OPT_fsycl_host_compiler_EQ)) {
80078058 // -fsycl-host-compiler will create a bundled object instead of an
@@ -8150,6 +8201,23 @@ Action *Driver::ConstructPhaseAction(
81508201 return C.MakeAction <VerifyPCHJobAction>(Input, types::TY_Nothing);
81518202 if (Args.hasArg (options::OPT_extract_api))
81528203 return C.MakeAction <ExtractAPIJobAction>(Input, types::TY_API_INFO);
8204+ // New offload driver enabled with a Preprocessed input file - check to make
8205+ // sure that the input file is an offload binary - if so, we need to
8206+ // extract the actual preprocessed file from the package, and that is what
8207+ // we will compile.
8208+ if (getUseNewOffloadingDriver () &&
8209+ TargetDeviceOffloadKind == Action::OFK_None &&
8210+ Input->getType () == types::TY_PP_CXX) {
8211+ const InputAction *IA = dyn_cast<InputAction>(Input);
8212+ if (IA && isOffloadBinaryFile (IA->getInputArg ().getAsString (Args))) {
8213+ ActionList PPActions;
8214+ PPActions.push_back (Input);
8215+ Action *PackagerAction = C.MakeAction <OffloadPackagerExtractJobAction>(
8216+ PPActions, types::TY_PP_CXX);
8217+ return C.MakeAction <CompileJobAction>(PackagerAction,
8218+ types::TY_LLVM_BC);
8219+ }
8220+ }
81538221 return C.MakeAction <CompileJobAction>(Input, types::TY_LLVM_BC);
81548222 }
81558223 case phases::Backend: {
@@ -9429,7 +9497,8 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
94299497 // For /P, preprocess to file named after BaseInput.
94309498 if (C.getArgs ().hasArg (options::OPT__SLASH_P) &&
94319499 ((AtTopLevel && isa<PreprocessJobAction>(JA)) ||
9432- isa<OffloadBundlingJobAction>(JA))) {
9500+ isa<OffloadBundlingJobAction>(JA) ||
9501+ isa<OffloadPackagerJobAction>(JA))) {
94339502 StringRef BaseName = llvm::sys::path::filename (BaseInput);
94349503 StringRef NameArg;
94359504 if (Arg *A = C.getArgs ().getLastArg (options::OPT__SLASH_Fi))
@@ -9465,6 +9534,14 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
94659534 }
94669535 }
94679536
9537+ // When generating preprocessed files (-E) with offloading enabled, redirect
9538+ // the output to a properly named output file.
9539+ if (JA.getType () == types::TY_PP_CXX && isa<OffloadPackagerJobAction>(JA)) {
9540+ if (Arg *FinalOutput =
9541+ C.getArgs ().getLastArg (options::OPT_o, options::OPT__SLASH_o))
9542+ return C.addResultFile (FinalOutput->getValue (), &JA);
9543+ }
9544+
94689545 // Default to writing to stdout?
94699546 if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput (JA)) {
94709547 return " -" ;
@@ -10437,6 +10514,12 @@ bool clang::driver::isStaticArchiveFile(const StringRef &FileName) {
1043710514 return (Magic == llvm::file_magic::archive);
1043810515}
1043910516
10517+ bool clang::driver::isOffloadBinaryFile (const StringRef &FileName) {
10518+ llvm::file_magic Magic;
10519+ llvm::identify_magic (FileName, Magic);
10520+ return (Magic == llvm::file_magic::offload_binary);
10521+ }
10522+
1044010523bool clang::driver::willEmitRemarks (const ArgList &Args) {
1044110524 // -fsave-optimization-record enables it.
1044210525 if (Args.hasFlag (options::OPT_fsave_optimization_record,
0 commit comments