Skip to content

Commit 9a8613a

Browse files
[SYCL] Set -O0 as the default SYCL device optimization if -g is passed (#16408)
Set `O0` as the default SYCL device optimization if `-g` is passed and optimization level is not provided explicitly. Use of `'-g'` without any optimization-level option will turn off compiler optimizations similar to use of `'-O0'`.
1 parent fe98db3 commit 9a8613a

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5665,9 +5665,27 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56655665
CmdArgs.push_back("-Wno-sycl-strict");
56665666
}
56675667

5668-
// Set O2 optimization level by default
5669-
if (!Args.getLastArg(options::OPT_O_Group))
5670-
CmdArgs.push_back("-O2");
5668+
// If no optimization controlling flags (-O) are provided, check if
5669+
// any debug information flags(-g) are passed.
5670+
// "-fintelfpga" implies "-g" and we preserve the default optimization for
5671+
// this flow(-O2).
5672+
// if "-g" is explicitly passed from the command-line, set default
5673+
// optimization to -O0.
5674+
5675+
if (!Args.hasArgNoClaim(options::OPT_O_Group, options::OPT__SLASH_O)) {
5676+
StringRef OptLevel = "-O2";
5677+
const Arg *DebugInfoGroup = Args.getLastArg(options::OPT_g_Group);
5678+
// -fintelfpga -g case
5679+
if ((Args.hasArg(options::OPT_fintelfpga) &&
5680+
Args.hasMultipleArgs(options::OPT_g_Group)) ||
5681+
/* -fsycl -g case */ (!Args.hasArg(options::OPT_fintelfpga) &&
5682+
DebugInfoGroup)) {
5683+
if (!DebugInfoGroup->getOption().matches(options::OPT_g0)) {
5684+
OptLevel = "-O0";
5685+
}
5686+
}
5687+
CmdArgs.push_back(OptLevel.data());
5688+
}
56715689

56725690
// Add the integration header option to generate the header.
56735691
StringRef Header(D.getIntegrationHeader(Input.getBaseInput()));
@@ -10931,7 +10949,25 @@ static std::string getSYCLPostLinkOptimizationLevel(const ArgList &Args) {
1093110949
[=](char c) { return c == S[0]; }))
1093210950
return std::string("-O") + S[0];
1093310951
}
10934-
10952+
// If no optimization controlling flags (-O) are provided, check if
10953+
// any debug information flags(-g) are passed.
10954+
// "-fintelfpga" implies "-g" and we preserve the default optimization for
10955+
// this flow(-O2).
10956+
// if "-g" is explicitly passed from the command-line, set default
10957+
// optimization to -O0.
10958+
10959+
if (!Args.hasArg(options::OPT_O_Group)) {
10960+
const Arg *DebugInfoGroup = Args.getLastArg(options::OPT_g_Group);
10961+
// -fintelfpga -g case
10962+
if ((Args.hasArg(options::OPT_fintelfpga) &&
10963+
Args.hasMultipleArgs(options::OPT_g_Group)) ||
10964+
/* -fsycl -g case */
10965+
(!Args.hasArg(options::OPT_fintelfpga) && DebugInfoGroup)) {
10966+
if (!DebugInfoGroup->getOption().matches(options::OPT_g0)) {
10967+
return "-O0";
10968+
}
10969+
}
10970+
}
1093510971
// The default for SYCL device code optimization
1093610972
return "-O2";
1093710973
}

