This is an archive of the discontinued LLVM Phabricator instance.

[OpenCL] Restrict various keywords in OpenCL C++ mode
ClosedPublic

Authored by svenvh on Apr 24 2018, 9:33 AM.

Details

Summary

Restrict the following keywords in the OpenCL C++ language mode,
according to Section 2.9 of the OpenCL C++ 1.0 Specification.

  • dynamic_cast
  • typeid
  • register (already restricted in OpenCL C, update the diagnostic)
  • thread_local
  • exceptions (try/catch/throw)
  • OpenCL access qualifiers

Support the __global, __local, __constant, __private, and __generic
keywords in OpenCL C++. Leave the unprefixed address space qualifiers
such as global available, i.e., do not mark them as reserved keywords
in OpenCL C++. libclcxx provides explicit address space pointer
classes such as global_ptr and global<T> that are implemented using
the __-prefixed qualifiers.

This patch is only a first stab at implementing the restrictions of
OpenCL C++ and is by no means complete. It primarily covers
restrictions that are easily caught up to Sema.

Diff Detail

Repository
rC Clang

Event Timeline

svenvh created this revision.Apr 24 2018, 9:33 AM

I'm not sure that doing this in the lexer is appropriate; you should just diagnose the unsupported feature in Sema, or at best the parser.

svenvh updated this revision to Diff 143951.Apr 25 2018, 10:21 AM
svenvh edited the summary of this revision. (Show Details)

Implemented most of the restrictions as parser or Sema checks instead. This results in nicer diagnostics too, thanks for the suggestion!

For the address space qualifiers such as global / __global / local / ..., I still think we should differentiate them in the lexer, as "global", "local" etc. are not reserved keywords in OpenCL C++.

Yeah, if there are actually differences in the set of keywords, that's a totally reasonable thing to handle in the lexer.

include/clang/Basic/TokenKinds.def
255

KEYNOOPENCL is dead now, I think.

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

Do you really care about the spelling of the specifier? Presumably __thread (the GNU extension) and _Thread_local (the C11 feature) are unsupported; where are those diagnosed?

svenvh updated this revision to Diff 144098.Apr 26 2018, 4:39 AM
svenvh edited the summary of this revision. (Show Details)

Updated patch to reject any thread storage class specifier, not just thread_local.

Also mark the OpenCL access qualifiers as reserved keywords as per OpenCL C++ 1.0 s2.2, and add a test for those.

svenvh added inline comments.Apr 26 2018, 4:40 AM
include/clang/Basic/TokenKinds.def
255

There are still some other uses of KEYNOOPENCL (already there before this patch).

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

Fair enough, I have updated the patch to just reject any thread qualifier here.

rjmccall added inline comments.Apr 26 2018, 8:14 AM
include/clang/Basic/TokenKinds.def
255

Oh, I see.

lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

You might consider parsing the statement normally and then just diagnosing after the fact, maybe in Sema. You'd have to add the check in a couple different places, though.

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

I meant it as a question, but I take it from this response that we don't actually diagnose these.

It might make more sense to diagnose this in Sema::ActOnVariableDeclarator, where we're already doing some checking about whether e.g. the target supports the feature.

lib/Sema/SemaStmt.cpp
2791 ↗(On Diff #144098)

Is this restriction really OpenCL C++-specific? I mean, if that's what the language spec says, that's what it says, but I don't know what about OpenCL C++ would make goto unsupportable when it's supportable in ordinary OpenCL.

You should also add this check to ActOnIndirectGotoStmt.

svenvh added inline comments.Apr 26 2018, 9:26 AM
lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

Precisely the reason why I did it in the parser, otherwise we'd have to duplicate the check in multiple places.

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

We do actually diagnose the others in Sema::ActOnVariableDeclarator ("thread-local storage is not supported for the current target"). But your question made me realize there is no real need to differentiate here for the OpenCL C++ case.

lib/Sema/SemaStmt.cpp
2791 ↗(On Diff #144098)

Yes (oddly enough). The OpenCL C++ 1.0 spec explicitly mentions goto in the Restrictions section on page 51. The OpenCL C 1.1 / 2.0 specs ("ordinary OpenCL") do not restrict goto.

I'll add the check for indirect goto statements.

svenvh updated this revision to Diff 144138.Apr 26 2018, 9:29 AM

Reject goto in Sema::ActOnIndirectGotoStmt too, and add a test for indirect goto.

rjmccall added inline comments.Apr 26 2018, 9:53 AM
lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

The downside of this is that the parser recovery is probably very bad. But since this is asm, it's not likely to matter too much.

...is this *also* really only an OpenCL C++ restriction? Maybe we should read this one as a general restriction that happens to have been overlooked in the OpenCL C spec.

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

Okay. So we can remove this code, then.

svenvh added inline comments.Apr 26 2018, 10:23 AM
lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

Yes, asm is only explicitly restricted in OpenCL C++. Not sure if it's safe to assume asm has been overlooked for OpenCL C though, it's not explicitly forbidden so people may be using it.

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

Sorry if my previous comment was ambiguous: I meant here we now reject *any* thread storage class specifier for OpenCL C++, not just the thread_local spelling (my original patch was only rejecting TSCS_thread_local).

rjmccall added inline comments.Apr 26 2018, 10:48 AM
lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

Do you have any contact with the OpenCL committee? You might want to collect these issues and present them to them. They may just not be aware that asm is just as much a feature in C as it is in C++, and it would be good to understand what the purpose of the goto restriction is.

lib/Sema/DeclSpec.cpp
621 ↗(On Diff #143951)

But you said we already diagnose this in Sema, so we don't need any code here. If you want a special-case diagnostic, that's easy to do there; CUDA already does the same thing.

svenvh updated this revision to Diff 144583.Apr 30 2018, 10:16 AM

Moved thread storage class specifier diagnosing to ActOnVariableDeclarator.

svenvh added inline comments.Apr 30 2018, 10:18 AM
lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

Yes, we can clarify this with Khronos. Shall I leave the asm and goto restrictions out of this patch for now until we have clarification?

rjmccall added inline comments.Apr 30 2018, 1:59 PM
lib/Parse/ParseStmtAsm.cpp
696 ↗(On Diff #144098)

That might be best. It'd be easy enough to apply them later.

lib/Sema/SemaDecl.cpp
6371

This is another thing to check out with Khronos, because _Thread_local and __thread are also available in C.

In this case, I think it's almost certain that they intend the restriction to be general, so I'd just proactively apply it in an OpenCL modes.

svenvh updated this revision to Diff 144745.May 1 2018, 10:10 AM
svenvh edited the summary of this revision. (Show Details)

Dropped the asm and goto restrictions from this patch for now until we have clarification from Khronos.

Applied the thread_local restrictions to OpenCL C too.

rjmccall accepted this revision.May 1 2018, 12:18 PM

Alrght, LGTM.

This revision is now accepted and ready to land.May 1 2018, 12:18 PM
yaxunl accepted this revision.May 2 2018, 10:26 AM

LGTM. Thanks!

This revision was automatically updated to reflect the committed changes.