[PATCH] D101519: [C++4OpenCL] Fix reinterpret_cast of vectors and types with address spaces

Anastasia Stulova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 30 06:19:05 PDT 2021


Anastasia added a subscriber: rjmccall.
Anastasia added inline comments.


================
Comment at: clang/lib/Sema/SemaCast.cpp:2352
+  if (SrcType == DestType ||
+      (Self.LangOpts.OpenCL &&
+       Self.Context.removeAddrSpaceQualType(SrcType) ==
----------------
I am thinking of whether we can allow something weird to be written by relaxing this address space case

```
    local int i;
    const global int & ii = reinterpret_cast<global int>(i);
```

I think this code is completely meaningless as it requires creating a temporary in global address space? I am not sure what we will even emit in IR?

Embedded C doesn't regulate the conversions between non-pointer types with different address spaces but in OpenCL C we have allowed them apparently https://godbolt.org/z/9f75zxj7v. I am not sure whether this was done intentionally or not.

However, in C there are no references and other features that might create problems.

At the same time it seems C++ allows casting away `const` https://godbolt.org/z/fo3axbq3e

But it contradicts the comment below:

```
    // 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,
    // since all other paragraphs explicitly forbid casts to the same type.
    // C++11 clarifies this case with p2.
```

I would like to see if @rjmccall has any opinion on whether reinterpreting between non-pointer types with different address spaces is reasonable?


================
Comment at: clang/test/SemaOpenCLCXX/reinterpret-cast.clcpp:1
+// RUN: %clang_cc1 %s -pedantic -verify -fsyntax-only
+
----------------
Btw I assume your patch also allows reinterpret_cast between vectors? What happens if we cast b/w vector of 3 elements and vector of 4 elements?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101519/new/

https://reviews.llvm.org/D101519



More information about the cfe-commits mailing list