Skip to content

[SYCL] Add __builtin_num_fields and __builtin_field_type #3658

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 21 commits into from
Jun 4, 2021

Conversation

AaronBallman
Copy link
Contributor

This is an experimental branch to see whether we can implement the
device copyability checking functionality within the SYCL library by
using some builtins to provide super rudimentary introspection of
structures and lambdas. These builtins will tell you how many fields
are in the structure or how many captures are in the lambda, and tell
you what types those fields are.

__builtin_num_fields() accepts a type id and returns the number of
fields in the given type as an integer constant expression.
__builtin_field_type() accepts a type id and an integer constant
expression and returns a "value" of the field at the given offset from
the given type. The call should always be wrapped in a decltype (or
similar) expression to get the actual type of the field. The value
returned is meaningless and it is UB to inspect it. Note: if we wish to
upstream any of this functionality, this builtin should be replaced by
a new type in the type system, akin to how decltype and typeof work.

More work is still needed for the SYCL library to fully support the
checking in that there is a need to get the number of base classes and
the base class type information.

This is an experimental branch to see whether we can implement the
device copyability checking functionality within the SYCL library by
using some builtins to provide super rudimentary introspection of
structures and lambdas. These builtins will tell you how many fields
are in the structure or how many captures are in the lambda, and tell
you what types those fields are.

__builtin_num_fields() accepts a type id and returns the number of
fields in the given type as an integer constant expression.
__builtin_field_type() accepts a type id and an integer constant
expression and returns a "value" of the field at the given offset from
the given type. The call should always be wrapped in a decltype (or
similar) expression to get the actual type of the field. The value
returned is meaningless and it is UB to inspect it. Note: if we wish to
upstream any of this functionality, this builtin should be replaced by
a new type in the type system, akin to how decltype and typeof work.

More work is still needed for the SYCL library to fully support the
checking in that there is a need to get the number of base classes and
the base class type information.
@AaronBallman AaronBallman requested a review from erichkeane April 29, 2021 12:28
Copy link
Contributor

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

just a couple of little things. Would love ot see some sort of test with dependent lambdas, but I can't come up with any situations where I think they would matter (besides just being able to check on them).

This saves 8 bytes from the AST node.
NOTE: this has a fair amount of code duplication that should be cleaned
up if we're happy with the experiment. The num_bases and num_fields AST
nodes are basically the same thing and so those could be differentiated
with a single bit in the AST node. A common base class might also be a
reasonable approach.
@AaronBallman
Copy link
Contributor Author

The remaining clang-format issues are not ones I am willing to fix. The formatting in RecursiveASTVisitor matches the surrounding code and would make the code harder to read instead of easier. The formatting in ItaniumMangle is existing formatting in upstream and serves to make it easier to append additional cases with less diff marking.

Returning Loc instead of making a call to do it
Changed the names of some diagnostics to be more agnostic
Changed the name of a parameter to be more bool-like
Fixed up some assertions that were doing too much work
We don't need to store this information *twice* -- the expression type
itself is already storing the same information, so just rely on that.
This allows experimentation with the new builtins.
@AaronBallman AaronBallman marked this pull request as ready for review May 26, 2021 18:49
@AaronBallman
Copy link
Contributor Author

#3794 validated the design, so I think this is now ready for review.

@AaronBallman
Copy link
Contributor Author

Ping

Copy link
Contributor

@elizabethandrews elizabethandrews left a comment

Choose a reason for hiding this comment

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

I'm sorry for the delay. It took me a while to go through the patch. It looks ok to me barring a few nits. Having said that, I don't feel very qualified to review this and so I'm hoping @erichkeane and/or @premanandrao can also take a look.

Copy link
Contributor

@elizabethandrews elizabethandrews left a comment

Choose a reason for hiding this comment

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

LGTM! @erichkeane and/or @premanandrao please confirm as well.

@bader bader requested a review from erichkeane June 2, 2021 15:09
erichkeane
erichkeane previously approved these changes Jun 2, 2021
@premanandrao
Copy link
Contributor

Ping

I have started looking at this one @AaronBallman, sorry for the delay.

Copy link
Contributor

@premanandrao premanandrao left a comment

Choose a reason for hiding this comment

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

@AaronBallman, I just have a couple nits. My review was mostly my trying to understand these changes, some in areas that I am not familiar with.

Also, clang-format does not seem to like the formatting in a couple of files.

if (!Idx->isValueDependent()) {
Optional<llvm::APSInt> IdxVal = Idx->getIntegerConstantExpr(Context);
if (IdxVal) {
CXXRecordDecl *RD = SourceTy->getAsCXXRecordDecl();
Copy link
Contributor

Choose a reason for hiding this comment

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

auto here?

@AaronBallman
Copy link
Contributor Author

Also, clang-format does not seem to like the formatting in a couple of files.

Yup, clang-format can get over itself. :-D It's barking about following existing style patterns in the file, so I don't intend to fix those.

@AaronBallman AaronBallman dismissed stale reviews from erichkeane and elizabethandrews via 510357d June 2, 2021 19:50
@AaronBallman
Copy link
Contributor Author

Build bot failures are unrelated to the changes in the patch:

Cloning into 'OpenCL-Headers'...
fatal: unable to access 'https://github.com/KhronosGroup/OpenCL-Headers/': Couldn't connect to server
Traceback (most recent call last):
  File "/localdisk2/sycl_ci/buildbot/worker/sycl-ubu-x64-pr/llvm.src/buildbot/dependency.py", line 124, in <module>
    ret = main()
  File "/localdisk2/sycl_ci/buildbot/worker/sycl-ubu-x64-pr/llvm.src/buildbot/dependency.py", line 121, in main
    return do_dependency(args)
  File "/localdisk2/sycl_ci/buildbot/worker/sycl-ubu-x64-pr/llvm.src/buildbot/dependency.py", line 54, in do_dependency
    subprocess.check_call(clone_cmd, cwd=args.obj_dir)
  File "/usr/local/lib/python3.7/subprocess.py", line 347, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['git', 'clone', 'https://github.com/KhronosGroup/OpenCL-Headers', 'OpenCL-Headers', '-b', 'master']' returned non-zero exit status 128.

premanandrao
premanandrao previously approved these changes Jun 3, 2021
Copy link
Contributor

@premanandrao premanandrao left a comment

Choose a reason for hiding this comment

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

LGTM, hopefully the conflicts are a simple merge.

@AaronBallman
Copy link
Contributor Author

LGTM, hopefully the conflicts are a simple merge.

The merge conflicts were from Erich's builtin unique stable name. It turns out that landing two different new builtins at the same time has some conflicts. ;-) Thankfully, they were all easy to resolve.

@bader bader merged commit 525d253 into intel:sycl Jun 4, 2021
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.

6 participants