Skip to content

Commit 3883e04

Browse files
authored
[LLVM 22] fix calls to llvm.lifetime.start (#5017)
1 parent eb2e827 commit 3883e04

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

gen/llvmhelpers.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -906,27 +906,18 @@ void DtoVarDeclaration(VarDeclaration *vd) {
906906

907907
// We also allocate a variable for zero-sized variables, because they are technically not `null` when loaded.
908908
// The x86_64 ABI "loads" zero-sized function arguments, and without an allocation ASan will report an error (Github #4816).
909-
llvm::Value *allocainst;
910-
bool isRealAlloca = false;
911909
LLType *lltype = DtoType(type); // void for noreturn
912910
if (lltype->isVoidTy()) {
913-
allocainst = getNullPtr();
914-
} else if (type != vd->type) {
915-
allocainst = DtoAlloca(type, vd->toChars());
916-
isRealAlloca = true;
911+
irLocal->value = getNullPtr();
917912
} else {
918-
allocainst = DtoAlloca(vd, vd->toChars());
919-
isRealAlloca = true;
920-
}
921-
922-
irLocal->value = allocainst;
913+
auto allocainst = type != vd->type ? DtoAlloca(type, vd->toChars())
914+
: DtoAlloca(vd, vd->toChars());
923915

924-
if (!lltype->isVoidTy())
916+
irLocal->value = allocainst;
925917
gIR->DBuilder.EmitLocalVariable(allocainst, vd);
926918

927-
// Lifetime annotation is only valid on alloca.
928-
if (isRealAlloca) {
929-
// The lifetime of a stack variable starts from the point it is declared
919+
// The lifetime of a stack variable starts from the
920+
// point it is declared
930921
gIR->funcGen().localVariableLifetimeAnnotator.addLocalVariable(
931922
allocainst, DtoConstUlong(size(type)));
932923
}

gen/variable_lifetime.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LocalVariableLifetimeAnnotator::LocalVariableLifetimeAnnotator(IRState &irs)
3737

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

40-
void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::Value *address,
40+
void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::AllocaInst *address,
4141
llvm::Value *size) {
4242
assert(address);
4343
assert(size);
@@ -52,8 +52,13 @@ void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::Value *address,
5252
scopes.back().variables.emplace_back(size, address);
5353

5454
// Emit lifetime start
55-
irs.CreateCallOrInvoke(getLLVMLifetimeStartFn(), {size, address}, "",
56-
true /*nothrow*/);
55+
irs.CreateCallOrInvoke(getLLVMLifetimeStartFn(),
56+
#if LDC_LLVM_VER >= 2200
57+
{address},
58+
#else
59+
{size, address},
60+
#endif
61+
"", true /*nothrow*/);
5762
}
5863

5964
// Emits end-of-lifetime annotation for all variables in current scope.
@@ -62,13 +67,21 @@ void LocalVariableLifetimeAnnotator::popScope() {
6267
return;
6368

6469
for (const auto &var : scopes.back().variables) {
70+
#if LDC_LLVM_VER < 2200
6571
auto size = var.first;
72+
#endif
6673
auto address = var.second;
6774

6875
assert(address);
6976

70-
irs.CreateCallOrInvoke(getLLVMLifetimeEndFn(), {size, address}, "",
71-
true /*nothrow*/);
77+
irs.CreateCallOrInvoke(getLLVMLifetimeEndFn(),
78+
#if LDC_LLVM_VER >= 2200
79+
{address},
80+
#else
81+
{size, address},
82+
#endif
83+
"", true /*nothrow*/);
84+
7285
}
7386
scopes.pop_back();
7487
}

gen/variable_lifetime.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ namespace llvm {
2121
class Function;
2222
class Type;
2323
class Value;
24+
class AllocaInst;
2425
}
2526
struct IRState;
2627

2728
struct LocalVariableLifetimeAnnotator {
2829
struct LocalVariableScope {
29-
std::vector<std::pair<llvm::Value *, llvm::Value *>> variables;
30+
std::vector<std::pair<llvm::Value *, llvm::AllocaInst *>> variables;
3031
};
3132
/// Stack of scopes, each scope can have multiple variables.
3233
std::vector<LocalVariableScope> scopes;
@@ -52,5 +53,5 @@ struct LocalVariableLifetimeAnnotator {
5253
void popScope();
5354

5455
/// Register a new local variable for lifetime annotation.
55-
void addLocalVariable(llvm::Value *address, llvm::Value *size);
56+
void addLocalVariable(llvm::AllocaInst *address, llvm::Value *size);
5657
};

runtime/druntime/src/ldc/intrinsics.di

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ else version (LDC_LLVM_1801) enum LLVM_version = 1801;
2727
else version (LDC_LLVM_1901) enum LLVM_version = 1901;
2828
else version (LDC_LLVM_2001) enum LLVM_version = 2001;
2929
else version (LDC_LLVM_2100) enum LLVM_version = 2100;
30+
else version (LDC_LLVM_2200) enum LLVM_version = 2200;
3031
else static assert(false, "LDC LLVM version not supported");
3132

3233
enum LLVM_atleast(int major) = (LLVM_version >= major * 100);

0 commit comments

Comments
 (0)