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
25 changes: 11 additions & 14 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,30 +897,27 @@ 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();
return;
}

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
gIR->funcGen().localVariableLifetimeAnnotator.addLocalVariable(
allocainst, DtoConstUlong(size(type)));
}
// The lifetime of a stack variable starts from the point it is declared
gIR->funcGen().localVariableLifetimeAnnotator.addLocalVariable(
allocainst, DtoConstUlong(size(type)));
}

IF_LOG Logger::cout() << "llvm value for decl: " << *getIrLocal(vd)->value
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);
};
1 change: 1 addition & 0 deletions runtime/druntime/src/ldc/intrinsics.di
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ else version (LDC_LLVM_1801) enum LLVM_version = 1801;
else version (LDC_LLVM_1901) enum LLVM_version = 1901;
else version (LDC_LLVM_2001) enum LLVM_version = 2001;
else version (LDC_LLVM_2100) enum LLVM_version = 2100;
else version (LDC_LLVM_2200) enum LLVM_version = 2200;
else static assert(false, "LDC LLVM version not supported");

enum LLVM_atleast(int major) = (LLVM_version >= major * 100);
Expand Down
Loading