From 96597d371605d78d4a1a2c822c26f70b6e070868 Mon Sep 17 00:00:00 2001 From: Evghenii Date: Tue, 7 Jan 2014 11:07:54 +0100 Subject: [PATCH] identified where to add shared memory. other problems showed up.. --- ptxtestcc.sh | 6 ++++- stmt.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/ptxtestcc.sh b/ptxtestcc.sh index ad621b22..80b672ca 100755 --- a/ptxtestcc.sh +++ b/ptxtestcc.sh @@ -1,8 +1,12 @@ #!/bin/sh LLC=$HOME/usr/local/llvm/bin-trunk/bin/llc +DIS=$HOME/usr/local/llvm/bin-3.2/bin/llvm-dis + ISPC=ispc PTXCC=ptxcc -$(cat $1 |grep -v 'width'|$ISPC --target=nvptx --emit-llvm -o -|$LLC -march=nvptx64 -mcpu=sm_35 -o $1.ptx) && \ +PTXGEN=~/ptxgen +#$(cat $1 |grep -v 'width'|$ISPC --target=nvptx --emit-llvm -o -|$LLC -march=nvptx64 -mcpu=sm_35 -o $1.ptx) && \ +$(cat $1 |grep -v 'width'|$ISPC --target=nvptx --emit-llvm -o -|$DIS -o $1_32_ptx.ll && $PTXGEN $1_32_ptx.ll > $1.ptx) && \ $($PTXCC $1.ptx -Xptxas=-v -o $1.ptx.o) && \ nvcc -o test_nvptx test_static_nvptx.cpp examples_ptx/nvcc_helpers.cu examples_ptx/ispc_malloc.cpp $1.ptx.o -arch=sm_35 -Iexamples_ptx/ -D_CUDA_ -lcudadevrt -DTEST_SIG=$2 diff --git a/stmt.cpp b/stmt.cpp index 05d84a93..936141f5 100644 --- a/stmt.cpp +++ b/stmt.cpp @@ -206,9 +206,10 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const { } if (sym->storageClass == SC_STATIC) { - if (g->target->getISA() == Target::NVPTX) - if (!sym->type->IsConstType()) - Error(initExpr->pos, "Non-constant static variable ""\"%s\" is not supported with ""\"cuda\" target.", + + if (g->target->getISA() == Target::NVPTX && !sym->type->IsConstType()) + Error(sym->pos, + "Non-constant static variable ""\"%s\" is not supported with ""\"nvptx\" target.", sym->name.c_str()); // For static variables, we need a compile-time constant value @@ -247,10 +248,70 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const { llvm::Twine("static_") + llvm::Twine(sym->pos.first_line) + llvm::Twine("_") + sym->name.c_str()); +#if 0 + NULL, + llvm::GlobalVariable::NotThreadLocal, + 3); +#endif // Tell the FunctionEmitContext about the variable ctx->EmitVariableDebugInfo(sym); } else { +#if 0 /* PTX:: shared/uniform data types are here */ + if (sym->type->IsArrayType() && sym->type->IsUniformType() + && g->target->getISA() == Target::NVPTX) + { +#if 0 + if (initExpr != NULL) + Error(initExpr->pos, "Initializer for static variable " + "\"%s\" must be a constant.", sym->name.c_str()); +#endif + + PerformanceWarning(sym->pos, + "\"uniform\" arrays may be slow with \"nvptx\" target. Use \"varying\" if possible."); + + llvm::Constant *cinit = NULL; + if (initExpr != NULL) + { + if (PossiblyResolveFunctionOverloads(initExpr, sym->type) == false) + continue; + // FIXME: we only need this for function pointers; it was + // already done for atomic types and enums in + // DeclStmt::TypeCheck()... + if (dynamic_cast(initExpr) == NULL) { + initExpr = TypeConvertExpr(initExpr, sym->type, + "initializer"); + // FIXME: and this is only needed to re-establish + // constant-ness so that GetConstant below works for + // constant artithmetic expressions... + initExpr = ::Optimize(initExpr); + } + + cinit = initExpr->GetConstant(sym->type); + } + if (cinit == NULL) + cinit = llvm::Constant::getNullValue(llvmType); + + // Allocate space for the static variable in global scope, so + // that it persists across function calls + sym->storagePtr = + new llvm::GlobalVariable(*m->module, llvmType, + sym->type->IsConstType(), + llvm::GlobalValue::InternalLinkage, cinit, + llvm::Twine("local") + + llvm::Twine(sym->pos.first_line) + + llvm::Twine("_") + sym->name.c_str(), + NULL, + llvm::GlobalVariable::NotThreadLocal, + /*AddressSpace=*/ 3); + llvm::GlobalVariable *var = llvm::dyn_cast(sym->storagePtr); + var->setAlignment(128); + // Tell the FunctionEmitContext about the variable + ctx->EmitVariableDebugInfo(sym); + } + else +#endif + { // For non-static variables, allocate storage on the stack sym->storagePtr = ctx->AllocaInst(llvmType, sym->name.c_str()); @@ -261,6 +322,7 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const { // And then get it initialized... sym->parentFunction = ctx->GetFunction(); InitSymbol(sym->storagePtr, sym->type, initExpr, ctx, sym->pos); + } } } }