Skip to content

Commit 3742b93

Browse files
authored
[Driver][SYCL] Do device section checking only when offloading is enabled (#5384)
Currently, device section checking is always called whether offloading is enabled or not. However, it is not needed when offloading is not enabled. Signed-off-by: Qichao Gu <[email protected]>
1 parent 43a4192 commit 3742b93

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5551,6 +5551,59 @@ class OffloadingActionBuilder final {
55515551
/*BoundArch*/ nullptr, ActiveOffloadKinds);
55525552
return C.MakeAction<OffloadAction>(HDep, DDeps);
55535553
}
5554+
5555+
void unbundleStaticArchives(Compilation &C, DerivedArgList &Args,
5556+
DeviceActionBuilder::PhasesTy &PL) {
5557+
if (!Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false))
5558+
return;
5559+
5560+
// Go through all of the args, and create a Linker specific argument list.
5561+
// When dealing with fat static archives each archive is individually
5562+
// unbundled.
5563+
SmallVector<const char *, 16> LinkArgs(getLinkerArgs(C, Args));
5564+
const llvm::opt::OptTable &Opts = C.getDriver().getOpts();
5565+
auto unbundleStaticLib = [&](types::ID T, const StringRef &A) {
5566+
Arg *InputArg = MakeInputArg(Args, Opts, Args.MakeArgString(A));
5567+
Action *Current = C.MakeAction<InputAction>(*InputArg, T);
5568+
addHostDependenceToDeviceActions(Current, InputArg, Args);
5569+
addDeviceDependencesToHostAction(Current, InputArg, phases::Link,
5570+
PL.back(), PL);
5571+
};
5572+
for (StringRef LA : LinkArgs) {
5573+
// At this point, we will process the archives for FPGA AOCO and
5574+
// individual archive unbundling for Windows.
5575+
if (!isStaticArchiveFile(LA))
5576+
continue;
5577+
// FPGA AOCX/AOCR files are archives, but we do not want to unbundle them
5578+
// here as they have already been unbundled and processed for linking.
5579+
// TODO: The multiple binary checks for FPGA types getting a little out
5580+
// of hand. Improve this by doing a single scan of the args and holding
5581+
// that in a data structure for reference.
5582+
if (hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCX) ||
5583+
hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCR) ||
5584+
hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCR_EMU))
5585+
continue;
5586+
// For offload-static-libs we add an unbundling action for each static
5587+
// archive which produces list files with extracted objects. Device lists
5588+
// are then added to the appropriate device link actions and host list is
5589+
// ignored since we are adding offload-static-libs as normal libraries to
5590+
// the host link command.
5591+
if (hasOffloadSections(C, LA, Args)) {
5592+
// Pass along the static libraries to check if we need to add them for
5593+
// unbundling for FPGA AOT static lib usage. Uses FPGA aoco type to
5594+
// differentiate if aoco unbundling is needed. Unbundling of aoco is
5595+
// not needed for emulation, as these are treated as regular archives.
5596+
if (!C.getDriver().isFPGAEmulationMode())
5597+
unbundleStaticLib(types::TY_FPGA_AOCO, LA);
5598+
// Do not unbundle any AOCO archive as a regular archive when we are
5599+
// in FPGA Hardware/Simulation mode.
5600+
if (!C.getDriver().isFPGAEmulationMode() &&
5601+
hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCO))
5602+
continue;
5603+
unbundleStaticLib(types::TY_Archive, LA);
5604+
}
5605+
}
5606+
}
55545607
};
55555608
} // anonymous namespace.
55565609

@@ -5892,52 +5945,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
58925945
if (!LinkerInputs.empty() && C.getDriver().getOffloadStaticLibSeen())
58935946
OffloadBuilder.addDeviceLinkDependenciesFromHost(LinkerInputs);
58945947

