Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/CodeGen/CGOpenCLRuntime.cpp
Line
Count
Source (jump to first uncovered line)
1
//===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===//
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
// This provides an abstract class for OpenCL code generation.  Concrete
10
// subclasses of this implement code generation for specific OpenCL
11
// runtime libraries.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "CGOpenCLRuntime.h"
16
#include "CodeGenFunction.h"
17
#include "TargetInfo.h"
18
#include "clang/CodeGen/ConstantInitBuilder.h"
19
#include "llvm/IR/DerivedTypes.h"
20
#include "llvm/IR/GlobalValue.h"
21
#include <assert.h>
22
23
using namespace clang;
24
using namespace CodeGen;
25
26
308
CGOpenCLRuntime::~CGOpenCLRuntime() {}
27
28
void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
29
122
                                                const VarDecl &D) {
30
122
  return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
31
122
}
32
33
74
llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
34
74
  assert(T->isOpenCLSpecificType() &&
35
74
         "Not an OpenCL specific type!");
36
74
37
74
  llvm::LLVMContext& Ctx = CGM.getLLVMContext();
38
74
  uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
39
74
      CGM.getContext().getOpenCLTypeAddrSpace(T));
40
74
  switch (cast<BuiltinType>(T)->getKind()) {
41
74
  default:
42
0
    llvm_unreachable("Unexpected opencl builtin type!");
43
74
    
return nullptr0
;
44
74
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
45
74
  case BuiltinType::Id: \
46
33
    return llvm::PointerType::get( \
47
33
        llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \
48
33
        AddrSpc);
49
74
#include 
"clang/Basic/OpenCLImageTypes.def"0
50
74
  case BuiltinType::OCLSampler:
51
5
    return getSamplerType(T);
52
74
  case BuiltinType::OCLEvent:
53
3
    return llvm::PointerType::get(
54
3
        llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc);
55
74
  case BuiltinType::OCLClkEvent:
56
5
    return llvm::PointerType::get(
57
5
        llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc);
58
74
  case BuiltinType::OCLQueue:
59
12
    return llvm::PointerType::get(
60
12
        llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc);
61
74
  case BuiltinType::OCLReserveID:
62
4
    return llvm::PointerType::get(
63
4
        llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc);
64
74
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
65
74
  case BuiltinType::Id: \
66
12
    return llvm::PointerType::get( \
67
12
        llvm::StructType::create(Ctx, "opencl." #ExtType), AddrSpc);
68
74
#include 
"clang/Basic/OpenCLExtensionTypes.def"1
69
74
  }
70
74
}
71
72
18
llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
73
18
  if (T->isReadOnly())
74
13
    return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
75
5
  else
76
5
    return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
77
18
}
78
79
llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
80
18
                                         llvm::Type *&PipeTy) {
81
18
  if (!PipeTy)
82
14
    PipeTy = llvm::PointerType::get(llvm::StructType::create(
83
14
      CGM.getLLVMContext(), Name),
84
14
      CGM.getContext().getTargetAddressSpace(
85
14
          CGM.getContext().getOpenCLTypeAddrSpace(T)));
86
18
  return PipeTy;
87
18
}
88
89
27
llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) {
90
27
  if (!SamplerTy)
91
5
    SamplerTy = llvm::PointerType::get(llvm::StructType::create(
92
5
      CGM.getLLVMContext(), "opencl.sampler_t"),
93
5
      CGM.getContext().getTargetAddressSpace(
94
5
          CGM.getContext().getOpenCLTypeAddrSpace(T)));
95
27
  return SamplerTy;
96
27
}
97
98
28
llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
99
28
  const PipeType *PipeTy = PipeArg->getType()->getAs<PipeType>();
100
28
  // The type of the last (implicit) argument to be passed.
101
28
  llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
102
28
  unsigned TypeSize = CGM.getContext()
103
28
                          .getTypeSizeInChars(PipeTy->getElementType())
104
28
                          .getQuantity();
105
28
  return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
106
28
}
107
108
28
llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
109
28
  const PipeType *PipeTy = PipeArg->getType()->getAs<PipeType>();
110
28
  // The type of the last (implicit) argument to be passed.
111
28
  llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
112
28
  unsigned TypeSize = CGM.getContext()
113
28
                          .getTypeAlignInChars(PipeTy->getElementType())
114
28
                          .getQuantity();
115
28
  return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
116
28
}
117
118
251
llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() {
119
251
  assert(CGM.getLangOpts().OpenCL);
120
251
  return llvm::IntegerType::getInt8PtrTy(
121
251
      CGM.getLLVMContext(),
122
251
      CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
123
251
}
124
125
// Get the block literal from an expression derived from the block expression.
126
// OpenCL v2.0 s6.12.5:
127
// Block variable declarations are implicitly qualified with const. Therefore
128
// all block variables must be initialized at declaration time and may not be
129
// reassigned.
130
86
static const BlockExpr *getBlockExpr(const Expr *E) {
131
86
  const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop.
132
170
  while(!isa<BlockExpr>(E) && 
E != Prev84
) {
133
84
    Prev = E;
134
84
    E = E->IgnoreCasts();
135
84
    if (auto DR = dyn_cast<DeclRefExpr>(E)) {
136
48
      E = cast<VarDecl>(DR->getDecl())->getInit();
137
48
    }
138
84
  }
139
86
  return cast<BlockExpr>(E);
140
86
}
141
142
/// Record emitted llvm invoke function and llvm block literal for the
143
/// corresponding block expression.
144
void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E,
145
                                      llvm::Function *InvokeF,
146
80
                                      llvm::Value *Block) {
147
80
  assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() &&
148
80
         "Block expression emitted twice");
149
80
  assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
150
80
  assert(Block->getType()->isPointerTy() && "Invalid block literal type");
151
80
  EnqueuedBlockMap[E].InvokeFunc = InvokeF;
152
80
  EnqueuedBlockMap[E].BlockArg = Block;
153
80
  EnqueuedBlockMap[E].Kernel = nullptr;
154
80
}
155
156
18
llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) {
157
18
  return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
158
18
}
159
160
CGOpenCLRuntime::EnqueuedBlockInfo
161
68
CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) {
162
68
  CGF.EmitScalarExpr(E);
163
68
164
68
  // The block literal may be assigned to a const variable. Chasing down
165
68
  // to get the block literal.
166
68
  const BlockExpr *Block = getBlockExpr(E);
167
68
168
68
  assert(EnqueuedBlockMap.find(Block) != EnqueuedBlockMap.end() &&
169
68
         "Block expression not emitted");
170
68
171
68
  // Do not emit the block wrapper again if it has been emitted.
172
68
  if (EnqueuedBlockMap[Block].Kernel) {
173
9
    return EnqueuedBlockMap[Block];
174
9
  }
175
59
176
59
  auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel(
177
59
      CGF, EnqueuedBlockMap[Block].InvokeFunc,
178
59
      EnqueuedBlockMap[Block].BlockArg->stripPointerCasts());
179
59
180
59
  // The common part of the post-processing of the kernel goes here.
181
59
  F->addFnAttr(llvm::Attribute::NoUnwind);
182
59
  F->setCallingConv(
183
59
      CGF.getTypes().ClangCallConvToLLVMCallConv(CallingConv::CC_OpenCLKernel));
184
59
  EnqueuedBlockMap[Block].Kernel = F;
185
59
  return EnqueuedBlockMap[Block];
186
59
}