49 #include <llvm/IR/DerivedTypes.h> 50 #include <llvm/IR/Instructions.h> 51 #include <llvm/IR/Intrinsics.h> 52 #include <llvm/IR/LLVMContext.h> 53 #include <llvm/IR/Module.h> 54 #include <llvm/IR/Type.h> 56 #include "llvm/IR/LegacyPassManager.h" 57 #include <llvm/IR/CFG.h> 58 #include <llvm/IR/IRPrintingPasses.h> 59 #include <llvm/IR/Verifier.h> 60 #include <llvm/PassRegistry.h> 61 #include <llvm/Support/FileUtilities.h> 62 #include <llvm/Support/FormattedStream.h> 63 #include <llvm/Target/TargetMachine.h> 64 #include <llvm/Target/TargetOptions.h> 65 #include <llvm/Transforms/IPO.h> 67 #include <llvm/Support/ToolOutputFile.h> 80 printf(
"After typechecking function \"%s\":\n",
sym->
name.c_str());
82 printf(
"---------------------\n");
88 printf(
"After optimizing function \"%s\":\n",
sym->
name.c_str());
90 printf(
"---------------------\n");
96 printf(
"Add Function %s\n",
sym->
name.c_str());
109 if (paramSym == NULL)
110 Assert(strncmp(paramName,
"__anon_parameter_", 17) == 0);
111 args.push_back(paramSym);
114 if (paramSym != NULL && CastType<ReferenceType>(t) == NULL)
169 const llvm::Type *structArgType = structArgPtr->getType();
170 Assert(llvm::isa<llvm::PointerType>(structArgType));
171 const llvm::PointerType *pt = llvm::dyn_cast<
const llvm::PointerType>(structArgType);
173 Assert(llvm::isa<llvm::StructType>(pt->getElementType()));
210 function->addFnAttr(
"no-frame-pointer-elim",
"true");
212 function->addFnAttr(
"target-features",
"+simd128");
220 if (type->
isTask ==
true) {
224 llvm::Function::arg_iterator argIter =
function->arg_begin();
226 llvm::Value *structParamPtr = &*(argIter++);
227 llvm::Value *threadIndex = &*(argIter++);
228 llvm::Value *threadCount = &*(argIter++);
229 llvm::Value *taskIndex = &*(argIter++);
230 llvm::Value *taskCount = &*(argIter++);
231 llvm::Value *taskIndex0 = &*(argIter++);
232 llvm::Value *taskIndex1 = &*(argIter++);
233 llvm::Value *taskIndex2 = &*(argIter++);
234 llvm::Value *taskCount0 = &*(argIter++);
235 llvm::Value *taskCount1 = &*(argIter++);
236 llvm::Value *taskCount2 = &*(argIter++);
239 for (
unsigned int i = 0; i <
args.size(); ++i)
244 int nArgs = (int)
args.size();
246 llvm::Value *ptr = ctx->
AddElementOffset(structParamPtr, nArgs, NULL,
"task_struct_mask");
247 llvm::Value *ptrval = ctx->
LoadInst(ptr, NULL,
"mask");
282 llvm::Function::arg_iterator argIter =
function->arg_begin();
283 for (
unsigned int i = 0; i <
args.size(); ++i, ++argIter) {
289 argIter->setName(argSym->
name.c_str());
304 if (argIter == function->arg_end()) {
311 argIter->setName(
"__mask");
315 Assert(++argIter == function->arg_end());
325 Debug(
code->
pos,
"Estimated cost for function \"%s\" = %d\n",
sym->
name.c_str(), costEstimate);
333 ((function->getAttributes().getFnAttributes().hasAttribute(llvm::Attribute::AlwaysInline) ==
false) &&
341 llvm::Value *allOn = ctx->
All(mask);
398 (pred_begin(ec.bblock) != pred_end(ec.bblock) || (ec.bblock == entryBBlock)))
399 Warning(
sym->
pos,
"Missing return statement in function returning \"%s\".",
421 if (function->empty() ==
false) {
430 Error(
sym->
pos,
"\n\'extern \"C\"\' function \"%s\" cannot be defined in ISPC.",
sym->
name.c_str());
440 if (sl && sl->
stmts.size() > 0 && sl->
stmts[0] != NULL)
441 firstStmtPos = sl->
stmts[0]->pos;
449 emitCode(&ec,
function, firstStmtPos);
459 llvm::GlobalValue::LinkageTypes linkage = llvm::GlobalValue::ExternalLinkage;
460 std::string functionName =
sym->
name;
465 llvm::Function *appFunction = llvm::Function::Create(ftype, linkage, functionName.c_str(),
m->
module);
466 appFunction->setDoesNotThrow();
468 for (
int i = 0; i <
function->getFunctionType()->getNumParams() - 1; i++) {
469 if (function->hasParamAttribute(i, llvm::Attribute::NoAlias)) {
470 appFunction->addParamAttr(i, llvm::Attribute::NoAlias);
475 if (appFunction->getName() != functionName) {
478 appFunction->eraseFromParent();
482 emitCode(&ec, appFunction, firstStmtPos);
llvm::Function * function
void InitializeLabelMap(Stmt *code)
llvm::Value * AddElementOffset(llvm::Value *basePtr, int elementNum, const Type *ptrType, const char *name=NULL, const PointerType **resultPtrType=NULL)
const Type * GetReturnType() const
Declaration of the FunctionEmitContext class
void EmitVariableDebugInfo(Symbol *sym)
void BranchInst(llvm::BasicBlock *block)
Interface class for statements in the ispc language.
llvm::Value * LoadInst(llvm::Value *ptr, llvm::Value *mask, const Type *ptrType, const char *name=NULL, bool one_elem=false)
ASTNode * TypeCheck(ASTNode *root)
const std::string & GetParameterName(int i) const
void emitCode(FunctionEmitContext *ctx, llvm::Function *function, SourcePos firstStmtPos)
llvm::Constant * LLVMMaskAllOn
llvm::Value * AllocaInst(llvm::Type *llvmType, const char *name=NULL, int align=0, bool atEntryBlock=true)
bool getMaskingIsFree() const
std::vector< Symbol * > args
void AddInstrumentationPoint(const char *note)
int GetNumParameters() const
Symbol * LookupVariable(const char *name)
llvm::BasicBlock * GetCurrentBasicBlock()
virtual void EmitCode(FunctionEmitContext *ctx) const =0
llvm::BasicBlock * CreateBasicBlock(const char *name)
header file with declarations for symbol and symbol table classes.
bool disableMaskAllOnOptimizations
File with declarations for classes related to statements in the language.
void StoreInst(llvm::Value *value, llvm::Value *ptr, const Type *ptrType=NULL)
llvm::Value * GetFunctionMask()
bool disableCoherentControlFlow
const FunctionType * GetType() const
bool mangleFunctionsWithTarget
Header file with declarations for various LLVM utility stuff.
Function(Symbol *sym, Stmt *code)
Representation of a range of positions in a source file.
void markFuncWithTargetAttr(llvm::Function *func)
void Error(SourcePos p, const char *fmt,...)
static llvm::Type * Int32Type
void SetDebugPos(SourcePos pos)
Representation of a function in a source file.
llvm::Value * GetFullMaskPointer()
const Type * GetParameterType(int i) const
const char * GetISAString() const
Type representing a function (return type + argument types)
Representation of a program symbol.
Interface class that defines the type abstraction.
std::vector< Stmt * > stmts
Expr abstract base class and expression implementations.
void SetCurrentBasicBlock(llvm::BasicBlock *bblock)
static llvm::VectorType * MaskType
int EstimateCost(ASTNode *root)
void Debug(SourcePos p, const char *fmt,...)
static void lCopyInTaskParameter(int i, llvm::Value *structArgPtr, const std::vector< Symbol *> &args, FunctionEmitContext *ctx)
void EmitFunctionParameterDebugInfo(Symbol *sym, int parameterNum)
const Function * parentFunction
Representation of a list of statements in the program.
llvm::Value * All(llvm::Value *mask)
virtual void Print(int indent) const =0
ASTNode * Optimize(ASTNode *root)
const Type * GetReturnType() const
Declaration of the Module class, which is the ispc-side representation of the results of compiling a ...
std::string GetString() const
void Warning(SourcePos p, const char *fmt,...)
llvm::Instruction * ReturnInst()
void SetFunctionMask(llvm::Value *val)
SymbolTable * symbolTable
llvm::Function * exportedFunction
File with declarations for classes related to type representation.
llvm::FunctionType * LLVMFunctionType(llvm::LLVMContext *ctx, bool disableMask=false) const