Intel SPMD Program Compiler  1.9.1
func.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2011-2016, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file func.cpp
35  @brief
36 */
37 
38 #include "func.h"
39 #include "ctx.h"
40 #include "expr.h"
41 #include "llvmutil.h"
42 #include "module.h"
43 #include "type.h"
44 #include "stmt.h"
45 #include "sym.h"
46 #include "util.h"
47 #include <stdio.h>
48 
49 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2 // 3.2
50 #ifdef ISPC_NVPTX_ENABLED
51  #include <llvm/Metadata.h>
52 #endif /* ISPC_NVPTX_ENABLED */
53  #include <llvm/LLVMContext.h>
54  #include <llvm/Module.h>
55  #include <llvm/Type.h>
56  #include <llvm/Instructions.h>
57  #include <llvm/Intrinsics.h>
58  #include <llvm/DerivedTypes.h>
59 #else
60 #ifdef ISPC_NVPTX_ENABLED
61  #include <llvm/IR/Metadata.h>
62 #endif /* ISPC_NVPTX_ENABLED */
63  #include <llvm/IR/LLVMContext.h>
64  #include <llvm/IR/Module.h>
65  #include <llvm/IR/Type.h>
66  #include <llvm/IR/Instructions.h>
67  #include <llvm/IR/Intrinsics.h>
68  #include <llvm/IR/DerivedTypes.h>
69 #endif
70 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
71  #include "llvm/PassManager.h"
72 #else // LLVM 3.7+
73  #include "llvm/IR/LegacyPassManager.h"
74 #endif
75 #include <llvm/PassRegistry.h>
76 #include <llvm/Transforms/IPO.h>
77 #include <llvm/Support/FormattedStream.h>
78 #include <llvm/Support/FileUtilities.h>
79 #include <llvm/Target/TargetMachine.h>
80 #include <llvm/Target/TargetOptions.h>
81 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
82  #include <llvm/IR/Verifier.h>
83  #include <llvm/IR/IRPrintingPasses.h>
84  #include <llvm/IR/CFG.h>
85 #else
86  #include <llvm/Analysis/Verifier.h>
87  #include <llvm/Assembly/PrintModulePass.h>
88  #include <llvm/Support/CFG.h>
89 #endif
90 #include <llvm/Support/ToolOutputFile.h>
91 
93  sym = s;
94  code = c;
95 
96  maskSymbol = m->symbolTable->LookupVariable("__mask");
97  Assert(maskSymbol != NULL);
98 
99  if (code != NULL) {
100  code = TypeCheck(code);
101 
102  if (code != NULL && g->debugPrint) {
103  printf("After typechecking function \"%s\":\n",
104  sym->name.c_str());
105  code->Print(0);
106  printf("---------------------\n");
107  }
108 
109  if (code != NULL) {
110  code = Optimize(code);
111  if (g->debugPrint) {
112  printf("After optimizing function \"%s\":\n",
113  sym->name.c_str());
114  code->Print(0);
115  printf("---------------------\n");
116  }
117  }
118  }
119 
120  if (g->debugPrint) {
121  printf("Add Function %s\n", sym->name.c_str());
122  code->Print(0);
123  printf("\n\n\n");
124  }
125 
126  const FunctionType *type = CastType<FunctionType>(sym->type);
127  Assert(type != NULL);
128 
129  for (int i = 0; i < type->GetNumParameters(); ++i) {
130  const char *paramName = type->GetParameterName(i).c_str();
131  Symbol *sym = m->symbolTable->LookupVariable(paramName);
132  if (sym == NULL)
133  Assert(strncmp(paramName, "__anon_parameter_", 17) == 0);
134  args.push_back(sym);
135 
136  const Type *t = type->GetParameterType(i);
137  if (sym != NULL && CastType<ReferenceType>(t) == NULL)
138  sym->parentFunction = this;
139  }
140 
141  if (type->isTask
142 #ifdef ISPC_NVPTX_ENABLED
143  && (g->target->getISA() != Target::NVPTX)
144 #endif
145  ){
146  threadIndexSym = m->symbolTable->LookupVariable("threadIndex");
148  threadCountSym = m->symbolTable->LookupVariable("threadCount");
150  taskIndexSym = m->symbolTable->LookupVariable("taskIndex");
152  taskCountSym = m->symbolTable->LookupVariable("taskCount");
154 
155  taskIndexSym0 = m->symbolTable->LookupVariable("taskIndex0");
157  taskIndexSym1 = m->symbolTable->LookupVariable("taskIndex1");
159  taskIndexSym2 = m->symbolTable->LookupVariable("taskIndex2");
161 
162 
163  taskCountSym0 = m->symbolTable->LookupVariable("taskCount0");
165  taskCountSym1 = m->symbolTable->LookupVariable("taskCount1");
167  taskCountSym2 = m->symbolTable->LookupVariable("taskCount2");
169  }
170  else
171  {
175  }
176 }
177 
178 
179 const Type *
181  const FunctionType *type = CastType<FunctionType>(sym->type);
182  Assert(type != NULL);
183  return type->GetReturnType();
184 }
185 
186 
187 const FunctionType *
189  const FunctionType *type = CastType<FunctionType>(sym->type);
190  Assert(type != NULL);
191  return type;
192 }
193 
194 
195 /** Parameters for tasks are stored in a big structure; this utility
196  function emits code to copy those values out of the task structure into
197  local stack-allocated variables. (Which we expect that LLVM's
198  'mem2reg' pass will in turn promote to SSA registers..
199  */
200 static void
201 lCopyInTaskParameter(int i, llvm::Value *structArgPtr, const
202  std::vector<Symbol *> &args,
203  FunctionEmitContext *ctx) {
204  // We expect the argument structure to come in as a poitner to a
205  // structure. Confirm and figure out its type here.
206  const llvm::Type *structArgType = structArgPtr->getType();
207  Assert(llvm::isa<llvm::PointerType>(structArgType));
208  const llvm::PointerType *pt = llvm::dyn_cast<const llvm::PointerType>(structArgType);
209  Assert(llvm::isa<llvm::StructType>(pt->getElementType()));
210  const llvm::StructType *argStructType =
211  llvm::dyn_cast<const llvm::StructType>(pt->getElementType());
212 
213  // Get the type of the argument we're copying in and its Symbol pointer
214  llvm::Type *argType = argStructType->getElementType(i);
215  Symbol *sym = args[i];
216 
217  if (sym == NULL)
218  // anonymous parameter, so don't worry about it
219  return;
220 
221  // allocate space to copy the parameter in to
222  sym->storagePtr = ctx->AllocaInst(argType, sym->name.c_str());
223 
224  // get a pointer to the value in the struct
225  llvm::Value *ptr = ctx->AddElementOffset(structArgPtr, i, NULL, sym->name.c_str());
226 
227  // and copy the value from the struct and into the local alloca'ed
228  // memory
229  llvm::Value *ptrval = ctx->LoadInst(ptr, sym->name.c_str());
230  ctx->StoreInst(ptrval, sym->storagePtr);
231  ctx->EmitFunctionParameterDebugInfo(sym, i);
232 }
233 
234 
235 /** Given the statements implementing a function, emit the code that
236  implements the function. Most of the work do be done here just
237  involves wiring up the function parameter values to be available in the
238  function body code.
239  */
240 void
241 Function::emitCode(FunctionEmitContext *ctx, llvm::Function *function,
242  SourcePos firstStmtPos) {
243  // Connect the __mask builtin to the location in memory that stores its
244  // value
246 
247  // add debugging info for __mask
248  maskSymbol->pos = firstStmtPos;
250 
251 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_7 // LLVM 3.7+
252  if (g->NoOmitFramePointer)
253  function->addFnAttr("no-frame-pointer-elim", "true");
254 #endif
255 
256 #if 0
257  llvm::BasicBlock *entryBBlock = ctx->GetCurrentBasicBlock();
258 #endif
259  const FunctionType *type = CastType<FunctionType>(sym->type);
260  Assert(type != NULL);
261  if (type->isTask == true
262 #ifdef ISPC_NVPTX_ENABLED
263  && (g->target->getISA() != Target::NVPTX)
264 #endif
265  ){
266  // For tasks, there should always be three parameters: the
267  // pointer to the structure that holds all of the arguments, the
268  // thread index, and the thread count variables.
269  llvm::Function::arg_iterator argIter = function->arg_begin();
270 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
271  llvm::Value *structParamPtr = argIter++;
272  llvm::Value *threadIndex = argIter++;
273  llvm::Value *threadCount = argIter++;
274  llvm::Value *taskIndex = argIter++;
275  llvm::Value *taskCount = argIter++;
276  llvm::Value *taskIndex0 = argIter++;
277  llvm::Value *taskIndex1 = argIter++;
278  llvm::Value *taskIndex2 = argIter++;
279  llvm::Value *taskCount0 = argIter++;
280  llvm::Value *taskCount1 = argIter++;
281  llvm::Value *taskCount2 = argIter++;
282 #else /* LLVM 3.8+ */
283  llvm::Value *structParamPtr = &*(argIter++);
284  llvm::Value *threadIndex = &*(argIter++);
285  llvm::Value *threadCount = &*(argIter++);
286  llvm::Value *taskIndex = &*(argIter++);
287  llvm::Value *taskCount = &*(argIter++);
288  llvm::Value *taskIndex0 = &*(argIter++);
289  llvm::Value *taskIndex1 = &*(argIter++);
290  llvm::Value *taskIndex2 = &*(argIter++);
291  llvm::Value *taskCount0 = &*(argIter++);
292  llvm::Value *taskCount1 = &*(argIter++);
293  llvm::Value *taskCount2 = &*(argIter++);
294 #endif
295  // Copy the function parameter values from the structure into local
296  // storage
297  for (unsigned int i = 0; i < args.size(); ++i)
298  lCopyInTaskParameter(i, structParamPtr, args, ctx);
299 
300  if (type->isUnmasked == false) {
301  // Copy in the mask as well.
302  int nArgs = (int)args.size();
303  // The mask is the last parameter in the argument structure
304  llvm::Value *ptr = ctx->AddElementOffset(structParamPtr, nArgs, NULL,
305  "task_struct_mask");
306  llvm::Value *ptrval = ctx->LoadInst(ptr, "mask");
307  ctx->SetFunctionMask(ptrval);
308  }
309 
310  // Copy threadIndex and threadCount into stack-allocated storage so
311  // that their symbols point to something reasonable.
313  ctx->StoreInst(threadIndex, threadIndexSym->storagePtr);
314 
316  ctx->StoreInst(threadCount, threadCountSym->storagePtr);
317 
318  // Copy taskIndex and taskCount into stack-allocated storage so
319  // that their symbols point to something reasonable.
321  ctx->StoreInst(taskIndex, taskIndexSym->storagePtr);
322 
324  ctx->StoreInst(taskCount, taskCountSym->storagePtr);
325 
327  ctx->StoreInst(taskIndex0, taskIndexSym0->storagePtr);
329  ctx->StoreInst(taskIndex1, taskIndexSym1->storagePtr);
331  ctx->StoreInst(taskIndex2, taskIndexSym2->storagePtr);
332 
334  ctx->StoreInst(taskCount0, taskCountSym0->storagePtr);
336  ctx->StoreInst(taskCount1, taskCountSym1->storagePtr);
338  ctx->StoreInst(taskCount2, taskCountSym2->storagePtr);
339  }
340  else {
341  // Regular, non-task function
342  llvm::Function::arg_iterator argIter = function->arg_begin();
343  for (unsigned int i = 0; i < args.size(); ++i, ++argIter) {
344  Symbol *sym = args[i];
345  if (sym == NULL)
346  // anonymous function parameter
347  continue;
348 
349  argIter->setName(sym->name.c_str());
350 
351  // Allocate stack storage for the parameter and emit code
352  // to store the its value there.
353  sym->storagePtr = ctx->AllocaInst(argIter->getType(), sym->name.c_str());
354 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
355  ctx->StoreInst(argIter, sym->storagePtr);
356 #else /* LLVM 3.8+ */
357  ctx->StoreInst(&*argIter, sym->storagePtr);
358 #endif
359  ctx->EmitFunctionParameterDebugInfo(sym, i);
360  }
361 
362  // If the number of actual function arguments is equal to the
363  // number of declared arguments in decl->functionParams, then we
364  // don't have a mask parameter, so set it to be all on. This
365  // happens for exmaple with 'export'ed functions that the app
366  // calls.
367  if (argIter == function->arg_end()) {
368  Assert(type->isUnmasked || type->isExported);
370  }
371  else {
372  Assert(type->isUnmasked == false);
373 
374  // Otherwise use the mask to set the entry mask value
375  argIter->setName("__mask");
376  Assert(argIter->getType() == LLVMTypes::MaskType);
377 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
378  ctx->SetFunctionMask(argIter);
379 #else /* LLVM 3.8+ */
380  ctx->SetFunctionMask(&*argIter);
381 #endif
382  Assert(++argIter == function->arg_end());
383  }
384 #ifdef ISPC_NVPTX_ENABLED
385  if (type->isTask == true && g->target->getISA() == Target::NVPTX)
386  {
387  llvm::NamedMDNode* annotations =
388  m->module->getOrInsertNamedMetadata("nvvm.annotations");
389 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
390  llvm::SmallVector<llvm::Metadata*, 3> av;
391  av.push_back(llvm::ValueAsMetadata::get(function));
392  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
393  av.push_back(llvm::ConstantAsMetadata::get(LLVMInt32(1)));
394  annotations->addOperand(llvm::MDNode::get(*g->ctx, llvm::ArrayRef<llvm::Metadata*>(av)));
395 #else
396  llvm::SmallVector<llvm::Value*, 3> av;
397  av.push_back(function);
398  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
399  av.push_back(LLVMInt32(1));
400  annotations->addOperand(llvm::MDNode::get(*g->ctx, av));
401 #endif
402  }
403 #endif /* ISPC_NVPTX_ENABLED */
404  }
405 
406  // Finally, we can generate code for the function
407  if (code != NULL) {
408  ctx->SetDebugPos(code->pos);
409  ctx->AddInstrumentationPoint("function entry");
410 
411  int costEstimate = EstimateCost(code);
412  Debug(code->pos, "Estimated cost for function \"%s\" = %d\n",
413  sym->name.c_str(), costEstimate);
414 
415  // If the body of the function is non-trivial, then we wrap the
416  // entire thing inside code that tests to see if the mask is all
417  // on, all off, or mixed. If this is a simple function, then this
418  // isn't worth the code bloat / overhead.
419  bool checkMask = (type->isTask == true) ||
420  (
421 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2 // 3.2
422  (function->getFnAttributes().hasAttribute(llvm::Attributes::AlwaysInline) == false)
423 #else // LLVM 3.3+
424  (function->getAttributes().getFnAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::AlwaysInline) == false)
425 #endif
426  &&
427  costEstimate > CHECK_MASK_AT_FUNCTION_START_COST);
428  checkMask &= (type->isUnmasked == false);
429  checkMask &= (g->target->getMaskingIsFree() == false);
430  checkMask &= (g->opt.disableCoherentControlFlow == false);
431 
432  if (checkMask) {
433  llvm::Value *mask = ctx->GetFunctionMask();
434  llvm::Value *allOn = ctx->All(mask);
435  llvm::BasicBlock *bbAllOn = ctx->CreateBasicBlock("all_on");
436  llvm::BasicBlock *bbSomeOn = ctx->CreateBasicBlock("some_on");
437 
438  // Set up basic blocks for goto targets
439  ctx->InitializeLabelMap(code);
440 
441  ctx->BranchInst(bbAllOn, bbSomeOn, allOn);
442  // all on: we've determined dynamically that the mask is all
443  // on. Set the current mask to "all on" explicitly so that
444  // codegen for this path can be improved with this knowledge in
445  // hand...
446  ctx->SetCurrentBasicBlock(bbAllOn);
449  code->EmitCode(ctx);
450  if (ctx->GetCurrentBasicBlock())
451  ctx->ReturnInst();
452 
453  // not all on: however, at least one lane must be running,
454  // since we should never run with all off... some on: reset
455  // the mask to the value it had at function entry and emit the
456  // code. Resetting the mask here is important, due to the "all
457  // on" setting of it for the path above.
458  ctx->SetCurrentBasicBlock(bbSomeOn);
459  ctx->SetFunctionMask(mask);
460 
461  // Set up basic blocks for goto targets again; we want to have
462  // one set of them for gotos in the 'all on' case, and a
463  // distinct set for the 'mixed mask' case.
464  ctx->InitializeLabelMap(code);
465 
466  code->EmitCode(ctx);
467  if (ctx->GetCurrentBasicBlock())
468  ctx->ReturnInst();
469  }
470  else {
471  // Set up basic blocks for goto targets
472  ctx->InitializeLabelMap(code);
473  // No check, just emit the code
474  code->EmitCode(ctx);
475  }
476  }
477 
478  if (ctx->GetCurrentBasicBlock()) {
479  // FIXME: We'd like to issue a warning if we've reached the end of
480  // the function without a return statement (for non-void
481  // functions). But the test below isn't right, since we can have
482  // (with 'x' a varying test) "if (x) return a; else return b;", in
483  // which case we have a valid basic block but its unreachable so ok
484  // to not have return statement.
485 #if 0
486  // If the bblock has no predecessors, then it doesn't matter if it
487  // doesn't have a return; it'll never be reached. If it does,
488  // issue a warning. Also need to warn if it's the entry block for
489  // the function (in which case it will not have predeccesors but is
490  // still reachable.)
491  if (type->GetReturnType()->IsVoidType() == false &&
492  (pred_begin(ec.bblock) != pred_end(ec.bblock) || (ec.bblock == entryBBlock)))
493  Warning(sym->pos, "Missing return statement in function returning \"%s\".",
494  type->rType->GetString().c_str());
495 #endif
496 
497  // FIXME: would like to set the context's current position to
498  // e.g. the end of the function code
499 
500  // if bblock is non-NULL, it hasn't been terminated by e.g. a
501  // return instruction. Need to add a return instruction.
502  ctx->ReturnInst();
503  }
504 }
505 
506 
507 void
509  if (sym == NULL)
510  // May be NULL due to error earlier in compilation
511  return;
512 
513  llvm::Function *function = sym->function;
514  Assert(function != NULL);
515 
516  // But if that function has a definition, we don't want to redefine it.
517  if (function->empty() == false) {
518  Error(sym->pos, "Ignoring redefinition of function \"%s\".",
519  sym->name.c_str());
520  return;
521  }
522 
523  // Figure out a reasonable source file position for the start of the
524  // function body. If possible, get the position of the first actual
525  // non-StmtList statment...
526  SourcePos firstStmtPos = sym->pos;
527  if (code) {
528  StmtList *sl = llvm::dyn_cast<StmtList>(code);
529  if (sl && sl->stmts.size() > 0 && sl->stmts[0] != NULL)
530  firstStmtPos = sl->stmts[0]->pos;
531  else
532  firstStmtPos = code->pos;
533  }
534 
535  // And we can now go ahead and emit the code
536  {
537  FunctionEmitContext ec(this, sym, function, firstStmtPos);
538  emitCode(&ec, function, firstStmtPos);
539  }
540 
541  if (m->errorCount == 0) {
542  // If the function is 'export'-qualified, emit a second version of
543  // it without a mask parameter and without name mangling so that
544  // the application can call it
545  const FunctionType *type = CastType<FunctionType>(sym->type);
546  Assert(type != NULL);
547  if (type->isExported) {
548  if (!type->isTask) {
549  llvm::FunctionType *ftype = type->LLVMFunctionType(g->ctx, true);
550  llvm::GlobalValue::LinkageTypes linkage = llvm::GlobalValue::ExternalLinkage;
551  std::string functionName = sym->name;
553  // If we treat generic as smth, we should have appropriate mangling
554  if (g->target->getISA() == Target::GENERIC &&
555  !g->target->getTreatGenericAsSmth().empty())
556  functionName += std::string("_") + g->target->getTreatGenericAsSmth();
557  else
558  functionName += std::string("_") + g->target->GetISAString();
559  }
560 #ifdef ISPC_NVPTX_ENABLED
561  if (g->target->getISA() == Target::NVPTX)
562  {
563  functionName += std::string("___export"); /* add ___export to the end, for ptxcc to recognize it is exported */
564 #if 0
565  llvm::NamedMDNode* annotations =
566  m->module->getOrInsertNamedMetadata("nvvm.annotations");
567  llvm::SmallVector<llvm::Value*, 3> av;
568  av.push_back(function);
569  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
570  av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1));
571  annotations->addOperand(llvm::MDNode::get(*g->ctx, av));
572 #endif
573  }
574 #endif /* ISPC_NVPTX_ENABLED */
575  llvm::Function *appFunction =
576  llvm::Function::Create(ftype, linkage, functionName.c_str(), m->module);
577  appFunction->setDoesNotThrow();
578 
579  // We should iterate from 1 because zero parameter is return.
580  // We should iterate till getNumParams instead of getNumParams+1 because new
581  // function is export function and doesn't contain the last parameter "mask".
582  for (int i = 1; i < function->getFunctionType()->getNumParams(); i++) {
583  if (function->doesNotAlias(i)) {
584  appFunction->setDoesNotAlias(i);
585  }
586  }
587  g->target->markFuncWithTargetAttr(appFunction);
588 
589  if (appFunction->getName() != functionName) {
590  // this was a redefinition for which we already emitted an
591  // error, so don't worry about this one...
592  appFunction->eraseFromParent();
593  }
594  else {
595  // And emit the code again
596  FunctionEmitContext ec(this, sym, appFunction, firstStmtPos);
597  emitCode(&ec, appFunction, firstStmtPos);
598  if (m->errorCount == 0) {
599  sym->exportedFunction = appFunction;
600  }
601 #ifdef ISPC_NVPTX_ENABLED
602  if (g->target->getISA() == Target::NVPTX)
603  {
604  llvm::NamedMDNode* annotations =
605  m->module->getOrInsertNamedMetadata("nvvm.annotations");
606 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
607 
608  llvm::SmallVector<llvm::Metadata*, 3> av;
609  av.push_back(llvm::ValueAsMetadata::get(appFunction));
610  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
611  av.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1)));
612  annotations->addOperand(llvm::MDNode::get(*g->ctx, llvm::ArrayRef<llvm::Metadata*>(av)));
613 #else
614  llvm::SmallVector<llvm::Value*, 3> av;
615  av.push_back(appFunction);
616  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
617  av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1));
618  annotations->addOperand(llvm::MDNode::get(*g->ctx, av));
619 #endif
620 
621  }
622 #endif /* ISPC_NVPTX_ENABLED */
623  }
624  }
625  }
626  }
627 }
bool IsVoidType() const
Definition: type.cpp:246
llvm::Value * storagePtr
Definition: sym.h:72
llvm::Constant * LLVMMaskAllOn
Definition: llvmutil.cpp:92
llvm::Function * function
Definition: sym.h:76
void InitializeLabelMap(Stmt *code)
Definition: ctx.cpp:1276
std::string GetString() const
Definition: type.cpp:3011
llvm::Value * AddElementOffset(llvm::Value *basePtr, int elementNum, const Type *ptrType, const char *name=NULL, const PointerType **resultPtrType=NULL)
Definition: ctx.cpp:2589
Opt opt
Definition: ispc.h:541
Declaration of the FunctionEmitContext class
void EmitVariableDebugInfo(Symbol *sym)
Definition: ctx.cpp:1762
Symbol * taskIndexSym
Definition: func.h:63
Symbol * threadIndexSym
Definition: func.h:62
void BranchInst(llvm::BasicBlock *block)
Definition: ctx.cpp:3457
Module * m
Definition: ispc.cpp:89
Interface class for statements in the ispc language.
Definition: stmt.h:49
virtual void EmitCode(FunctionEmitContext *ctx) const =0
Target * target
Definition: ispc.h:543
llvm::Value * LoadInst(llvm::Value *ptr, llvm::Value *mask, const Type *ptrType, const char *name=NULL, bool one_elem=false)
Definition: ctx.cpp:2808
void emitCode(FunctionEmitContext *ctx, llvm::Function *function, SourcePos firstStmtPos)
Definition: func.cpp:241
Symbol * maskSymbol
Definition: func.h:61
llvm::Value * AllocaInst(llvm::Type *llvmType, const char *name=NULL, int align=0, bool atEntryBlock=true)
Definition: ctx.cpp:3042
bool NoOmitFramePointer
Definition: ispc.h:566
#define Assert(expr)
Definition: ispc.h:170
std::vector< Symbol * > args
Definition: func.h:59
llvm::FunctionType * LLVMFunctionType(llvm::LLVMContext *ctx, bool disableMask=false) const
Definition: type.cpp:3203
Symbol * taskIndexSym2
Definition: func.h:66
const char * GetISAString() const
Definition: ispc.cpp:1307
void AddInstrumentationPoint(const char *note)
Definition: ctx.cpp:1640
std::string name
Definition: sym.h:71
Symbol * LookupVariable(const char *name)
Definition: sym.cpp:131
llvm::BasicBlock * GetCurrentBasicBlock()
Definition: ctx.cpp:450
llvm::BasicBlock * CreateBasicBlock(const char *name)
Definition: ctx.cpp:1582
header file with declarations for symbol and symbol table classes.
bool disableMaskAllOnOptimizations
Definition: ispc.h:470
ASTNode * Optimize(ASTNode *root)
Definition: ast.cpp:282
llvm::ConstantInt * LLVMInt32(int32_t i)
Definition: llvmutil.cpp:263
void StoreInst(llvm::Value *value, llvm::Value *ptr)
Definition: ctx.cpp:3324
llvm::Module * module
Definition: module.h:158
ASTNode * TypeCheck(ASTNode *root)
Definition: ast.cpp:306
File with declarations for classes related to statements in the language.
Symbol * taskCountSym0
Definition: func.h:64
Globals * g
Definition: ispc.cpp:88
bool debugPrint
Definition: ispc.h:560
llvm::Value * GetFunctionMask()
Definition: ctx.cpp:462
bool disableCoherentControlFlow
Definition: ispc.h:492
Symbol * taskIndexSym1
Definition: func.h:65
const std::string & GetParameterName(int i) const
Definition: type.cpp:3291
Symbol * taskCountSym2
Definition: func.h:66
void Error(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:385
const FunctionType * GetType() const
Definition: func.cpp:188
bool mangleFunctionsWithTarget
Definition: ispc.h:621
Header file with declarations for various LLVM utility stuff.
const Type * GetParameterType(int i) const
Definition: type.cpp:3270
ISA getISA() const
Definition: ispc.h:267
Stmt * code
Definition: func.h:60
SourcePos pos
Definition: sym.h:70
Symbol * taskCountSym
Definition: func.h:63
Function(Symbol *sym, Stmt *code)
Definition: func.cpp:92
Representation of a range of positions in a source file.
Definition: ispc.h:134
Symbol * taskCountSym1
Definition: func.h:65
void markFuncWithTargetAttr(llvm::Function *func)
Definition: ispc.cpp:1477
virtual void Print(int indent) const =0
SourcePos pos
Definition: ast.h:77
Symbol * taskIndexSym0
Definition: func.h:64
void Warning(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:410
const Type * GetReturnType() const
Definition: func.cpp:180
const Type * GetReturnType() const
Definition: type.h:924
static llvm::Type * Int32Type
Definition: llvmutil.h:74
void SetDebugPos(SourcePos pos)
Definition: ctx.cpp:1661
std::string getTreatGenericAsSmth() const
Definition: ispc.h:269
Representation of a function in a source file.
Symbol * sym
Definition: func.h:58
const bool isTask
Definition: type.h:943
void GenerateIR()
Definition: func.cpp:508
llvm::Value * GetFullMaskPointer()
Definition: ctx.cpp:481
Type representing a function (return type + argument types)
Definition: type.h:883
Representation of a program symbol.
Definition: sym.h:63
Interface class that defines the type abstraction.
Definition: type.h:101
std::vector< Stmt * > stmts
Definition: stmt.h:519
Expr abstract base class and expression implementations.
void SetCurrentBasicBlock(llvm::BasicBlock *bblock)
Definition: ctx.cpp:456
static llvm::VectorType * MaskType
Definition: llvmutil.h:86
const bool isUnmasked
Definition: type.h:956
int EstimateCost(ASTNode *root)
Definition: ast.cpp:352
void EmitFunctionParameterDebugInfo(Symbol *sym, int parameterNum)
Definition: ctx.cpp:1820
const Function * parentFunction
Definition: sym.h:104
Representation of a list of statements in the program.
Definition: stmt.h:502
static void lCopyInTaskParameter(int i, llvm::Value *structArgPtr, const std::vector< Symbol * > &args, FunctionEmitContext *ctx)
Definition: func.cpp:201
llvm::Value * All(llvm::Value *mask)
Definition: ctx.cpp:1398
const bool isExported
Definition: type.h:947
Declaration of the Module class, which is the ispc-side representation of the results of compiling a ...
int errorCount
Definition: module.h:151
llvm::LLVMContext * ctx
Definition: ispc.h:632
bool getMaskingIsFree() const
Definition: ispc.h:287
const Type * type
Definition: sym.h:84
Symbol * threadCountSym
Definition: func.h:62
void Debug(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:398
llvm::Instruction * ReturnInst()
Definition: ctx.cpp:3831
int GetNumParameters() const
Definition: type.h:935
void SetFunctionMask(llvm::Value *val)
Definition: ctx.cpp:487
SymbolTable * symbolTable
Definition: module.h:155
llvm::Function * exportedFunction
Definition: sym.h:79
File with declarations for classes related to type representation.