[PATCH] D125293: [Coroutines] Run EarlyCSE for changed functions in CoroCleanup (3/3)

Chuanqi Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 10 00:03:49 PDT 2022


ChuanqiXu created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
ChuanqiXu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

After the previous revision (https://reviews.llvm.org/D125292) landed, the optimizations for readnone calls in coroutines would be blocked even if these calls don't cross suspend points.

This patch tries to solve the problem by run EarlyCSE for the functions which is changed in CoroCleanup pass. It wouldn't affect normal functions.


https://reviews.llvm.org/D125293

Files:
  llvm/lib/Transforms/Coroutines/CoroCleanup.cpp
  llvm/test/Transforms/Coroutines/coro-readnone-06.ll


Index: llvm/test/Transforms/Coroutines/coro-readnone-06.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/Coroutines/coro-readnone-06.ll
@@ -0,0 +1,58 @@
+; Tests that the readnone function which don't cross suspend points could be optimized expectly.
+; RUN: opt < %s -S -passes='default<O1>' -opaque-pointers | FileCheck %s
+
+define ptr @f() "coroutine.presplit" {
+entry:
+  %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
+  %size = call i32 @llvm.coro.size.i32()
+  %alloc = call i8* @malloc(i32 %size)
+  %hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc)
+  %sus_result = call i8 @llvm.coro.suspend(token none, i1 false)
+  switch i8 %sus_result, label %suspend [i8 0, label %resume
+                                         i8 1, label %cleanup]
+resume:
+  %i = call i32 @readnone_func() readnone
+  %j = call i32 @readnone_func() readnone
+  %cmp = icmp eq i32 %i, %j
+  br i1 %cmp, label %same, label %diff
+
+same:
+  call void @print_same()
+  br label %cleanup
+
+diff:
+  call void @print_diff()
+  br label %cleanup
+
+cleanup:
+  %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
+  call void @free(i8* %mem)
+  br label %suspend
+
+suspend:
+  call i1 @llvm.coro.end(i8* %hdl, i1 0)
+  ret i8* %hdl
+}
+
+; CHECK-LABEL: f.resume(
+; CHECK-NEXT: entry
+; CHECK-NEXT:   call i32 @readnone_func()
+; CHECK-NEXT:   call void @print_same()
+; CHECK-NEXT:   call void @free
+; CHECK-NEXT:   ret void
+
+declare i32 @readnone_func() readnone
+
+declare void @print_same()
+declare void @print_diff()
+declare i8* @llvm.coro.free(token, i8*)
+declare i32 @llvm.coro.size.i32()
+declare i8  @llvm.coro.suspend(token, i1)
+
+declare token @llvm.coro.id(i32, i8*, i8*, i8*)
+declare i1 @llvm.coro.alloc(token)
+declare i8* @llvm.coro.begin(token, i8*)
+declare i1 @llvm.coro.end(i8*, i1)
+
+declare noalias i8* @malloc(i32)
+declare void @free(i8*)
Index: llvm/lib/Transforms/Coroutines/CoroCleanup.cpp
===================================================================
--- llvm/lib/Transforms/Coroutines/CoroCleanup.cpp
+++ llvm/lib/Transforms/Coroutines/CoroCleanup.cpp
@@ -12,6 +12,7 @@
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
+#include "llvm/Transforms/Scalar/EarlyCSE.h"
 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
 
 using namespace llvm;
@@ -146,6 +147,7 @@
 
   FunctionPassManager FPM;
   FPM.addPass(SimplifyCFGPass());
+  FPM.addPass(EarlyCSEPass());
 
   Lowerer L(M);
   for (auto &F : M)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125293.428302.patch
Type: text/x-patch
Size: 2557 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220510/f67fc632/attachment.bin>


More information about the llvm-commits mailing list