Skip to content

[sycl-post-link] Don't remove llvm.compiler.used for NVPTX #15224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 6, 2024

Conversation

hdelan
Copy link
Contributor

@hdelan hdelan commented Aug 29, 2024

llvm.compiler.used is a global symbol which tells the compiler not to touch some other global symbols until backend lowering. The llvm.compiler.used symbol itself is thus removed automatically during lowering for NVPTX and AMDGCN. Removal at sycl-post-link, prior to lowering, was causing issues for these backends, where symbols protected by llvm.compiler.used were getting removed after sycl-post-link and before lowering.

We retain the current behaviour for SPIR-V, as SPIR-V generation is handled in llvm-spirv anyway, not in the LLVM SPIR-V backend.

Also adds tests to make sure static device_globals are handled properly for NVPTX/AMDGCN.

@hdelan hdelan requested review from a team as code owners August 29, 2024 11:05
@hdelan hdelan requested a review from aelovikov-intel August 29, 2024 11:05
@hdelan hdelan force-pushed the dont-remove-llvm-compiler-used branch from 3cfff78 to 8ca0294 Compare August 29, 2024 11:14
@hdelan hdelan force-pushed the dont-remove-llvm-compiler-used branch from 8ca0294 to 9784bb5 Compare August 29, 2024 11:25
@hdelan hdelan requested a review from a team as a code owner August 29, 2024 15:22
@hdelan hdelan changed the title [AMDGCN][NVPTX] Don't remove llvm.compiler.used for NVPTX/AMDGCN [sycl-post-link] Don't remove llvm.compiler.used for NVPTX Aug 29, 2024
@@ -0,0 +1,29 @@
// Tests that the llvm.compiler.used symbol, which is used to implement static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why sycl/test instead of sycl-post-link test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a mess but the llvm.used symbol is handled in different parts of the compilation depending on backend. So it's important to make sure that the tests are working for an entire compilation pipeline, not just for a particular stage of the action.

Copy link
Contributor

@aelovikov-intel aelovikov-intel Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the latest stage it is being removed? Why can't we have test for that component? Is the "mess" final, or do we expect pipelines to be unified?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm.used should survive to final linking, llvm.compiler.used is okay for intermediary tools to drop: https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a test when programmer uses __attribute__((used)) on something to ensure that something isn't being dropped?

Copy link
Contributor Author

