This is an archive of the discontinued LLVM Phabricator instance.

[clang] Replace asm with __asm__ in cuda header
ClosedPublic

Authored by JonChesterfield on Aug 4 2021, 11:38 AM.

Details

Summary

Asm is a gnu extension for C, so at present -fopenmp -std=c99
and similar fail to compile on nvptx, bug 51344

Changing to __asm__ or __asm works for openmp, all three appear to work
for cuda. Suggesting __asm__ here as __asm is used by MSVC with different
syntax, so this should make for better error diagnostics if the header is
passed to a compiler other than clang.

Diff Detail

Event Timeline

JonChesterfield requested review of this revision.Aug 4 2021, 11:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2021, 11:38 AM

Implemented by
F=__clang_cuda_device_functions.h ; sed -i 's$asm$__asm__$g' $F && clang-format -i $F

tra accepted this revision.Aug 4 2021, 1:08 PM

LGTM in general.

clang/lib/Headers/__clang_cuda_device_functions.h
37

Should we also change volatile -> __volatile__ here in other places in the file?

This revision is now accepted and ready to land.Aug 4 2021, 1:08 PM
emankov requested changes to this revision.Aug 4 2021, 3:10 PM
emankov added inline comments.
clang/lib/Headers/__clang_cuda_device_functions.h
1043

Unneeded formatting.

1057

Tabs are not allowed, please use whitespaces instead.

This revision now requires changes to proceed.Aug 4 2021, 3:10 PM

Re: __volatile__. I've done some background reading on it but can't find the original motivation. __asm__ exists because the asm word is in the application namespace and asm is an extension. Volatile has been with us since C89 though, and is also accepted under -ansi. I haven't been able to find a set of compiler flags that reject it.

Cuda however uses volatile to mean a different thing to C as it is part of the atomic model. From poking at -x cuda in godbolt it looks like clang lowers cuda volatile to IR volatile, and not to relaxed atomic. I'm not confident that's correct, in particular i++ on a volatile variable turns into a load and a store, not an atomicrmw. That's orthogonal to this particular change but does make me reluctant to touch the word 'volatile' in anything that is compiled as cuda.

Therefore I'd like to leave it as __asm__ volatile.

clang/lib/Headers/__clang_cuda_device_functions.h
37

Replying at the end

1043

Correct formatting though. This is what clang-format did to the whole file. I'll revert the space in favour of git-clang-format if you prefer

1057

I've been there! It's not a tab, that's the symbol phabricator unhelpfully uses to indicate whitespace change. In this case, four extra spaces to keep the : lined up with the brace

  • whitespace change
JonChesterfield added inline comments.Aug 4 2021, 3:34 PM
clang/lib/Headers/__clang_cuda_device_functions.h
1043

git-clang-format also wanted to insert the space here and I then had to use -nolint. However hopefully that removes an async round trip in the review chain. Some of the previous lines were 81 characters. I suppose we could apply a whitespace-only clang-format patch first but it doesn't seem worth the time.

emankov added inline comments.Aug 4 2021, 3:37 PM
clang/lib/Headers/__clang_cuda_device_functions.h
1043

The point is that this formatting is not related to the change.

tra added a comment.Aug 4 2021, 3:41 PM

Therefore I'd like to leave it as __asm__ volatile.

Being the one who introduced inconsistent use of __volatile__ and volatile in this header, I'm pretty sure that PTX's notion of volatile is not involved for inline asm of trap and brkpt. It should be fine changing them to __volatile__ for the sake of uniformity.
Up to you.

Therefore I'd like to leave it as __asm__ volatile.

Being the one who introduced inconsistent use of __volatile__ and volatile in this header, I'm pretty sure that PTX's notion of volatile is not involved for inline asm of trap and brkpt. It should be fine changing them to __volatile__ for the sake of uniformity.
Up to you.

Ah, I had missed that we already had an instance of __volatile__. We have one __volatile__ and three volatile. I like consistency and it seems you would prefer '__' to '', will patch that at the same time.

  • consistent volatile
emankov accepted this revision.Aug 5 2021, 1:37 AM
This revision is now accepted and ready to land.Aug 5 2021, 1:37 AM
This revision was landed with ongoing or failed builds.Aug 5 2021, 10:47 AM
This revision was automatically updated to reflect the committed changes.