Use -O3 for cuda compiles in opt, as significant cuda optimizations in
cuda-clang only get enabled at -O3.

PiperOrigin-RevId: 302467718
Change-Id: I7f6448c83762a9fb38d0ce47dd9b9850f89ba22d
diff --git a/third_party/gpus/cuda/build_defs.bzl.tpl b/third_party/gpus/cuda/build_defs.bzl.tpl
index 845866b..3280d6b 100644
--- a/third_party/gpus/cuda/build_defs.bzl.tpl
+++ b/third_party/gpus/cuda/build_defs.bzl.tpl
@@ -24,9 +24,28 @@
        "//conditions:default": if_false
    })
 
+def if_cuda_clang_opt(if_true, if_false = []):
+   """Shorthand for select()'ing on wheteher we're building with cuda-clang
+   in opt mode.
+
+    Returns a select statement which evaluates to if_true if we're building
+    with cuda-clang in opt mode. Otherwise, the select statement evaluates to
+    if_false.
+
+   """
+   return select({
+       "@local_config_cuda//cuda:using_clang_opt": if_true,
+       "//conditions:default": if_false
+   })
+
 def cuda_default_copts():
     """Default options for all CUDA compilations."""
-    return if_cuda(["-x", "cuda", "-DGOOGLE_CUDA=1"]) + %{cuda_extra_copts}
+    return if_cuda(
+        ["-x", "cuda", "-DGOOGLE_CUDA=1"]
+    ) + if_cuda_clang_opt(
+        # Some important CUDA optimizations are only enabled at O3.
+        ["-O3"]
+    ) + %{cuda_extra_copts}
 
 def cuda_is_configured():
     """Returns true if CUDA was enabled during the configure process."""