Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGGPUBuiltin.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h"
20
21
using namespace clang;
22
using namespace CodeGen;
23
24
namespace {
25
0
llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
26
0
  llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
27
0
                            llvm::Type::getInt8PtrTy(M.getContext())};
28
0
  llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
29
0
      llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
30
31
0
  if (auto *F = M.getFunction("vprintf")) {
32
    // Our CUDA system header declares vprintf with the right signature, so
33
    // nobody else should have been able to declare vprintf with a bogus
34
    // signature.
35
0
    assert(F->getFunctionType() == VprintfFuncType);
36
0
    return F;
37
0
  }
38
39
  // vprintf doesn't already exist; create a declaration and insert it into the
40
  // module.
41
0
  return llvm::Function::Create(
42
0
      VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
43
0
}
44
45
6
llvm::Function *GetOpenMPVprintfDeclaration(CodeGenModule &CGM) {
46
6
  const char *Name = "__llvm_omp_vprintf";
47
6
  llvm::Module &M = CGM.getModule();
48
6
  llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
49
6
                            llvm::Type::getInt8PtrTy(M.getContext()),
50
6
                            llvm::Type::getInt32Ty(M.getContext())};
51
6
  llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
52
6
      llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
53
54
6
  if (auto *F = M.getFunction(Name)) {
55
4
    if (F->getFunctionType() != VprintfFuncType) {
56
0
      CGM.Error(SourceLocation(),
57
0
                "Invalid type declaration for __llvm_omp_vprintf");
58
0
      return nullptr;
59
0
    }
60
4
    return F;
61
4
  }
62
63
2
  return llvm::Function::Create(
64
2
      VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, Name, &M);
65
6
}
66
67
// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
68
// isn't particularly special; it's invoked just like a regular function).
69
// vprintf takes two args: A format string, and a pointer to a buffer containing
70
// the varargs.
71
//
72
// For example, the call
73
//
74
//   printf("format string", arg1, arg2, arg3);
75
//
76
// is converted into something resembling
77
//
78
//   struct Tmp {
79
//     Arg1 a1;
80
//     Arg2 a2;
81
//     Arg3 a3;
82
//   };
83
//   char* buf = alloca(sizeof(Tmp));
84
//   *(Tmp*)buf = {a1, a2, a3};
85
//   vprintf("format string", buf);
86
//
87
// buf is aligned to the max of {alignof(Arg1), ...}.  Furthermore, each of the
88
// args is itself aligned to its preferred alignment.
89
//
90
// Note that by the time this function runs, E's args have already undergone the
91
// standard C vararg promotion (short -> int, float -> double, etc.).
92
93
std::pair<llvm::Value *, llvm::TypeSize>
94
6
packArgsIntoNVPTXFormatBuffer(CodeGenFunction *CGF, const CallArgList &Args) {
95
6
  const llvm::DataLayout &DL = CGF->CGM.getDataLayout();
96
6
  llvm::LLVMContext &Ctx = CGF->CGM.getLLVMContext();
97
6
  CGBuilderTy &Builder = CGF->Builder;
98
99
  // Construct and fill the args buffer that we'll pass to vprintf.
100
6
  if (Args.size() <= 1) {
101
    // If there are no args, pass a null pointer and size 0
102
2
    llvm::Value * BufferPtr = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
103
2
    return {BufferPtr, llvm::TypeSize::Fixed(0)};
104
4
  } else {
105
4
    llvm::SmallVector<llvm::Type *, 8> ArgTypes;
106
12
    for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; 
++I8
)
107
8
      ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
108
109
    // Using llvm::StructType is correct only because printf doesn't accept
110
    // aggregates.  If we had to handle aggregates here, we'd have to manually
111
    // compute the offsets within the alloca -- we wouldn't be able to assume
112
    // that the alignment of the llvm type was the same as the alignment of the
113
    // clang type.
114
4
    llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
115
4
    llvm::Value *Alloca = CGF->CreateTempAlloca(AllocaTy);
116
117
12
    for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; 
++I8
) {
118
8
      llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
119
8
      llvm::Value *Arg = Args[I].getRValue(*CGF).getScalarVal();
120
8
      Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlign(Arg->getType()));
121
8
    }
