This is an archive of the discontinued LLVM Phabricator instance.

[OpenCL] Add generic type handling for builtin functions
ClosedPublic

Authored by svenvh on Jul 30 2019, 9:30 AM.

Details

Summary

Generic types are an abstraction of type sets. It mimics the way
functions are defined in the OpenCL specification. For example,
floatN can abstract all the vector sizes of the float type.

This allows to

  • stick more closely to the specification, which uses generic types;
  • factorize definitions of functions with numerous prototypes in the tablegen file; and
  • reduce the memory impact of functions with many overloads.

Patch by Pierre Gondois and Sven van Haastregt.

Continuation of https://reviews.llvm.org/D63434

Diff Detail

Event Timeline

svenvh created this revision.Jul 30 2019, 9:30 AM
Herald added a project: Restricted Project. · View Herald Transcript

Main changes since D63434:

  • Rename List* to Vec*.
  • Rename TLnn -> TLAll, TLInt, TLFloat.
  • Apply clang-format.
  • Improve/update documentation.
  • Factor out renaming of base types into separate commit.
  • Change return type of OCL2Qual.
  • Remove default case from OCL2Qual switch statement: it should be fully covering the enum.
Anastasia added inline comments.Aug 1 2019, 7:38 AM
clang/lib/Sema/OpenCLBuiltins.td
51

Are the qualifiers added elsewhere?

246

Is this float GenType?

247

Would it make sense to use the same name scheme as below?

clang/lib/Sema/SemaLookup.cpp
675–676

Btw you are not marking param as (in) or (out) here.

679

What if both return and args use GenType, does GenTypeMaxCnt return max of all? Or do they have to align?

707

I don't get this logic?

809–812

I guess this should be done conditionally for C++ mode. But perhaps we don't have to do this now. It might be worth adding a FIXME.

clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
70

Perhaps not something for this patch, but I am wondering if a better name for this class would be OpenCLBuiltinTrieEmitter?

94

I am a bit confused about the purpose of this function as input and output seem to be both vectors of the Record. It might make sense to explain the difference. :)

220

I find this comment confusing... may be because it belongs to the code later. I am not sure it adds any value.

322

Is this logic to avoid duplicate signatures? If so it might be worth explaining it.

419

I feel it would be good to explain the structure of the function we are trying to generate i.e something like:

  • We first generate switch/case statement over all type names that populates the list of type IDs
  • Then we walk over the type IDs from the list and map them to the AST type and attributes accordingly.
451

I am not sure what you are trying to say about ExtVector...

504

I am really confused as the above comment on line 432 makes examples of non scalar types too i.e. int4.

svenvh marked 23 inline comments as done.Aug 7 2019, 8:19 AM
svenvh added a subscriber: Pierre.
svenvh added inline comments.
clang/lib/Sema/OpenCLBuiltins.td
51

Good point, this change should be part of the next patch (D63442). I've taken it out of this patch.

246

We will be adding more definitions for integers here in followup patches.

247

No, because FGenType is for all floating point types (half / float / double), as it uses the TLFloat type list. GenTypeFloatVecAndScalar is for the float type (i.e. fp32) only.

I will update the comments of both definitions to clarify the difference.

clang/lib/Sema/SemaLookup.cpp
679

They have to be compatible, otherwise we will hit the llvm_reachable below.

707

While trying to clarify this, I realized this checking should probably be moved to the TableGen emitter, as this is checking validity of the .td input so ideally we should check that at compiler compile time. @Pierre mentioned that this might not be trivial, but I'll have a look at it.

clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
70

It's emitting more than a trie; also tables and structs. How about OpenCLBuiltinEmitter (which aligns nicely with the file name)?

94

Clarified comment.

220

Elaborated.

322

Added comment.

419

Added comment.

451

Rephrased.

504

I guess you got confused about the two different steps; I have rephrased the comment on line 432.

svenvh updated this revision to Diff 213904.Aug 7 2019, 8:21 AM
svenvh marked 9 inline comments as done.

