Skip to content

[Driver][SYCL] Remove partial-link path when dealing with fat static … #2867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 28 additions & 122 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4605,41 +4605,27 @@ class OffloadingActionBuilder final {
!InputArg->getOption().hasFlag(options::LinkerInput) &&
!types::isSrcFile(HostAction->getType())) {
std::string InputName = InputArg->getAsString(Args);
// Do not create an unbundling action for an object when we know a fat
// static library is being used. A separate unbundling action is created
// for all objects and the fat static library.
// But in MSVC environment static offload archives are handled differently
// due to absence of partial linking support in the linker. Instead of
// partially linking input objects and static archives and then unbundling
// result we are unbundling all objects and offload archives to extract
// device parts. Therefore, in case on MSVC environment unbundling action
// for objects is still needed.
if (C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() ||
ActionList HostActionList;
Action *A(HostAction);
// Only check for FPGA device information when using fpga SubArch.
if (Args.hasArg(options::OPT_fintelfpga) &&
!(HostAction->getType() == types::TY_Object &&
isObjectFile(InputName) &&
C.getDriver().getOffloadStaticLibSeen())) {
ActionList HostActionList;
Action *A(HostAction);
// Only check for FPGA device information when using fpga SubArch.
if (Args.hasArg(options::OPT_fintelfpga) &&
!(HostAction->getType() == types::TY_Object &&
isObjectFile(InputName))) {
// Type FPGA aoco is a special case for -foffload-static-lib.
if (HostAction->getType() == types::TY_FPGA_AOCO) {
if (!hasFPGABinary(C, InputName, types::TY_FPGA_AOCO))
return false;
A = C.MakeAction<InputAction>(*InputArg, types::TY_FPGA_AOCO);
} else if (hasFPGABinary(C, InputName, types::TY_FPGA_AOCX))
A = C.MakeAction<InputAction>(*InputArg, types::TY_FPGA_AOCX);
else if (hasFPGABinary(C, InputName, types::TY_FPGA_AOCR))
A = C.MakeAction<InputAction>(*InputArg, types::TY_FPGA_AOCR);
}
auto UnbundlingHostAction = C.MakeAction<OffloadUnbundlingJobAction>(A);
UnbundlingHostAction->registerDependentActionInfo(
C.getSingleOffloadToolChain<Action::OFK_Host>(),
/*BoundArch=*/StringRef(), Action::OFK_Host);
HostAction = UnbundlingHostAction;
isObjectFile(InputName))) {
// Type FPGA aoco is a special case for -foffload-static-lib.
if (HostAction->getType() == types::TY_FPGA_AOCO) {
if (!hasFPGABinary(C, InputName, types::TY_FPGA_AOCO))
return false;
A = C.MakeAction<InputAction>(*InputArg, types::TY_FPGA_AOCO);
} else if (hasFPGABinary(C, InputName, types::TY_FPGA_AOCX))
A = C.MakeAction<InputAction>(*InputArg, types::TY_FPGA_AOCX);
else if (hasFPGABinary(C, InputName, types::TY_FPGA_AOCR))
A = C.MakeAction<InputAction>(*InputArg, types::TY_FPGA_AOCR);
}
auto UnbundlingHostAction = C.MakeAction<OffloadUnbundlingJobAction>(A);
UnbundlingHostAction->registerDependentActionInfo(
C.getSingleOffloadToolChain<Action::OFK_Host>(),
/*BoundArch=*/StringRef(), Action::OFK_Host);
HostAction = UnbundlingHostAction;
}

assert(HostAction && "Invalid host action!");
Expand Down Expand Up @@ -5096,84 +5082,9 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
OffloadBuilder.appendTopLevelLinkAction(Actions);

// Go through all of the args, and create a Linker specific argument list.
// When dealing with fat static archives, this is fed into the partial link
// step on Linux or each archive is individually addressed on Windows.
// When dealing with fat static archives each archive is individually
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit town:

Suggested change
// When dealing with fat static archives each archive is individually
// When dealing with fat static archives, each archive is individually

