This is an archive of the discontinued LLVM Phabricator instance.

[CUDA][HIP] Promote const variables to constant
ClosedPublic

Authored by yaxunl on May 25 2021, 11:42 AM.

Details

Summary

Recently we added diagnosing ODR-use of host variables
in device functions, which includes ODR-use of const
host variables since they are not really emitted on
device side. This caused regressions since we used
to allow ODR-use of const host variables in device
functions.

This patch allows ODR-use of const variables in device
functions if the const variables can be statically initialized
and have an empty dtor. Such variables are marked with
implicit constant attrs and emitted on device side. This is
in line with what clang does for constexpr variables.

Diff Detail

Event Timeline

yaxunl requested review of this revision.May 25 2021, 11:42 AM
yaxunl created this revision.
yaxunl updated this revision to Diff 347958.May 26 2021, 7:36 AM

do not promote or check dependent variables since their ctor/dtor/initializers are not determined yet

tra added inline comments.May 26 2021, 10:50 AM
clang/lib/Sema/SemaCUDA.cpp
568

Nit: add an empty line to separate from the preceding function.

722

One of the issues with automatic promotion I've ran into in the past is that __constant__ is a very limited resource.

It would be nearly impossible for the end user to predict how much __constant__ space is available for their own use and if we use too much, the failure would only manifest at runtime when we fail to load the GPU code. That will be hard to troubleshoot.

We may need to add some sort of safeguard and report an error when we see too much constant data for a given GPU.
That would have to be done somewhere at the end of the compilation pipeline.

It's not likely to affect any/many users yet so it can be done later. It's probably worth a comment and a TODO here in case someone does run into this.

clang/test/CodeGenCUDA/device-use-host-var.cu
51

It would be great to have a test which verifies that we only emit const variables that we need.
It would be bad if we'd end up with *all* host-side consts in the GPU binary.

clang/test/SemaCUDA/device-use-host-var.cu
82

This diag is a bit misleading now. While it's technically true that we're not allowed to refer to a host variables from device in general, the real reason it's not allowed in *this* case is that the variable has a non-empty ctor.

I wonder if we could add a note pointing that out. Otherwise it would be rather confusing why I can access other host vars few lines above, but not here.

yaxunl updated this revision to Diff 348089.May 26 2021, 2:05 PM
yaxunl marked 4 inline comments as done.

revised by Artem's comments

yaxunl added inline comments.May 26 2021, 2:07 PM
clang/lib/Sema/SemaCUDA.cpp
568

done

722

added a TODO

clang/test/CodeGenCUDA/device-use-host-var.cu
51

added test for that

clang/test/SemaCUDA/device-use-host-var.cu
82

done

tra added a comment.May 26 2021, 2:37 PM

Overall looks good, though I've got one more question.

clang/test/SemaCUDA/device-use-host-var.cu
90

What happens if we have const int &ref_foo = something_we_cant_emit_on_device;?
Both if ref_foo is and isn't used from device code.

yaxunl marked an inline comment as done.May 26 2021, 6:49 PM
yaxunl added inline comments.
clang/test/SemaCUDA/device-use-host-var.cu
90

If we have const int &ref_foo = something_we_cant_emit_on_device, __constant__ attribute will not be added. If ref_foo is used in device code, it will be diagnosed. If ref_foo is not used in device code, no diagnostics is emitted.

yaxunl updated this revision to Diff 348149.May 26 2021, 8:32 PM
yaxunl marked an inline comment as done.

fix test

tra added a comment.Jun 1 2021, 12:40 PM

LGTM. I would like to test the patch on our code first. Please wait a bit before landing the patch. I should be able to have the results tomorrow.

tra accepted this revision.Jun 1 2021, 2:46 PM

I'm done with testing. The patch does not seem to break anything obvious. Tensorflow builds and works.

This revision is now accepted and ready to land.Jun 1 2021, 2:46 PM
This revision was landed with ongoing or failed builds.Jun 1 2021, 6:29 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJun 1 2021, 6:29 PM
sbc100 added a subscriber: sbc100.Jun 4 2021, 3:46 PM
sbc100 added inline comments.
clang/test/CodeGenCUDA/device-use-host-var.cu
68

This seems to break if the pathname where the llvm checkout lives contains the word "external"

https://logs.chromium.org/logs/emscripten-releases/buildbucket/cr-buildbucket.appspot.com/8845343598940893760/+/steps/LLVM_regression/0/stdout

--
/b/s/w/ir/cache/builder/emscripten-releases/llvm-project/clang/test/CodeGenCUDA/device-use-host-var.cu:68:13: error: NEG-NOT: excluded string found in input
// NEG-NOT: external
            ^
<stdin>:69:78: note: found here
!1 = !{!"clang version 13.0.0 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-llvm-llvm--project db3e4faa4d2cadf204e67f42bccd98957496a87a)"}
                                                                             ^~~~~~~~

Unfortunate choice of directory names I know .. but probably worth fixing.

yaxunl marked an inline comment as done.Jun 4 2021, 3:55 PM
yaxunl added inline comments.
clang/test/CodeGenCUDA/device-use-host-var.cu
68

thanks. this has been fixed by https://reviews.llvm.org/D103658

tra added inline comments.Jun 4 2021, 3:55 PM
clang/test/CodeGenCUDA/device-use-host-var.cu
68

This should already be fixed by D103658

sbc100 added inline comments.Jun 4 2021, 4:09 PM
clang/test/CodeGenCUDA/device-use-host-var.cu
68

Indeed! Our latest build is now passing. Sorry for the noise.