[PATCH] D66230: [coroutine] Fixes "cannot move instruction since its users are not dominated by CoroBegin" problem.

Xun Li via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 09:54:10 PDT 2020


lxfind added a comment.
Herald added a subscriber: danielkiss.

@GorNishanov This fix seems problematic.
Consider this code:

  %var = alloca i32
  %1 = getelementptr .. %var; stays put
  %f = call i8* @llvm.coro.begin
  store ... %1

After this fix, `%1` will now stay put, however if a `store` happens after `coro.begin` and hence modifies the content, this change will not be reflected in the coroutine frame (and will eventually be DCEed).
To generalize the problem, if any alias ptr is created before coro.begin for an Alloca and that alias ptr is latter written into after `coro.begin`, it will lead to incorrect behavior.
I wonder what would be a correct fix?

Also, there seems to be a few other minor problems with this fix, for instance, in AllocaUseVisitor, we are only checking escape and store instructions. However this is insufficient to cover all potential writes. You can have a call instruction that's non-escaping but modifies the content of the pointer (e.g. llvm.memcpy). Also, in AllocaUseVisitor::visit, we are checking `DT.dominates(&I, &CoroBegin)`, which should really be `!DT.dominates(&CoroBegin, &I)`.

Overall, I find it difficult to patch this change to make it correct. Some fundamental rewrite of this part of the algorithm seems necessary. I would be happy to look into it but would like to hear your opinions first @GorNishanov.

A full repro IR of the issue for reference:

  %"struct_foo" = type <{ i64, i64, [8 x i8] }>
  
  define i8* @foo(%"struct_foo"* byval(%"struct_foo") align 8 %arg) "coroutine.presplit"="1" {
  entry:
    %local = alloca [24 x i8], align 8
    %local.sub = getelementptr inbounds [24 x i8], [24 x i8]* %local, i64 0, i64 0
    %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
    %size = call i32 @llvm.coro.size.i32()
    %alloc = call i8* @myAlloc(i32 %size)
    %hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc)
    %arg.addr = bitcast %"struct_foo"* %arg to i8*
    call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 8 dereferenceable(24) %local.sub, i8* nonnull align 8 dereferenceable(24) %arg.addr, i64 24, i1 false)
    %0 = call i8 @llvm.coro.suspend(token none, i1 false)
    switch i8 %0, label %suspend [i8 0, label %resume
                                  i8 1, label %cleanup]
  resume:
    call void @print2([24 x i8]* %local)
    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
  }
  
  declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg)
  
  declare i8* @llvm.coro.free(token, i8*)
  declare i32 @llvm.coro.size.i32()
  declare i8  @llvm.coro.suspend(token, i1)
  declare void @llvm.coro.resume(i8*)
  declare void @llvm.coro.destroy(i8*)
  
  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* @myAlloc(i32)
  declare double @print(double)
  declare void @print2([24 x i8]*)
  declare void @free(i8*)


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66230/new/

https://reviews.llvm.org/D66230



More information about the llvm-commits mailing list