Skip to content

Commit c9a65fc

Browse files
authored
[Driver][SYCL][FPGA] Improve FPGA AOT when using Triple (#3330)
Behaviors for enabling AOT for FPGA is allowed when using both -fintelfpga and -fsycl-targets=spir64_fpga-unknown-unknown-sycldevice. Improve the usage when using the -fsycl-targets method as we were a bit too reliant on -fintelfpga for some checks, causing some of the tool chain construction to fail. Use of -fintelfpga should effectively only enable specific settings such as debug and dependency generation.
1 parent ce1b7aa commit c9a65fc

File tree

6 files changed

+66
-46
lines changed

6 files changed

+66
-46
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,10 +2810,8 @@ bool Driver::checkForOffloadStaticLib(Compilation &C,
28102810
if (isStaticArchiveFile(OLArg) && hasOffloadSections(C, OLArg, Args)) {
28112811
// FPGA binaries with AOCX or AOCR sections are not considered fat
28122812
// static archives.
2813-
if (Args.hasArg(options::OPT_fintelfpga))
2814-
return !(hasFPGABinary(C, OLArg.str(), types::TY_FPGA_AOCR) ||
2815-
hasFPGABinary(C, OLArg.str(), types::TY_FPGA_AOCX));
2816-
return true;
2813+
return !(hasFPGABinary(C, OLArg.str(), types::TY_FPGA_AOCR) ||
2814+
hasFPGABinary(C, OLArg.str(), types::TY_FPGA_AOCX));
28172815
}
28182816
return false;
28192817
}
@@ -3877,11 +3875,12 @@ class OffloadingActionBuilder final {
38773875
ActionList FullSYCLLinkBinaryList;
38783876
bool SYCLDeviceLibLinked = false;
38793877
FullSYCLLinkBinaryList.push_back(DeviceLinkAction);
3880-
// If used without -fintelfpga, -fsycl-link is used to wrap device
3881-
// objects for future host link. Device libraries should be linked
3882-
// by default to resolve any undefined reference.
3883-
if (!Args.hasArg(options::OPT_fintelfpga)) {
3884-
const auto *TC = ToolChains.front();
3878+
// If used without the FPGA target, -fsycl-link is used to wrap
3879+
// device objects for future host link. Device libraries should
3880+
// be linked by default to resolve any undefined reference.
3881+
const auto *TC = ToolChains.front();
3882+
if (TC->getTriple().getSubArch() !=
3883+
llvm::Triple::SPIRSubArch_fpga) {
38853884
SYCLDeviceLibLinked =
38863885
addSYCLDeviceLibs(TC, FullSYCLLinkBinaryList, true,
38873886
C.getDefaultToolChain()
@@ -3984,8 +3983,9 @@ class OffloadingActionBuilder final {
39843983
return ABRT_Inactive;
39853984
// For SYCL device libraries, don't need to add them to
39863985
// FPGAObjectInputs as there is no FPGA dep files inside.
3987-
3988-
if (Args.hasArg(options::OPT_fintelfpga) &&
3986+
const auto *TC = ToolChains.front();
3987+
if (TC->getTriple().getSubArch() ==
3988+
llvm::Triple::SPIRSubArch_fpga &&
39893989
!IsSYCLDeviceLibObj(FileName, C.getDefaultToolChain()
39903990
.getTriple()
39913991
.isWindowsMSVCEnvironment()))
@@ -4332,7 +4332,7 @@ class OffloadingActionBuilder final {
43324332
BEInputs.push_back(UnbundleAction);
43334333
};
43344334
// Send any known objects/archives through the unbundler to grab the
4335-
// dependency file associated.
4335+
// dependency file associated. This is only done for -fintelfpga.
43364336
for (Action *A : FPGAObjectInputs)
43374337
unbundleAdd(A, types::TY_FPGA_Dependencies);
43384338
for (Action *A : FPGAArchiveInputs)
@@ -4460,11 +4460,6 @@ class OffloadingActionBuilder final {
44604460
// -fsycl-device-code-split=per_source
44614461
DeviceCodeSplit = DeviceCodeSplitArg &&
44624462
DeviceCodeSplitArg->getValue() != StringRef("off");
4463-
// Device only compilation for -fsycl-link (no FPGA) and
4464-
// -fsycl-link-targets
4465-
CompileDeviceOnly =
4466-
(SYCLLinkTargets ||
4467-
(WrapDeviceOnlyBinary && !Args.hasArg(options::OPT_fintelfpga)));
44684463
// Gather information about the SYCL Ahead of Time targets. The targets
44694464
// are determined on the SubArch values passed along in the triple.
44704465
Arg *SYCLTargets =
@@ -4519,14 +4514,19 @@ class OffloadingActionBuilder final {
45194514
SYCLfpgaTriple = true;
45204515
}
45214516

4517+
// Device only compilation for -fsycl-link (no FPGA) and
4518+
// -fsycl-link-targets
4519+
CompileDeviceOnly =
4520+
(SYCLLinkTargets || (WrapDeviceOnlyBinary && !SYCLfpgaTriple));
4521+
45224522
// Set the FPGA output type based on command line (-fsycl-link).
45234523
if (auto * A = C.getInputArgs().getLastArg(options::OPT_fsycl_link_EQ))
45244524
FPGAOutType = (A->getValue() == StringRef("early"))
45254525
? types::TY_FPGA_AOCR : types::TY_FPGA_AOCX;
45264526

45274527
// Populate FPGA static archives that could contain dep files to be
45284528
// incorporated into the aoc compilation
4529-
if (SYCLfpgaTriple) {
4529+
if (SYCLfpgaTriple && Args.hasArg(options::OPT_fintelfpga)) {
45304530
SmallVector<const char *, 16> LinkArgs(getLinkerArgs(C, Args));
45314531
for (StringRef LA : LinkArgs) {
45324532
if (isStaticArchiveFile(LA) && hasOffloadSections(C, LA, Args)) {
@@ -4685,6 +4685,7 @@ class OffloadingActionBuilder final {
46854685
// is a bundle or not and if the input is not a bundle it assumes it is a
46864686
// host file. Therefore it is safe to create an unbundling action even if
46874687
// the input is not a bundle.
4688+
bool HasFPGATarget = false;
46884689
if (CanUseBundler && isa<InputAction>(HostAction) &&
46894690
InputArg->getOption().getKind() == llvm::opt::Option::InputClass &&
46904691
!InputArg->getOption().hasFlag(options::LinkerInput) &&
@@ -4694,9 +4695,12 @@ class OffloadingActionBuilder final {
46944695
ActionList HostActionList;
46954696
Action *A(HostAction);
46964697
// Only check for FPGA device information when using fpga SubArch.
4697-
if (Args.hasArg(options::OPT_fintelfpga) &&
4698-
!(HostAction->getType() == types::TY_Object &&
4699-
isObjectFile(InputName))) {
4698+
auto SYCLTCRange = C.getOffloadToolChains<Action::OFK_SYCL>();
4699+
for (auto TI = SYCLTCRange.first, TE = SYCLTCRange.second; TI != TE; ++TI)
4700+
HasFPGATarget |= TI->second->getTriple().getSubArch() ==
4701+
llvm::Triple::SPIRSubArch_fpga;
4702+
if (HasFPGATarget && !(HostAction->getType() == types::TY_Object &&
4703+
isObjectFile(InputName))) {
47004704
// Type FPGA aoco is a special case for -foffload-static-lib.
47014705
if (HostAction->getType() == types::TY_FPGA_AOCO) {
47024706
if (!hasFPGABinary(C, InputName, types::TY_FPGA_AOCO))
@@ -4741,10 +4745,9 @@ class OffloadingActionBuilder final {
47414745
// For unbundling of an FPGA AOCX binary, we want to link with the original
47424746
// FPGA device archive.
47434747
if ((OffloadKind == Action::OFK_None && CanUseBundler) ||
4744-
(Args.hasArg(options::OPT_fintelfpga) &&
4745-
((Args.hasArg(options::OPT_fsycl_link_EQ) &&
4746-
HostAction->getType() == types::TY_Object) ||
4747-
HostAction->getType() == types::TY_FPGA_AOCX)))
4748+
(HasFPGATarget && ((Args.hasArg(options::OPT_fsycl_link_EQ) &&
4749+
HostAction->getType() == types::TY_Object) ||
4750+
HostAction->getType() == types::TY_FPGA_AOCX)))
47484751
if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction))
47494752
HostAction = UA->getInputs().back();
47504753

@@ -5187,13 +5190,13 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
51875190
// are then added to the appropriate device link actions and host list is
51885191
// ignored since we are adding offload-static-libs as normal libraries to
51895192
// the host link command.
5190-
if (hasOffloadSections(C, LA, Args))
5193+
if (hasOffloadSections(C, LA, Args)) {
51915194
unbundleStaticLib(types::TY_Archive, LA);
5192-
// Pass along the static libraries to check if we need to add them for
5193-
// unbundling for FPGA AOT static lib usage. Uses FPGA aoco type to
5194-
// differentiate if aoco unbundling is needed.
5195-
if (Args.hasArg(options::OPT_fintelfpga))
5195+
// Pass along the static libraries to check if we need to add them for
5196+
// unbundling for FPGA AOT static lib usage. Uses FPGA aoco type to
5197+
// differentiate if aoco unbundling is needed.
51965198
unbundleStaticLib(types::TY_FPGA_AOCO, LA);
5199+
}
51975200
}
51985201

51995202
// For an FPGA archive, we add the unbundling step above to take care of
@@ -6152,16 +6155,17 @@ InputInfo Driver::BuildJobsForActionNoCache(
61526155
// unbundling action does not change the type of the output which can
61536156
// cause a overwrite.
61546157
InputInfo CurI;
6155-
bool IsFPGAObjLink = (JA->getType() == types::TY_Object &&
6156-
C.getInputArgs().hasArg(options::OPT_fintelfpga) &&
6157-
C.getInputArgs().hasArg(options::OPT_fsycl_link_EQ));
6158+
bool IsFPGAObjLink =
6159+
(JA->getType() == types::TY_Object &&
6160+
EffectiveTriple.getSubArch() == llvm::Triple::SPIRSubArch_fpga &&
6161+
C.getInputArgs().hasArg(options::OPT_fsycl_link_EQ));
61586162
if (C.getDriver().getOffloadStaticLibSeen() &&
61596163
JA->getType() == types::TY_Archive) {
61606164
// Host part of the unbundled static archive is not used.
61616165
if (UI.DependentOffloadKind == Action::OFK_Host)
61626166
continue;
6163-
// Host part of the unbundled object when -fintelfpga -fsycl-link is
6164-
// enabled is not used
6167+
// Host part of the unbundled object is not used when using the
6168+
// FPGA target and -fsycl-link is enabled.
61656169
if (UI.DependentOffloadKind == Action::OFK_Host && IsFPGAObjLink)
61666170
continue;
61676171
std::string TmpFileName = C.getDriver().GetTemporaryPath(
@@ -6203,8 +6207,8 @@ InputInfo Driver::BuildJobsForActionNoCache(
62036207
C.addTempFile(C.getArgs().MakeArgString(TmpFileName));
62046208
CurI = InputInfo(TI, TmpFile, TmpFile);
62056209
} else {
6206-
// Host part of the unbundled object is not used when -fintelfpga
6207-
// -fsycl-link is enabled
6210+
// Host part of the unbundled object is not used when -fsycl-link is
6211+
// enabled with FPGA target
62086212
if (UI.DependentOffloadKind == Action::OFK_Host && IsFPGAObjLink)
62096213
continue;
62106214
std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11051105
const InputInfo &Output,
11061106
const InputInfoList &Inputs) const {
11071107
const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
1108+
const bool IsIntelFPGA = Args.hasArg(options::OPT_fintelfpga);
11081109

11091110
CheckPreprocessingOptions(D, Args);
11101111

@@ -1143,16 +1144,14 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11431144
C.addFailureResultFile(DepFile, &JA);
11441145
// Populate the named dependency file to be used in the bundle
11451146
// or passed to the offline compilation.
1146-
if (Args.hasArg(options::OPT_fintelfpga) &&
1147-
JA.isDeviceOffloading(Action::OFK_SYCL))
1147+
if (IsIntelFPGA && JA.isDeviceOffloading(Action::OFK_SYCL))
11481148
C.getDriver().addFPGATempDepFile(
11491149
DepFile, Clang::getBaseInputName(Args, Inputs[0]));
11501150
} else if (Output.getType() == types::TY_Dependencies) {
11511151
DepFile = Output.getFilename();
11521152
} else if (!ArgMD) {
11531153
DepFile = "-";
1154-
} else if (Args.hasArg(options::OPT_fintelfpga) &&
1155-
JA.isDeviceOffloading(Action::OFK_SYCL)) {
1154+
} else if (IsIntelFPGA && JA.isDeviceOffloading(Action::OFK_SYCL)) {
11561155
createFPGATempDepFile(DepFile);
11571156
} else {
11581157
DepFile = getDependencyFileName(Args, Inputs);
@@ -1208,8 +1207,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
12081207
CmdArgs.push_back("-module-file-deps");
12091208
}
12101209

1211-
if (!ArgM && Args.hasArg(options::OPT_fintelfpga) &&
1212-
JA.isDeviceOffloading(Action::OFK_SYCL)) {
1210+
if (!ArgM && IsIntelFPGA && JA.isDeviceOffloading(Action::OFK_SYCL)) {
12131211
// No dep generation option was provided, add all of the needed options
12141212
// to ensure a successful dep generation.
12151213
const char *DepFile;
@@ -7686,9 +7684,12 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
76867684
Triples += CurDep->getOffloadingArch();
76877685
}
76887686
}
7689-
bool IsFPGADepBundle = (TCArgs.hasArg(options::OPT_fintelfpga) &&
7690-
Output.getType() == types::TY_Object);
7691-
// For -fintelfpga, when bundling objects we also want to bundle up the
7687+
// If we see we are bundling for FPGA using -fintelfpga, add the
7688+
// dependency bundle
7689+
bool IsFPGADepBundle = TCArgs.hasArg(options::OPT_fintelfpga) &&
7690+
Output.getType() == types::TY_Object;
7691+
7692+
// For spir64_fpga target, when bundling objects we also want to bundle up the
76927693
// named dependency file.
76937694
// TODO - We are currently using the target triple inputs to slot a location
76947695
// of the dependency information into the bundle. It would be good to

clang/test/Driver/sycl-intelfpga-static-lib-win.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@
3333
// RUN: | FileCheck -check-prefix=CHECK_UNBUNDLE %s
3434
// CHECK_UNBUNDLE: clang-offload-bundler" "-type=aoo" "-targets=sycl-fpga_dep" "-inputs={{.*}}" "-outputs=[[DEPFILES:.+\.txt]]" "-unbundle"
3535
// CHECK_UNBUNDLE: aoc{{.*}} "-dep-files=@[[DEPFILES]]"
36+
37+
/// Check for no unbundle and use of deps in static lib when using triple
38+
// RUN: %clang_cl --target=x86_64-pc-windows-msvc -fsycl -fno-sycl-device-lib=all -fsycl-targets=spir64_fpga-unknown-unknown-sycldevice %t.lib -### 2>&1 \
39+
// RUN: | FileCheck -check-prefix=CHECK_NO_UNBUNDLE %s
40+
// CHECK_NO_UNBUNDLE-NOT: clang-offload-bundler" "-type=aoo" "-targets=sycl-fpga_dep"
41+
// CHECK_NO_UNBUNDLE-NOT: aoc{{.*}} "-dep-files={{.*}}"

clang/test/Driver/sycl-intelfpga-static-lib.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@
3232
// RUN: | FileCheck -check-prefix=CHECK_UNBUNDLE %s
3333
// CHECK_UNBUNDLE: clang-offload-bundler" "-type=aoo" "-targets=sycl-fpga_dep" "-inputs={{.*}}" "-outputs=[[DEPFILES:.+\.txt]]" "-unbundle"
3434
// CHECK_UNBUNDLE: aoc{{.*}} "-dep-files=@[[DEPFILES]]"
35+
36+
/// Check for no unbundle and use of deps in static lib when using triple
37+
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -fsycl-targets=spir64_fpga-unknown-unknown-sycldevice %t.a -### 2>&1 \
38+
// RUN: | FileCheck -check-prefix=CHECK_NO_UNBUNDLE %s
39+
// CHECK_NO_UNBUNDLE-NOT: clang-offload-bundler" "-type=aoo" "-targets=sycl-fpga_dep"
40+
// CHECK_NO_UNBUNDLE-NOT: aoc{{.*}} "-dep-files={{.*}}"

clang/test/Driver/sycl-offload-intelfpga.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/// Check SYCL headers path
77
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu -fsycl -fintelfpga %s 2>&1 \
8+
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu -fsycl -fsycl-targets=spir64_fpga-unknown-unknown-sycldevice %s 2>&1 \
89
// RUN: | FileCheck -check-prefix=CHK-HEADERS-INTELFPGA %s
910
// CHK-HEADERS-INTELFPGA: clang{{.*}} "-internal-isystem" "{{.*}}bin{{[/\\]+}}..{{[/\\]+}}include{{[/\\]+}}sycl"
1011

clang/test/Driver/sycl-offload-with-split.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@
313313
// RUN: %clang -### -fsycl -fno-sycl-device-code-split-esimd %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-SPLIT
314314
// RUN: %clang_cl -### -fsycl -fno-sycl-device-code-split-esimd %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-SPLIT
315315
// RUN: %clang -### -fsycl -fintelfpga %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-SPLIT
316+
// RUN: %clang -### -fsycl -fsycl-targets=spir64_fpga-unknown-unknown-sycldevice %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-SPLIT
316317
// RUN: %clang_cl -### -fsycl -fintelfpga %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-SPLIT
317318
// CHK-ESIMD-SPLIT: sycl-post-link{{.*}} "-split-esimd"
318319
// CHK-NO-ESIMD-SPLIT-NOT: sycl-post-link{{.*}} "-split-esimd"
@@ -325,6 +326,7 @@
325326
// RUN: %clang -### -fsycl -fno-sycl-device-code-lower-esimd %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-LOWER
326327
// RUN: %clang_cl -### -fsycl -fno-sycl-device-code-lower-esimd %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-LOWER
327328
// RUN: %clang -### -fsycl -fintelfpga %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-LOWER
329+
// RUN: %clang -### -fsycl -fsycl-targets=spir64_fpga-unknown-unknown-sycldevice %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-LOWER
328330
// RUN: %clang_cl -### -fsycl -fintelfpga %s 2>&1 | FileCheck %s -check-prefixes=CHK-NO-ESIMD-LOWER
329331
// CHK-ESIMD-LOWER: sycl-post-link{{.*}} "-lower-esimd"
330332
// CHK-NO-ESIMD-LOWER-NOT: sycl-post-link{{.*}} "-lower-esimd"

0 commit comments

Comments
 (0)