Skip to content

Commit

Permalink
Have cuda_library output RDC (#125)
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`.

Change-Id: I1014d28a0ab3a9c76b788821211b13c4a9956d2a
  • Loading branch information
garymm authored Jul 13, 2023
1 parent b9cf8ec commit 555f88f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ jobs:
if: ${{ startsWith(matrix.cases.os, 'windows') }}
run: .github/workflows/Set-VSEnv.ps1 2019

- run: bazelisk build @rules_cuda_examples//basic:main
- run: bazelisk build @rules_cuda_examples//rdc:main
- run: bazelisk build @rules_cuda_examples//basic:all
- run: bazelisk build @rules_cuda_examples//rdc:all
- run: bazelisk build @rules_cuda_examples//if_cuda:main
- run: bazelisk build @rules_cuda_examples//if_cuda:main --enable_cuda=False

- run: cd examples && bazelisk build //basic:main --config=bzlmod
- run: cd examples && bazelisk build //rdc:main --config=bzlmod
- run: cd examples && bazelisk build //basic:all --config=bzlmod
- run: cd examples && bazelisk build //rdc:all --config=bzlmod
- run: cd examples && bazelisk build //if_cuda:main --config=bzlmod
- run: cd examples && bazelisk build //if_cuda:main --enable_cuda=False --config=bzlmod

Expand Down
22 changes: 20 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,20 @@ def _cuda_library_impl(ctx):
pic_lib = pic_libs,
objects = objects,
pic_objects = pic_objects,
rdc_objects = rdc_objects,
rdc_pic_objects = rdc_pic_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 +116,13 @@ 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(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 555f88f

Please sign in to comment.