This is an archive of the discontinued LLVM Phabricator instance.

[MemCpyOpt] Fixing Incorrect Code Motion while Handling Aggregate Type Values
ClosedPublic

Authored by myhsu on Aug 10 2019, 9:40 PM.

Details

Summary

When MemCpyOpt is handling aggregate type values, if an instruction (let's call it P) between the targeting load (L) and store (S) clobbers the source pointer of L, it will try to hoist S before P. This process will also hoist S's data dependency instructions.

However, the current implementation has a bug that if one of S's dependency instructions is also a user of P, MemCpyOpt will not prevent it from being hoisted above P and cause a use-before-define error. For example, in the newly added test file (i.e. aggregate-type-crash.ll), it will try to hoist both store %my_struct %1, %my_struct* %3 and its dependent, %3 = bitcast i8* %2 to %my_struct*, above %2 = call i8* @my_malloc(%my_struct* %0). Creating the following BB:

entry:
  %1 = bitcast i8* %4 to %my_struct*
  %2 = bitcast %my_struct* %1 to i8*
  %3 = bitcast %my_struct* %0 to i8*
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %2, i8* align 4 %3, i64 8, i1 false)
  %4 = call i8* @my_malloc(%my_struct* %0)
  ret void

Where there is a use-before-define error between %1 and %4.

Update: The compiler for the Pony Programming Language also encounter the same bug

Diff Detail

Event Timeline

myhsu created this revision.Aug 10 2019, 9:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 10 2019, 9:40 PM
myhsu edited the summary of this revision. (Show Details)Aug 11 2019, 7:05 AM
myhsu edited the summary of this revision. (Show Details)Aug 11 2019, 7:07 AM
lenary added a subscriber: lenary.Aug 27 2019, 3:36 AM
myhsu added a comment.Oct 16 2019, 9:33 AM

Ping on this review.
Also, the compiler for the Pony Programming Language bumped into the same bug. Since MemCpyOpt will run by default in O3, it would be a little bit hard for a downstream user to avoid this bug without changing the LLVM source tree.

myhsu edited the summary of this revision. (Show Details)Oct 16 2019, 9:34 AM
eugenis accepted this revision.Oct 17 2019, 3:58 PM

LGTM
Looks great.

This revision is now accepted and ready to land.Oct 17 2019, 3:58 PM

@myhsu do you need me to land this for you? I presume you don't yet have commit privileges.

myhsu added a comment.Oct 19 2019, 1:50 PM

@myhsu do you need me to land this for you? I presume you don't yet have commit privileges.

That would be great. Thank you very much

This revision was automatically updated to reflect the committed changes.