1 // REQUIRES: x86-registered-target
2 // REQUIRES: nvptx-registered-target
3 
4 // RUN: %clang_cc1 -std=c++11 -fcuda-is-device -triple nvptx64-nvidia-cuda -emit-llvm -o - %s | FileCheck --check-prefix=DEVICE %s
5 // RUN: echo "GPU binary would be here" > %t
6 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-linux-gnu -target-sdk-version=8.0 -fcuda-include-gpubinary %t -emit-llvm -o - %s | FileCheck --check-prefix=HOST %s
7 
8 struct textureReference {
9   int desc;
10 };
11 
12 enum ReadMode {
13   ElementType = 0,
14   NormalizedFloat = 1
15 };
16 
17 template <typename T, int dim = 1, enum ReadMode mode = ElementType>
18 struct __attribute__((device_builtin_texture_type)) texture : public textureReference {
19 };
20 
21 // On the device side, texture references are represented as `i64` handles.
22 // DEVICE: @tex ={{.*}} addrspace(1) externally_initialized global i64 undef, align 4
23 // DEVICE: @norm ={{.*}} addrspace(1) externally_initialized global i64 undef, align 4
24 // On the host side, they remain in the original type.
25 // HOST: @tex = internal global %struct.texture
26 // HOST: @norm = internal global %struct.texture
27 // HOST: @0 = private unnamed_addr constant [4 x i8] c"tex\00"
28 // HOST: @1 = private unnamed_addr constant [5 x i8] c"norm\00"
29 texture<float, 2, ElementType> tex;
30 texture<float, 2, NormalizedFloat> norm;
31 
32 struct v4f {
33   float x, y, z, w;
34 };
35 
36 __attribute__((device)) v4f tex2d_ld(texture<float, 2, ElementType>, float, float) asm("llvm.nvvm.tex.unified.2d.v4f32.f32");
37 __attribute__((device)) v4f tex2d_ld(texture<float, 2, NormalizedFloat>, int, int) asm("llvm.nvvm.tex.unified.2d.v4f32.s32");
38 
39 // DEVICE-LABEL: float @_Z3fooff(float noundef %x, float noundef %y)
40 // DEVICE: call i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)* @tex)
41 // DEVICE: call %struct.v4f @llvm.nvvm.tex.unified.2d.v4f32.f32(i64 %{{.*}}, float noundef %{{.*}}, float noundef %{{.*}})
42 // DEVICE: call i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)* @norm)
43 // DEVICE: call %struct.v4f @llvm.nvvm.tex.unified.2d.v4f32.s32(i64 %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}})
44 __attribute__((device)) float foo(float x, float y) {
45   return tex2d_ld(tex, x, y).x + tex2d_ld(norm, int(x), int(y)).x;
46 }
47 
48 // HOST: define internal void @[[PREFIX:__cuda]]_register_globals
49 // Texture references need registering with correct arguments.
50 // HOST: call void @[[PREFIX]]RegisterTexture(i8** %0, i8*{{.*}}({{.*}}@tex{{.*}}), i8*{{.*}}({{.*}}@0{{.*}}), i8*{{.*}}({{.*}}@0{{.*}}), i32 2, i32 0, i32 0)
51 // HOST: call void @[[PREFIX]]RegisterTexture(i8** %0, i8*{{.*}}({{.*}}@norm{{.*}}), i8*{{.*}}({{.*}}@1{{.*}}), i8*{{.*}}({{.*}}@1{{.*}}), i32 2, i32 1, i32 0)
52 
53 // They also need annotating in metadata.
54 // DEVICE: !0 = !{i64 addrspace(1)* @tex, !"texture", i32 1}
55 // DEVICE: !1 = !{i64 addrspace(1)* @norm, !"texture", i32 1}
56