Implemented atomic intrinsics for llvm 3.0

Unline older versions, llvm 3.0 provides atomic instructions, not intrinsics.
The patch wraps the instructions into intrinsics, so they will be accessible
from d code.
This commit is contained in:
Alexey Prokhin
2011-10-25 15:43:49 +04:00
parent 81426b8cc8
commit 45426ca60d
5 changed files with 158 additions and 3 deletions
+75 -1
View File
@@ -1176,7 +1176,64 @@ void PragmaDeclaration::semantic(Scope *sc)
}
llvm_internal = LLVMva_arg;
}
// pragma(fence) { templdecl(s) }
else if (ident == Id::fence)
{
if (args && args->dim > 0)
{
error("takes no parameters");
fatal();
}
llvm_internal = LLVMfence;
}
// pragma(atomic_load) { templdecl(s) }
else if (ident == Id::atomic_load)
{
if (args && args->dim > 0)
{
error("takes no parameters");
fatal();
}
llvm_internal = LLVMatomic_load;
}
// pragma(atomic_store) { templdecl(s) }
else if (ident == Id::atomic_store)
{
if (args && args->dim > 0)
{
error("takes no parameters");
fatal();
}
llvm_internal = LLVMatomic_store;
}
// pragma(atomic_cmp_xchg) { templdecl(s) }
else if (ident == Id::atomic_cmp_xchg)
{
if (args && args->dim > 0)
{
error("takes no parameters");
fatal();
}
llvm_internal = LLVMatomic_cmp_xchg;
}
// pragma(atomic_rmw, "string") { templdecl(s) }
else if (ident == Id::atomic_rmw)
{
Expression* expr = (Expression *)args->data[0];
expr = expr->semantic(sc);
if (!args || args->dim != 1 || !parseStringExp(expr, arg1str))
{
error("requires exactly 1 string literal parameter");
fatal();
}
llvm_internal = LLVMatomic_rmw;
}
// pragma(ldc, "string") { templdecl(s) }
else if (ident == Id::ldc)
{
@@ -1288,8 +1345,24 @@ void PragmaDeclaration::semantic(Scope *sc)
}
break;
case LLVMatomic_rmw:
if (TemplateDeclaration* td = s->isTemplateDeclaration())
{
td->llvmInternal = llvm_internal;
td->intrinsicName = arg1str;
}
else
{
error("the '%s' pragma is only allowed on template declarations", ident->toChars());
fatal();
}
break;
case LLVMva_start:
case LLVMva_arg:
case LLVMatomic_load:
case LLVMatomic_store:
case LLVMatomic_cmp_xchg:
if (TemplateDeclaration* td = s->isTemplateDeclaration())
{
if (td->parameters->dim != 1)
@@ -1318,6 +1391,7 @@ void PragmaDeclaration::semantic(Scope *sc)
case LLVMva_copy:
case LLVMva_end:
case LLVMfence:
if (FuncDeclaration* fd = s->isFuncDeclaration())
{
fd->llvmInternal = llvm_internal;
+5
View File
@@ -281,6 +281,11 @@ Msgtable msgtable[] =
{ "ldc" },
{ "allow_inline" },
{ "llvm_inline_asm" },
{ "fence" },
{ "atomic_load" },
{ "atomic_store" },
{ "atomic_cmp_xchg" },
{ "atomic_rmw" },
#endif
// For special functions
+6 -1
View File
@@ -9,5 +9,10 @@ enum
LLVMva_copy,
LLVMva_end,
LLVMva_arg,
LLVMinline_asm
LLVMinline_asm,
LLVMfence,
LLVMatomic_store,
LLVMatomic_load,
LLVMatomic_cmp_xchg,
LLVMatomic_rmw
};
+71
View File
@@ -935,6 +935,77 @@ DValue* CallExp::toElem(IRState* p)
if (expv->getType()->toBasetype()->ty != Tint32)
expv = DtoCast(loc, expv, Type::tint32);
return new DImValue(type, p->ir->CreateAlloca(LLType::getInt8Ty(gIR->context()), expv->getRVal(), ".alloca"));
// fence instruction
} else if (fndecl->llvmInternal == LLVMfence) {
gIR->ir->CreateFence(llvm::AtomicOrdering(((Expression*)arguments->data[0])->toInteger()));
return NULL;
// atomic store instruction
} else if (fndecl->llvmInternal == LLVMatomic_store) {
Expression* exp1 = (Expression*)arguments->data[0];
Expression* exp2 = (Expression*)arguments->data[1];
int atomicOrdering = ((Expression*)arguments->data[2])->toInteger();
LLValue* val = exp1->toElem(p)->getRVal();
LLValue* ptr = exp2->toElem(p)->getRVal();
llvm::StoreInst* ret = gIR->ir->CreateStore(val, ptr, "tmp");
ret->setAtomic(llvm::AtomicOrdering(atomicOrdering));
ret->setAlignment(exp1->type->alignsize());
return NULL;
// atomic load instruction
} else if (fndecl->llvmInternal == LLVMatomic_load) {
Expression* exp = (Expression*)arguments->data[0];
int atomicOrdering = ((Expression*)arguments->data[1])->toInteger();
LLValue* ptr = exp->toElem(p)->getRVal();
Type* retType = exp->type->nextOf();
llvm::LoadInst* val = gIR->ir->CreateLoad(ptr, "tmp");
val->setAlignment(retType->alignsize());
val->setAtomic(llvm::AtomicOrdering(atomicOrdering));
return new DImValue(retType, val);
// cmpxchg instruction
} else if (fndecl->llvmInternal == LLVMatomic_cmp_xchg) {
Expression* exp1 = (Expression*)arguments->data[0];
Expression* exp2 = (Expression*)arguments->data[1];
Expression* exp3 = (Expression*)arguments->data[2];
int atomicOrdering = ((Expression*)arguments->data[3])->toInteger();
LLValue* ptr = exp1->toElem(p)->getRVal();
LLValue* cmp = exp2->toElem(p)->getRVal();
LLValue* val = exp3->toElem(p)->getRVal();
LLValue* ret = gIR->ir->CreateAtomicCmpXchg(ptr, cmp, val, llvm::AtomicOrdering(atomicOrdering));
return new DImValue(exp3->type, ret);
// atomicrmw instruction
} else if (fndecl->llvmInternal == LLVMatomic_rmw) {
static char *ops[] = {
"xchg",
"add",
"sub",
"and",
"nand",
"or",
"xor",
"max",
"min",
"umax",
"umin",
0
};
int op = 0;
for (; ; ++op) {
if (ops[op] == 0) {
error("unknown atomic_rmw operation %s", fndecl->intrinsicName.c_str());
return NULL;
}
if (fndecl->intrinsicName == ops[op])
break;
}
Expression* exp1 = (Expression*)arguments->data[0];
Expression* exp2 = (Expression*)arguments->data[1];
int atomicOrdering = ((Expression*)arguments->data[2])->toInteger();
LLValue* ptr = exp1->toElem(p)->getRVal();
LLValue* val = exp2->toElem(p)->getRVal();
LLValue* ret = gIR->ir->CreateAtomicRMW(llvm::AtomicRMWInst::BinOp(op), ptr, val,
llvm::AtomicOrdering(atomicOrdering));
return new DImValue(exp2->type, ret);
}
}
return DtoCallFunction(loc, type, fnval, arguments);