Skip to content

Commit 0e44dd2

Browse files
mdtoguchibader
authored andcommitted
[SYCL] Add support for -fsycl-help=arg
Use -fsycl-help=arg where are is: gen, fpga, x86_64, all These correspond to the subarch values used to trigger AOT compilation. Use of this option will call the corresponding offline compiler to emit the help directly from that tool. Signed-off-by: Michael D Toguchi <[email protected]>
1 parent f4ffe62 commit 0e44dd2

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ class Driver {
440440
/// \param ShowHidden - Show hidden options.
441441
void PrintHelp(bool ShowHidden) const;
442442

443+
/// PrintSYCLToolHelp - Print help text from offline compiler tools.
444+
void PrintSYCLToolHelp(const Compilation &C) const;
445+
443446
/// PrintVersion - Print the driver version.
444447
void PrintVersion(const Compilation &C, raw_ostream &OS) const;
445448

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,11 @@ def fsycl_link : Flag<["-"], "fsycl-link">, Alias<fsycl_link_EQ>,
17531753
HelpText<"Generate partially linked device object to be used with the host link">;
17541754
def fsycl_unnamed_lambda : Flag<["-"], "fsycl-unnamed-lambda">,
17551755
Flags<[CC1Option]>, HelpText<"Allow unnamed SYCL lambda kernels">;
1756+
def fsycl_help_EQ : Joined<["-"], "fsycl-help=">,
1757+
Flags<[DriverOption]>, HelpText<"Emit help information from the related offline compilation tool">, Values<"all,fpga,gen,x86_64">;
1758+
def fsycl_help : Flag<["-"], "fsycl-help">, Alias<fsycl_help_EQ>,
1759+
Flags<[DriverOption]>, AliasArgs<["all"]>, HelpText<"Emit help information "
1760+
"from all of the offline compilation tools">;
17561761
def fsyntax_only : Flag<["-"], "fsyntax-only">,
17571762
Flags<[DriverOption,CoreOption,CC1Option]>, Group<Action_Group>;
17581763
def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group<f_Group>;

clang/lib/Driver/Driver.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,59 @@ void Driver::PrintHelp(bool ShowHidden) const {
16691669
/*ShowAllAliases=*/false);
16701670
}
16711671

1672+
llvm::Triple makeDeviceTriple(StringRef subArch) {
1673+
llvm::Triple TT;
1674+
TT.setArchName(subArch);
1675+
TT.setVendor(llvm::Triple::UnknownVendor);
1676+
TT.setOS(llvm::Triple(llvm::sys::getProcessTriple()).getOS());
1677+
TT.setEnvironment(llvm::Triple::SYCLDevice);
1678+
return TT;
1679+
}
1680+
1681+
// Print the help from any of the given tools which are used for AOT
1682+
// compilation for SYCL
1683+
void Driver::PrintSYCLToolHelp(const Compilation &C) const {
1684+
SmallVector<std::tuple<llvm::Triple, StringRef, StringRef>, 4> HelpArgs;
1685+
// Populate the vector with the tools and help options
1686+
if (Arg *A = C.getArgs().getLastArg(options::OPT_fsycl_help_EQ)) {
1687+
StringRef AV(A->getValue());
1688+
llvm::Triple T;
1689+
if (AV == "gen" || AV == "all")
1690+
HelpArgs.push_back(std::make_tuple(makeDeviceTriple("spir64_gen"),
1691+
"ocloc", "-?"));
1692+
if (AV == "fpga" || AV == "all")
1693+
HelpArgs.push_back(std::make_tuple(makeDeviceTriple("spir64_fpga"),
1694+
"aoc", "-help"));
1695+
if (AV == "x86_64" || AV == "all")
1696+
HelpArgs.push_back(std::make_tuple(makeDeviceTriple("spir64_x86_64"),
1697+
"ioc64", "-help"));
1698+
if (HelpArgs.empty()) {
1699+
C.getDriver().Diag(diag::err_drv_unsupported_option_argument)
1700+
<< A->getOption().getName() << AV;
1701+
return;
1702+
}
1703+
}
1704+
1705+
// Go through the args and emit the help information for each.
1706+
for (auto &HA : HelpArgs) {
1707+
llvm::outs() << "Emitting help information for " << std::get<1>(HA) << '\n'
1708+
<< "Use triple of '" << std::get<0>(HA).normalize() <<
1709+
"' to enable ahead of time compilation\n";
1710+
// do not run the tools with -###.
1711+
if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
1712+
continue;
1713+
std::vector<StringRef> ToolArgs = { std::get<1>(HA), std::get<2>(HA) };
1714+
StringRef ExecPath(C.getDefaultToolChain().GetProgramPath(std::get<1>(HA).data()));
1715+
auto ToolBinary = llvm::sys::findProgramByName(ExecPath);
1716+
if (ToolBinary.getError()) {
1717+
C.getDriver().Diag(diag::err_drv_command_failure) << ExecPath;
1718+
continue;
1719+
}
1720+
// Run the Tool.
1721+
llvm::sys::ExecuteAndWait(ToolBinary.get(), ToolArgs);
1722+
}
1723+
}
1724+
16721725
void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
16731726
// FIXME: The following handlers should use a callback mechanism, we don't
16741727
// know what the client would like to do.
@@ -1809,6 +1862,11 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
18091862
return false;
18101863
}
18111864

