Skip to content

[llvm][Coroutines] Remove no-op ptr-to-ptr bitcasts (NFC) #73427

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
Nov 26, 2023
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
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/Coroutines/CoroCleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ struct Lowerer : coro::LowererBase {

static void lowerSubFn(IRBuilder<> &Builder, CoroSubFnInst *SubFn) {
Builder.SetInsertPoint(SubFn);
Value *FrameRaw = SubFn->getFrame();
Value *FramePtr = SubFn->getFrame();
int Index = SubFn->getIndex();

auto *FrameTy = StructType::get(SubFn->getContext(),
{Builder.getPtrTy(), Builder.getPtrTy()});
PointerType *FramePtrTy = FrameTy->getPointerTo();

Builder.SetInsertPoint(SubFn);
auto *FramePtr = Builder.CreateBitCast(FrameRaw, FramePtrTy);
auto *Gep = Builder.CreateConstInBoundsGEP2_32(FrameTy, FramePtr, 0, Index);
auto *Load = Builder.CreateLoad(FrameTy->getElementType(Index), Gep);

Expand Down
13 changes: 4 additions & 9 deletions llvm/lib/Transforms/Coroutines/Coroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ coro::LowererBase::LowererBase(Module &M)
/*isVarArg=*/false)),
NullPtr(ConstantPointerNull::get(Int8Ptr)) {}

// Creates a sequence of instructions to obtain a resume function address using
// llvm.coro.subfn.addr. It generates the following sequence:
// Creates a call to llvm.coro.subfn.addr to obtain a resume function address.
// It generates the following:
//
// call i8* @llvm.coro.subfn.addr(i8* %Arg, i8 %index)
// bitcast i8* %2 to void(i8*)*
// call ptr @llvm.coro.subfn.addr(ptr %Arg, i8 %index)

Value *coro::LowererBase::makeSubFnCall(Value *Arg, int Index,
Instruction *InsertPt) {
Expand All @@ -56,11 +55,7 @@ Value *coro::LowererBase::makeSubFnCall(Value *Arg, int Index,
assert(Index >= CoroSubFnInst::IndexFirst &&
Index < CoroSubFnInst::IndexLast &&
"makeSubFnCall: Index value out of range");
auto *Call = CallInst::Create(Fn, {Arg, IndexVal}, "", InsertPt);

auto *Bitcast =
new BitCastInst(Call, ResumeFnType->getPointerTo(), "", InsertPt);
return Bitcast;
return CallInst::Create(Fn, {Arg, IndexVal}, "", InsertPt);
}

// NOTE: Must be sorted!
Expand Down
11 changes: 4 additions & 7 deletions llvm/test/Transforms/Coroutines/coro-resume-destroy.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ define void @callResume(ptr %hdl) {
; CHECK-NEXT: entry
entry:
; CHECK-NEXT: %0 = call ptr @llvm.coro.subfn.addr(ptr %hdl, i8 0)
; CHECK-NEXT: %1 = bitcast ptr %0 to ptr
; CHECK-NEXT: call fastcc void %1(ptr %hdl)
; CHECK-NEXT: call fastcc void %0(ptr %hdl)
call void @llvm.coro.resume(ptr %hdl)

; CHECK-NEXT: %2 = call ptr @llvm.coro.subfn.addr(ptr %hdl, i8 1)
; CHECK-NEXT: %3 = bitcast ptr %2 to ptr
; CHECK-NEXT: call fastcc void %3(ptr %hdl)
; CHECK-NEXT: %1 = call ptr @llvm.coro.subfn.addr(ptr %hdl, i8 1)
; CHECK-NEXT: call fastcc void %1(ptr %hdl)
call void @llvm.coro.destroy(ptr %hdl)

ret void
Expand All @@ -24,8 +22,7 @@ define void @eh(ptr %hdl) personality ptr null {
; CHECK-NEXT: entry
entry:
; CHECK-NEXT: %0 = call ptr @llvm.coro.subfn.addr(ptr %hdl, i8 0)
; CHECK-NEXT: %1 = bitcast ptr %0 to ptr
; CHECK-NEXT: invoke fastcc void %1(ptr %hdl)
; CHECK-NEXT: invoke fastcc void %0(ptr %hdl)
invoke void @llvm.coro.resume(ptr %hdl)
to label %cont unwind label %ehcleanup
cont:
Expand Down