clang/test/Driver/sycl-device-optimizations.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,59 @@
5353
// RUN: %clang -### -fsycl --offload-new-driver %s 2>&1 \
5454
// RUN: | FileCheck -check-prefix=CHECK-NO-THRESH %s
5555
// CHECK-NO-THRESH-NOT: "-mllvm" "-inline-threshold
56+
57+
/// Check that optimizations for sycl device are disabled with -g passed:
58+
// RUN: %clang -### -fsycl -g %s 2>&1 \
59+
// RUN: | FileCheck -check-prefix=CHECK-DEBUG %s
60+
// RUN: %clang_cl -### -fsycl -g %s 2>&1 \
61+
// RUN: | FileCheck -check-prefix=CHECK-DEBUG %s
62+
// CHECK-DEBUG: clang{{.*}} "-fsycl-is-device{{.*}}" "-O0"
63+
// CHECK-DEBUG: sycl-post-link{{.*}} "-O0"
64+
// CHECK-DEBUG-NOT: "-O2"
65+
66+
/// Check that optimizations for sycl device are enabled with -g and O2 passed:
67+
// RUN: %clang -### -fsycl -O2 -g %s 2>&1 \
68+
// RUN: | FileCheck -check-prefix=CHECK-G-O2 %s
69+
// For clang_cl, -O2 maps to -O3
70+
// RUN: %clang_cl -### -fsycl -O2 -g %s 2>&1 \
71+
// RUN: | FileCheck -check-prefix=CHECK-G-O3 %s
72+
// CHECK-G-O2: clang{{.*}} "-fsycl-is-device{{.*}}" "-O2"
73+
// CHECK-G-O2: sycl-post-link{{.*}} "-O2"
74+
// CHECK-G-O2-NOT: "-O0"
75+
// CHECK-G-O3: clang{{.*}} "-fsycl-is-device{{.*}}" "-O3"
76+
// CHECK-G-O3: sycl-post-link{{.*}} "-O3"
77+
// CHECK-G-O3-NOT: "-O0"
78+
79+
/// Check that -O2 is passed for FPGA
80+
// RUN: %clang -### -fintelfpga -fsycl-early-optimizations %s 2>&1 \
81+
// RUN: | FileCheck -check-prefix=CHECK-FPGA %s
82+
// RUN: %clang_cl -### -fintelfpga -fsycl-early-optimizations %s 2>&1 \
83+
// RUN: | FileCheck -check-prefix=CHECK-FPGA %s
84+
// CHECK-FPGA: clang{{.*}} "-fsycl-is-device{{.*}}" "-O2"
85+
// CHECK-FPGA: sycl-post-link{{.*}} "-O2"
86+
// CHECK-FPGA-NOT: "-O0"
87+
88+
/// Check that -O2 preserves for FPGA when it's explicitly passed
89+
// RUN: %clang -### -O2 -fintelfpga -fsycl-early-optimizations %s 2>&1 \
90+
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O2 %s
91+
// For clang_cl, -O2 maps to -O3
92+
// RUN: %clang_cl -### -O2 -fintelfpga -fsycl-early-optimizations %s 2>&1 \
93+
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O3 %s
94+
// CHECK-FPGA-O2: clang{{.*}} "-fsycl-is-device{{.*}}" "-O2"
95+
// CHECK-FPGA-O2: sycl-post-link{{.*}} "-O2"
96+
// CHECK-FPGA-O2-NOT: "-O0"
97+
// CHECK-FPGA-O3: clang{{.*}} "-fsycl-is-device{{.*}}" "-O3"
98+
// CHECK-FPGA-O3: sycl-post-link{{.*}} "-O3"
99+
// CHECK-FPGA-O3-NOT: "-O0"
100+
101+
/// Check that -O0 is passed for FPGA when -g is explicitly passed
102+
// RUN: %clang -### -fintelfpga -g %s 2>&1 \
103+
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O0 %s
104+
// RUN: %clang_cl -### -fintelfpga -g %s 2>&1 \
105+
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O0 %s
106+
// CHECK-FPGA-O0: clang{{.*}} "-fsycl-is-device{{.*}}" "-O0"
107+
// CHECK-FPGA-O0: sycl-post-link{{.*}} "-O0"
108+
// CHECK-FPGA-O0-NOT: "-O2"
109+
110+
111+

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@
228228
// RUN: | FileCheck -check-prefix=CHK-TOOLS-IMPLIED-OPTS %s
229229
// RUN: %clang_cl -### -fintelfpga -Zi -Od -Xs "-DFOO1 -DFOO2" -Xshardware %s 2>&1 \
230230
// RUN: | FileCheck -check-prefix=CHK-TOOLS-IMPLIED-OPTS %s
231-
// CHK-TOOLS-IMPLIED-OPTS-NOT: clang{{.*}} "-fsycl-is-device"{{.*}} "-O0"
232-
// CHK-TOOLS-IMPLIED-OPTS: sycl-post-link{{.*}} "-O2"
231+
// CHK-TOOLS-IMPLIED-OPTS: clang{{.*}} "-fsycl-is-device"{{.*}} "-fno-sycl-early-optimizations"{{.*}} "-O0"
232+
// CHK-TOOLS-IMPLIED-OPTS: sycl-post-link{{.*}} "-O0"
233233
// CHK-TOOLS-IMPLIED-OPTS: aoc{{.*}} "-g" "-DFOO1" "-DFOO2"
234234

235235
/// shared objects should not be checked for FPGA contents

sycl/test-e2e/InvokeSimd/Regression/ImplicitSubgroup/debug_symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Check that full compilation works:
2-
// RUN: %clangxx -DIMPL_SUBGROUP -fsycl -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g %S/../debug_symbols.cpp -o %t.out
2+
// RUN: %clangxx -DIMPL_SUBGROUP -fsycl -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g -O2 %S/../debug_symbols.cpp -o %t.out
33
// RUN: env IGC_VCSaveStackCallLinkage=1 IGC_VCDirectCallsOnly=1 %{run} %t.out
44
//
55
// VISALTO enable run

sycl/test-e2e/InvokeSimd/Regression/debug_symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Check that full compilation works:
2-
// RUN: %{build} -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g -o %t.out
2+
// RUN: %{build} -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g -O2 -o %t.out
33
// RUN: env IGC_VCSaveStackCallLinkage=1 IGC_VCDirectCallsOnly=1 %{run} %t.out
44
//
55
// VISALTO enable run

0 commit comments

Comments
 (0)