122
4
    llvm::Value *BufferPtr =
123
4
        Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
124
4
    return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
125
4
  }
126
6
}
127
128
6
bool containsNonScalarVarargs(CodeGenFunction *CGF, CallArgList Args) {
129
8
  return llvm::any_of(llvm::drop_begin(Args), [&](const CallArg &A) {
130
8
    return !A.getRValue(*CGF).isScalar();
131
8
  });
132
6
}
133
134
RValue EmitDevicePrintfCallExpr(const CallExpr *E, CodeGenFunction *CGF,
135
6
                                llvm::Function *Decl, bool WithSizeArg) {
136
6
  CodeGenModule &CGM = CGF->CGM;
137
6
  CGBuilderTy &Builder = CGF->Builder;
138
6
  assert(E->getBuiltinCallee() == Builtin::BIprintf);
139
0
  assert(E->getNumArgs() >= 1); // printf always has at least one arg.
140
141
  // Uses the same format as nvptx for the argument packing, but also passes
142
  // an i32 for the total size of the passed pointer
143
0
  CallArgList Args;
144
6
  CGF->EmitCallArgs(Args,
145
6
                    E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
146
6
                    E->arguments(), E->getDirectCallee(),
147
6
                    /* ParamsToSkip = */ 0);
148
149
  // We don't know how to emit non-scalar varargs.
150
6
  if (containsNonScalarVarargs(CGF, Args)) {
151
0
    CGM.ErrorUnsupported(E, "non-scalar arg to printf");
152
0
    return RValue::get(llvm::ConstantInt::get(CGF->IntTy, 0));
153
0
  }
154
155
6
  auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
156
6
  llvm::Value *BufferPtr = r.first;
157
158
6
  llvm::SmallVector<llvm::Value *, 3> Vec = {
159
6
      Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
160
6
  if (WithSizeArg) {
161
    // Passing > 32bit of data as a local alloca doesn't work for nvptx or
162
    // amdgpu
163
6
    llvm::Constant *Size =
164
6
        llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.getLLVMContext()),
165
6
                               static_cast<uint32_t>(r.second.getFixedSize()));
166
167
6
    Vec.push_back(Size);
168
6
  }
169
6
  return RValue::get(Builder.CreateCall(Decl, Vec));
170
6
}
171
} // namespace
172
173
0
RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E) {
174
0
  assert(getTarget().getTriple().isNVPTX());
175
0
  return EmitDevicePrintfCallExpr(
176
0
      E, this, GetVprintfDeclaration(CGM.getModule()), false);
177
0
}
178
179
4
RValue CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E) {
180
4
  assert(getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
181
0
  assert(E->getBuiltinCallee() == Builtin::BIprintf ||
182
4
         E->getBuiltinCallee() == Builtin::BI__builtin_printf);
183
0
  assert(E->getNumArgs() >= 1); // printf always has at least one arg.
184
185
0
  CallArgList CallArgs;
186
4
  EmitCallArgs(CallArgs,
187
4
               E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
188
4
               E->arguments(), E->getDirectCallee(),
189
4
               /* ParamsToSkip = */ 0);
190
191
4
  SmallVector<llvm::Value *, 8> Args;
192
14
  for (auto A : CallArgs) {
193
    // We don't know how to emit non-scalar varargs.
194
14
    if (!A.getRValue(*this).isScalar()) {
195
1
      CGM.ErrorUnsupported(E, "non-scalar arg to printf");
196
1
      return RValue::get(llvm::ConstantInt::get(IntTy, -1));
197
1
    }
198
199
13
    llvm::Value *Arg = A.getRValue(*this).getScalarVal();
200
13
    Args.push_back(Arg);
201
13
  }
202
203
3
  llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
204
3
  IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
205
3
  auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args);
206
3
  Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
207
3
  return RValue::get(Printf);
208
4
}
209
210
6
RValue CodeGenFunction::EmitOpenMPDevicePrintfCallExpr(const CallExpr *E) {
211
6
  assert(getTarget().getTriple().isNVPTX() ||
212
6
         getTarget().getTriple().isAMDGCN());
213
0
  return EmitDevicePrintfCallExpr(E, this, GetOpenMPVprintfDeclaration(CGM),
214
6
                                  true);
215
6
}