[PATCH] D71015: [OpenCL] Handle address space conversions for constexpr (PR44177)

Sven van Haastregt via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 4 07:03:29 PST 2019


svenvh created this revision.
svenvh added a reviewer: Anastasia.
Herald added subscribers: ebevhan, yaxunl.
Herald added a project: clang.

The AST for the `constexpr.cl` test contains address space conversion
nodes to cast through the implicit generic address space.  These
caused the evaluator to reject the input as `constexpr` in C++ for
OpenCL mode, whereas the input was considered `constexpr` in plain C++
mode.


Repository:
  rC Clang

https://reviews.llvm.org/D71015

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/CodeGenOpenCLCXX/address-space-deduction.cl
  clang/test/CodeGenOpenCLCXX/constexpr.cl


Index: clang/test/CodeGenOpenCLCXX/constexpr.cl
===================================================================
--- /dev/null
+++ clang/test/CodeGenOpenCLCXX/constexpr.cl
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 %s -triple spir-unknown-unknown -cl-std=clc++ -O0 -emit-llvm -o - | FileCheck %s
+
+struct Storage final {
+  constexpr const float& operator[](const int index) const noexcept {
+    return InternalStorage[index];
+  }
+
+  const float InternalStorage[1];
+};
+
+constexpr Storage getStorage() {
+  return Storage{{1.0f}};
+}
+
+constexpr float compute() {
+  constexpr auto s = getStorage();
+  return 2.0f / (s[0]);
+}
+
+constexpr float FloatConstant = compute();
+
+// CHECK-LABEL: define spir_kernel void @foo
+// CHECK: store float 2.000000e+00
+kernel void foo(global float *x) {
+  *x = FloatConstant;
+}
Index: clang/test/CodeGenOpenCLCXX/address-space-deduction.cl
===================================================================
--- clang/test/CodeGenOpenCLCXX/address-space-deduction.cl
+++ clang/test/CodeGenOpenCLCXX/address-space-deduction.cl
@@ -12,17 +12,15 @@
 //COMMON: @glob = addrspace(1) global i32
 int glob;
 //PTR: @glob_p = addrspace(1) global i32 addrspace(4)* addrspacecast (i32 addrspace(1)* @glob to i32 addrspace(4)*)
-//REF: @glob_p = addrspace(1) global i32 addrspace(4)* null
+//REF: @glob_p = addrspace(1) constant i32 addrspace(4)* addrspacecast (i32 addrspace(1)* @glob to i32 addrspace(4)*)
 int PTR glob_p = ADR(glob);
 
 //COMMON: @_ZZ3fooi{{P|R}}U3AS4iE6loc_st = internal addrspace(1) global i32
 //PTR: @_ZZ3fooiPU3AS4iE8loc_st_p = internal addrspace(1) global i32 addrspace(4)* addrspacecast (i32 addrspace(1)* @_ZZ3fooiPU3AS4iE6loc_st to i32 addrspace(4)*)
-//REF: @_ZZ3fooiRU3AS4iE8loc_st_p = internal addrspace(1) global i32 addrspace(4)* null
+//REF: @_ZZ3fooiRU3AS4iE8loc_st_p = internal addrspace(1) constant i32 addrspace(4)* addrspacecast (i32 addrspace(1)* @_ZZ3fooiRU3AS4iE6loc_st to i32 addrspace(4)*)
 //COMMON: @loc_ext_p = external addrspace(1) {{global|constant}} i32 addrspace(4)*
 //COMMON: @loc_ext = external addrspace(1) global i32
 
-//REF: store i32 addrspace(4)* addrspacecast (i32 addrspace(1)* @glob to i32 addrspace(4)*), i32 addrspace(4)* addrspace(1)* @glob_p
-
 //COMMON: define spir_func i32 @_Z3fooi{{P|R}}U3AS4i(i32 %par, i32 addrspace(4)*{{.*}} %par_p)
 int foo(int par, int PTR par_p){
   //COMMON: %loc = alloca i32
@@ -37,7 +35,6 @@
 
   // CHECK directives for the following code are located above.
   static int loc_st;
-  //REF: store i32 addrspace(4)* addrspacecast (i32 addrspace(1)* @_ZZ3fooiRU3AS4iE6loc_st to i32 addrspace(4)*), i32 addrspace(4)* addrspace(1)* @_ZZ3fooiRU3AS4iE8loc_st_p
   static int PTR loc_st_p = ADR(loc_st);
   extern int loc_ext;
   extern int PTR loc_ext_p;
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -7087,6 +7087,13 @@
         return false;
       return DerivedSuccess(DestValue, E);
     }
+
+    case CK_AddressSpaceConversion: {
+      APValue Value;
+      if (!Evaluate(Value, Info, E->getSubExpr()))
+        return false;
+      return DerivedSuccess(Value, E);
+    }
     }
 
     return Error(E);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71015.232127.patch
Type: text/x-patch
Size: 3273 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191204/a1fca8ec/attachment-0001.bin>


More information about the cfe-commits mailing list