Skip to content

Revert "[CodeExtractor] Don't create bitcasts when inserting lifetime markers (NFCI)" #2025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2020
Merged
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
35 changes: 27 additions & 8 deletions llvm/lib/Transforms/Utils/CodeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,31 +1023,50 @@ static void insertLifetimeMarkersSurroundingCall(
Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd,
CallInst *TheCall) {
LLVMContext &Ctx = M->getContext();
auto Int8PtrTy = Type::getInt8PtrTy(Ctx);
auto NegativeOne = ConstantInt::getSigned(Type::getInt64Ty(Ctx), -1);
Instruction *Term = TheCall->getParent()->getTerminator();

// The memory argument to a lifetime marker must be a i8*. Cache any bitcasts
// needed to satisfy this requirement so they may be reused.
DenseMap<Value *, Value *> Bitcasts;

// Emit lifetime markers for the pointers given in \p Objects. Insert the
// markers before the call if \p InsertBefore, and after the call otherwise.
auto insertMarkers = [&](Intrinsic::ID IID, ArrayRef<Value *> Objects,
auto insertMarkers = [&](Function *MarkerFunc, ArrayRef<Value *> Objects,
bool InsertBefore) {
for (Value *Mem : Objects) {
assert((!isa<Instruction>(Mem) || cast<Instruction>(Mem)->getFunction() ==
TheCall->getFunction()) &&
"Input memory not defined in original function");
assert(Mem->getType()->isPointerTy() && "Expected pointer to memory");
Function *MarkerFunc =
llvm::Intrinsic::getDeclaration(M, IID, Mem->getType());
auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, Mem});
Value *&MemAsI8Ptr = Bitcasts[Mem];
if (!MemAsI8Ptr) {
if (Mem->getType() == Int8PtrTy)
MemAsI8Ptr = Mem;
else
MemAsI8Ptr =
CastInst::CreatePointerCast(Mem, Int8PtrTy, "lt.cast", TheCall);
}

auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, MemAsI8Ptr});
if (InsertBefore)
Marker->insertBefore(TheCall);
else
Marker->insertBefore(Term);
}
};

insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
/*InsertBefore=*/true);
insertMarkers(Intrinsic::lifetime_end, LifetimesEnd, /*InsertBefore=*/false);
if (!LifetimesStart.empty()) {
auto StartFn = llvm::Intrinsic::getDeclaration(
M, llvm::Intrinsic::lifetime_start, Int8PtrTy);
insertMarkers(StartFn, LifetimesStart, /*InsertBefore=*/true);
}

if (!LifetimesEnd.empty()) {
auto EndFn = llvm::Intrinsic::getDeclaration(
M, llvm::Intrinsic::lifetime_end, Int8PtrTy);
insertMarkers(EndFn, LifetimesEnd, /*InsertBefore=*/false);
}
}

