Skip to content

Commit 631fd69

Browse files
author
Artem Gindinson
authored
[SYCL][Driver] Diagnose incorrect inputs to CLI options (#4515)
Instead of assertions and silent fallthrough scenarios, incorrect values to SYCL options should lead to user-friendly diagnostics. The long-term fix for this should be a SYCL-agnostic solution in the LLVM comunity code - allowed values to driver options are known at the TableGen level via the `Values<>` class, so the validation should be happening there too. This would minimize code duplication for individual compilation pipelines and bring the TableGen CLI library closer to the `llvm::cl` functionality. Signed-off-by: Artem Gindinson <[email protected]>
1 parent facec17 commit 631fd69

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,29 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
898898
<< "-ffreestanding";
899899
}
900900

901+
// Diagnose incorrect inputs to SYCL options.
902+
// FIXME: Since the option definition includes the list of possible values,
903+
// the validation must be automatic, not requiring separate disjointed code
904+
// blocks accross the driver code. Long-term, the detection of incorrect
905+
// values must happen at the level of TableGen and Arg class design, with
906+
// Compilation/Driver class constructors handling the driver-specific
907+
// diagnostic output.
908+
auto checkSingleArgValidity = [&](Arg *A,
909+
SmallVector<StringRef, 4> AllowedValues) {
910+
if (!A)
911+
return;
912+
const char *ArgValue = A->getValue();
913+
for (const StringRef AllowedValue : AllowedValues)
914+
if (AllowedValue.equals(ArgValue))
915+
return;
916+
Diag(clang::diag::err_drv_invalid_argument_to_option)
917+
<< ArgValue << A->getOption().getName();
918+
};
919+
checkSingleArgValidity(SYCLLink, {"early", "image"});
920+
checkSingleArgValidity(
921+
C.getInputArgs().getLastArg(options::OPT_fsycl_device_code_split_EQ),
922+
{"per_kernel", "per_source", "auto", "off"});
923+
901924
bool HasSYCLTargetsOption = SYCLTargets || SYCLLinkTargets || SYCLAddTargets;
902925
llvm::StringMap<StringRef> FoundNormalizedTriples;
903926
llvm::SmallVector<llvm::Triple, 4> UniqueSYCLTriplesVec;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8909,15 +8909,15 @@ void SYCLPostLink::ConstructJob(Compilation &C, const JobAction &JA,
89098909

89108910
// See if device code splitting is requested
89118911
if (Arg *A = TCArgs.getLastArg(options::OPT_fsycl_device_code_split_EQ)) {
8912-
if (StringRef(A->getValue()) == "per_kernel")
8912+
auto CodeSplitValue = StringRef(A->getValue());
8913+
if (CodeSplitValue == "per_kernel")
89138914
addArgs(CmdArgs, TCArgs, {"-split=kernel"});
8914-
else if (StringRef(A->getValue()) == "per_source")
8915+
else if (CodeSplitValue == "per_source")
89158916
addArgs(CmdArgs, TCArgs, {"-split=source"});
8916-
else if (StringRef(A->getValue()) == "auto")
8917+
else if (CodeSplitValue == "auto")
89178918
addArgs(CmdArgs, TCArgs, {"-split=auto"});
8918-
else
8919-
// split must be off
8920-
assert(StringRef(A->getValue()) == "off");
8919+
else { // Device code split is off
8920+
}
89218921
} else {
89228922
// auto is the default split mode
89238923
addArgs(CmdArgs, TCArgs, {"-split=auto"});

clang/test/Driver/sycl-offload.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787

8888
/// ###########################################################################
8989

90+
/// Validate SYCL option values
91+
// RUN: %clang -### -fsycl-device-code-split=bad_value -fsycl %s 2>&1 \
92+
// RUN: | FileCheck -check-prefix=CHK-SYCL-BAD-OPT-VALUE -Doption=-fsycl-device-code-split %s
93+
// RUN: %clang -### -fsycl-link=bad_value -fsycl %s 2>&1 \
94+
// RUN: | FileCheck -check-prefix=CHK-SYCL-BAD-OPT-VALUE -Doption=-fsycl-link %s
95+
// CHK-SYCL-BAD-OPT-VALUE: error: invalid argument 'bad_value' to [[option]]=
96+
9097
/// Check error for -fsycl-targets with bad triple
9198
// RUN: %clang -### -fsycl-targets=spir64_bad-unknown-unknown -fsycl %s 2>&1 \
9299
// RUN: | FileCheck -check-prefix=CHK-SYCL-FPGA-BAD-TRIPLE %s

0 commit comments

Comments
 (0)