This is an archive of the discontinued LLVM Phabricator instance.

[Internalize] Preserve variables externally initialized.
ClosedPublic

Authored by hliao on Jun 29 2021, 10:31 AM.

Details

Summary
  • `externally_initialized variables would be initialized or modified elsewhere. Particularly, CUDA or HIP may have host code to initialize or modify externally_initialized` device variables, which may not be explicitly referenced on the device side but may still be used through the host side interfaces. Not preserving them triggers the elimination of them in the GlobalDCE and breaks the user code.

Diff Detail

Event Timeline

hliao created this revision.Jun 29 2021, 10:31 AM
hliao requested review of this revision.Jun 29 2021, 10:31 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 29 2021, 10:31 AM
hliao added inline comments.Jun 29 2021, 10:46 AM
clang/test/CodeGenCUDA/host-used-device-var.cu
20

BTW, as clang codegen tests, those checks should not rely on middle-end optimizations to work correctly.

clang/test/CodeGenCUDA/unused-global-var.cu
20

BTW, as clang codegen tests, those checks should not rely on middle-end optimizations to work correctly.

hliao updated this revision to Diff 355764.Jun 30 2021, 8:52 PM

Fix typo. Kindly PING for review.

tra added a subscriber: tra.Jul 1 2021, 9:33 AM
tra added inline comments.
clang/test/CodeGenCUDA/host-used-device-var.cu
20

What happens if you remove -O3?
Sometimes optimization is helpful as it reduces the amount of IR we need to check and how it's structured. It's much easier to see what went wrong when you get just a handful of IR statements in the failing test output.

21–24

These should be changed to positive checks to verify that they are emitted.
Ditto for other tests.

hliao updated this revision to Diff 356721.Jul 6 2021, 7:44 AM

Kindly PING for review.

LGTM. Pls change the negative tests to positive tests as Artem suggested.

clang/test/CodeGenCUDA/host-used-device-var.cu
20

These tests were introduced with -O3 since they can only be tested with clang and -O3. (https://reviews.llvm.org/D98783) Clang may emit IR containing unused LLVM constants. Saving the IR as a file will cause such unused LLVM constants to disappear, therefore unable to reproduce the issue.

20

If -O3 is removed, those unused variables will not be internalized and removed.

hliao updated this revision to Diff 357053.Jul 7 2021, 12:46 PM

Revert part of tests back and convert them into positive ones.

yaxunl accepted this revision.Jul 7 2021, 8:52 PM

LGTM. Thanks.

This revision is now accepted and ready to land.Jul 7 2021, 8:52 PM
This revision was automatically updated to reflect the committed changes.