@@ -7730,7 +7730,8 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
77307730 Action *HostAction) const {
77317731 // Don't build offloading actions if explicitly disabled or we do not have a
77327732 // valid source input.
7733- if (offloadHostOnly () || !types::isSrcFile (Input.first ))
7733+ if (offloadHostOnly () ||
7734+ !(types::isSrcFile (Input.first ) || Input.first == types::TY_PP_CXX))
77347735 return HostAction;
77357736
77367737 bool HIPNoRDC =
@@ -7795,6 +7796,25 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
77957796 llvm::sort (Sorted);
77967797 for (StringRef Arch : Sorted) {
77977798 TCAndArchs.push_back (std::make_pair (TC, Arch));
7799+ // Check if the InputArg is a preprocessed file that is created by the
7800+ // clang-offload-packager.
7801+ if (InputType == types::TY_PP_CXX &&
7802+ isOffloadBinaryFile (InputArg->getAsString (Args))) {
7803+ // Extract the specific preprocessed file given the current arch
7804+ // and triple. Add to DeviceActions if one was extracted.
7805+ ActionList PPActions;
7806+ OffloadAction::DeviceDependences DDep;
7807+ Action *IA = C.MakeAction <InputAction>(*InputArg, InputType, CUID);
7808+ PPActions.push_back (IA);
7809+ Action *PackagerAction =
7810+ C.MakeAction <OffloadPackagerExtractJobAction>(PPActions,
7811+ types::TY_PP_CXX);
7812+ DDep.add (*PackagerAction,
7813+ *C.getSingleOffloadToolChain <Action::OFK_Host>(), nullptr ,
7814+ C.getActiveOffloadKinds ());
7815+ DeviceActions.push_back (PackagerAction);
7816+ continue ;
7817+ }
77987818 DeviceActions.push_back (
77997819 C.MakeAction <InputAction>(*InputArg, InputType, CUID));
78007820 }
@@ -7952,6 +7972,41 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
79527972 DDep.add (*LinkAction, *C.getSingleOffloadToolChain <Action::OFK_Host>(),
79537973 nullptr , C.getActiveOffloadKinds ());
79547974 return C.MakeAction <OffloadAction>(DDep, types::TY_Nothing);
7975+ } else if (C.isOffloadingHostKind (Action::OFK_SYCL) &&
7976+ isa<PreprocessJobAction>(HostAction) &&
7977+ getFinalPhase (Args) == phases::Preprocess &&
7978+ Args.hasArg (options::OPT_o, options::OPT__SLASH_P,
7979+ options::OPT__SLASH_o)) {
7980+ // Performing preprocessing only. Take the host and device preprocessed
7981+ // files and package them together.
7982+ ActionList PackagerActions;
7983+ // Only add the preprocess actions from the device side. When one is
7984+ // found, add an additional compilation to generate the integration
7985+ // header/footer that is used for the host compile.
7986+ for (auto OA : OffloadActions) {
7987+ if (const OffloadAction *CurOA = dyn_cast<OffloadAction>(OA)) {
7988+ CurOA->doOnEachDependence (
7989+ [&](Action *A, const ToolChain *TC, const char *BoundArch) {
7990+ assert (TC && " Unknown toolchain" );
7991+ if (isa<PreprocessJobAction>(A)) {
7992+ PackagerActions.push_back (OA);
7993+ // DDep.add(*A, *TC, BoundArch, Action::OFK_SYCL);
7994+ // Action *AA = C.MakeAction<OffloadAction>(DDep,
7995+ // types::TY_PP_CXX);
7996+ A->setCannotBeCollapsedWithNextDependentAction ();
7997+ Action *CompileAction =
7998+ C.MakeAction <CompileJobAction>(A, types::TY_Nothing);
7999+ DDeps.add (*CompileAction, *TC, BoundArch, Action::OFK_SYCL);
8000+ // PackagerActions.push_back(AA);
8001+ }
8002+ });
8003+ }
8004+ }
8005+ PackagerActions.push_back (HostAction);
8006+ Action *PackagerAction = C.MakeAction <OffloadPackagerJobAction>(
8007+ PackagerActions, types::TY_PP_CXX);
8008+ DDeps.add (*PackagerAction, *C.getSingleOffloadToolChain <Action::OFK_Host>(),
8009+ nullptr , C.getActiveOffloadKinds ());
79558010 } else if (C.isOffloadingHostKind (Action::OFK_SYCL) &&
79568011 Args.hasArg (options::OPT_fsycl_host_compiler_EQ)) {
79578012 // -fsycl-host-compiler will create a bundled object instead of an
@@ -7974,7 +8029,7 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
79748029 // add each device output as a host dependency to ensure they are still built.
79758030 bool SingleDeviceOutput = !llvm::any_of (OffloadActions, [](Action *A) {
79768031 return A->getType () == types::TY_Nothing;
7977- }) && isa<CompileJobAction>(HostAction);
8032+ }) && ( isa<CompileJobAction>(HostAction) );
79788033 OffloadAction::HostDependence HDep (
79798034 *HostAction, *C.getSingleOffloadToolChain <Action::OFK_Host>(),
79808035 /* BoundArch=*/ nullptr , SingleDeviceOutput ? DDep : DDeps);
@@ -8100,6 +8155,23 @@ Action *Driver::ConstructPhaseAction(
81008155 return C.MakeAction <VerifyPCHJobAction>(Input, types::TY_Nothing);
81018156 if (Args.hasArg (options::OPT_extract_api))
81028157 return C.MakeAction <ExtractAPIJobAction>(Input, types::TY_API_INFO);
8158+ // New offload driver enabled with a Preprocessed input file - check to make
8159+ // sure that the input file is an offload binary - if so, we need to
8160+ // extract the actual preprocessed file from the package, and that is what
8161+ // we will compile.
8162+ if (getUseNewOffloadingDriver () &&
8163+ TargetDeviceOffloadKind == Action::OFK_None &&
8164+ Input->getType () == types::TY_PP_CXX) {
8165+ const InputAction *IA = dyn_cast<InputAction>(Input);
8166+ if (IA && isOffloadBinaryFile (IA->getInputArg ().getAsString (Args))) {
8167+ ActionList PPActions;
8168+ PPActions.push_back (Input);
8169+ Action *PackagerAction = C.MakeAction <OffloadPackagerExtractJobAction>(
8170+ PPActions, types::TY_PP_CXX);
8171+ return C.MakeAction <CompileJobAction>(PackagerAction,
8172+ types::TY_LLVM_BC);
8173+ }
8174+ }
81038175 return C.MakeAction <CompileJobAction>(Input, types::TY_LLVM_BC);
81048176 }
81058177 case phases::Backend: {
@@ -9379,7 +9451,8 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
93799451 // For /P, preprocess to file named after BaseInput.
93809452 if (C.getArgs ().hasArg (options::OPT__SLASH_P) &&
93819453 ((AtTopLevel && isa<PreprocessJobAction>(JA)) ||
9382- isa<OffloadBundlingJobAction>(JA))) {
9454+ isa<OffloadBundlingJobAction>(JA) ||
9455+ isa<OffloadPackagerJobAction>(JA))) {
93839456 StringRef BaseName = llvm::sys::path::filename (BaseInput);
93849457 StringRef NameArg;
93859458 if (Arg *A = C.getArgs ().getLastArg (options::OPT__SLASH_Fi))
@@ -9415,6 +9488,14 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
94159488 }
94169489 }
94179490
9491+ // When generating preprocessed files (-E) with offloading enabled, redirect
9492+ // the output to a properly named output file.
9493+ if (JA.getType () == types::TY_PP_CXX && isa<OffloadPackagerJobAction>(JA)) {
9494+ if (Arg *FinalOutput =
9495+ C.getArgs ().getLastArg (options::OPT_o, options::OPT__SLASH_o))
9496+ return C.addResultFile (FinalOutput->getValue (), &JA);
9497+ }
9498+
94189499 // Default to writing to stdout?
94199500 if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput (JA)) {
94209501 return " -" ;
@@ -10383,6 +10464,12 @@ bool clang::driver::isStaticArchiveFile(const StringRef &FileName) {
1038310464 return (Magic == llvm::file_magic::archive);
1038410465}
1038510466
10467+ bool clang::driver::isOffloadBinaryFile (const StringRef &FileName) {
10468+ llvm::file_magic Magic;
10469+ llvm::identify_magic (FileName, Magic);
10470+ return (Magic == llvm::file_magic::offload_binary);
10471+ }
10472+
1038610473bool clang::driver::willEmitRemarks (const ArgList &Args) {
1038710474 // -fsave-optimization-record enables it.
1038810475 if (Args.hasFlag (options::OPT_fsave_optimization_record,
0 commit comments