Skip to content

Commit a50a566

Browse files
authored
[Driver][SYCL][FPGA] Do not optimize after wrapping an object (#2695)
When performing -fsycl-link=early and generating an archive for FPGA, the host object ended up being truncated in the archive. After wrapping the host object (which generates a .bc file), that wrapped object goes through an additional compilation step. When enabling optimizations (-O2, etc), the wrapped host object would be optimized away, causing problems when going from the early device to either exe or image. Do not pass the optimization option during this specific step.
1 parent 4f6ae91 commit a50a566

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,6 +4013,17 @@ static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
40134013
RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
40144014
}
40154015

4016+
/// Check whether the given input tree contains any wrapper actions
4017+
static bool ContainsWrapperAction(const Action *A) {
4018+
if (isa<OffloadWrapperJobAction>(A))
4019+
return true;
4020+
for (const auto &AI : A->inputs())
4021+
if (ContainsWrapperAction(AI))
4022+
return true;
4023+
4024+
return false;
4025+
}
4026+
40164027
void Clang::ConstructJob(Compilation &C, const JobAction &JA,
40174028
const InputInfo &Output, const InputInfoList &Inputs,
40184029
const ArgList &Args, const char *LinkingOutput) const {
@@ -5177,13 +5188,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51775188
// preprocessed inputs and configure concludes that -fPIC is not supported.
51785189
Args.ClaimAllArgs(options::OPT_D);
51795190

5191+
bool SkipO =
5192+
Args.hasArg(options::OPT_fsycl_link_EQ) && ContainsWrapperAction(&JA);
5193+
const Arg *OArg = Args.getLastArg(options::OPT_O_Group);
51805194
// Manually translate -O4 to -O3; let clang reject others.
5181-
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
5182-
if (A->getOption().matches(options::OPT_O4)) {
5195+
// When compiling a wrapped binary, do not optimize.
5196+
if (!SkipO && OArg) {
5197+
if (OArg->getOption().matches(options::OPT_O4)) {
51835198
CmdArgs.push_back("-O3");
51845199
D.Diag(diag::warn_O4_is_O3);
51855200
} else {
5186-
A->render(Args, CmdArgs);
5201+
OArg->render(Args, CmdArgs);
51875202
}
51885203
}
51895204

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// RUN: touch %t.o
2424
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -fintelfpga -fsycl-link %t.o -o libfoo.a 2>&1 \
2525
// RUN: | FileCheck -check-prefixes=CHK-FPGA-LINK,CHK-FPGA-EARLY %s
26-
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -fintelfpga -fsycl-link=early %t.o -o libfoo.a 2>&1 \
26+
// RUN: %clangxx -### -O2 -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -fintelfpga -fsycl-link=early %t.o -o libfoo.a 2>&1 \
2727
// RUN: | FileCheck -check-prefixes=CHK-FPGA-LINK,CHK-FPGA-EARLY %s
2828
// RUN: %clangxx -### -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -fintelfpga -fsycl-link=image %t.o -o libfoo.a 2>&1 \
2929
// RUN: | FileCheck -check-prefixes=CHK-FPGA-LINK,CHK-FPGA-IMAGE %s
@@ -38,7 +38,8 @@
3838
// CHK-FPGA-LINK: clang-offload-wrapper{{.*}} "-o=[[WRAPOUT:.+\.bc]]" "-host=x86_64-unknown-linux-gnu" {{.*}} "-kind=sycl"
3939
// CHK-FPGA-LINK: llc{{.*}} "-o" "[[OBJOUTDEV:.+\.o]]" "[[WRAPOUT]]"
4040
// CHK-FPGA-EARLY: clang-offload-wrapper{{.*}} "-host" "x86_64-unknown-linux-gnu" "-o" "[[WRAPOUTHOST:.+\.bc]]" "-kind=host"
41-
// CHK-FPGA-EARLY: clang{{.*}} "-o" "[[OBJOUT:.+\.o]]" {{.*}} "[[WRAPOUTHOST]]"
41+
// CHK-FPGA-EARLY-NOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-O2"
42+
// CHK-FPGA-EARLY: "-o" "[[OBJOUT:.+\.o]]" {{.*}} "[[WRAPOUTHOST]]"
4243
// CHK-FPGA-EARLY: llvm-ar{{.*}} "cr" "libfoo.a" "[[OBJOUT]]" "[[OBJOUTDEV]]"
4344
// CHK-FPGA-IMAGE: llvm-ar{{.*}} "cr" "libfoo.a" "[[INPUT]]" "[[OBJOUTDEV]]"
4445

0 commit comments

Comments
 (0)