[PATCH] D102689: [C++4OpenCL] Allow address space conversion in reinterpret_cast

Ole Strohm via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 18 07:00:22 PDT 2021


olestrohm created this revision.
olestrohm added reviewers: Anastasia, rjmccall.
olestrohm added a project: clang.
Herald added subscribers: ldrumm, yaxunl.
olestrohm requested review of this revision.
Herald added a subscriber: cfe-commits.

Allow converting between types with different address spaces.

This allows converting a `__private int` to a `__private int`, which is currently not possible.
It also allows converting a value to any other address space, which can make sense since
the actual bit representation of the values doesn't depend on the address space.

This is the solution I chose currently allows converting between any address space
because the address space of `SrcExpr` is erased before entering the function,
so allowing only converting when the address spaces is the same would require larger changes.

I'm not sure if this conversion should be allowed, though converting to the exact same type definitely should.

Fixes the first issue in PR49221


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102689

Files:
  clang/lib/Sema/SemaCast.cpp
  clang/test/SemaOpenCLCXX/reinterpret-cast.clcpp


Index: clang/test/SemaOpenCLCXX/reinterpret-cast.clcpp
===================================================================
--- clang/test/SemaOpenCLCXX/reinterpret-cast.clcpp
+++ clang/test/SemaOpenCLCXX/reinterpret-cast.clcpp
@@ -13,6 +13,11 @@
   auto i2_to_i = reinterpret_cast<int>(i2); // expected-error{{reinterpret_cast from vector 'int2' (vector of 2 'int' values) to scalar 'int' of different size}}
   auto i2_to_i2 = reinterpret_cast<int2>(i2);
 
+  __private short s;
+  auto s2 = reinterpret_cast<__private short>(s);
+  auto s3 = reinterpret_cast<decltype(s)>(s);
+  auto s4 = reinterpret_cast<__global short>(s);
+
   // Only integral types (and pointer/references) can be reinterpret casted to themselves.
   // Currently this does not include any opencl types.
   reserve_id_t r_id1;
Index: clang/lib/Sema/SemaCast.cpp
===================================================================
--- clang/lib/Sema/SemaCast.cpp
+++ clang/lib/Sema/SemaCast.cpp
@@ -2356,7 +2356,10 @@
     return TC_Failed;
   }
 
-  if (SrcType == DestType) {
+  if (SrcType == DestType ||
+      (Self.LangOpts.OpenCL &&
+        Self.Context.removeAddrSpaceQualType(SrcType) ==
+          Self.Context.removeAddrSpaceQualType(DestType))) {
     // C++ 5.2.10p2 has a note that mentions that, subject to all other
     // restrictions, a cast to the same type is allowed so long as it does not
     // cast away constness. In C++98, the intent was not entirely clear here,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102689.346155.patch
Type: text/x-patch
Size: 1465 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210518/44f87a8f/attachment-0001.bin>


More information about the cfe-commits mailing list