Skip to content
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

GVN hoists loads over intrinsics #54851

Closed
jdoerfert opened this issue Apr 11, 2022 · 4 comments
Closed

GVN hoists loads over intrinsics #54851

jdoerfert opened this issue Apr 11, 2022 · 4 comments

Comments

@jdoerfert
Copy link
Member

Originally reported here: https://discourse.llvm.org/t/bug-gvn-memdep-bug-in-the-presence-of-intrinsics/59402

GVN, or probably memdep analysis, seem to think intrinsics are somewhat special
and consequently assume they cannot "effectively clobber" a memory location. It
might also be globals-mod-ref or something else that provides AA information.

The following example (https://godbolt.org/z/TG6dYvP6M 1) shows how an unknown function
prevents an invalid load hoist while an unknown intrinsic does not. Again, the preload
is not sound in a multi-thread environment.

The simple reproducer can be found here: https://godbolt.org/z/cKMEezbjY
Compiled with -globals-aa -gvn the below code will hoist the load above the barrier but not the unknown function.

@G = internal global i32 undef

define void @test_barrier(i1 %c) {
  br i1 %c, label %init, label %check
init:
  store i32 0, ptr @G
  br label %check
check:
  call void @llvm.amdgcn.s.barrier()
  %v = load i32, ptr @G
  %cmp = icmp eq i32 %v, 0
  call void @llvm.assume(i1 %cmp)
  ret void
}

define void @test_unknown(i1 %c) {
  br i1 %c, label %init, label %check
init:
  store i32 0, ptr @G
  br label %check
check:
  call void @unknown()
  %v = load i32, ptr @G
  %cmp = icmp eq i32 %v, 0
  call void @llvm.assume(i1 %cmp)
  ret void
}

declare void @unknown()
declare void @llvm.amdgcn.s.barrier()
declare void @llvm.assume(i1 noundef)
@jdoerfert
Copy link
Member Author

@efriedma-quic @arsenm Any thoughts?

@jdoerfert jdoerfert changed the title GVN hoists loads over unknown intrinsics GVN hoists loads over intrinsics Apr 11, 2022
@efriedma-quic
Copy link
Collaborator

Is this distinct from https://reviews.llvm.org/D115302 ?

@jdoerfert
Copy link
Member Author

Is this distinct from https://reviews.llvm.org/D115302 ?

I think it is. Or at least the fix I'm preparing is. I think the reason for this problem is d4133ac which survived one way or another until today.

@jdoerfert
Copy link
Member Author

Proposed fix: https://reviews.llvm.org/D123531

mem-frob pushed a commit to draperlaboratory/hope-llvm-project that referenced this issue Oct 7, 2022
This is a long standing problem that resurfaces once in a while [0].
There might actually be two problems because I'm not 100% sure if the
issue underlying https://reviews.llvm.org/D115302 would be solved by
this or not. Anyway.

In 2008 we thought intrinsics do not read/write globals passed to them:
llvm/llvm-project@d4133ac
This is not correct given that intrinsics can synchronize threads and
cause effects to effectively become visible.

NOTE: I did not yet modify any tests but only tried out the reproducer
      of llvm/llvm-project#54851.

Fixes: llvm/llvm-project#54851

[0] https://discourse.llvm.org/t/bug-gvn-memdep-bug-in-the-presence-of-intrinsics/59402

Differential Revision: https://reviews.llvm.org/D123531
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants