Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 67 additions & 11 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,64 @@ void DtoResolveVariable(VarDeclaration *vd) {
}
}

namespace {
bool eval(VarDeclaration *vd, Expression *e) {

auto ve = e->isVarExp();
if (!ve) {
return false;
}
VarDeclaration *v = nullptr;
if (ve)
v = ve->var->isVarDeclaration();
return vd == v;
}
bool walk(VarDeclaration *vd, CommaExp *ce) {
IF_LOG Logger::println("ce = %s", ce->toChars());
if (auto ce2 = ce->e2->isCommaExp()) {
if (walk(vd, ce2))
return true;
}
if (auto ce1 = ce->e1->isCommaExp()) {
if (walk(vd, ce1))
return true;
}
if (eval(vd, ce->e2))
return true;
if (eval(vd, ce->e1))
return true;
return false;
}
bool varIsSret(VarDeclaration *vd, IrFunction *f) {
if (!f->sretArg)
return false;
auto fd = f->decl;
auto rets = fd->returns;
if (!rets)
return false;
#if LDC_LLVM_VER >= 1800
#define startswith starts_with
#endif
llvm::StringRef name = vd->ident->toChars();
if (name.startswith("__tmpfordtor") ||name.startswith("__sl")) {
return true;
}
#if LDC_LLVM_VER >= 1800
#undef startswith
#endif
for (d_size_t i = 0; i < rets->length; i++) {
auto rs = (*rets)[i];
Expression *e = rs->exp;
CommaExp *ce = e->isCommaExp();
if (ce) {
if (walk(vd, ce)) return true;
}
if (eval(vd, e))
return true;
}
return false;
}
}
/******************************************************************************
* DECLARATION EXP HELPER
******************************************************************************/
Expand Down Expand Up @@ -906,27 +964,25 @@ void DtoVarDeclaration(VarDeclaration *vd) {

// We also allocate a variable for zero-sized variables, because they are technically not `null` when loaded.
// The x86_64 ABI "loads" zero-sized function arguments, and without an allocation ASan will report an error (Github #4816).
llvm::Value *allocainst;
bool isRealAlloca = false;
LLType *lltype = DtoType(type); // void for noreturn
if (lltype->isVoidTy()) {
allocainst = getNullPtr();
} else if (type != vd->type) {
irLocal->value = getNullPtr();
}

llvm::AllocaInst *allocainst;

if (type != vd->type) {
allocainst = DtoAlloca(type, vd->toChars());
isRealAlloca = true;
} else {
allocainst = DtoAlloca(vd, vd->toChars());
isRealAlloca = true;
}

irLocal->value = allocainst;

if (!lltype->isVoidTy())
gIR->DBuilder.EmitLocalVariable(allocainst, vd);
gIR->DBuilder.EmitLocalVariable(allocainst, vd);

// Lifetime annotation is only valid on alloca.
if (isRealAlloca) {
// The lifetime of a stack variable starts from the point it is declared
// The lifetime of a stack variable starts from the point it is declared
if (!vd->isParameter() && !varIsSret(vd, gIR->func())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not simply an allocainst->isAllocaInst() ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is that isRealAlloca is apparently not what is says it is. Seems to me that the best way to fix that is to replace it with an actual check whether allocainst is a real alloca instruction.

gIR->funcGen().localVariableLifetimeAnnotator.addLocalVariable(
allocainst, DtoConstUlong(size(type)));
}
Expand Down
21 changes: 16 additions & 5 deletions gen/variable_lifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LocalVariableLifetimeAnnotator::LocalVariableLifetimeAnnotator(IRState &irs)

void LocalVariableLifetimeAnnotator::pushScope() { scopes.emplace_back(); }

void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::Value *address,
void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::AllocaInst *address,
llvm::Value *size) {
assert(address);
assert(size);
Expand All @@ -52,8 +52,13 @@ void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::Value *address,
scopes.back().variables.emplace_back(size, address);

// Emit lifetime start
irs.CreateCallOrInvoke(getLLVMLifetimeStartFn(), {size, address}, "",
true /*nothrow*/);
irs.CreateCallOrInvoke(getLLVMLifetimeStartFn(),
#if LDC_LLVM_VER >= 2100
{address},
#else
{size, address},
#endif
"", true /*nothrow*/);
}

// Emits end-of-lifetime annotation for all variables in current scope.
Expand All @@ -67,8 +72,14 @@ void LocalVariableLifetimeAnnotator::popScope() {

assert(address);

irs.CreateCallOrInvoke(getLLVMLifetimeEndFn(), {size, address}, "",
true /*nothrow*/);
irs.CreateCallOrInvoke(getLLVMLifetimeEndFn(),
#if LDC_LLVM_VER >= 2100
{address},
#else
{size, address},
#endif
"", true /*nothrow*/);

}
scopes.pop_back();
}
Expand Down
5 changes: 3 additions & 2 deletions gen/variable_lifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ namespace llvm {
class Function;
class Type;
class Value;
class AllocaInst;
}
struct IRState;

struct LocalVariableLifetimeAnnotator {
struct LocalVariableScope {
std::vector<std::pair<llvm::Value *, llvm::Value *>> variables;
std::vector<std::pair<llvm::Value *, llvm::AllocaInst *>> variables;
};
/// Stack of scopes, each scope can have multiple variables.
std::vector<LocalVariableScope> scopes;
Expand All @@ -52,5 +53,5 @@ struct LocalVariableLifetimeAnnotator {
void popScope();

/// Register a new local variable for lifetime annotation.
void addLocalVariable(llvm::Value *address, llvm::Value *size);
void addLocalVariable(llvm::AllocaInst *address, llvm::Value *size);
};
Loading