Skip to content

Commit 099bffe

Browse files
committed
Revert "[CodeExtractor] Don't create bitcasts when inserting lifetime markers (NFCI)"
This reverts commit 26ee8af. It's necessary to insert bitcast the pointer operand of a lifetime marker if it has an opaque pointer type. rdar://70560161
1 parent 36ce915 commit 099bffe

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,31 +1025,50 @@ static void insertLifetimeMarkersSurroundingCall(
10251025
Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd,
10261026
CallInst *TheCall) {
10271027
LLVMContext &Ctx = M->getContext();
1028+
auto Int8PtrTy = Type::getInt8PtrTy(Ctx);
10281029
auto NegativeOne = ConstantInt::getSigned(Type::getInt64Ty(Ctx), -1);
10291030
Instruction *Term = TheCall->getParent()->getTerminator();
10301031

1032+
// The memory argument to a lifetime marker must be a i8*. Cache any bitcasts
1033+
// needed to satisfy this requirement so they may be reused.
1034+
DenseMap<Value *, Value *> Bitcasts;
1035+
10311036
// Emit lifetime markers for the pointers given in \p Objects. Insert the
10321037
// markers before the call if \p InsertBefore, and after the call otherwise.
1033-
auto insertMarkers = [&](Intrinsic::ID IID, ArrayRef<Value *> Objects,
1038+
auto insertMarkers = [&](Function *MarkerFunc, ArrayRef<Value *> Objects,
10341039
bool InsertBefore) {
10351040
for (Value *Mem : Objects) {
10361041
assert((!isa<Instruction>(Mem) || cast<Instruction>(Mem)->getFunction() ==
10371042
TheCall->getFunction()) &&
10381043
"Input memory not defined in original function");
1039-
assert(Mem->getType()->isPointerTy() && "Expected pointer to memory");
1040-
Function *MarkerFunc =
1041-
llvm::Intrinsic::getDeclaration(M, IID, Mem->getType());
1042-
auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, Mem});
1044+
Value *&MemAsI8Ptr = Bitcasts[Mem];
1045+
if (!MemAsI8Ptr) {
1046+
if (Mem->getType() == Int8PtrTy)
1047+
MemAsI8Ptr = Mem;
1048+
else
1049+
MemAsI8Ptr =
1050+
CastInst::CreatePointerCast(Mem, Int8PtrTy, "lt.cast", TheCall);
1051+
}
1052+
1053+
auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, MemAsI8Ptr});
10431054
if (InsertBefore)
10441055
Marker->insertBefore(TheCall);
10451056
else
10461057
Marker->insertBefore(Term);
10471058
}
10481059
};
10491060

1050-
insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1051-
/*InsertBefore=*/true);
1052-
insertMarkers(Intrinsic::lifetime_end, LifetimesEnd, /*InsertBefore=*/false);
1061+
if (!LifetimesStart.empty()) {
1062+
auto StartFn = llvm::Intrinsic::getDeclaration(
1063+
M, llvm::Intrinsic::lifetime_start, Int8PtrTy);
1064+
insertMarkers(StartFn, LifetimesStart, /*InsertBefore=*/true);
1065+
}
1066+
1067+
if (!LifetimesEnd.empty()) {
1068+
auto EndFn = llvm::Intrinsic::getDeclaration(
1069+
M, llvm::Intrinsic::lifetime_end, Int8PtrTy);
1070+
insertMarkers(EndFn, LifetimesEnd, /*InsertBefore=*/false);
1071+
}
10531072
}
10541073

10551074
/// emitCallAndSwitchStatement - This method sets up the caller side by adding

llvm/test/Transforms/CodeExtractor/PartialInlineInvokeProducesOutVal.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ bb5: ; preds = %bb4, %bb1, %bb
2626
; CHECK-LABEL: bb:
2727
; CHECK-NEXT: [[CALL26LOC:%.*]] = alloca i8*
2828
; CHECK-LABEL: codeRepl.i:
29-
; CHECK-NEXT: call void @llvm.lifetime.start.p0p0i8(i64 -1, i8** [[CALL26LOC]])
29+
; CHECK-NEXT: %lt.cast.i = bitcast i8** [[CALL26LOC]] to i8*
30+
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* %lt.cast.i)
3031
; CHECK-NEXT: call void @bar.1.bb1(i8** [[CALL26LOC]])
3132
; CHECK-NEXT: %call26.reload.i = load i8*, i8** [[CALL26LOC]]
32-
; CHECK-NEXT: call void @llvm.lifetime.end.p0p0i8(i64 -1, i8** [[CALL26LOC]])
33+
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 -1, i8* %lt.cast.i)
3334
define i8* @dummy_caller(i32 %arg) {
3435
bb:
3536
%tmp = tail call i8* @bar(i32 %arg)

llvm/test/Transforms/HotColdSplit/lifetime-markers-on-inputs-1.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ normalPath:
2929
ret void
3030

3131
; CHECK-LABEL: codeRepl:
32-
; CHECK: call void @llvm.lifetime.start.p0i256(i64 -1, i256* %local1)
33-
; CHECK-NEXT: call void @llvm.lifetime.start.p0i256(i64 -1, i256* %local2)
32+
; CHECK: [[local1_cast:%.*]] = bitcast i256* %local1 to i8*
33+
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[local1_cast]])
34+
; CHECK-NEXT: [[local2_cast:%.*]] = bitcast i256* %local2 to i8*
35+
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[local2_cast]])
3436
; CHECK-NEXT: call i1 @foo.cold.1(i8* %local1_cast, i8* %local2_cast)
3537
; CHECK-NEXT: br i1
3638

