xref: /llvm-project/clang/test/CodeGenOpenCLCXX/constexpr.clcpp (revision 8e954541581270c497cc961b08eff69dc41bc18d)
1// RUN: %clang_cc1 %s -triple spir-unknown-unknown -O0 -emit-llvm -o - | FileCheck %s
2// RUN: %clang_cc1 %s -triple spir-unknown-unknown -O0 -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s
3
4typedef int int2 __attribute__((ext_vector_type(2)));
5typedef int int4 __attribute__((ext_vector_type(4)));
6
7struct Storage final {
8  constexpr const float& operator[](const int index) const noexcept {
9    return InternalStorage[index];
10  }
11
12  const float InternalStorage[1];
13};
14
15constexpr Storage getStorage() {
16  return Storage{{1.0f}};
17}
18
19constexpr float compute() {
20  constexpr auto s = getStorage();
21  return 2.0f / (s[0]);
22}
23
24constexpr float FloatConstant = compute();
25
26// CHECK-LABEL: define{{.*}} spir_kernel void @foo
27// CHECK: store float 2.000000e+00
28kernel void foo(global float *x) {
29  *x = FloatConstant;
30}
31
32// Test evaluation of constant vectors.
33// CHECK-LABEL: define{{.*}} spir_kernel void @vecEval
34// CHECK: store i32 3
35// CHECK: store <2 x i32> <i32 22, i32 33>, ptr
36
37const int oneElt = int4(3).x;
38const int2 twoElts = (int4)(11, 22, 33, 44).yz;
39
40kernel void vecEval(global int *x, global int2 *y) {
41  *x = oneElt;
42  *y = twoElts;
43}
44
45// Test evaluation of vectors initialized through a constexpr function.
46// CHECK-LABEL: define{{.*}} spir_kernel void @vecEval2
47// CHECK: store <2 x i32>
48constexpr int2 addOne(int2 x) {
49  return (int2)(x.x + 1, x.y + 1);
50}
51const int2 fromConstexprFunc = addOne(int2(2));
52
53kernel void vecEval2(global int2 *x) {
54  *x = fromConstexprFunc;
55}
56
57// Test evaluation of vec_step
58// CHECK-LABEL: define{{.*}} spir_kernel void @vec_step_test
59// CHECK: store i32 6
60constexpr int vsize1 = vec_step(fromConstexprFunc);
61constexpr int vsize2 = vec_step(int4);
62
63kernel void vec_step_test(global int *x) {
64  *x = vsize1 + vsize2;
65}
66