Intel SPMD Program Compiler  1.9.2
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 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
424  (function->getAttributes().getFnAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::AlwaysInline) == false)
425 #else // LLVM 5.0+
426  (function->getAttributes().getFnAttributes().hasAttribute(llvm::Attribute::AlwaysInline) == false)
427 #endif
428  &&
429  costEstimate > CHECK_MASK_AT_FUNCTION_START_COST);
430  checkMask &= (type->isUnmasked == false);
431  checkMask &= (g->target->getMaskingIsFree() == false);
432  checkMask &= (g->opt.disableCoherentControlFlow == false);
433 
434  if (checkMask) {
435  llvm::Value *mask = ctx->GetFunctionMask();
436  llvm::Value *allOn = ctx->All(mask);
437  llvm::BasicBlock *bbAllOn = ctx->CreateBasicBlock("all_on");
438  llvm::BasicBlock *bbSomeOn = ctx->CreateBasicBlock("some_on");
439 
440  // Set up basic blocks for goto targets
441  ctx->InitializeLabelMap(code);
442 
443  ctx->BranchInst(bbAllOn, bbSomeOn, allOn);
444  // all on: we've determined dynamically that the mask is all
445  // on. Set the current mask to "all on" explicitly so that
446  // codegen for this path can be improved with this knowledge in
447  // hand...
448  ctx->SetCurrentBasicBlock(bbAllOn);
451  code->EmitCode(ctx);
452  if (ctx->GetCurrentBasicBlock())
453  ctx->ReturnInst();
454 
455  // not all on: however, at least one lane must be running,
456  // since we should never run with all off... some on: reset
457  // the mask to the value it had at function entry and emit the
458  // code. Resetting the mask here is important, due to the "all
459  // on" setting of it for the path above.
460  ctx->SetCurrentBasicBlock(bbSomeOn);
461  ctx->SetFunctionMask(mask);
462 
463  // Set up basic blocks for goto targets again; we want to have
464  // one set of them for gotos in the 'all on' case, and a
465  // distinct set for the 'mixed mask' case.
466  ctx->InitializeLabelMap(code);
467 
468  code->EmitCode(ctx);
469  if (ctx->GetCurrentBasicBlock())
470  ctx->ReturnInst();
471  }
472  else {
473  // Set up basic blocks for goto targets
474  ctx->InitializeLabelMap(code);
475  // No check, just emit the code
476  code->EmitCode(ctx);
477  }
478  }
479 
480  if (ctx->GetCurrentBasicBlock()) {
481  // FIXME: We'd like to issue a warning if we've reached the end of
482  // the function without a return statement (for non-void
483  // functions). But the test below isn't right, since we can have
484  // (with 'x' a varying test) "if (x) return a; else return b;", in
485  // which case we have a valid basic block but its unreachable so ok
486  // to not have return statement.
487 #if 0
488  // If the bblock has no predecessors, then it doesn't matter if it
489  // doesn't have a return; it'll never be reached. If it does,
490  // issue a warning. Also need to warn if it's the entry block for
491  // the function (in which case it will not have predeccesors but is
492  // still reachable.)
493  if (type->GetReturnType()->IsVoidType() == false &&
494  (pred_begin(ec.bblock) != pred_end(ec.bblock) || (ec.bblock == entryBBlock)))
495  Warning(sym->pos, "Missing return statement in function returning \"%s\".",
496  type->rType->GetString().c_str());
497 #endif
498 
499  // FIXME: would like to set the context's current position to
500  // e.g. the end of the function code
501 
502  // if bblock is non-NULL, it hasn't been terminated by e.g. a
503  // return instruction. Need to add a return instruction.
504  ctx->ReturnInst();
505  }
506 }
507 
508 
509 void
511  if (sym == NULL)
512  // May be NULL due to error earlier in compilation
513  return;
514 
515  llvm::Function *function = sym->function;
516  Assert(function != NULL);
517 
518  // But if that function has a definition, we don't want to redefine it.
519  if (function->empty() == false) {
520  Error(sym->pos, "Ignoring redefinition of function \"%s\".",
521  sym->name.c_str());
522  return;
523  }
524 
525  // Figure out a reasonable source file position for the start of the
526  // function body. If possible, get the position of the first actual
527  // non-StmtList statment...
528  SourcePos firstStmtPos = sym->pos;
529  if (code) {
530  StmtList *sl = llvm::dyn_cast<StmtList>(code);
531  if (sl && sl->stmts.size() > 0 && sl->stmts[0] != NULL)
532  firstStmtPos = sl->stmts[0]->pos;
533  else
534  firstStmtPos = code->pos;
535  }
536 
537  // And we can now go ahead and emit the code
538  {
539  FunctionEmitContext ec(this, sym, function, firstStmtPos);
540  emitCode(&ec, function, firstStmtPos);
541  }
542 
543  if (m->errorCount == 0) {
544  // If the function is 'export'-qualified, emit a second version of
545  // it without a mask parameter and without name mangling so that
546  // the application can call it
547  const FunctionType *type = CastType<FunctionType>(sym->type);
548  Assert(type != NULL);
549  if (type->isExported) {
550  if (!type->isTask) {
551  llvm::FunctionType *ftype = type->LLVMFunctionType(g->ctx, true);
552  llvm::GlobalValue::LinkageTypes linkage = llvm::GlobalValue::ExternalLinkage;
553  std::string functionName = sym->name;
555  // If we treat generic as smth, we should have appropriate mangling
556  if (g->target->getISA() == Target::GENERIC &&
557  !g->target->getTreatGenericAsSmth().empty())
558  functionName += std::string("_") + g->target->getTreatGenericAsSmth();
559  else
560  functionName += std::string("_") + g->target->GetISAString();
561  }
562 #ifdef ISPC_NVPTX_ENABLED
563  if (g->target->getISA() == Target::NVPTX)
564  {
565  functionName += std::string("___export"); /* add ___export to the end, for ptxcc to recognize it is exported */
566 #if 0
567  llvm::NamedMDNode* annotations =
568  m->module->getOrInsertNamedMetadata("nvvm.annotations");
569  llvm::SmallVector<llvm::Value*, 3> av;
570  av.push_back(function);
571  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
572  av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1));
573  annotations->addOperand(llvm::MDNode::get(*g->ctx, av));
574 #endif
575  }
576 #endif /* ISPC_NVPTX_ENABLED */
577  llvm::Function *appFunction =
578  llvm::Function::Create(ftype, linkage, functionName.c_str(), m->module);
579  appFunction->setDoesNotThrow();
580 
581 #if ISPC_LLVM_VERSION < ISPC_LLVM_5_0
582  // We should iterate from 1 because zero parameter is return.
583  // We should iterate till getNumParams instead of getNumParams+1 because new
584  // function is export function and doesn't contain the last parameter "mask".
585  for (int i = 1; i < function->getFunctionType()->getNumParams(); i++) {
586  if (function->doesNotAlias(i)) {
587  appFunction->setDoesNotAlias(i);
588  }
589  }
590 #else // LLVM 5.0+
591  for (int i = 0; i < function->getFunctionType()->getNumParams()-1; i++) {
592  if (function->hasParamAttribute(i, llvm::Attribute::NoAlias)) {
593  appFunction->addParamAttr(i, llvm::Attribute::NoAlias);
594  }
595  }
596 #endif
597  g->target->markFuncWithTargetAttr(appFunction);
598 
599  if (appFunction->getName() != functionName) {
600  // this was a redefinition for which we already emitted an
601  // error, so don't worry about this one...
602  appFunction->eraseFromParent();
603  }
604  else {
605  // And emit the code again
606  FunctionEmitContext ec(this, sym, appFunction, firstStmtPos);
607  emitCode(&ec, appFunction, firstStmtPos);
608  if (m->errorCount == 0) {
609  sym->exportedFunction = appFunction;
610  }
611 #ifdef ISPC_NVPTX_ENABLED
612  if (g->target->getISA() == Target::NVPTX)
613  {
614  llvm::NamedMDNode* annotations =
615  m->module->getOrInsertNamedMetadata("nvvm.annotations");
616 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
617 
618  llvm::SmallVector<llvm::Metadata*, 3> av;
619  av.push_back(llvm::ValueAsMetadata::get(appFunction));
620  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
621  av.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1)));
622  annotations->addOperand(llvm::MDNode::get(*g->ctx, llvm::ArrayRef<llvm::Metadata*>(av)));
623 #else
624  llvm::SmallVector<llvm::Value*, 3> av;
625  av.push_back(appFunction);
626  av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
627  av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1));
628  annotations->addOperand(llvm::MDNode::get(*g->ctx, av));
629 #endif
630 
631  }
632 #endif /* ISPC_NVPTX_ENABLED */
633  }
634  }
635  }
636  }
637 }
bool IsVoidType() const
Definition: type.cpp:250
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:1296
std::string GetString() const
Definition: type.cpp:3127
llvm::Value * AddElementOffset(llvm::Value *basePtr, int elementNum, const Type *ptrType, const char *name=NULL, const PointerType **resultPtrType=NULL)
Definition: ctx.cpp:2598
Opt opt
Definition: ispc.h:547
Declaration of the FunctionEmitContext class
void EmitVariableDebugInfo(Symbol *sym)
Definition: ctx.cpp:1773
Symbol * taskIndexSym
Definition: func.h:63
Symbol * threadIndexSym
Definition: func.h:62
void BranchInst(llvm::BasicBlock *block)
Definition: ctx.cpp:3485
Module * m
Definition: ispc.cpp:93
Interface class for statements in the ispc language.
Definition: stmt.h:49
virtual void EmitCode(FunctionEmitContext *ctx) const =0
Target * target
Definition: ispc.h:549
llvm::Value * LoadInst(llvm::Value *ptr, llvm::Value *mask, const Type *ptrType, const char *name=NULL, bool one_elem=false)
Definition: ctx.cpp:2817
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:3051
bool NoOmitFramePointer
Definition: ispc.h:572
#define Assert(expr)
Definition: ispc.h:172
std::vector< Symbol * > args
Definition: func.h:59
llvm::FunctionType * LLVMFunctionType(llvm::LLVMContext *ctx, bool disableMask=false) const
Definition: type.cpp:3319
Symbol * taskIndexSym2
Definition: func.h:66
const char * GetISAString() const
Definition: ispc.cpp:1315
void AddInstrumentationPoint(const char *note)
Definition: ctx.cpp:1660
std::string name
Definition: sym.h:71
Symbol * LookupVariable(const char *name)
Definition: sym.cpp:131
llvm::BasicBlock * GetCurrentBasicBlock()
Definition: ctx.cpp:470
llvm::BasicBlock * CreateBasicBlock(const char *name)
Definition: ctx.cpp:1602
header file with declarations for symbol and symbol table classes.
bool disableMaskAllOnOptimizations
Definition: ispc.h:476
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:3344
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:92
bool debugPrint
Definition: ispc.h:566
llvm::Value * GetFunctionMask()
Definition: ctx.cpp:482
bool disableCoherentControlFlow
Definition: ispc.h:498
Symbol * taskIndexSym1
Definition: func.h:65
const std::string & GetParameterName(int i) const
Definition: type.cpp:3407
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:627
Header file with declarations for various LLVM utility stuff.
const Type * GetParameterType(int i) const
Definition: type.cpp:3386
ISA getISA() const
Definition: ispc.h:269
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:136
Symbol * taskCountSym1
Definition: func.h:65
void markFuncWithTargetAttr(llvm::Function *func)
Definition: ispc.cpp:1485
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:925
static llvm::Type * Int32Type
Definition: llvmutil.h:78
void SetDebugPos(SourcePos pos)
Definition: ctx.cpp:1681
std::string getTreatGenericAsSmth() const
Definition: ispc.h:271
Representation of a function in a source file.
Symbol * sym
Definition: func.h:58
const bool isTask
Definition: type.h:944
void GenerateIR()
Definition: func.cpp:510
llvm::Value * GetFullMaskPointer()
Definition: ctx.cpp:501
Type representing a function (return type + argument types)
Definition: type.h:884
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:476
static llvm::VectorType * MaskType
Definition: llvmutil.h:90
const bool isUnmasked
Definition: type.h:957
int EstimateCost(ASTNode *root)
Definition: ast.cpp:352
void EmitFunctionParameterDebugInfo(Symbol *sym, int parameterNum)
Definition: ctx.cpp:1828
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:1418
const bool isExported
Definition: type.h:948
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:638
bool getMaskingIsFree() const
Definition: ispc.h:289
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:3866
int GetNumParameters() const
Definition: type.h:936
void SetFunctionMask(llvm::Value *val)
Definition: ctx.cpp:507
SymbolTable * symbolTable
Definition: module.h:155
llvm::Function * exportedFunction
Definition: sym.h:79
File with declarations for classes related to type representation.