diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index f62192d7b8fe0..7ff2193952ce3 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