Skip to content

Commit

Permalink
have cuda_library output RDC
Browse files Browse the repository at this point in the history
This allows a `cuda_library` that was built with `rdc=True` to be
depended upon by another such library. This is convenient as such a
library can be consumed by either another `cuda_library` OR a
`cc_library`.

Note: if we have this, I'm not sure why we need a separate cuda_objects
rule anymore. Perhaps it can be removed in a later PR.

Change-Id: I1014d28a0ab3a9c76b788821211b13c4a9956d2a
  • Loading branch information
garymm committed Jul 6, 2023
1 parent 33c3843 commit d4d4226
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
15 changes: 13 additions & 2 deletions cuda/private/rules/cuda_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ def _cuda_library_impl(ctx):
# outputs
objects = depset(compile(ctx, cuda_toolchain, cc_toolchain, src_files, common, pic = False, rdc = use_rdc))
pic_objects = depset(compile(ctx, cuda_toolchain, cc_toolchain, src_files, common, pic = True, rdc = use_rdc))
rdc_objects = depset([])
rdc_pic_objects = depset([])

# if rdc is enabled for this cuda_library, then we need futher do a pass of device link
if use_rdc:
transitive_objects = depset(transitive = [dep[CudaInfo].rdc_objects for dep in attr.deps if CudaInfo in dep])
transitive_pic_objects = depset(transitive = [dep[CudaInfo].rdc_pic_objects for dep in attr.deps if CudaInfo in dep])
objects = depset(transitive = [objects, transitive_objects])
rdc_objects = objects
pic_objects = depset(transitive = [pic_objects, transitive_pic_objects])
rdc_pic_objects = pic_objects
dlink_object = depset([device_link(ctx, cuda_toolchain, cc_toolchain, objects, common, pic = False, rdc = use_rdc)])
dlink_pic_object = depset([device_link(ctx, cuda_toolchain, cc_toolchain, pic_objects, common, pic = True, rdc = use_rdc)])
objects = depset(transitive = [objects, dlink_object])
Expand Down Expand Up @@ -87,12 +91,19 @@ def _cuda_library_impl(ctx):
pic_lib = pic_libs,
objects = objects,
pic_objects = pic_objects,
rdc_objects = rdc_objects,
),
CcInfo(
compilation_context = cc_info.compilation_context,
linking_context = cc_info.linking_context,
),
cuda_helper.create_cuda_info(defines = depset(common.defines)),
cuda_helper.create_cuda_info(
defines = depset(common.defines),
objects = objects,
pic_objects = pic_objects,
rdc_objects = rdc_objects,
rdc_pic_objects = rdc_pic_objects,
),
]

cuda_library = rule(
Expand All @@ -104,7 +115,7 @@ cuda_library = rule(
"hdrs": attr.label_list(allow_files = ALLOW_CUDA_HDRS),
"deps": attr.label_list(providers = [[CcInfo], [CudaInfo]]),
"alwayslink": attr.bool(default = False),
"rdc": attr.bool(default = False, doc = "whether to perform relocateable device code linking, otherwise, normal device link."),
"rdc": attr.bool(default = False, doc = "Whether to produce and consume relocateable device code. Transitive deps that contain device code must all either be cuda_objects or cuda_library that has rdc = True. If False, all device code must be in the same translation unit. May have performance implications. See https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#using-separate-compilation-in-cuda."),
"includes": attr.string_list(doc = "List of include dirs to be added to the compile line."),
"host_copts": attr.string_list(doc = "Add these options to the CUDA host compilation command."),
"host_defines": attr.string_list(doc = "List of defines to add to the compile line."),
Expand Down
39 changes: 31 additions & 8 deletions examples/rdc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
load("@rules_cuda//cuda:defs.bzl", "cuda_library", "cuda_objects")

cuda_objects(
name = "a",
name = "a_objects",
srcs = ["a.cu"],
deps = [":b"],
)

cuda_objects(
name = "b",
name = "b_objects",
srcs = ["b.cu"],
hdrs = ["b.cuh"],
)

cuda_library(
name = "librdc",
rdc = 1,
name = "lib_from_objects",
rdc = True,
deps = [
":a",
":b",
":a_objects",
":b_objects",
],
)

cc_binary(
name = "main",
name = "main_from_objects",
deps = [
":librdc",
":lib_from_objects",
],
)

# Another way of doing it is to just use cuda_library
cuda_library(
name = "a",
srcs = ["a.cu"],
rdc = True,
deps = [":b"],
)

cuda_library(
name = "b",
srcs = ["b.cu"],
hdrs = ["b.cuh"],
rdc = True,
)

cc_binary(
name = "main_from_library",
deps = [
":a",
":b",
],
)

0 comments on commit d4d4226

Please sign in to comment.