From fd17ad236a234533a1b2de58f35575c4034fa6bc Mon Sep 17 00:00:00 2001 From: Evghenii Date: Tue, 12 Nov 2013 14:05:12 +0100 Subject: [PATCH] export functions are now also generated... next add proper CDP calls.. --- ctx.cpp | 12 +++++++++--- func.cpp | 32 +++++++++++++++++++++----------- module.cpp | 1 + 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ctx.cpp b/ctx.cpp index 45130b66..8355bbc6 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -3595,7 +3595,7 @@ FunctionEmitContext::LaunchInst(llvm::Value *callee, } else /* isPTX == true */ { - assert(0); /* must only be called in export */ + //assert(0); /* must only be called in export */ // assert(g->target->getISA() != Target::NVPTX64); if (callee == NULL) { @@ -3606,6 +3606,7 @@ FunctionEmitContext::LaunchInst(llvm::Value *callee, launchedTasks = true; AssertPos(currentPos, llvm::isa(callee)); +#if 0 llvm::Type *argType = (llvm::dyn_cast(callee))->arg_begin()->getType(); AssertPos(currentPos, llvm::PointerType::classof(argType)); @@ -3614,6 +3615,7 @@ FunctionEmitContext::LaunchInst(llvm::Value *callee, AssertPos(currentPos, llvm::StructType::classof(pt->getElementType())); llvm::StructType *argStructType = static_cast(pt->getElementType()); +#endif llvm::Function *falloc = m->module->getFunction("CUDAAlloc"); AssertPos(currentPos, falloc != NULL); @@ -3648,9 +3650,13 @@ FunctionEmitContext::LaunchInst(llvm::Value *callee, * this, and other parts are reverse enginered via * cpp(clang++ -S -emit-llvm)->IR and then IR(llc -march=cpp)->cpp */ - for (unsigned int i = 0; i < argVals.size(); ++i) + llvm::Function *func = llvm::dyn_cast(callee); + llvm::Function::arg_iterator argIter =func->arg_begin(); + llvm::Function::arg_iterator argIterEnd =func->arg_end(); + int i = 0; + for (; argIter != argIterEnd; argIter++, i++) { - llvm::Type* type = argStructType->getElementType(i); + llvm::Type* type = argIter->getType(); //argStructType->getElementType(i); llvm::Value* ptr_arg1_addr = AllocaInst(type, "argptr"); StoreInst(argVals[i], ptr_arg1_addr); diff --git a/func.cpp b/func.cpp index 6417df7a..e2e8ba84 100644 --- a/func.cpp +++ b/func.cpp @@ -354,15 +354,16 @@ Function::emitCode(FunctionEmitContext *ctx, llvm::Function *function, Assert(++argIter == function->arg_end()); } -#if 0 - llvm::NamedMDNode* annotations = - m->module->getOrInsertNamedMetadata("nvvm.annotations"); - llvm::SmallVector av; - av.push_back(function); - av.push_back(llvm::MDString::get(*g->ctx, "kernel")); - av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1)); - annotations->addOperand(llvm::MDNode::get(*g->ctx, av)); -#endif + if (g->target->isPTX() && g->target->getISA() == Target::NVPTX64) + { + llvm::NamedMDNode* annotations = + m->module->getOrInsertNamedMetadata("nvvm.annotations"); + llvm::SmallVector av; + av.push_back(function); + av.push_back(llvm::MDString::get(*g->ctx, "kernel")); + av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1)); + annotations->addOperand(llvm::MDNode::get(*g->ctx, av)); + } } } else { @@ -538,7 +539,6 @@ Function::GenerateIR() { /* export function with NVPTX64 target should be emitted host architecture */ #if 0 const FunctionType *func_type= CastType(sym->type); - /* exported functions are not supported yet */ if (g->target->getISA() == Target::NVPTX64 && func_type->isExported) return; #endif @@ -567,7 +567,7 @@ Function::GenerateIR() { // the application can call it const FunctionType *type = CastType(sym->type); Assert(type != NULL); - if (type->isExported) { // && g->target->getISA() != Target::NVPTX64) { + if (type->isExported) { // && g->target->getISA() != Target::VPTX64) { if (!type->isTask) { llvm::FunctionType *ftype = type->LLVMFunctionType(g->ctx, true); llvm::GlobalValue::LinkageTypes linkage = llvm::GlobalValue::ExternalLinkage; @@ -602,6 +602,16 @@ Function::GenerateIR() { FATAL("Function verificication failed"); } } + if (g->target->isPTX() && g->target->getISA() == Target::NVPTX64) + { + llvm::NamedMDNode* annotations = + m->module->getOrInsertNamedMetadata("nvvm.annotations"); + llvm::SmallVector av; + av.push_back(appFunction); + av.push_back(llvm::MDString::get(*g->ctx, "kernel")); + av.push_back(llvm::ConstantInt::get(llvm::IntegerType::get(*g->ctx,32), 1)); + annotations->addOperand(llvm::MDNode::get(*g->ctx, av)); + } } } } diff --git a/module.cpp b/module.cpp index def0c6d7..743e3adf 100644 --- a/module.cpp +++ b/module.cpp @@ -796,6 +796,7 @@ Module::AddFunctionDeclaration(const std::string &name, #else // LLVM 3.1 and 3.3+ function->addFnAttr(llvm::Attribute::AlwaysInline); #endif + /* evghenii: on PTX target this must not be used, cause crash, dunno why */ if (functionType->isTask && g->target->getISA() != Target::NVPTX64) // This also applies transitively to members I think? #if defined(LLVM_3_1)