added experimental support for uniform variables, not only arrays. makes applications slower
This commit is contained in:
2
ctx.cpp
2
ctx.cpp
@@ -1848,7 +1848,7 @@ static llvm::Value* lAddWarpOffset(FunctionEmitContext *ctx, llvm::Value *value)
|
|||||||
return llvm::GetElementPtrInst::Create(value, __offset, "warpOffset_gep", ctx->GetCurrentBasicBlock());
|
return llvm::GetElementPtrInst::Create(value, __offset, "warpOffset_gep", ctx->GetCurrentBasicBlock());
|
||||||
}
|
}
|
||||||
|
|
||||||
static llvm::Value* lConvertGepToGenericPtr(FunctionEmitContext *ctx, llvm::Value *value, const SourcePos ¤tPos)
|
llvm::Value* lConvertGepToGenericPtr(FunctionEmitContext *ctx, llvm::Value *value, const SourcePos ¤tPos)
|
||||||
{
|
{
|
||||||
if (!value->getType()->isPointerTy() || g->target->getISA() != Target::NVPTX)
|
if (!value->getType()->isPointerTy() || g->target->getISA() != Target::NVPTX)
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
90
stmt.cpp
90
stmt.cpp
@@ -142,6 +142,7 @@ lHasUnsizedArrays(const Type *type) {
|
|||||||
return lHasUnsizedArrays(at->GetElementType());
|
return lHasUnsizedArrays(at->GetElementType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern llvm::Value* lConvertGepToGenericPtr(FunctionEmitContext *ctx, llvm::Value *value, const SourcePos ¤tPos);
|
||||||
|
|
||||||
void
|
void
|
||||||
DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
||||||
@@ -258,8 +259,7 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
|||||||
ctx->EmitVariableDebugInfo(sym);
|
ctx->EmitVariableDebugInfo(sym);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (sym->type->IsArrayType() &&
|
if (sym->type->IsUniformType() &&
|
||||||
sym->type->IsUniformType() &&
|
|
||||||
g->target->getISA() == Target::NVPTX)
|
g->target->getISA() == Target::NVPTX)
|
||||||
{
|
{
|
||||||
/* deal with "const uniform" or "uniform" arrays for nvptx target */
|
/* deal with "const uniform" or "uniform" arrays for nvptx target */
|
||||||
@@ -273,16 +273,9 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
|||||||
"Please use \"varying\", \"const static uniform\" or define initializer in the global scope.",
|
"Please use \"varying\", \"const static uniform\" or define initializer in the global scope.",
|
||||||
sym->name.c_str());
|
sym->name.c_str());
|
||||||
|
|
||||||
llvm::Constant *cinit = NULL;
|
|
||||||
llvm::Type *llvmTypeUn;
|
|
||||||
int addressSpace;
|
|
||||||
if (sym->type->IsConstType())
|
if (sym->type->IsConstType())
|
||||||
{
|
{
|
||||||
#if 1
|
llvm::Constant *cinit = NULL;
|
||||||
addressSpace = 4; /* constant */
|
|
||||||
#else
|
|
||||||
addressSpace = 0; /* use global for now */
|
|
||||||
#endif
|
|
||||||
if (initExpr != NULL) {
|
if (initExpr != NULL) {
|
||||||
if (PossiblyResolveFunctionOverloads(initExpr, sym->type) == false)
|
if (PossiblyResolveFunctionOverloads(initExpr, sym->type) == false)
|
||||||
continue;
|
continue;
|
||||||
@@ -310,12 +303,25 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
|||||||
}
|
}
|
||||||
if (cinit == NULL)
|
if (cinit == NULL)
|
||||||
cinit = llvm::Constant::getNullValue(llvmType);
|
cinit = llvm::Constant::getNullValue(llvmType);
|
||||||
llvmTypeUn = llvmType;
|
|
||||||
|
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=*/4); /* constant address space */
|
||||||
|
// Tell the FunctionEmitContext about the variable
|
||||||
|
ctx->EmitVariableDebugInfo(sym);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* fails if pointer passed to function argument, need conversion beforehand */
|
/* fails if pointer passed to function argument, need conversion beforehand */
|
||||||
addressSpace = 3; /* local */
|
llvm::Constant *cinit = NULL;
|
||||||
const ArrayType *at = CastType<ArrayType>(sym->type);
|
const ArrayType *at = CastType<ArrayType>(sym->type);
|
||||||
const int nel = at->GetElementCount();
|
const int nel = at->GetElementCount();
|
||||||
/* we must scale # elements by 4, because a thread-block will run 4 warps
|
/* we must scale # elements by 4, because a thread-block will run 4 warps
|
||||||
@@ -326,12 +332,11 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
|||||||
*/
|
*/
|
||||||
const int nel4 = nel*4;
|
const int nel4 = nel*4;
|
||||||
ArrayType nat(at->GetElementType(), nel4);
|
ArrayType nat(at->GetElementType(), nel4);
|
||||||
llvmTypeUn = nat.LLVMType(g->ctx);
|
llvm::Type *llvmType = nat.LLVMType(g->ctx);
|
||||||
cinit = llvm::UndefValue::get(llvmTypeUn);
|
cinit = llvm::UndefValue::get(llvmType);
|
||||||
}
|
|
||||||
|
|
||||||
sym->storagePtr =
|
sym->storagePtr =
|
||||||
new llvm::GlobalVariable(*m->module, llvmTypeUn,
|
new llvm::GlobalVariable(*m->module, llvmType,
|
||||||
sym->type->IsConstType(),
|
sym->type->IsConstType(),
|
||||||
llvm::GlobalValue::InternalLinkage,
|
llvm::GlobalValue::InternalLinkage,
|
||||||
cinit,
|
cinit,
|
||||||
@@ -340,14 +345,57 @@ DeclStmt::EmitCode(FunctionEmitContext *ctx) const {
|
|||||||
llvm::Twine("_") + sym->name.c_str(),
|
llvm::Twine("_") + sym->name.c_str(),
|
||||||
NULL,
|
NULL,
|
||||||
llvm::GlobalVariable::NotThreadLocal,
|
llvm::GlobalVariable::NotThreadLocal,
|
||||||
addressSpace);
|
/*AddressSpace=*/3);
|
||||||
#if 0
|
|
||||||
llvm::GlobalVariable *var = llvm::dyn_cast<llvm::GlobalVariable>(sym->storagePtr);
|
|
||||||
var->setAlignment(128);
|
|
||||||
#endif
|
|
||||||
// Tell the FunctionEmitContext about the variable
|
// Tell the FunctionEmitContext about the variable
|
||||||
ctx->EmitVariableDebugInfo(sym);
|
ctx->EmitVariableDebugInfo(sym);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else if (
|
||||||
|
sym->type->IsUniformType() &&
|
||||||
|
g->target->getISA() == Target::NVPTX)
|
||||||
|
{
|
||||||
|
#if 1
|
||||||
|
// For non-static variables, allocate storage on the stack
|
||||||
|
sym->storagePtr = ctx->AllocaInst(llvmType, sym->name.c_str());
|
||||||
|
#else
|
||||||
|
PerformanceWarning(sym->pos,
|
||||||
|
"\"uniform\" variables might be slow with \"nvptx\" target. "
|
||||||
|
"Please use \"varying\" if possible.");
|
||||||
|
|
||||||
|
ArrayType nat(sym->type, 4);
|
||||||
|
llvm::Type *llvmType = nat.LLVMType(g->ctx);
|
||||||
|
llvm::Constant *cinit = llvm::UndefValue::get(llvmType);
|
||||||
|
|
||||||
|
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);
|
||||||
|
sym->storagePtr = lConvertGepToGenericPtr(ctx, sym->storagePtr, sym->pos);
|
||||||
|
llvm::PointerType *ptrTy =
|
||||||
|
llvm::PointerType::get(sym->type->LLVMType(g->ctx),0);
|
||||||
|
sym->storagePtr = ctx->BitCastInst(sym->storagePtr, ptrTy, "uniform_alloc");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// Tell the FunctionEmitContext about the variable; must do
|
||||||
|
// this before the initializer stuff.
|
||||||
|
ctx->EmitVariableDebugInfo(sym);
|
||||||
|
|
||||||
|
if (initExpr == 0 && sym->type->IsConstType())
|
||||||
|
Error(sym->pos, "Missing initializer for const variable "
|
||||||
|
"\"%s\".", sym->name.c_str());
|
||||||
|
|
||||||
|
// And then get it initialized...
|
||||||
|
sym->parentFunction = ctx->GetFunction();
|
||||||
|
InitSymbol(sym->storagePtr, sym->type, initExpr, ctx, sym->pos);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// For non-static variables, allocate storage on the stack
|
// For non-static variables, allocate storage on the stack
|
||||||
|
|||||||
Reference in New Issue
Block a user