1 // REQUIRES: x86-registered-target
2 // REQUIRES: nvptx-registered-target
3 
4 // RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device -emit-llvm \
5 // RUN:   -o - %s | FileCheck %s
6 
7 #include "Inputs/cuda.h"
8 
9 extern "C" __device__ int vprintf(const char*, const char*);
10 
11 // Check a simple call to printf end-to-end.
12 // CHECK: [[SIMPLE_PRINTF_TY:%[a-zA-Z0-9_]+]] = type { i32, i64, double }
CheckSimple()13 __device__ int CheckSimple() {
14   // CHECK: [[BUF:%[a-zA-Z0-9_]+]] = alloca [[SIMPLE_PRINTF_TY]]
15   // CHECK: [[FMT:%[0-9]+]] = load{{.*}}%fmt
16   const char* fmt = "%d %lld %f";
17   // CHECK: [[PTR0:%[0-9]+]] = getelementptr inbounds [[SIMPLE_PRINTF_TY]], [[SIMPLE_PRINTF_TY]]* [[BUF]], i32 0, i32 0
18   // CHECK: store i32 1, i32* [[PTR0]], align 4
19   // CHECK: [[PTR1:%[0-9]+]] = getelementptr inbounds [[SIMPLE_PRINTF_TY]], [[SIMPLE_PRINTF_TY]]* [[BUF]], i32 0, i32 1
20   // CHECK: store i64 2, i64* [[PTR1]], align 8
21   // CHECK: [[PTR2:%[0-9]+]] = getelementptr inbounds [[SIMPLE_PRINTF_TY]], [[SIMPLE_PRINTF_TY]]* [[BUF]], i32 0, i32 2
22   // CHECK: store double 3.0{{[^,]*}}, double* [[PTR2]], align 8
23   // CHECK: [[BUF_CAST:%[0-9]+]] = bitcast [[SIMPLE_PRINTF_TY]]* [[BUF]] to i8*
24   // CHECK: [[RET:%[0-9]+]] = call i32 @vprintf(i8* [[FMT]], i8* [[BUF_CAST]])
25   // CHECK: ret i32 [[RET]]
26   return printf(fmt, 1, 2ll, 3.0);
27 }
28 
CheckNoArgs()29 __device__ void CheckNoArgs() {
30   // CHECK: call i32 @vprintf({{.*}}, i8* null){{$}}
31   printf("hello, world!");
32 }
33 
34 // Check that printf's alloca happens in the entry block, not inside the if
35 // statement.
36 __device__ bool foo();
CheckAllocaIsInEntryBlock()37 __device__ void CheckAllocaIsInEntryBlock() {
38   // CHECK: alloca %printf_args
39   // CHECK: call {{.*}} @_Z3foov()
40   if (foo()) {
41     printf("%d", 42);
42   }
43 }
44