/// emitCallAndSwitchStatement - This method sets up the caller side by adding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ bb5: ; preds = %bb4, %bb1, %bb
; CHECK-LABEL: bb:
; CHECK-NEXT: [[CALL26LOC:%.*]] = alloca i8*
; CHECK-LABEL: codeRepl.i:
; CHECK-NEXT: call void @llvm.lifetime.start.p0p0i8(i64 -1, i8** [[CALL26LOC]])
; CHECK-NEXT: %lt.cast.i = bitcast i8** [[CALL26LOC]] to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* %lt.cast.i)
; CHECK-NEXT: call void @bar.1.bb1(i8** [[CALL26LOC]])
; CHECK-NEXT: %call26.reload.i = load i8*, i8** [[CALL26LOC]]
; CHECK-NEXT: call void @llvm.lifetime.end.p0p0i8(i64 -1, i8** [[CALL26LOC]])
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 -1, i8* %lt.cast.i)
define i8* @dummy_caller(i32 %arg) {
bb:
%tmp = tail call i8* @bar(i32 %arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ normalPath:
ret void

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

Expand Down Expand Up @@ -59,4 +61,4 @@ outlinedPathExit:
}

; CHECK-LABEL: define {{.*}}@foo.cold.1(
; CHECK-NOT: call void @llvm.lifetime
; CHECK-NOT: @llvm.lifetime
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ declare void @use(i8*)
define void @only_lifetime_start_is_cold() {
; CHECK-LABEL: @only_lifetime_start_is_cold(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
; CHECK-NEXT: br i1 undef, label [[CODEREPL:%.*]], label [[NO_EXTRACT1:%.*]]
; CHECK: codeRepl:
; CHECK-NEXT: call void @llvm.lifetime.start.p0i256(i64 -1, i256* [[LOCAL1]])
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @only_lifetime_start_is_cold.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3:#.*]]
; CHECK-NEXT: [[LT_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[LT_CAST]])
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @only_lifetime_start_is_cold.cold.1(i8* [[LOCAL1_CAST]]) #3
; CHECK-NEXT: br i1 [[TARGETBLOCK]], label [[NO_EXTRACT1]], label [[EXIT:%.*]]
; CHECK: no-extract1:
; CHECK-NEXT: br label [[EXIT]]
Expand Down Expand Up @@ -97,15 +98,15 @@ exit:
define void @only_lifetime_end_is_cold() {
; CHECK-LABEL: @only_lifetime_end_is_cold(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* [[LOCAL1_CAST]])
; CHECK-NEXT: br i1 undef, label [[NO_EXTRACT1:%.*]], label [[CODEREPL:%.*]]
; CHECK: no-extract1:
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 1, i8* [[LOCAL1_CAST]])
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: codeRepl:
; CHECK-NEXT: call void @only_lifetime_end_is_cold.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3]]
; CHECK-NEXT: call void @only_lifetime_end_is_cold.cold.1(i8* [[LOCAL1_CAST]]) #3
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: ret void
Expand Down Expand Up @@ -137,15 +138,15 @@ exit:
define void @do_not_lift_lifetime_end() {
; CHECK-LABEL: @do_not_lift_lifetime_end(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* [[LOCAL1_CAST]])
; CHECK-NEXT: br label [[HEADER:%.*]]
; CHECK: header:
; CHECK-NEXT: call void @use(i8* [[LOCAL1_CAST]])
; CHECK-NEXT: br i1 undef, label [[EXIT:%.*]], label [[CODEREPL:%.*]]
; CHECK: codeRepl:
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @do_not_lift_lifetime_end.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3]]
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @do_not_lift_lifetime_end.cold.1(i8* [[LOCAL1_CAST]]) #3
; CHECK-NEXT: br i1 [[TARGETBLOCK]], label [[HEADER]], label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: ret void
Expand Down
47 changes: 47 additions & 0 deletions llvm/test/Transforms/HotColdSplit/lifetime-markers-on-inputs-3.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
; RUN: opt -S -hotcoldsplit -hotcoldsplit-threshold=0 < %s 2>&1 | FileCheck %s

%type1 = type opaque
%type2 = type opaque

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

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

declare void @use(%type1**, %type2**)

declare void @use2(%type1**, %type2**) cold

; CHECK-LABEL: define {{.*}}@foo(
define void @foo() {
entry:
%local1 = alloca %type1*
%local2 = alloca %type2*
%local1_cast = bitcast %type1** %local1 to i8*
%local2_cast = bitcast %type2** %local2 to i8*
br i1 undef, label %normalPath, label %outlinedPath

normalPath:
call void @use(%type1** %local1, %type2** %local2)
ret void

; CHECK-LABEL: codeRepl:
; CHECK: [[local1_cast:%.*]] = bitcast %type1** %local1 to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[local1_cast]])
; CHECK-NEXT: [[local2_cast:%.*]] = bitcast %type2** %local2 to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[local2_cast]])
; CHECK-NEXT: call void @foo.cold.1(i8* %local1_cast, i8* %local2_cast

outlinedPath:
call void @llvm.lifetime.start.p0i8(i64 1, i8* %local1_cast)
call void @llvm.lifetime.start.p0i8(i64 1, i8* %local2_cast)
call void @use2(%type1** %local1, %type2** %local2)
call void @llvm.lifetime.end.p0i8(i64 1, i8* %local1_cast)
call void @llvm.lifetime.end.p0i8(i64 1, i8* %local2_cast)
br label %outlinedPathExit

outlinedPathExit:
ret void
}

; CHECK-LABEL: define {{.*}}@foo.cold.1(
; CHECK-NOT: @llvm.lifetime
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ target triple = "x86_64-apple-macosx10.14.0"
; CHECK-NEXT: ]
;
; CHECK: codeRepl:
; CHECK: lifetime.start
; CHECK-NEXT: bitcast
; CHECK-NEXT: lifetime.start
; CHECK-NEXT: call void @pluto.cold.1(i1* %tmp8.ce.loc)
; CHECK-NEXT: %tmp8.ce.reload = load i1, i1* %tmp8.ce.loc
; CHECK-NEXT: lifetime.end
Expand Down