1865+
if (C.getArgs().hasArg(options::OPT_fsycl_help_EQ)) {
1866+
PrintSYCLToolHelp(C);
1867+
return false;
1868+
}
1869+
18121870
if (C.getArgs().hasArg(options::OPT__version)) {
18131871
// Follow gcc behavior and use stdout for --version and stderr for -v.
18141872
PrintVersion(C, llvm::outs());

clang/test/Driver/sycl.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,18 @@
1111
// NO-BITCODE: "{{.*}}llvm-spirv"{{.*}} "-spirv-no-deref-attr"
1212
// TARGET: "-triple" "spir64-unknown-linux-sycldevice"{{.*}} "-fsycl-is-device"{{.*}} "-emit-llvm-bc"
1313
// COMBINED: "-triple" "spir64-unknown-{{.*}}-sycldevice"{{.*}} "-fsycl-is-device"{{.*}} "-emit-llvm-bc"
14+
15+
// -fsycl-help tests
16+
// Test with a bad argument is expected to fail
17+
// RUN: not %clang -fsycl-help=foo %s 2>&1 | FileCheck %s --check-prefix=SYCL-HELP-BADARG
18+
// RUN: %clang -### -fsycl-help=gen %s 2>&1 | FileCheck %s --check-prefix=SYCL-HELP-GEN
19+
// RUN: %clang -### -fsycl-help=fpga %s 2>&1 | FileCheck %s --check-prefix=SYCL-HELP-FPGA
20+
// RUN: %clang -### -fsycl-help=x86_64 %s 2>&1 | FileCheck %s --check-prefix=SYCL-HELP-CPU
21+
// RUN: %clang -### -fsycl-help %s 2>&1 | FileCheck %s --check-prefixes=SYCL-HELP-GEN,SYCL-HELP-FPGA,SYCL-HELP-CPU
22+
// SYCL-HELP-BADARG: unsupported argument 'foo' to option 'fsycl-help='
23+
// SYCL-HELP-GEN: Emitting help information for ocloc
24+
// SYCL-HELP-GEN: Use triple of 'spir64_gen-unknown-{{.*}}-sycldevice' to enable ahead of time compilation
25+
// SYCL-HELP-FPGA: Emitting help information for aoc
26+
// SYCL-HELP-FPGA: Use triple of 'spir64_fpga-unknown-{{.*}}-sycldevice' to enable ahead of time compilation
27+
// SYCL-HELP-CPU: Emitting help information for ioc64
28+
// SYCL-HELP-CPU: Use triple of 'spir64_x86_64-unknown-{{.*}}-sycldevice' to enable ahead of time compilation

0 commit comments

Comments
 (0)