// unbundled.
SmallVector<const char *, 16> LinkArgs(getLinkerArgs(C, Args));
// When a static fat archive is provided, create a new unbundling step
// for all of the objects.
if (!C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() &&
C.getDriver().getOffloadStaticLibSeen()) {
ActionList UnbundlerInputs;
for (const auto &LI : LinkerInputs) {
// Unbundler only handles objects.
if (auto *IA = dyn_cast<InputAction>(LI)) {
std::string FileName = IA->getInputArg().getAsString(Args);
if ((IA->getType() == types::TY_Object && !isObjectFile(FileName)) ||
IA->getInputArg().getOption().hasFlag(options::LinkerInput))
// Pass the Input along to linker only.
continue;
}
UnbundlerInputs.push_back(LI);
}
const Arg *LastArg = nullptr;
auto addUnbundlerInput = [&](types::ID T, const StringRef &A) {
const llvm::opt::OptTable &Opts = getOpts();
Arg *InputArg = MakeInputArg(Args, Opts, C.getArgs().MakeArgString(A));
LastArg = InputArg;
Action *Current = C.MakeAction<InputAction>(*InputArg, T);
UnbundlerInputs.push_back(Current);
};
bool IsWholeArchive = false;
for (StringRef LA : LinkArgs) {
if (isStaticArchiveFile(LA)) {
addUnbundlerInput(
IsWholeArchive ? types::TY_WholeArchive : types::TY_Archive, LA);
continue;
}
if (optionMatches("-no-whole-archive", LA.str())) {
IsWholeArchive = false;
continue;
}
if (optionMatches("-whole-archive", LA.str())) {
IsWholeArchive = true;
continue;
}
if (isObjectFile(LA.str())) {
// Add any objects to the unbundler step. These objects are passed
// directly to the linker, so the driver does not know about them.
// FIXME - Better process objects passed to the linker. We are only
// adding these objects to the unbundler step, but these objects can
// potentially be fat objects that should be processed by the driver.
addUnbundlerInput(types::TY_Object, LA);
continue;
}
}

if (!UnbundlerInputs.empty() && !PL.empty()) {
Action *PartialLink =
C.MakeAction<PartialLinkJobAction>(UnbundlerInputs, types::TY_Object);
if (!LastArg) {
auto *TA = dyn_cast<Action>(UnbundlerInputs.back());
assert(TA->getKind() == Action::InputClass ||
TA->getKind() == Action::OffloadClass);

// If the Action is of OffloadAction type, use the HostAction of the
// specific Offload Action to set LastArg
if (auto *OA = dyn_cast<OffloadAction>(UnbundlerInputs.back()))
LastArg =
&(dyn_cast<InputAction>(OA->getHostDependence())->getInputArg());
else if (auto *IA = dyn_cast<InputAction>(UnbundlerInputs.back()))
// else set the LastArg based on Last InputAction
LastArg = &(IA->getInputArg());
}
Action *Current = C.MakeAction<InputAction>(*LastArg, types::TY_Object);
ActionList AL;
AL.push_back(PartialLink);
OffloadBuilder.addHostDependenceToUnbundlingAction(Current, AL, LastArg);
Current = OffloadBuilder.addDeviceDependencesToHostAction(Current,
LastArg, phases::Link, PL.back(), PL);
}
}
const llvm::opt::OptTable &Opts = getOpts();
auto unbundleStaticLib = [&](types::ID T, const StringRef &A) {
Arg *InputArg = MakeInputArg(Args, Opts, Args.MakeArgString(A));
Expand All @@ -5191,14 +5102,12 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
// as they have already been unbundled and processed for linking.
if (hasFPGABinary(C, LA.str(), types::TY_FPGA_AOCX))
continue;
// In MSVC environment offload-static-libs are handled slightly different
// because of missing support for partial linking in the linker. We add an
// unbundling action for each static archive which produces list files with
// extracted objects. Device lists are then added to the appropriate device
// link actions and host list is ignored since we are adding
// offload-static-libs as normal libraries to the host link command.
if (C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() &&
hasOffloadSections(C, LA, Args))
// For offload-static-libs we add an unbundling action for each static
// archive which produces list files with extracted objects. Device lists
// are then added to the appropriate device link actions and host list is
// ignored since we are adding offload-static-libs as normal libraries to
// the host link command.
if (hasOffloadSections(C, LA, Args))
unbundleStaticLib(types::TY_Archive, LA);
// Pass along the static libraries to check if we need to add them for
// unbundling for FPGA AOT static lib usage. Uses FPGA aoco type to
Expand Down Expand Up @@ -6160,14 +6069,11 @@ InputInfo Driver::BuildJobsForActionNoCache(
// unbundling action does not change the type of the output which can
// cause a overwrite.
InputInfo CurI;
bool IsMSVCEnv =
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
bool IsFPGAObjLink = (JA->getType() == types::TY_Object &&
C.getInputArgs().hasArg(options::OPT_fintelfpga) &&
C.getInputArgs().hasArg(options::OPT_fsycl_link_EQ));
if (C.getDriver().getOffloadStaticLibSeen() &&
(JA->getType() == types::TY_Archive ||
(JA->getType() == types::TY_Object && !IsMSVCEnv))) {
JA->getType() == types::TY_Archive) {
// Host part of the unbundled static archive is not used.
if (UI.DependentOffloadKind == Action::OFK_Host)
continue;
Expand Down
16 changes: 4 additions & 12 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7657,19 +7657,11 @@ void OffloadBundler::ConstructJobMultipleOutputs(
InputInfo Input = Inputs.front();
const char *TypeArg = types::getTypeTempSuffix(Input.getType());
const char *InputFileName = Input.getFilename();
bool IsMSVCEnv =
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
types::ID InputType(Input.getType());
bool IsFPGADepUnbundle = JA.getType() == types::TY_FPGA_Dependencies;
bool IsFPGADepLibUnbundle = JA.getType() == types::TY_FPGA_Dependencies_List;
bool IsArchiveUnbundle =
(!IsMSVCEnv && C.getDriver().getOffloadStaticLibSeen() &&
(types::isArchive(InputType) || InputType == types::TY_Object));

if (IsArchiveUnbundle)
TypeArg = "oo";
else if (InputType == types::TY_FPGA_AOCX ||
InputType == types::TY_FPGA_AOCR) {

if (InputType == types::TY_FPGA_AOCX || InputType == types::TY_FPGA_AOCR) {
// Override type with archive object
if (getToolChain().getTriple().getSubArch() ==
llvm::Triple::SPIRSubArch_fpga)
Expand All @@ -7678,7 +7670,7 @@ void OffloadBundler::ConstructJobMultipleOutputs(
TypeArg = "aoo";
}
if (InputType == types::TY_FPGA_AOCO || IsFPGADepLibUnbundle ||
(IsMSVCEnv && types::isArchive(InputType)))
types::isArchive(InputType))
TypeArg = "aoo";
if (IsFPGADepUnbundle)
TypeArg = "o";
Expand Down Expand Up @@ -7714,7 +7706,7 @@ void OffloadBundler::ConstructJobMultipleOutputs(
Triples += Dep.DependentToolChain->getTriple().normalize();
}
continue;
} else if (InputType == types::TY_Archive || IsArchiveUnbundle ||
} else if (InputType == types::TY_Archive ||
(TCArgs.hasArg(options::OPT_fintelfpga) &&
TCArgs.hasArg(options::OPT_fsycl_link_EQ))) {
// Do not extract host part if we are unbundling archive on Windows
Expand Down
7 changes: 4 additions & 3 deletions clang/test/Driver/openmp-offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,13 @@
/// ###########################################################################

/// test behaviors of -foffload-static-lib=<lib>
// RUN: touch %t.a
// RUN: echo "void foo(void) {}" > %t1.cpp
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fopenmp -fopenmp-targets=x86_64-pc-linux-gnu %t1.cpp -c -o %t1_bundle.o
// RUN: llvm-ar cr %t.a %t1_bundle.o
// RUN: touch %t.o
// RUN: %clang -fopenmp -fopenmp-targets=x86_64-pc-linux-gnu -foffload-static-lib=%t.a -### %t.o 2>&1 \
// RUN: | FileCheck %s -check-prefix=FOFFLOAD_STATIC_LIB
// FOFFLOAD_STATIC_LIB: ld{{(.exe)?}}" "-r" "-o" {{.*}} "[[INPUT:.+\.o]]"
// FOFFLOAD_STATIC_LIB: clang-offload-bundler{{.*}} "-type=oo"
// FOFFLOAD_STATIC_LIB: clang-offload-bundler{{.*}} "-type=aoo"
// FOFFLOAD_STATIC_LIB: ld{{.*}} "@{{.*}}"

// TODO: SYCL specific fail - analyze and enable
Expand Down
29 changes: 12 additions & 17 deletions clang/test/Driver/sycl-intelfpga-aoco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@
// CHK-FPGA-AOCO-PHASES: 10: linker, {0, 9}, image, (host-sycl)
// CHK-FPGA-AOCO-PHASES: 11: compiler, {4}, ir, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 12: input, "[[INPUTA]]", archive
// CHK-FPGA-AOCO-PHASES: 13: partial-link, {9, 12}, object
// CHK-FPGA-AOCO-PHASES: 14: clang-offload-unbundler, {13}, object
// CHK-FPGA-AOCO-PHASES: 15: linker, {11, 14}, ir, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 16: sycl-post-link, {15}, ir, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 17: llvm-spirv, {16}, spirv, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 18: input, "[[INPUTA]]", archive
// CHK-FPGA-AOCO-PHASES: 19: clang-offload-unbundler, {18}, fpga_dependencies_list
// CHK-FPGA-AOCO-PHASES: 20: input, "[[INPUTA]]", fpga_aoco
// CHK-FPGA-AOCO-PHASES: 21: clang-offload-unbundler, {20}, fpga_aoco
// CHK-FPGA-AOCO-PHASES: 22: backend-compiler, {17, 19, 21}, fpga_aocx, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 23: clang-offload-wrapper, {22}, object, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 24: offload, "host-sycl (x86_64-unknown-linux-gnu)" {10}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {23}, image
// CHK-FPGA-AOCO-PHASES: 13: clang-offload-unbundler, {12}, archive
// CHK-FPGA-AOCO-PHASES: 14: linker, {11, 13}, ir, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 15: sycl-post-link, {14}, ir, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 16: llvm-spirv, {15}, spirv, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 17: input, "[[INPUTA]]", archive
// CHK-FPGA-AOCO-PHASES: 18: clang-offload-unbundler, {17}, fpga_dependencies_list
// CHK-FPGA-AOCO-PHASES: 19: input, "[[INPUTA]]", fpga_aoco
// CHK-FPGA-AOCO-PHASES: 20: clang-offload-unbundler, {19}, fpga_aoco
// CHK-FPGA-AOCO-PHASES: 21: backend-compiler, {16, 18, 20}, fpga_aocx, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 22: clang-offload-wrapper, {21}, object, (device-sycl)
// CHK-FPGA-AOCO-PHASES: 23: offload, "host-sycl (x86_64-unknown-linux-gnu)" {10}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {22}, image

/// FPGA AOCO Windows phases check
// RUN: %clang_cl -fsycl -fno-sycl-device-lib=all -fintelfpga -foffload-static-lib=%t_aoco_cl.a %s -### -ccc-print-phases 2>&1 \
Expand Down Expand Up @@ -78,11 +77,7 @@
// RUN: | FileCheck -check-prefixes=CHK-FPGA-AOCO,CHK-FPGA-AOCO-WIN %s
// RUN: %clang_cl -fsycl -fno-sycl-device-lib=all -fintelfpga %t_aoco_cl.a -### %s 2>&1 \
// RUN: | FileCheck -check-prefixes=CHK-FPGA-AOCO,CHK-FPGA-AOCO-WIN %s
// CHK-FPGA-AOCO-LIN: clang-offload-bundler{{.*}} "-type=ao" "-targets=sycl-fpga_aoco-intel-unknown-sycldevice" "-inputs=[[INPUTLIB:.+\.a]]" "-check-section"
// CHK-FPGA-AOCO-LIN: clang{{.*}} "-emit-obj" {{.*}} "-o" "[[HOSTOBJ:.+\.o]]"
// CHK-FPGA-AOCO-LIN: ld{{.*}} "-r" "-o" "[[PARTLINKOBJ:.+\.o]]" "{{.*}}crt1.o" "{{.*}}crti.o" {{.*}} "[[HOSTOBJ]]" "[[INPUTLIB]]" "{{.*}}crtn.o"
// CHK-FPGA-AOCO-LIN: clang-offload-bundler{{.*}} "-type=oo" "-targets=sycl-spir64_fpga-unknown-unknown-sycldevice" "-inputs=[[PARTLINKOBJ]]" "-outputs={{.*}}" "-unbundle"
// CHK-FPGA-AOCO-WIN: clang-offload-bundler{{.*}} "-type=aoo" "-targets=sycl-spir64_fpga-unknown-unknown-sycldevice" "-inputs=[[INPUTLIB:.+\.a]]" "-outputs={{.*}}" "-unbundle"
// CHK-FPGA-AOCO: clang-offload-bundler{{.*}} "-type=aoo" "-targets=sycl-spir64_fpga-unknown-unknown-sycldevice" "-inputs=[[INPUTLIB:.+\.a]]" "-outputs={{.*}}" "-unbundle"
// CHK-FPGA-AOCO: llvm-link{{.*}} "@{{.*}}" "-o" "[[LINKEDBC:.+\.bc]]"
// CHK-FPGA-AOCO: sycl-post-link{{.*}} "-ir-output-only" "-spec-const=default" "-o" "[[PLINKEDBC:.+\.bc]]" "[[LINKEDBC]]"
// CHK-FPGA-AOCO: llvm-spirv{{.*}} "-o" "[[TARGSPV:.+\.spv]]" {{.*}} "[[PLINKEDBC]]"
Expand Down
19 changes: 9 additions & 10 deletions clang/test/Driver/sycl-intelfpga-static-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
// CHECK_PHASES: 0: input, "[[INPUT:.+\.a]]", object, (host-sycl)
// CHECK_PHASES: 1: linker, {0}, image, (host-sycl)
// CHECK_PHASES: 2: input, "[[INPUT]]", archive
// CHECK_PHASES: 3: partial-link, {2}, object
// CHECK_PHASES: 4: clang-offload-unbundler, {3}, object
// CHECK_PHASES: 5: linker, {4}, ir, (device-sycl)
// CHECK_PHASES: 6: sycl-post-link, {5}, ir, (device-sycl)
// CHECK_PHASES: 7: llvm-spirv, {6}, spirv, (device-sycl)
// CHECK_PHASES: 8: input, "[[INPUT]]", archive
// CHECK_PHASES: 9: clang-offload-unbundler, {8}, fpga_dependencies_list
// CHECK_PHASES: 10: backend-compiler, {7, 9}, fpga_aocx, (device-sycl)
// CHECK_PHASES: 11: clang-offload-wrapper, {10}, object, (device-sycl)
// CHECK_PHASES: 12: offload, "host-sycl (x86_64-unknown-linux-gnu)" {1}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {11}, image
// CHECK_PHASES: 3: clang-offload-unbundler, {2}, archive
// CHECK_PHASES: 4: linker, {3}, ir, (device-sycl)
// CHECK_PHASES: 5: sycl-post-link, {4}, ir, (device-sycl)
// CHECK_PHASES: 6: llvm-spirv, {5}, spirv, (device-sycl)
// CHECK_PHASES: 7: input, "[[INPUT]]", archive
// CHECK_PHASES: 8: clang-offload-unbundler, {7}, fpga_dependencies_list
// CHECK_PHASES: 9: backend-compiler, {6, 8}, fpga_aocx, (device-sycl)
// CHECK_PHASES: 10: clang-offload-wrapper, {9}, object, (device-sycl)
// CHECK_PHASES: 11: offload, "host-sycl (x86_64-unknown-linux-gnu)" {1}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {10}, image

/// Check for unbundle and use of deps in static lib
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -fintelfpga %t.a -### 2>&1 \
Expand Down
Loading