5895-
// Go through all of the args, and create a Linker specific argument list.
5896-
// When dealing with fat static archives each archive is individually
5897-
// unbundled.
5898-
SmallVector<const char *, 16> LinkArgs(getLinkerArgs(C, Args));
5899-
const llvm::opt::OptTable &Opts = getOpts();
5900-
auto unbundleStaticLib = [&](types::ID T, const StringRef &A) {
5901-
Arg *InputArg = MakeInputArg(Args, Opts, Args.MakeArgString(A));
5902-
Action *Current = C.MakeAction<InputAction>(*InputArg, T);
5903-
OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg, Args);
5904-
OffloadBuilder.addDeviceDependencesToHostAction(
5905-
Current, InputArg, phases::Link, PL.back(), PL);
5906-
};
5907-
for (StringRef LA : LinkArgs) {
5908-
// At this point, we will process the archives for FPGA AOCO and individual
5909-
// archive unbundling for Windows.
5910-
if (!isStaticArchiveFile(LA))
5911-
continue;
5912-
// FPGA AOCX/AOCR files are archives, but we do not want to unbundle them
5913-
// here as they have already been unbundled and processed for linking.
5914-
// TODO: The multiple binary checks for FPGA types getting a little out
5915-
// of hand. Improve this by doing a single scan of the args and holding
5916-
// that in a data structure for reference.
5917-
if (hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCX) ||
5918-
hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCR) ||
5919-
hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCR_EMU))
5920-
continue;
5921-
// For offload-static-libs we add an unbundling action for each static
5922-
// archive which produces list files with extracted objects. Device lists
5923-
// are then added to the appropriate device link actions and host list is
5924-
// ignored since we are adding offload-static-libs as normal libraries to
5925-
// the host link command.
5926-
if (hasOffloadSections(C, LA, Args)) {
5927-
// Pass along the static libraries to check if we need to add them for
5928-
// unbundling for FPGA AOT static lib usage. Uses FPGA aoco type to
5929-
// differentiate if aoco unbundling is needed. Unbundling of aoco is not
5930-
// needed for emulation, as these are treated as regular archives.
5931-
if (!C.getDriver().isFPGAEmulationMode())
5932-
unbundleStaticLib(types::TY_FPGA_AOCO, LA);
5933-
// Do not unbundle any AOCO archive as a regular archive when we are
5934-
// in FPGA Hardware/Simulation mode.
5935-
if (!C.getDriver().isFPGAEmulationMode() &&
5936-
hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCO))
5937-
continue;
5938-
unbundleStaticLib(types::TY_Archive, LA);
5939-
}
5940-
}
5948+
OffloadBuilder.unbundleStaticArchives(C, Args, PL);
59415949

59425950
// For an FPGA archive, we add the unbundling step above to take care of
59435951
// the device side, but also unbundle here to extract the host side
@@ -5973,7 +5981,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
59735981
if (UnbundlerInput && !PL.empty()) {
59745982
if (auto *IA = dyn_cast<InputAction>(UnbundlerInput)) {
59755983
std::string FileName = IA->getInputArg().getAsString(Args);
5976-
Arg *InputArg = MakeInputArg(Args, Opts, FileName);
5984+
Arg *InputArg = MakeInputArg(Args, getOpts(), FileName);
59775985
OffloadBuilder.addHostDependenceToDeviceActions(UnbundlerInput,
59785986
InputArg, Args);
59795987
OffloadBuilder.addDeviceDependencesToHostAction(

clang/test/Driver/sycl-offload.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,17 @@
132132
// RUN: %clangxx -### -Wl,-rpath,%S -fsycl -fintelfpga %t_empty.o %s 2>&1 \
133133
// RUN: | FileCheck -check-prefix NO_DIR_CHECK %s
134134
// NO_DIR_CHECK-NOT: clang-offload-bundler: error: '{{.*}}': Is a directory
135+
136+
// Device section checking only occur when offloading is enabled
137+
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu -fsycl %S/Inputs/SYCL/liblin64.a %s 2>&1 \
138+
// RUN: | FileCheck -check-prefix CHECK_SECTION %s
139+
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu %S/Inputs/SYCL/liblin64.a %s 2>&1 \
140+
// RUN: | FileCheck -check-prefix NO_CHECK_SECTION %s
141+
// CHECK_SECTION: clang-offload-bundler{{.*}} "-type=ao" "-targets=host-x86_64-unknown-linux-gnu"{{.*}} "-check-section"
142+
// CHECK_SECTION: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aocr-intel-unknown"{{.*}} "-check-section"
143+
// CHECK_SECTION: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aocx-intel-unknown"{{.*}} "-check-section"
144+
// CHECK_SECTION: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aocr_emu-intel-unknown"{{.*}} "-check-section"
145+
// NO_CHECK_SECTION-NOT: clang-offload-bundler{{.*}} "-type=ao" "-targets=host-x86_64-unknown-linux-gnu"{{.*}} "-check-section"
146+
// NO_CHECK_SECTION-NOT: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aocr-intel-unknown"{{.*}} "-check-section"
147+
// NO_CHECK_SECTION-NOT: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aocx-intel-unknown"{{.*}} "-check-section"
148+
// NO_CHECK_SECTION-NOT: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aocr_emu-intel-unknown"{{.*}} "-check-section"

0 commit comments

Comments
 (0)