Skip to content

[SYCL] Set -O0 as the default SYCL device optimization if -g is passed #16408

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 5 commits into from
Dec 19, 2024
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
44 changes: 40 additions & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5606,9 +5606,27 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-Wno-sycl-strict");
}

// Set O2 optimization level by default
if (!Args.getLastArg(options::OPT_O_Group))
CmdArgs.push_back("-O2");
// If no optimization controlling flags (-O) are provided, check if
// any debug information flags(-g) are passed.
// "-fintelfpga" implies "-g" and we preserve the default optimization for
// this flow(-O2).
// if "-g" is explicitly passed from the command-line, set default
// optimization to -O0.

if (!Args.hasArgNoClaim(options::OPT_O_Group, options::OPT__SLASH_O)) {
StringRef OptLevel = "-O2";
const Arg *DebugInfoGroup = Args.getLastArg(options::OPT_g_Group);
// -fintelfpga -g case
if ((Args.hasArg(options::OPT_fintelfpga) &&
Args.hasMultipleArgs(options::OPT_g_Group)) ||
/* -fsycl -g case */ (!Args.hasArg(options::OPT_fintelfpga) &&
DebugInfoGroup)) {
if (!DebugInfoGroup->getOption().matches(options::OPT_g0)) {
OptLevel = "-O0";
}
}
CmdArgs.push_back(OptLevel.data());
}

// Add the integration header option to generate the header.
StringRef Header(D.getIntegrationHeader(Input.getBaseInput()));
Expand Down Expand Up @@ -10876,7 +10894,25 @@ static std::string getSYCLPostLinkOptimizationLevel(const ArgList &Args) {
[=](char c) { return c == S[0]; }))
return std::string("-O") + S[0];
}

// If no optimization controlling flags (-O) are provided, check if
// any debug information flags(-g) are passed.
// "-fintelfpga" implies "-g" and we preserve the default optimization for
// this flow(-O2).
// if "-g" is explicitly passed from the command-line, set default
// optimization to -O0.

if (!Args.hasArg(options::OPT_O_Group)) {
const Arg *DebugInfoGroup = Args.getLastArg(options::OPT_g_Group);
// -fintelfpga -g case
if ((Args.hasArg(options::OPT_fintelfpga) &&
Args.hasMultipleArgs(options::OPT_g_Group)) ||
/* -fsycl -g case */
(!Args.hasArg(options::OPT_fintelfpga) && DebugInfoGroup)) {
if (!DebugInfoGroup->getOption().matches(options::OPT_g0)) {
return "-O0";
}
}
}
// The default for SYCL device code optimization
return "-O2";
}
Expand Down
56 changes: 56 additions & 0 deletions clang/test/Driver/sycl-device-optimizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,59 @@
// RUN: %clang -### -fsycl --offload-new-driver %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-NO-THRESH %s
// CHECK-NO-THRESH-NOT: "-mllvm" "-inline-threshold

/// Check that optimizations for sycl device are disabled with -g passed:
// RUN: %clang -### -fsycl -g %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-DEBUG %s
// RUN: %clang_cl -### -fsycl -g %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-DEBUG %s
// CHECK-DEBUG: clang{{.*}} "-fsycl-is-device{{.*}}" "-O0"
// CHECK-DEBUG: sycl-post-link{{.*}} "-O0"
// CHECK-DEBUG-NOT: "-O2"

/// Check that optimizations for sycl device are enabled with -g and O2 passed:
// RUN: %clang -### -fsycl -O2 -g %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-G-O2 %s
// For clang_cl, -O2 maps to -O3
// RUN: %clang_cl -### -fsycl -O2 -g %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-G-O3 %s
// CHECK-G-O2: clang{{.*}} "-fsycl-is-device{{.*}}" "-O2"
// CHECK-G-O2: sycl-post-link{{.*}} "-O2"
// CHECK-G-O2-NOT: "-O0"
// CHECK-G-O3: clang{{.*}} "-fsycl-is-device{{.*}}" "-O3"
// CHECK-G-O3: sycl-post-link{{.*}} "-O3"
Copy link
Contributor

