Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/CodeGen/CGGPUBuiltin.cpp
Line
Count
Source
1
//===------ CGGPUBuiltin.cpp - Codegen for GPU builtins -------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Generates code for built-in GPU calls which are not runtime-specific.
10
// (Runtime-specific codegen lives in programming model specific files.)
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "CodeGenFunction.h"
15
#include "clang/Basic/Builtins.h"
16
#include "llvm/IR/DataLayout.h"
17
#include "llvm/IR/Instruction.h"
18
#include "llvm/Support/MathExtras.h"
19
20
using namespace clang;
21
using namespace CodeGen;
22
23
9
static llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
24
9
  llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
25
9
                            llvm::Type::getInt8PtrTy(M.getContext())};
26
9
  llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
27
9
      llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
28
9
29
9
  if (auto* F = M.getFunction("vprintf")) {
30
6
    // Our CUDA system header declares vprintf with the right signature, so
31
6
    // nobody else should have been able to declare vprintf with a bogus
32
6
    // signature.
33
6
    assert(F->getFunctionType() == VprintfFuncType);
34
6
    return F;
35
6
  }
36
3
37
3
  // vprintf doesn't already exist; create a declaration and insert it into the
38
3
  // module.
39
3
  return llvm::Function::Create(
40
3
      VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
41
3
}
42
43
// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
44
// isn't particularly special; it's invoked just like a regular function).
45
// vprintf takes two args: A format string, and a pointer to a buffer containing
46
// the varargs.
47
//
48
// For example, the call
49
//
50
//   printf("format string", arg1, arg2, arg3);
51
//
52
// is converted into something resembling
53
//
54
//   struct Tmp {
55
//     Arg1 a1;
56
//     Arg2 a2;
57
//     Arg3 a3;
58
//   };
59
//   char* buf = alloca(sizeof(Tmp));
60
//   *(Tmp*)buf = {a1, a2, a3};
61
//   vprintf("format string", buf);
62
//
63
// buf is aligned to the max of {alignof(Arg1), ...}.  Furthermore, each of the
64
// args is itself aligned to its preferred alignment.
65
//
66
// Note that by the time this function runs, E's args have already undergone the
67
// standard C vararg promotion (short -> int, float -> double, etc.).
68
RValue
69
CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E,
70
10
                                               ReturnValueSlot ReturnValue) {
71
10
  assert(getTarget().getTriple().isNVPTX());
72
10
  assert(E->getBuiltinCallee() == Builtin::BIprintf);
73
10
  assert(E->getNumArgs() >= 1); // printf always has at least one arg.
74
10
75
10
  const llvm::DataLayout &DL = CGM.getDataLayout();
76
10
  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
77
10
78
10
  CallArgList Args;
79
10
  EmitCallArgs(Args,
80
10
               E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
81
10
               E->arguments(), E->getDirectCallee(),
82
10
               /* ParamsToSkip = */ 0);
83
10
84
10
  // We don't know how to emit non-scalar varargs.
85
13
  if (
std::any_of(Args.begin() + 1, Args.end(), [&](const CallArg &A) 10
{
86
13
        return !A.getRValue(*this).isScalar();
87
13
      })) {
88
1
    CGM.ErrorUnsupported(E, "non-scalar arg to printf");
89
1
    return RValue::get(llvm::ConstantInt::get(IntTy, 0));
90
1
  }
91
9
92
9
  // Construct and fill the args buffer that we'll pass to vprintf.
93
9
  llvm::Value *BufferPtr;
94
9
  if (Args.size() <= 1) {
95
3
    // If there are no args, pass a null pointer to vprintf.
96
3
    BufferPtr = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
97
6
  } else {
98
6
    llvm::SmallVector<llvm::Type *, 8> ArgTypes;
99
18
    for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; 
++I12
)
100
12
      ArgTypes.push_back(Args[I].getRValue(*this).getScalarVal()->getType());
101
6
102
6
    // Using llvm::StructType is correct only because printf doesn't accept
103
6
    // aggregates.  If we had to handle aggregates here, we'd have to manually
104
6
    // compute the offsets within the alloca -- we wouldn't be able to assume
105
6
    // that the alignment of the llvm type was the same as the alignment of the
106
6
    // clang type.
107
6
    llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
108
6
    llvm::Value *Alloca = CreateTempAlloca(AllocaTy);
109
6
110
18
    for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; 
++I12
) {
111
12
      llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
112
12
      llvm::Value *Arg = Args[I].getRValue(*this).getScalarVal();
113
12
      Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlignment(Arg->getType()));
114
12
    }
115
6
    BufferPtr = Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
116
6
  }
117
9
118
9
  // Invoke vprintf and return.
119
9
  llvm::Function* VprintfFunc = GetVprintfDeclaration(CGM.getModule());
120
9
  return RValue::get(Builder.CreateCall(
121
9
      VprintfFunc, {Args[0].getRValue(*this).getScalarVal(), BufferPtr}));
122
9
}