diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp --- a/clang/lib/Driver/ToolChains/Cuda.cpp +++ b/clang/lib/Driver/ToolChains/Cuda.cpp @@ -686,7 +686,8 @@ "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs."); if (DeviceOffloadingKind == Action::OFK_Cuda) { - CC1Args.push_back("-fcuda-is-device"); + CC1Args.append( + {"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"}); if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, options::OPT_fno_cuda_approx_transcendentals, false)) diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp --- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -67,6 +67,10 @@ #define DEBUG_TYPE "memcpyopt" +static cl::opt EnableMemCpyOptWithoutLibcalls( + "enable-memcpyopt-without-libcalls", cl::init(false), cl::Hidden, + cl::desc("Enable memcpyopt even when libcalls are disabled")); + static cl::opt EnableMemorySSA("enable-memcpyopt-memoryssa", cl::init(true), cl::Hidden, cl::desc("Use MemorySSA-backed MemCpyOpt.")); @@ -1757,7 +1761,16 @@ // If we don't have at least memset and memcpy, there is little point of doing // anything here. These are required by a freestanding implementation, so if // even they are disabled, there is no point in trying hard. - if (!TLI->has(LibFunc_memset) || !TLI->has(LibFunc_memcpy)) + bool ShoudRunMemCpyOpt = + (TLI->has(LibFunc_memset) && TLI->has(LibFunc_memcpy)) + // Some targets have libcalls disabled, but still want to run the pass. + // TODO: the pass should not depend on libcall availability as the targets + // should provide alternative ways of lowering standard LLVM + // intrinsics. Alas, there are existing users that can't currently handle + // `memset` intrinsic materialized by this pass. See + // https://reviews.llvm.org/D104801 for the details. + || EnableMemCpyOptWithoutLibcalls; + if (!ShoudRunMemCpyOpt) return false; while (true) {