Choose a reason for hiding this comment

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

Side note - the -O2 to -O3 behavior is not in xmain, so some adjustments to the test will be needed when this is pulled down.

// CHECK-G-O3-NOT: "-O0"

/// Check that -O2 is passed for FPGA
// RUN: %clang -### -fintelfpga -fsycl-early-optimizations %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-FPGA %s
// RUN: %clang_cl -### -fintelfpga -fsycl-early-optimizations %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-FPGA %s
// CHECK-FPGA: clang{{.*}} "-fsycl-is-device{{.*}}" "-O2"
// CHECK-FPGA: sycl-post-link{{.*}} "-O2"
// CHECK-FPGA-NOT: "-O0"

/// Check that -O2 preserves for FPGA when it's explicitly passed
// RUN: %clang -### -O2 -fintelfpga -fsycl-early-optimizations %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O2 %s
// For clang_cl, -O2 maps to -O3
// RUN: %clang_cl -### -O2 -fintelfpga -fsycl-early-optimizations %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O3 %s
// CHECK-FPGA-O2: clang{{.*}} "-fsycl-is-device{{.*}}" "-O2"
// CHECK-FPGA-O2: sycl-post-link{{.*}} "-O2"
// CHECK-FPGA-O2-NOT: "-O0"
// CHECK-FPGA-O3: clang{{.*}} "-fsycl-is-device{{.*}}" "-O3"
// CHECK-FPGA-O3: sycl-post-link{{.*}} "-O3"
// CHECK-FPGA-O3-NOT: "-O0"

/// Check that -O0 is passed for FPGA when -g is explicitly passed
// RUN: %clang -### -fintelfpga -g %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O0 %s
// RUN: %clang_cl -### -fintelfpga -g %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-FPGA-O0 %s
// CHECK-FPGA-O0: clang{{.*}} "-fsycl-is-device{{.*}}" "-O0"
// CHECK-FPGA-O0: sycl-post-link{{.*}} "-O0"
// CHECK-FPGA-O0-NOT: "-O2"



4 changes: 2 additions & 2 deletions clang/test/Driver/sycl-offload-intelfpga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@
// RUN: | FileCheck -check-prefix=CHK-TOOLS-IMPLIED-OPTS %s
// RUN: %clang_cl -### -fintelfpga -Zi -Od -Xs "-DFOO1 -DFOO2" -Xshardware %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHK-TOOLS-IMPLIED-OPTS %s
// CHK-TOOLS-IMPLIED-OPTS-NOT: clang{{.*}} "-fsycl-is-device"{{.*}} "-O0"
// CHK-TOOLS-IMPLIED-OPTS: sycl-post-link{{.*}} "-O2"
// CHK-TOOLS-IMPLIED-OPTS: clang{{.*}} "-fsycl-is-device"{{.*}} "-fno-sycl-early-optimizations"{{.*}} "-O0"
// CHK-TOOLS-IMPLIED-OPTS: sycl-post-link{{.*}} "-O0"
// CHK-TOOLS-IMPLIED-OPTS: aoc{{.*}} "-g" "-DFOO1" "-DFOO2"

/// shared objects should not be checked for FPGA contents
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Check that full compilation works:
// RUN: %clangxx -DIMPL_SUBGROUP -fsycl -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g %S/../debug_symbols.cpp -o %t.out
// 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
// RUN: env IGC_VCSaveStackCallLinkage=1 IGC_VCDirectCallsOnly=1 %{run} %t.out
//
// VISALTO enable run
Expand Down
2 changes: 1 addition & 1 deletion sycl/test-e2e/InvokeSimd/Regression/debug_symbols.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Check that full compilation works:
// RUN: %{build} -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g -o %t.out
// RUN: %{build} -fno-sycl-device-code-split-esimd -Xclang -fsycl-allow-func-ptr -g -O2 -o %t.out
// RUN: env IGC_VCSaveStackCallLinkage=1 IGC_VCDirectCallsOnly=1 %{run} %t.out
//
// VISALTO enable run
Expand Down
Loading