Skip to content

[clang-sycl-linker] Improve --spirv-dump-device-code and harden CLI handling#200096

Merged
bader merged 10 commits into
llvm:mainfrom
bader:csl-fixes
May 29, 2026
Merged

[clang-sycl-linker] Improve --spirv-dump-device-code and harden CLI handling#200096
bader merged 10 commits into
llvm:mainfrom
bader:csl-fixes

Conversation

@bader
Copy link
Copy Markdown
Contributor

@bader bader commented May 28, 2026

Several small fixes and improvements to clang-sycl-linker's command-line
handling, plus completing the --spirv-dump-device-code option:

  • --version: now exits with EXIT_SUCCESS after printing, instead of
    falling through into the rest of main.
  • Empty input: report a clear "No input files provided" error from
    getInput rather than triggering an assertion deep inside linkDeviceCode.
  • --spirv-dump-device-code: previously parsed but ignored — now actually
    copies each generated .spv file into the requested directory. The
    directory is created up-front (mkdir -p semantics) so a missing path
    produces a friendly diagnostic instead of a low-level copy errno.

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

- --version now returns EXIT_SUCCESS immediately after printing; previously
  it fell through into the rest of main.
- Report an error early when no input files are provided rather than
  hitting the assert inside linkDeviceCode.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@llvmorg-github-actions llvmorg-github-actions Bot added the clang Clang issues not falling into any other category label May 28, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Alexey Bader (bader)

Changes
  • --version now returns EXIT_SUCCESS immediately after printing; previously it fell through into the rest of main.
  • Report an error early when no input files are provided rather than hitting the assert inside linkDeviceCode.

Full diff: https://github.com/llvm/llvm-project/pull/200096.diff

2 Files Affected:

  • (added) clang/test/OffloadTools/clang-sycl-linker/options.ll (+40)
  • (modified) clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp (+6-1)
diff --git a/clang/test/OffloadTools/clang-sycl-linker/options.ll b/clang/test/OffloadTools/clang-sycl-linker/options.ll
new file mode 100644
index 0000000000000..55b691187028f
--- /dev/null
+++ b/clang/test/OffloadTools/clang-sycl-linker/options.ll
@@ -0,0 +1,40 @@
+; Tests non-functional command line options of the clang-sycl-linker tool.
+;
+; REQUIRES: spirv-registered-target
+;
+; Test --help
+; RUN: clang-sycl-linker --help | FileCheck %s --check-prefix=HELP
+; HELP: OVERVIEW: A utility that wraps around several steps required to link SYCL device files.
+; HELP: USAGE: clang-sycl-linker
+; HELP: OPTIONS:
+;
+; Test --version
+; RUN: clang-sycl-linker --version | FileCheck %s --check-prefix=VERSION
+; VERSION: clang-sycl-linker version
+;
+; Test missing input files
+; RUN: not clang-sycl-linker --dry-run -triple=spirv64 -o %t.out 2>&1 | FileCheck %s --check-prefix=NO-INPUT
+; NO-INPUT: No input files provided
+;
+; Create a simple bitcode file for subsequent tests
+; RUN: llvm-as %s -o %t.bc
+;
+; Test --print-linked-module
+; RUN: clang-sycl-linker --dry-run -triple=spirv64 %t.bc --print-linked-module -o %t.out > %t.ll
+; RUN: FileCheck %s --check-prefix=PRINT-LINKED < %t.ll
+; PRINT-LINKED: target triple = "spirv64"
+;
+; Test --save-temps
+; RUN: rm -rf %t.dir && mkdir -p %t.dir
+; RUN: cd %t.dir && clang-sycl-linker --dry-run -triple=spirv64 %t.bc --save-temps -o out.spv
+; RUN: ls %t.dir/out.spv-*.bc | count 1
+;
+; Test --spirv-dump-device-code (should parse without error)
+; RUN: clang-sycl-linker --dry-run -triple=spirv64 %t.bc --spirv-dump-device-code=%t.dir -o %t.out
+;
+; Test --spirv-dump-device-code with no value (fallback to ./)
+; RUN: clang-sycl-linker --dry-run -triple=spirv64 %t.bc --spirv-dump-device-code= -o %t.out
+
+target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1"
+target triple = "spirv64"
+
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 88a09d0a3ecc7..0d83c93065e3f 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -786,8 +786,10 @@ int main(int argc, char **argv) {
     return EXIT_SUCCESS;
   }
 
