printf.cu 1.7 KB

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