-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[DSE] Delay deleting non-memory-defs until end of DSE. #83411
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f29e3f7
[DSE] Delay deleting non-memory-defs until end of DSE.
fhahn 952b51b
Merge remote-tracking branch 'origin/main' into perf/dse-delay-delete
fhahn 33f9631
!fixup fix build failure.
fhahn 1d036f4
Merge remote-tracking branch 'origin/main' into perf/dse-delay-delete
fhahn be3aaa1
!fixup remove assert, directly delete MemDefs with void ty.
fhahn 8fc503c
Merge remote-tracking branch 'origin/main' into perf/dse-delay-delete
fhahn c839e00
!fixup add extra tests.
fhahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
llvm/test/Transforms/DeadStoreElimination/batchaa-caching-new-pointers.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 | ||
; RUN: opt -S -passes=dse < %s | FileCheck %s | ||
; | ||
; DSE kills `store i32 44, ptr %struct.byte.4, align 4` but should not kill | ||
; `call void @llvm.memset.p0.i64(...)` because it has a clobber read: | ||
; `%ret = load ptr, ptr %struct.byte.8` | ||
|
||
|
||
%struct.type = type { ptr, ptr } | ||
|
||
define ptr @foo(ptr noundef %ptr) { | ||
; CHECK-LABEL: define ptr @foo( | ||
; CHECK-SAME: ptr noundef [[PTR:%.*]]) { | ||
; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8 | ||
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6:[0-9]+]] | ||
; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8 | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4 | ||
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false) | ||
; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4 | ||
; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8 | ||
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: ret ptr [[RET]] | ||
; | ||
%struct.alloca = alloca %struct.type, align 8 | ||
call void @llvm.lifetime.start.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
%struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8 | ||
; Set %struct.alloca[8, 16) to 42. | ||
call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false) | ||
; Set %struct.alloca[8, 12) to 43. | ||
store i32 43, ptr %struct.byte.8, align 4 | ||
; Set %struct.alloca[4, 8) to 44. | ||
%struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4 | ||
store i32 44, ptr %struct.byte.4, align 4 | ||
; Return %struct.alloca[8, 16). | ||
%ret = load ptr, ptr %struct.byte.8 | ||
call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
ret ptr %ret | ||
} | ||
|
||
; Set of tests based on @foo, but where the memset's operands cannot be erased | ||
; due to other uses. Instead, they contain a number of removable MemoryDefs; | ||
; with non-void types result types. | ||
|
||
define ptr @foo_with_removable_malloc() { | ||
; CHECK-LABEL: define ptr @foo_with_removable_malloc() { | ||
; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8 | ||
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 4 | ||
; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8 | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4 | ||
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false) | ||
; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4 | ||
; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8 | ||
; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]]) | ||
; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]]) | ||
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: ret ptr [[RET]] | ||
; | ||
%struct.alloca = alloca %struct.type, align 8 | ||
call void @llvm.lifetime.start.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
%struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4 | ||
%struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8 | ||
|
||
; Set of removable memory deffs | ||
%m2 = tail call ptr @malloc(i64 4) | ||
%m1 = tail call ptr @malloc(i64 4) | ||
store i32 0, ptr %struct.byte.8 | ||
store i32 0, ptr %struct.byte.8 | ||
store i32 123, ptr %m1 | ||
store i32 123, ptr %m2 | ||
|
||
; Set %struct.alloca[8, 16) to 42. | ||
call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false) | ||
; Set %struct.alloca[8, 12) to 43. | ||
store i32 43, ptr %struct.byte.8, align 4 | ||
; Set %struct.alloca[4, 8) to 44. | ||
store i32 44, ptr %struct.byte.4, align 4 | ||
; Return %struct.alloca[8, 16). | ||
%ret = load ptr, ptr %struct.byte.8 | ||
call void @readnone(ptr %struct.byte.4); | ||
call void @readnone(ptr %struct.byte.8); | ||
call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
ret ptr %ret | ||
} | ||
|
||
define ptr @foo_with_removable_malloc_free() { | ||
; CHECK-LABEL: define ptr @foo_with_removable_malloc_free() { | ||
; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8 | ||
; CHECK-NEXT: [[M1:%.*]] = tail call ptr @malloc(i64 4) | ||
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 4 | ||
; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8 | ||
; CHECK-NEXT: [[M2:%.*]] = tail call ptr @malloc(i64 4) | ||
; CHECK-NEXT: call void @free(ptr [[M1]]) | ||
; CHECK-NEXT: call void @free(ptr [[M2]]) | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4 | ||
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false) | ||
; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4 | ||
; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8 | ||
; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]]) | ||
; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]]) | ||
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: ret ptr [[RET]] | ||
; | ||
%struct.alloca = alloca %struct.type, align 8 | ||
%m1 = tail call ptr @malloc(i64 4) | ||
call void @llvm.lifetime.start.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
%struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4 | ||
%struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8 | ||
|
||
store i32 0, ptr %struct.byte.4 | ||
store i32 0, ptr %struct.byte.8 | ||
%m2 = tail call ptr @malloc(i64 4) | ||
store i32 123, ptr %m1 | ||
call void @free(ptr %m1); | ||
store i32 123, ptr %m2 | ||
call void @free(ptr %m2); | ||
|
||
; Set %struct.alloca[8, 16) to 42. | ||
call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false) | ||
; Set %struct.alloca[8, 12) to 43. | ||
store i32 43, ptr %struct.byte.8, align 4 | ||
; Set %struct.alloca[4, 8) to 44. | ||
store i32 44, ptr %struct.byte.4, align 4 | ||
; Return %struct.alloca[8, 16). | ||
%ret = load ptr, ptr %struct.byte.8 | ||
call void @readnone(ptr %struct.byte.4); | ||
call void @readnone(ptr %struct.byte.8); | ||
call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
ret ptr %ret | ||
} | ||
|
||
define ptr @foo_with_malloc_to_calloc() { | ||
; CHECK-LABEL: define ptr @foo_with_malloc_to_calloc() { | ||
; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8 | ||
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8 | ||
; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 4 | ||
; CHECK-NEXT: [[CALLOC1:%.*]] = call ptr @calloc(i64 1, i64 4) | ||
; CHECK-NEXT: [[CALLOC:%.*]] = call ptr @calloc(i64 1, i64 4) | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4 | ||
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false) | ||
; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4 | ||
; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8 | ||
; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]]) | ||
; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]]) | ||
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]] | ||
; CHECK-NEXT: call void @use(ptr [[CALLOC1]]) | ||
; CHECK-NEXT: call void @use(ptr [[CALLOC]]) | ||
; CHECK-NEXT: ret ptr [[RET]] | ||
; | ||
%struct.alloca = alloca %struct.type, align 8 | ||
call void @llvm.lifetime.start.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
%struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8 | ||
%struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4 | ||
|
||
; Set of removable memory deffs | ||
%m1 = tail call ptr @malloc(i64 4) | ||
%m2 = tail call ptr @malloc(i64 4) | ||
store i32 0, ptr %struct.byte.4 | ||
store i32 0, ptr %struct.byte.8 | ||
call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %m2, i8 0, i64 4, i1 false) | ||
call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %m1, i8 0, i64 4, i1 false) | ||
|
||
; Set %struct.alloca[8, 16) to 42. | ||
call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false) | ||
; Set %struct.alloca[8, 12) to 43. | ||
store i32 43, ptr %struct.byte.8, align 4 | ||
; Set %struct.alloca[4, 8) to 44. | ||
store i32 44, ptr %struct.byte.4, align 4 | ||
; Return %struct.alloca[8, 16). | ||
%ret = load ptr, ptr %struct.byte.8 | ||
call void @readnone(ptr %struct.byte.4); | ||
call void @readnone(ptr %struct.byte.8); | ||
call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %struct.alloca) nounwind | ||
call void @use(ptr %m1) | ||
call void @use(ptr %m2) | ||
ret ptr %ret | ||
} | ||
|
||
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) | ||
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) | ||
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) | ||
|
||
declare noalias ptr @malloc(i64) willreturn allockind("alloc,uninitialized") "alloc-family"="malloc" | ||
declare void @readnone(ptr) readnone nounwind | ||
declare void @free(ptr nocapture) allockind("free") "alloc-family"="malloc" | ||
|
||
declare void @use(ptr) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there could be cases where a MemoryDef also loads a value that is then used as pointer of MemoryLocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it safer to just delay the deletion for them too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to this question, I'm not clear if this is possible. MemoryDefs could be volatile loads, but this is not the case here since the pass is only looking at store/writing instructions. A memcpy does also load, so a memcpy to a location that is later overwritten, could be deleted here, while copying from a location that's already in the cache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this time I'm inclined to say this cannot happen. So let's have this fix in for the existing issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting a memcpy wouldn't be a problem, as long as its pointer operands aren't deleted.
But we might remove something like a
malloc
call, which is a memory def, and the result of the malloc could be used as address and be in the cache. Updated the code to remove memory-defs that do not produce a value (i.e. have type void)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add more tests to cover the void/non-void type cases mentioned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a couple of extra tests based on the original test.