-  if (Args.hasArg(OPT_version))
+  if (Args.hasArg(OPT_version)) {
     printVersion(outs());
+    return EXIT_SUCCESS;
+  }
 
   Verbose = Args.hasArg(OPT_verbose);
   DryRun = Args.hasArg(OPT_dry_run);
@@ -812,6 +814,9 @@ int main(int argc, char **argv) {
   if (!FilesOrErr)
     reportError(FilesOrErr.takeError());
 
+  if (FilesOrErr->empty())
+    reportError(createStringError("No input files provided"));
+
   // Run SYCL linking process on the generated inputs.
   if (Error Err = runSYCLLink(*FilesOrErr, Args))
     reportError(std::move(Err));

@bader bader requested review from YuriPlyakhin and sarnex May 28, 2026 01:20
Comment thread clang/test/OffloadTools/clang-sycl-linker/options.ll Outdated
Implement the previously unfinished --spirv-dump-device-code option by
copying each generated .spv file into the requested directory after code
generation; SPIRVDumpDir was set but never consumed before this change.

Fix the options.ll tests: drop --dry-run (tests now exercise real code
paths), verify that dump/save-temps output files actually exist, and
isolate each test into its own subdirectory to prevent interference.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bader bader requested a review from sarnex May 28, 2026 23:09
Comment thread clang/test/OffloadTools/clang-sycl-linker/options.ll Outdated
- Create the SPIR-V dump directory up-front so users get a clear error
  instead of a low-level copy failure when the path doesn't exist.
- Move the empty-input check into getInput so main has a single error path.
- Drop -triple=spirv64 from the options test (the IR module already sets
  the triple) and unify SmallString sizing for the dump path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@bader bader changed the title [clang-sycl-linker] Fix --version exit code and empty-input error [clang-sycl-linker] Improve --spirv-dump-device-code and harden CLI handling May 28, 2026
bader and others added 2 commits May 28, 2026 16:47
…-device-code

- getInput: report missing/invalid input files explicitly instead of
  silently skipping them; reserve "No input files provided" for the
  no-INPUT-args case and include the offending path in the
  unsupported-file-type diagnostic.
- main: run getInput before setting up the SPIR-V dump directory so
  missing-input is reported first; skip create_directories and the
  per-module copy_file in --dry-run mode.
- Tests: split CLI-only checks (--help, --version, missing/non-existent/
  non-bitcode input) into cli.test so they run on configs without the
  SPIR-V backend; replace fragile "ls | count 1" with "test -f"; add a
  case for dump-dir creation failure; reorder IR header above RUN lines.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@sarnex sarnex left a comment

Choose a reason for hiding this comment

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

lgtm with the --spirv-dump-device-code logic simplified

Comment thread clang/test/OffloadTools/clang-sycl-linker/cli.test Outdated
Comment thread clang/test/OffloadTools/clang-sycl-linker/cli.test Outdated
Comment thread clang/test/OffloadTools/clang-sycl-linker/cli.test Outdated
Comment thread clang/test/OffloadTools/clang-sycl-linker/options.ll Outdated
bader and others added 5 commits May 29, 2026 09:44
Remove the silent fallback to "." when an empty value is supplied.
An empty path is now an error with a clear diagnostic.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Move --help, --version, missing-input, and non-existent-input checks
from cli.test into basic.ll. Update the --help check to match the
actual tool output. Drop BAD-MAGIC as it is already covered by the
FILETYPEERROR check in basic.ll. Remove cli.test.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The option is validated by other checks.
@bader bader requested a review from YuriPlyakhin May 29, 2026 17:31
Copy link
Copy Markdown
Contributor

@YuriPlyakhin YuriPlyakhin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@bader bader merged commit 6da24e4 into llvm:main May 29, 2026
10 checks passed
@bader bader deleted the csl-fixes branch May 29, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants