From c36c96cd15e255bd80a86748798335f318e376aa Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Wed, 3 Dec 2025 11:06:54 -0800 Subject: [PATCH 1/3] [Driver][SYCL] Compile source file for integration header generation When compiler for preprocessing only, the preprocessing step that occurs does not generate the integration header/footer information that is used during the host compilation step. To generate the integration information in this case, the driver inserts an additional compilation step that creates the files to be consumed at the host step. The compilation step to produce the integration information is using the generated preprocessed file. For cases where there are build warnings that are emitted during this step, use of -Werror will cause the compilation to fail. These diagnostics are typically suppressed when not performing preprocess only compilations, but due to the fact that the build step against the preprocessed file does not know to suppress the diagnostics from system headers, compilation behaviors are at a disconnect. To fix this, the behavior when creating a preprocessed file (i.e. when we are using -E and stopping compilation), the driver will now compile the original source file instead of the preprocessed file. --- clang/lib/Driver/Driver.cpp | 22 +++++++++++++++---- clang/test/Driver/sycl-offload-nvptx.cpp | 2 +- .../test/Driver/sycl-preprocess-old-model.cpp | 2 +- clang/test/Driver/sycl-preprocess.cpp | 2 +- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 475c1fddb0363..4a1a5098fff91 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -5016,14 +5016,20 @@ class OffloadingActionBuilder final { llvm::zip(SYCLDeviceActions, SYCLTargetInfoList)) { Action *&A = std::get<0>(TargetActionInfo); auto &TargetInfo = std::get<1>(TargetActionInfo); - A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, + Action *PreprocAction = + C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, AssociatedOffloadKind); - if (SYCLDeviceOnly) + if (SYCLDeviceOnly) { + A = PreprocAction; continue; + } // Add an additional compile action to generate the integration - // header. + // header. This action compiles the source file instead of the + // generated preprocessed file to allow for control of the + // diagnostics that could come from the system headers. Action *CompileAction = C.MakeAction(A, types::TY_Nothing); + A = PreprocAction; DA.add(*CompileAction, *TargetInfo.TC, TargetInfo.BoundArch, Action::OFK_SYCL); } @@ -7974,8 +7980,16 @@ Action *Driver::BuildOffloadingActions(Compilation &C, if (isa(A)) { PackagerActions.push_back(OA); A->setCannotBeCollapsedWithNextDependentAction(); + // The input to the preprocessed job is the compilation job. + // Take that input action (it should be one input) which is + // the source file that should be compiled to generate the + // integration header/footer. + ActionList PreprocInputs = A->getInputs(); + assert(PreprocInputs.size() == 1 && + "Single input size to preprocess action expected."); Action *CompileAction = - C.MakeAction(A, types::TY_Nothing); + C.MakeAction(PreprocInputs.front(), + types::TY_Nothing); DDeps.add(*CompileAction, *TC, BoundArch, Action::OFK_SYCL); } }); diff --git a/clang/test/Driver/sycl-offload-nvptx.cpp b/clang/test/Driver/sycl-offload-nvptx.cpp index d2dcbada56def..0d69ce94dd953 100644 --- a/clang/test/Driver/sycl-offload-nvptx.cpp +++ b/clang/test/Driver/sycl-offload-nvptx.cpp @@ -106,7 +106,7 @@ // RUN: | FileCheck -check-prefix=CHK-PREPROC %s // CHK-PREPROC: 1: preprocessor, {0}, c++-cpp-output, (device-sycl, sm_[[CUDA_VERSION:[0-9.]+]]) // CHK-PREPROC: 2: offload, "device-sycl (nvptx64-nvidia-cuda:sm_[[CUDA_VERSION]])" {1}, c++-cpp-output -// CHK-PREPROC: 4: compiler, {1}, none, (device-sycl, sm_[[CUDA_VERSION]]) +// CHK-PREPROC: 4: compiler, {0}, none, (device-sycl, sm_[[CUDA_VERSION]]) // // RUN: not %clangxx -### -std=c++11 -target x86_64-unknown-linux-gnu -fsycl \ // RUN: -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=%S/Inputs/no/CUDA/path/here \ diff --git a/clang/test/Driver/sycl-preprocess-old-model.cpp b/clang/test/Driver/sycl-preprocess-old-model.cpp index 08c93a154d3ee..efe968b7b298a 100644 --- a/clang/test/Driver/sycl-preprocess-old-model.cpp +++ b/clang/test/Driver/sycl-preprocess-old-model.cpp @@ -22,7 +22,7 @@ // PREPROC_PHASES: 1: preprocessor, {0}, c++-cpp-output, (device-sycl) // PREPROC_PHASES: 2: offload, "device-sycl (spir64-unknown-unknown)" {1}, c++-cpp-output // PREPROC_PHASES: 3: input, "[[INPUT]]", c++, (host-sycl) -// PREPROC_PHASES: 4: compiler, {1}, none, (device-sycl) +// PREPROC_PHASES: 4: compiler, {0}, none, (device-sycl) // PREPROC_PHASES: 5: offload, "host-sycl (x86_64-unknown-linux-gnu)" {3}, "device-sycl (spir64-unknown-unknown)" {4}, c++ // PREPROC_PHASES: 6: preprocessor, {5}, c++-cpp-output, (host-sycl) // PREPROC_PHASES: 7: clang-offload-bundler, {2, 6}, c++-cpp-output, (host-sycl) diff --git a/clang/test/Driver/sycl-preprocess.cpp b/clang/test/Driver/sycl-preprocess.cpp index 91fd801e586db..41f037e246d75 100644 --- a/clang/test/Driver/sycl-preprocess.cpp +++ b/clang/test/Driver/sycl-preprocess.cpp @@ -24,7 +24,7 @@ // PREPROC_PHASES: 1: preprocessor, {0}, c++-cpp-output, (host-sycl) // PREPROC_PHASES: 2: input, "[[INPUT]]", c++, (device-sycl) // PREPROC_PHASES: 3: preprocessor, {2}, c++-cpp-output, (device-sycl) -// PREPROC_PHASES: 4: compiler, {3}, none, (device-sycl) +// PREPROC_PHASES: 4: compiler, {2}, none, (device-sycl) // PREPROC_PHASES: 5: offload, "device-sycl (spir64-unknown-unknown)" {3}, c++-cpp-output // PREPROC_PHASES: 6: llvm-offload-binary, {5, 1}, c++-cpp-output // PREPROC_PHASES: 7: offload, "host-sycl ([[TARGET]])" {1}, "device-sycl (spir64-unknown-unknown)" {3}, "device-sycl (spir64-unknown-unknown)" {4}, " ([[TARGET]])" {6}, c++-cpp-output From fcad067ac88dd37625c5d0fd1eeacb1566f457b2 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Wed, 3 Dec 2025 13:29:04 -0800 Subject: [PATCH 2/3] clang format --- clang/lib/Driver/Driver.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 4a1a5098fff91..06f362b18b65a 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -5016,9 +5016,8 @@ class OffloadingActionBuilder final { llvm::zip(SYCLDeviceActions, SYCLTargetInfoList)) { Action *&A = std::get<0>(TargetActionInfo); auto &TargetInfo = std::get<1>(TargetActionInfo); - Action *PreprocAction = - C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, - AssociatedOffloadKind); + Action *PreprocAction = C.getDriver().ConstructPhaseAction( + C, Args, CurPhase, A, AssociatedOffloadKind); if (SYCLDeviceOnly) { A = PreprocAction; continue; @@ -7986,10 +7985,9 @@ Action *Driver::BuildOffloadingActions(Compilation &C, // integration header/footer. ActionList PreprocInputs = A->getInputs(); assert(PreprocInputs.size() == 1 && - "Single input size to preprocess action expected."); - Action *CompileAction = - C.MakeAction(PreprocInputs.front(), - types::TY_Nothing); + "Single input size to preprocess action expected."); + Action *CompileAction = C.MakeAction( + PreprocInputs.front(), types::TY_Nothing); DDeps.add(*CompileAction, *TC, BoundArch, Action::OFK_SYCL); } }); From cb4a63471c7fd8a3899c085cc24dd3cfbc4658e2 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Mon, 8 Dec 2025 13:40:43 -0800 Subject: [PATCH 3/3] Update the comment, the usage of compile/preprocess was backwards --- clang/lib/Driver/Driver.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 06f362b18b65a..00463e7dfac94 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -7979,9 +7979,9 @@ Action *Driver::BuildOffloadingActions(Compilation &C, if (isa(A)) { PackagerActions.push_back(OA); A->setCannotBeCollapsedWithNextDependentAction(); - // The input to the preprocessed job is the compilation job. + // The input to the compilation job is the preprocessed job. // Take that input action (it should be one input) which is - // the source file that should be compiled to generate the + // the source file and compile that file to generate the // integration header/footer. ActionList PreprocInputs = A->getInputs(); assert(PreprocInputs.size() == 1 &&