Addressing review comments.

Pierre added a comment.Aug 8 2019, 3:18 AM

Just one comment,
Thank you again for making everything clearer :)

clang/lib/Sema/OpenCLBuiltins.td
116

Maybe it would be worth adding more information on how to use GenTypes. I was thinking of something like this :

When using multiple generic types :

  • The maximal cardinal of the used GenTypes must be the PGCM of all the cardinals.
  • The generic types combine as if it was an array like GenType[Type][VecSize].

I.e: With GT1 = [half, <1, 2>] and GT2 = [float, int, <1, 2>], they will combine as
<half, float>, <half2, float2>, <half, int>, <half, int2>
The maximal cardinal of GT1 and GT2 is 4 (= 2 types * 2 vector sizes).
4 is the PGCM of GT1 (=2) and GT2 (=4).

svenvh updated this revision to Diff 214169.Aug 8 2019, 9:08 AM
  • Move checking of GenType compatibility from SemaLookup to TableGen emitter.
  • Provide more elaborate explanation about combining GenTypes in a declaration.
  • Add max/min definitions to cover "sgentype" behavior and add test for max.
  • Minor scattered comment improvements.

Combining GenTypes in an incorrect way will now produce an error from TableGen, e.g.:

OpenCLBuiltins.td:1601:1: error: number of vector sizes should be equal or 1 for all gentypes in a declaration
def : Builtin<"crashme", [GenTypeFloatVecNoScalar, GenTypeFloatVecAndScalar]>
^

OpenCLBuiltins.td:1602:1: error: number of types should be equal or 1 for all gentypes in a declaration
def : Builtin<"crashme2", [IGenTypeN, FGenTypeN]>;
^
svenvh marked 6 inline comments as done.Aug 8 2019, 9:15 AM
svenvh added inline comments.
clang/lib/Sema/OpenCLBuiltins.td
116

I've added some rules about combining GenTypes in the comment below based on your example.

clang/lib/Sema/SemaLookup.cpp
707

The checking has been moved into ClangOpenCLBuiltinEmitter.cpp now, see VerifySignature.

809–812

Done.

Anastasia added inline comments.Aug 13 2019, 9:16 AM
clang/lib/Sema/OpenCLBuiltins.td
124

What does combining mean?

247

Ok, it could though be

GenTypeAllFloats

GenTypeF

Just that naming scheme feels a bit random right now.

clang/lib/Sema/SemaLookup.cpp
679

Do we say anywhere they have to be compatible?

clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
70

Yeah, makes sense!

261

I feel what you are checking is whether the vector sizes and types match but not just that the number of types match...

265

Can we rename B to something meaningful or add a comment explaining it?

269

I feel there is some black magic here. :)

svenvh updated this revision to Diff 215086.Aug 14 2019, 5:16 AM
svenvh marked 15 inline comments as done.
  • Update comments as per review comments.
  • Rename iterator List to VecSizes in OpenCLBuiltins.td
  • Format GenericType definition.
clang/lib/Sema/OpenCLBuiltins.td
124

Clarified.

247

The convention is a bit subtle perhaps, but it is not random. For manual definitions that have multiple base types, the convention is typeinfo#GenType#vecinfo. For generated definitions that have a single base type, the convention is GenType#typeinfo#vecinfo. Having different conventions prevents the manual definitions from colliding with the generated ones.

clang/lib/Sema/SemaLookup.cpp
679

Yes, in OpenCLBuiltins.td near the definition of GenericType.

clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
261

"actual types" here means "final" non-generic types, such as half2, int, short4, etc.

Rephrased as "actual scalar or vector types", does that help?

265

Renamed to BuiltinRec.

269

Added some comments.

Anastasia accepted this revision.Aug 19 2019, 3:45 AM

LGTM! Thanks!

This revision is now accepted and ready to land.Aug 19 2019, 3:45 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2019, 4:55 AM