From b3fb5addeb3d1b8368226814811bf83cdd4cd8fa Mon Sep 17 00:00:00 2001 From: Udit Agarwal Date: Wed, 6 Nov 2024 16:15:36 -0800 Subject: [PATCH] [Clang] Fix passing `-offload-compress` when compiling and linking device images separately (#15997) When compiling and linking device images separately, compression related flags are not propagated correctly to `offload-wrapper`. This PR fixes that and updates the test accordingly. --- clang/lib/Driver/ToolChains/Clang.cpp | 57 ++++++++++++------- .../compression_separate_compile.cpp | 7 +++ 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 3fa80d479936f..f90ba124e5a09 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -10353,33 +10353,48 @@ void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA, assert(JA.getInputs().size() == Inputs.size() && "Not have inputs for all dependence actions??"); - // For FPGA, we wrap the host objects before archiving them when using - // -fsycl-link. This allows for better extraction control from the - // archive when we need the host objects for subsequent compilations. if (OffloadingKind == Action::OFK_None && - C.getArgs().hasArg(options::OPT_fintelfpga) && C.getArgs().hasArg(options::OPT_fsycl_link_EQ)) { - // Add offload targets and inputs. - CmdArgs.push_back(C.getArgs().MakeArgString( - Twine("-kind=") + Action::GetOffloadKindName(OffloadingKind))); - CmdArgs.push_back( - TCArgs.MakeArgString(Twine("-target=") + Triple.getTriple())); + // For FPGA, we wrap the host objects before archiving them when using + // -fsycl-link. This allows for better extraction control from the + // archive when we need the host objects for subsequent compilations. + if (C.getArgs().hasArg(options::OPT_fintelfpga)) { - if (Inputs[0].getType() == types::TY_Tempfiletable || - Inputs[0].getType() == types::TY_Tempfilelist) - // Input files are passed via the batch job file table. - CmdArgs.push_back(C.getArgs().MakeArgString("-batch")); + // Add offload targets and inputs. + CmdArgs.push_back(C.getArgs().MakeArgString( + Twine("-kind=") + Action::GetOffloadKindName(OffloadingKind))); + CmdArgs.push_back( + TCArgs.MakeArgString(Twine("-target=") + Triple.getTriple())); - // Add input. - assert(Inputs[0].isFilename() && "Invalid input."); - CmdArgs.push_back(TCArgs.MakeArgString(Inputs[0].getFilename())); + if (Inputs[0].getType() == types::TY_Tempfiletable || + Inputs[0].getType() == types::TY_Tempfilelist) + // Input files are passed via the batch job file table. + CmdArgs.push_back(C.getArgs().MakeArgString("-batch")); - C.addCommand(std::make_unique( - JA, *this, ResponseFileSupport::None(), - TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), - CmdArgs, Inputs)); - return; + // Add input. + assert(Inputs[0].isFilename() && "Invalid input."); + CmdArgs.push_back(TCArgs.MakeArgString(Inputs[0].getFilename())); + + C.addCommand(std::make_unique( + JA, *this, ResponseFileSupport::None(), + TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), + CmdArgs, Inputs)); + return; + } else { + // When compiling and linking separately, we need to propagate the + // compression related CLI options to offload-wrapper. Don't propagate + // these options when wrapping objects for FPGA. + if (C.getInputArgs().getLastArg(options::OPT_offload_compress)) { + CmdArgs.push_back( + C.getArgs().MakeArgString(Twine("-offload-compress"))); + // -offload-compression-level=<> + if (Arg *A = C.getInputArgs().getLastArg( + options::OPT_offload_compression_level_EQ)) + CmdArgs.push_back(C.getArgs().MakeArgString( + Twine("-offload-compression-level=") + A->getValue())); + } + } } // Add offload targets and inputs. diff --git a/sycl/test-e2e/Compression/compression_separate_compile.cpp b/sycl/test-e2e/Compression/compression_separate_compile.cpp index 9e47bbebdc875..dab17e3506b4e 100644 --- a/sycl/test-e2e/Compression/compression_separate_compile.cpp +++ b/sycl/test-e2e/Compression/compression_separate_compile.cpp @@ -10,6 +10,13 @@ ////////////////////// Link device images // RUN: %clangxx --offload-compress -fsycl -fsycl-link -fsycl-targets=spir64_x86_64 -fPIC %t_kernel1_aot.o %t_kernel2_aot.o -o %t_compressed_image.o -v +// Make sure the clang-offload-wrapper is called with the --offload-compress +// option. +// RUN: %clangxx --offload-compress -fsycl -fsycl-link -fsycl-targets=spir64_x86_64 -fPIC %t_kernel1_aot.o %t_kernel2_aot.o -o %t_compressed_image.o -### &> %t_driver_opts.txt +// RUN: FileCheck -input-file=%t_driver_opts.txt %s --check-prefix=CHECK-DRIVER-OPTS + +// CHECK-DRIVER-OPTS: clang-offload-wrapper{{.*}} "-offload-compress" + ////////////////////// Compile the host program // RUN: %clangxx -fsycl -std=c++17 -Wno-attributes -Wno-deprecated-declarations -fPIC -c %s -o %t_main.o