From 9fa540ec35dd929a90033497660634b551cd2031 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Thu, 24 Apr 2025 17:06:32 -0700 Subject: [PATCH 1/3] [Driver][SYCL] Remove object upon failure Typical behaviors when the compilation fails is to have the resulting output object to be removed. Due to the fact that there are multiple compilations that occur during an offload compilation, the actual final output object may not be directly associated with the compile causing it to not be removed. Update the cleanup file list to remove the output result file regardless of the associated job action when we are performing offload. Signed-off-by: Michael D Toguchi --- clang/lib/Driver/Driver.cpp | 6 +++++- clang/test/Driver/sycl-obj-remove.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 clang/test/Driver/sycl-obj-remove.cpp diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index f62192d7b8fe0..5491edde1094e 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -2708,7 +2708,11 @@ int Driver::ExecuteCompilation( // Remove result files if we're not saving temps. if (!isSaveTempsEnabled()) { const JobAction *JA = cast(&FailingCommand->getSource()); - C.CleanupFileMap(C.getResultFiles(), JA, true); + // When performing offload compilations, the result files may not match + // the JobAction that fails. In that case, do not pass in the JobAction + // to allow for the proper resulting file to be removed upon failure. + C.CleanupFileMap(C.getResultFiles(), C.getActiveOffloadKinds() + ? nullptr : JA, true); // Failure result files are valid unless we crashed. if (CommandRes < 0) diff --git a/clang/test/Driver/sycl-obj-remove.cpp b/clang/test/Driver/sycl-obj-remove.cpp new file mode 100644 index 0000000000000..28b19586c8168 --- /dev/null +++ b/clang/test/Driver/sycl-obj-remove.cpp @@ -0,0 +1,16 @@ +/// Verify object removal when the offload compilation fails. + +// REQUIRES: system-linux + +// RUN: touch %t.o +// RUN: not %clangxx -fsycl -Xsycl-target-frontend -DCOMPILE_HOST_FAIL=1 -o %t.o %s +// RUN: not ls %t.o + +// RUN: touch %t.o +// RUN: not %clangxx --offload-new-driver -fsycl -Xsycl-target-frontend -DCOMPILE_HOST_FAIL=1 -o %t.o %s +// RUN: not ls %t.o + +void func(){}; +#ifdef COMPILE_HOST_FAIL +#error FAIL +#endif // COMPILE_HOST_FAIL From eaa113d67b1113d45582e320fbfbde2a51130920 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Thu, 24 Apr 2025 17:17:28 -0700 Subject: [PATCH 2/3] clang format Signed-off-by: Michael D Toguchi --- clang/lib/Driver/Driver.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 5491edde1094e..7ff2193952ce3 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -2711,8 +2711,8 @@ int Driver::ExecuteCompilation( // When performing offload compilations, the result files may not match // the JobAction that fails. In that case, do not pass in the JobAction // to allow for the proper resulting file to be removed upon failure. - C.CleanupFileMap(C.getResultFiles(), C.getActiveOffloadKinds() - ? nullptr : JA, true); + C.CleanupFileMap(C.getResultFiles(), + C.getActiveOffloadKinds() ? nullptr : JA, true); // Failure result files are valid unless we crashed. if (CommandRes < 0) From 805770e2b3b48db075c92b7c403c8b48973a4c27 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Tue, 29 Apr 2025 10:42:46 -0700 Subject: [PATCH 3/3] Update test, clean up predefine macro name, add -c and general test --- clang/test/Driver/sycl-obj-remove.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/clang/test/Driver/sycl-obj-remove.cpp b/clang/test/Driver/sycl-obj-remove.cpp index 28b19586c8168..62660447e11f3 100644 --- a/clang/test/Driver/sycl-obj-remove.cpp +++ b/clang/test/Driver/sycl-obj-remove.cpp @@ -2,15 +2,25 @@ // REQUIRES: system-linux +/// Force failure during device compilation // RUN: touch %t.o -// RUN: not %clangxx -fsycl -Xsycl-target-frontend -DCOMPILE_HOST_FAIL=1 -o %t.o %s +// RUN: not %clangxx -fsycl -Xsycl-target-frontend -DCOMPILE_FAIL=1 -c -o %t.o %s // RUN: not ls %t.o // RUN: touch %t.o -// RUN: not %clangxx --offload-new-driver -fsycl -Xsycl-target-frontend -DCOMPILE_HOST_FAIL=1 -o %t.o %s +// RUN: not %clangxx --offload-new-driver -fsycl -Xsycl-target-frontend -DCOMPILE_FAIL=1 -c -o %t.o %s +// RUN: not ls %t.o + +/// Force failure during compilation +// RUN: touch %t.o +// RUN: not %clangxx -fsycl -DCOMPILE_FAIL=1 -c -o %t.o %s +// RUN: not ls %t.o + +// RUN: touch %t.o +// RUN: not %clangxx --offload-new-driver -fsycl -DCOMPILE_FAIL=1 -c -o %t.o %s // RUN: not ls %t.o void func(){}; -#ifdef COMPILE_HOST_FAIL +#ifdef COMPILE_FAIL #error FAIL -#endif // COMPILE_HOST_FAIL +#endif // COMPILE_FAIL