@@ -59,4 +61,4 @@ outlinedPathExit:
5961
}
6062

6163
; CHECK-LABEL: define {{.*}}@foo.cold.1(
62-
; CHECK-NOT: call void @llvm.lifetime
64+
; CHECK-NOT: @llvm.lifetime

llvm/test/Transforms/HotColdSplit/lifetime-markers-on-inputs-2.ll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ declare void @use(i8*)
3737
define void @only_lifetime_start_is_cold() {
3838
; CHECK-LABEL: @only_lifetime_start_is_cold(
3939
; CHECK-NEXT: entry:
40-
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
40+
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
4141
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
4242
; CHECK-NEXT: br i1 undef, label [[CODEREPL:%.*]], label [[NO_EXTRACT1:%.*]]
4343
; CHECK: codeRepl:
44-
; CHECK-NEXT: call void @llvm.lifetime.start.p0i256(i64 -1, i256* [[LOCAL1]])
45-
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @only_lifetime_start_is_cold.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3:#.*]]
44+
; CHECK-NEXT: [[LT_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
45+
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[LT_CAST]])
46+
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @only_lifetime_start_is_cold.cold.1(i8* [[LOCAL1_CAST]]) #3
4647
; CHECK-NEXT: br i1 [[TARGETBLOCK]], label [[NO_EXTRACT1]], label [[EXIT:%.*]]
4748
; CHECK: no-extract1:
4849
; CHECK-NEXT: br label [[EXIT]]
@@ -97,15 +98,15 @@ exit:
9798
define void @only_lifetime_end_is_cold() {
9899
; CHECK-LABEL: @only_lifetime_end_is_cold(
99100
; CHECK-NEXT: entry:
100-
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
101+
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
101102
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
102103
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* [[LOCAL1_CAST]])
103104
; CHECK-NEXT: br i1 undef, label [[NO_EXTRACT1:%.*]], label [[CODEREPL:%.*]]
104105
; CHECK: no-extract1:
105106
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 1, i8* [[LOCAL1_CAST]])
106107
; CHECK-NEXT: br label [[EXIT:%.*]]
107108
; CHECK: codeRepl:
108-
; CHECK-NEXT: call void @only_lifetime_end_is_cold.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3]]
109+
; CHECK-NEXT: call void @only_lifetime_end_is_cold.cold.1(i8* [[LOCAL1_CAST]]) #3
109110
; CHECK-NEXT: br label [[EXIT]]
110111
; CHECK: exit:
111112
; CHECK-NEXT: ret void
@@ -137,15 +138,15 @@ exit:
137138
define void @do_not_lift_lifetime_end() {
138139
; CHECK-LABEL: @do_not_lift_lifetime_end(
139140
; CHECK-NEXT: entry:
140-
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
141+
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
141142
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
142143
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* [[LOCAL1_CAST]])
143144
; CHECK-NEXT: br label [[HEADER:%.*]]
144145
; CHECK: header:
145146
; CHECK-NEXT: call void @use(i8* [[LOCAL1_CAST]])
146147
; CHECK-NEXT: br i1 undef, label [[EXIT:%.*]], label [[CODEREPL:%.*]]
147148
; CHECK: codeRepl:
148-
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @do_not_lift_lifetime_end.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3]]
149+
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @do_not_lift_lifetime_end.cold.1(i8* [[LOCAL1_CAST]]) #3
149150
; CHECK-NEXT: br i1 [[TARGETBLOCK]], label [[HEADER]], label [[EXIT]]
150151
; CHECK: exit:
151152
; CHECK-NEXT: ret void

llvm/test/Transforms/HotColdSplit/split-phis-in-exit-blocks.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ target triple = "x86_64-apple-macosx10.14.0"
1212
; CHECK-NEXT: ]
1313
;
1414
; CHECK: codeRepl:
15-
; CHECK: lifetime.start
15+
; CHECK-NEXT: bitcast
16+
; CHECK-NEXT: lifetime.start
1617
; CHECK-NEXT: call void @pluto.cold.1(i1* %tmp8.ce.loc)
1718
; CHECK-NEXT: %tmp8.ce.reload = load i1, i1* %tmp8.ce.loc
1819
; CHECK-NEXT: lifetime.end

0 commit comments

Comments
 (0)