@hdelan hdelan Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device image compression will be optional (#15124) - unless you specify --offload-compress in CLI, by default, we won't compress images. So, image compression might not be an issue for this particular test.

If image compression is "opt-in" and we will not opt in in this test, then why is this an issue @aelovikov-intel ? It seems unlikely that binary formatting is going to change drastically in the next while, and if it does then I think this test will be the least of our problems.

I am using strings as it's a simpler, more portable way to find a global symbol, using the current device code formats, than using proprietary binutils that are likely to belong to proprietary toolkits. For instance cuobjdump is not necessarily distributed in every CUDA version. Relying on a tool like this may break CI with a CUDA toolkit version change. Likewise I don't want to rely on a tool like spirv-dis which often breaks and doesn't keep up to date with the latest spir-v features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for response @uditagarwal97 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the test to use llvm-strings instead of strings. Is this sufficient @aelovikov-intel ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I just delete the test will you approve this PR? I don't think this discussion is going anywhere

Yes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If image compression is "opt-in" and we will not opt in in this test, then why is this an issue @aelovikov-intel ?

Because that's an implementation detail of something unrelated to your test and it must not make any assumptions of it.

Copy link
Contributor

@asudarsa asudarsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Few minor comments. Thanks

These are removed automatically during lowering.
This is removed at lowering for NVPTX/AMDGCN.

TODO: check that this is working for SPIR-V.
sycl post link no longer removes llvm.compiler.used.
For spirv remove the llvm.compiler.used symbol at sycl-post-link. This
is because we use llvm-spirv for lowering, not the SPIR-V backend.
@hdelan hdelan force-pushed the dont-remove-llvm-compiler-used branch from eeff007 to 4b06c76 Compare September 3, 2024 16:51
// used inside the device code after they have been removed from
// "llvm.compiler.used" they can be erased safely.
Modified |= removeDeviceGlobalFromCompilerUsed(*M.get());
// to keep the optimizer from wrongfully removing them. llvm.compiler.used
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We intend to replace the translator with SPIR-V backend eventually. Can we remove the check at that point of time?

Thanks

Copy link
Contributor Author

@hdelan hdelan Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the SPIRV backend is being used instead of llvm-spirv I think it'd be better to remove the llvm.compiler.used symbol in the backend, instead of in sycl-post-link, although some might disagree. I think it's OK to keep the current behaviour for the moment unless it becomes a problem later.

Copy link
Contributor

@asudarsa asudarsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me. Just one clarification requested. Thanks

Make sure that `strings` can successfully detect the symbol in an object
file.
Copy link
Contributor

@aelovikov-intel aelovikov-intel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the test to be removed, but if you really want to keep it so be it.

@hdelan
Copy link
Contributor Author

hdelan commented Sep 6, 2024

Thanks @aelovikov-intel I'd like to keep it in if possible. I can remove at any point in time if it becomes a problem. Ping @intel/llvm-gatekeepers this can be merged, thanks.

@martygrant martygrant merged commit a8c60db into intel:sycl Sep 6, 2024
13 checks passed
@sarnex
Copy link
Contributor

sarnex commented Sep 6, 2024

@hdelan The test is failing on HIP in postcommit, can you take a look and disable the test if the fix isn't quick? Thx

https://github.com/intel/llvm/actions/runs/10735485792/job/29773246908

FAIL: SYCL :: DeviceGlobal/device_global_static.cpp (413 of 2168)
******************** TEST 'SYCL :: DeviceGlobal/device_global_static.cpp' FAILED ********************
Exit Code: -6

Command Output (stdout):
--
# RUN: at line 1
/__w/llvm/llvm/toolchain/bin//clang++  -Werror -Xsycl-target-backend=amdgcn-amd-amdhsa --offload-arch=gfx1031 -fsycl -fsycl-targets=amdgcn-amd-amdhsa  /__w/llvm/llvm/llvm/sycl/test-e2e/DeviceGlobal/device_global_static.cpp -o /__w/llvm/llvm/build-e2e/DeviceGlobal/Output/device_global_static.cpp.tmp.out
# executed command: /__w/llvm/llvm/toolchain/bin//clang++ -Werror -Xsycl-target-backend=amdgcn-amd-amdhsa --offload-arch=gfx1031 -fsycl -fsycl-targets=amdgcn-amd-amdhsa /__w/llvm/llvm/llvm/sycl/test-e2e/DeviceGlobal/device_global_static.cpp -o /__w/llvm/llvm/build-e2e/DeviceGlobal/Output/device_global_static.cpp.tmp.out
# note: command had no output on stdout or stderr
# RUN: at line 2
env ONEAPI_DEVICE_SELECTOR=hip:gpu  /__w/llvm/llvm/build-e2e/DeviceGlobal/Output/device_global_static.cpp.tmp.out
# executed command: env ONEAPI_DEVICE_SELECTOR=hip:gpu /__w/llvm/llvm/build-e2e/DeviceGlobal/Output/device_global_static.cpp.tmp.out
# .---command stderr------------
# | <HIP>[ERROR]: 
# | UR HIP ERROR:
# | 	Value:           500
# | 	Name:            hipErrorNotFound
# | 	Description:     named symbol not found
# | 	Function:        getGlobalVariablePointer
# | 	Source Location: /__w/llvm/llvm/build/_deps/unified-runtime-src/source/adapters/hip/program.cpp:254
# | 
# | <HIP>[ERROR]: 
# | UR HIP ERROR:
# | 	Value:           UR_RESULT_ERROR_UNKNOWN
# | 	Function:        deviceGlobalCopyHelper
# | 	Source Location: /__w/llvm/llvm/build/_deps/unified-runtime-src/source/adapters/hip/enqueue.cpp:1691
# | 
# | terminate called after throwing an instance of 'sycl::_V1::exception'
# |   what():  Native API failed. Native API returns: 2147483646 (UR_RESULT_ERROR_UNKNOWN)
# `-----------------------------
# error: command failed with exit status: -6

@hdelan
Copy link
Contributor Author

hdelan commented Sep 9, 2024

Thanks @sarnex . Have disabled test here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants