Skip to content

Commit 7547508

Browse files
committed
Revert "[StackSafety] Skip ambiguous lifetime analysis"
This reverts commit 0b2616a. Crashes with safe-stack.
1 parent 7d49960 commit 7547508

File tree

4 files changed

+37
-93
lines changed

4 files changed

+37
-93
lines changed

llvm/include/llvm/Analysis/StackLifetime.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ class StackLifetime {
121121
DenseMap<const BasicBlock *, SmallVector<std::pair<unsigned, Marker>, 4>>
122122
BBMarkers;
123123

124-
bool HasUnknownLifetimeStartOrEnd = false;
125-
126124
void dumpAllocas() const;
127125
void dumpBlockLiveness() const;
128126
void dumpLiveRanges() const;

llvm/lib/Analysis/StackLifetime.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "llvm/ADT/STLExtras.h"
1212
#include "llvm/ADT/SmallVector.h"
1313
#include "llvm/ADT/StringExtras.h"
14-
#include "llvm/Analysis/ValueTracking.h"
1514
#include "llvm/Config/llvm-config.h"
1615
#include "llvm/IR/AssemblyAnnotationWriter.h"
1716
#include "llvm/IR/BasicBlock.h"
@@ -64,27 +63,42 @@ bool StackLifetime::isAliveAfter(const AllocaInst *AI,
6463
return getLiveRange(AI).test(InstNum);
6564
}
6665

66+
static bool readMarker(const Instruction *I, bool *IsStart) {
67+
if (!I->isLifetimeStartOrEnd())
68+
return false;
69+
70+
auto *II = cast<IntrinsicInst>(I);
71+
*IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
72+
return true;
73+
}
74+
6775
void StackLifetime::collectMarkers() {
6876
InterestingAllocas.resize(NumAllocas);
6977
DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>>
7078
BBMarkerSet;
7179

7280
// Compute the set of start/end markers per basic block.
73-
for (const BasicBlock *BB : depth_first(&F)) {
74-
for (const Instruction &I : *BB) {
75-
const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
76-
if (!II || !II->isLifetimeStartOrEnd())
77-
continue;
78-
const AllocaInst *AI = llvm::findAllocaForValue(II->getArgOperand(1));
79-
if (!AI) {
80-
HasUnknownLifetimeStartOrEnd = true;
81-
continue;
81+
for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
82+
const AllocaInst *AI = Allocas[AllocaNo];
83+
SmallVector<const Instruction *, 8> WorkList;
84+
WorkList.push_back(AI);
85+
while (!WorkList.empty()) {
86+
const Instruction *I = WorkList.pop_back_val();
87+
for (const User *U : I->users()) {
88+
if (auto *BI = dyn_cast<BitCastInst>(U)) {
89+
WorkList.push_back(BI);
90+
continue;
91+
}
92+
auto *UI = dyn_cast<IntrinsicInst>(U);
93+
if (!UI)
94+
continue;
95+
bool IsStart;
96+
if (!readMarker(UI, &IsStart))
97+
continue;
98+
if (IsStart)
99+
InterestingAllocas.set(AllocaNo);
100+
BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
82101
}
83-
bool IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
84-
unsigned AllocaNo = AllocaNumbering[AI];
85-
if (IsStart)
86-
InterestingAllocas.set(AllocaNo);
87-
BBMarkerSet[BB][II] = {AllocaNo, IsStart};
88102
}
89103
}
90104

@@ -290,20 +304,6 @@ StackLifetime::StackLifetime(const Function &F,
290304
}
291305

292306
void StackLifetime::run() {
293-
if (HasUnknownLifetimeStartOrEnd) {
294-
// There is marker which we can't assign to a specific alloca, so we
295-
// fallback to the most conservative results for the type.
296-
switch (Type) {
297-
case LivenessType::May:
298-
LiveRanges.resize(NumAllocas, getFullLiveRange());
299-
break;
300-
case LivenessType::Must:
301-
LiveRanges.resize(NumAllocas, LiveRange(Instructions.size()));
302-
break;
303-
}
304-
return;
305-
}
306-
307307
LiveRanges.resize(NumAllocas, LiveRange(Instructions.size()));
308308
for (unsigned I = 0; I < NumAllocas; ++I)
309309
if (!InterestingAllocas.test(I))

llvm/test/Analysis/StackSafetyAnalysis/lifetime.ll

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ if.end:
742742
; MAY-NEXT: Alive: <x y>
743743
; MUST-NEXT: Alive: <y>
744744

745-
ret void
745+
ret void
746746
}
747747

748748
define void @unreachable() {
@@ -778,62 +778,7 @@ end:
778778
; CHECK: end:
779779
; CHECK-NEXT: Alive: <x y>
780780

781-
ret void
782-
}
783-
784-
define void @non_alloca(i8* %p) {
785-
; CHECK-LABEL: define void @non_alloca
786-
entry:
787-
; CHECK: entry:
788-
; MAY-NEXT: Alive: <x y>
789-
; MUST-NEXT: Alive: <>
790-
%x = alloca i8, align 4
791-
%y = alloca i8, align 4
792-
793-
call void @llvm.lifetime.start.p0i8(i64 4, i8* %p)
794-
; CHECK: call void @llvm.lifetime.start.p0i8(i64 4, i8* %p)
795-
; MAY-NEXT: Alive: <x y>
796-
; MUST-NEXT: Alive: <>
797-
798-
call void @llvm.lifetime.start.p0i8(i64 4, i8* %x)
799-
; CHECK: call void @llvm.lifetime.start.p0i8(i64 4, i8* %x)
800-
; MAY-NEXT: Alive: <x y>
801-
; MUST-NEXT: Alive: <>
802-
803-
call void @llvm.lifetime.end.p0i8(i64 4, i8* %p)
804-
; CHECK: call void @llvm.lifetime.end.p0i8(i64 4, i8* %p)
805-
; MAY-NEXT: Alive: <x y>
806-
; MUST-NEXT: Alive: <>
807-
808-
ret void
809-
}
810-
811-
define void @select_alloca(i1 %v) {
812-
; CHECK-LABEL: define void @select_alloca
813-
entry:
814-
; CHECK: entry:
815-
; MAY-NEXT: Alive: <x y>
816-
; MUST-NEXT: Alive: <>
817-
%x = alloca i8, align 4
818-
%y = alloca i8, align 4
819-
%cxcy = select i1 %v, i8* %x, i8* %y
820-
821-
call void @llvm.lifetime.start.p0i8(i64 1, i8* %cxcy)
822-
; CHECK: call void @llvm.lifetime.start.p0i8(i64 1, i8* %cxcy)
823-
; MAY-NEXT: Alive: <x y>
824-
; MUST-NEXT: Alive: <>
825-
826-
call void @llvm.lifetime.start.p0i8(i64 1, i8* %x)
827-
; CHECK: call void @llvm.lifetime.start.p0i8(i64 1, i8* %x)
828-
; MAY-NEXT: Alive: <x y>
829-
; MUST-NEXT: Alive: <>
830-
831-
call void @llvm.lifetime.end.p0i8(i64 1, i8* %x)
832-
; CHECK: call void @llvm.lifetime.end.p0i8(i64 1, i8* %x)
833-
; MAY-NEXT: Alive: <x y>
834-
; MUST-NEXT: Alive: <>
835-
836-
ret void
781+
ret void
837782
}
838783

839784
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)

llvm/test/CodeGen/AArch64/stack-tagging.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,18 @@ another_bb:
192192
; CHECK: alloca { i32, [12 x i8] }, align 16
193193
; CHECK: call { i32, [12 x i8] }* @llvm.aarch64.tagp
194194
; CHECK: call void @llvm.aarch64.settag(
195-
; CHECK: alloca { i32, [12 x i8] }, align 16
196-
; CHECK: call { i32, [12 x i8] }* @llvm.aarch64.tagp
197-
; CHECK: call void @llvm.aarch64.settag(
195+
; SSI: alloca i32, align 4
196+
; NOSSI: alloca { i32, [12 x i8] }, align 16
197+
; NOSSI: call { i32, [12 x i8] }* @llvm.aarch64.tagp
198+
; NOSSI: call void @llvm.aarch64.settag(
198199
; CHECK: store i32
199200
; CHECK: call void @noUse32(i32*
200201
; CHECK: store i32
201202
; CHECK: store i32
202203
; CHECK: call void @noUse32(i32*
203204
; CHECK: call void @llvm.aarch64.settag(
204205
; CHECK: call void @llvm.aarch64.settag(
205-
; CHECK: call void @llvm.aarch64.settag(
206+
; NOSSI: call void @llvm.aarch64.settag(
206207
; CHECK: ret void
207208

208-
!0 = !{}
209+
!0 = !{}

0 commit comments

Comments
 (0)