Skip to content

Commit

Permalink
Fix _wrapper_device_link artifact name conflict (#135)
Browse files Browse the repository at this point in the history
Compile the newly added example in #125 with `_wrapper_device_link` with rdc causes artifact name conflict.

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

The reason is that our compile always compile to <target_output_dir>/_objs:

 cuda_library(name="b") --> artifact base name is `b` during `compile`

with `compile` for following srcs
"b.cu" ------------------> artifact `<target_output_dir>/_objs/b.rdc.o`
`fatbin.cu` ------rdc----> artifact `<target_output_dir>/_objs/b.rdc.o`

After this PR:
"b.cu" ------------------> artifact `<target_output_dir>/_objs/b.rdc.o`
`fatbin.cu` ------rdc----> artifact `<target_output_dir>/_objs/_dlink/b.rdc.o`
  • Loading branch information
cloudhan authored Aug 9, 2023
1 parent 5e62be5 commit 05fb8f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions cuda/private/actions/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def compile(
srcs,
common,
pic = False,
rdc = False):
rdc = False,
_prefix = "_objs"):
"""Perform CUDA compilation, return compiled object files.
Notes:
Expand All @@ -26,6 +27,7 @@ def compile(
common: A cuda common object. Can be obtained with `cuda_helper.create_common(ctx)`
pic: Whether the `srcs` are compiled for position independent code.
rdc: Whether the `srcs` are compiled for relocatable device code.
_prefix: DON'T USE IT! Prefix of the output dir. Exposed for device link to redirect the output.
Returns:
An compiled object `File`.
Expand Down Expand Up @@ -53,13 +55,13 @@ def compile(
filename = None
filename = cuda_helper.get_artifact_name(cuda_toolchain, artifact_category_name, basename)

# Objects are placed in _objs/<tgt_name>/<filename>.
# Objects are placed in <_prefix>/<tgt_name>/<filename>.
# For files with the same basename, say srcs = ["kernel.cu", "foo/kernel.cu", "bar/kernel.cu"], we get
# _objs/<tgt_name>/0/kernel.<ext>, _objs/<tgt_name>/1/kernel.<ext>, _objs/<tgt_name>/2/kernel.<ext>.
# <_prefix>/<tgt_name>/0/kernel.<ext>, <_prefix>/<tgt_name>/1/kernel.<ext>, <_prefix>/<tgt_name>/2/kernel.<ext>.
# Otherwise, the index is not presented.
if basename_counter[basename] > 1:
filename = "{}/{}".format(basename_index, filename)
obj_file = actions.declare_file("_objs/{}/{}".format(ctx.attr.name, filename))
obj_file = actions.declare_file("{}/{}/{}".format(_prefix, ctx.attr.name, filename))
ret.append(obj_file)

var = cuda_helper.create_compile_variables(
Expand Down
2 changes: 1 addition & 1 deletion cuda/private/actions/dlink.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,5 @@ def _wrapper_device_link(
# suppress cuda mode as c++ mode
host_compile_flags = common.host_compile_flags + ["-x", "c++"],
)
ret = compile(ctx, cuda_toolchain, cc_toolchain, srcs = [fatbin_c], common = compile_common, pic = pic, rdc = rdc)
ret = compile(ctx, cuda_toolchain, cc_toolchain, srcs = [fatbin_c], common = compile_common, pic = pic, rdc = rdc, _prefix = "_objs/_dlink")
return ret[0]

0 comments on commit 05fb8f4

Please sign in to comment.