Skip to content

[DSE] Only consider provenance captures #138286

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 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,8 @@ struct DSEState {

auto I = InvisibleToCallerAfterRet.insert({V, false});
if (I.second && isInvisibleToCallerOnUnwind(V) && isNoAliasCall(V))
I.first->second = !PointerMayBeCaptured(V, /*ReturnCaptures=*/true);
I.first->second = capturesNothing(PointerMayBeCaptured(
V, /*ReturnCaptures=*/true, CaptureComponents::Provenance));
return I.first->second;
}

Expand All @@ -1230,7 +1231,8 @@ struct DSEState {
// with the killing MemoryDef. But we refrain from doing so for now to
// limit compile-time and this does not cause any changes to the number
// of stores removed on a large test set in practice.
I.first->second = PointerMayBeCaptured(V, /*ReturnCaptures=*/false);
I.first->second = capturesAnything(PointerMayBeCaptured(
V, /*ReturnCaptures=*/false, CaptureComponents::Provenance));
return !I.first->second;
}

Expand Down
2 changes: 0 additions & 2 deletions llvm/test/Transforms/DeadStoreElimination/assume.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ define void @f() {
; CHECK-NEXT: [[TMP1:%.*]] = call noalias ptr @_Znwm(i64 32)
; CHECK-NEXT: [[TMP2:%.*]] = icmp ugt ptr [[TMP1]], @global
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP2]])
; CHECK-NEXT: store i8 0, ptr [[TMP1]], align 1
; CHECK-NEXT: ret void
;
%tmp1 = call noalias ptr @_Znwm(i64 32)
Expand All @@ -23,7 +22,6 @@ define void @f2() {
; CHECK-NEXT: [[TMP1:%.*]] = call noalias ptr @_Znwm(i64 32)
; CHECK-NEXT: [[TMP2:%.*]] = icmp ugt ptr [[TMP1]], @global
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP2]])
; CHECK-NEXT: store i8 0, ptr [[TMP1]], align 1
; CHECK-NEXT: call void @quux(ptr @global)
; CHECK-NEXT: ret void
;
Expand Down
54 changes: 53 additions & 1 deletion llvm/test/Transforms/DeadStoreElimination/simple.ll
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,58 @@ define void @custom_malloc_no_escape() {
ret void
}

declare void @use.ptr(ptr)

define void @malloc_no_escape_via_attr() {
; CHECK-LABEL: @malloc_no_escape_via_attr(
; CHECK-NEXT: [[M:%.*]] = call ptr @malloc(i64 24)
; CHECK-NEXT: call void @use.ptr(ptr captures(none) [[M]])
; CHECK-NEXT: ret void
;
%m = call ptr @malloc(i64 24)
call void @use.ptr(ptr captures(none) %m)
store i8 0, ptr %m
ret void
}

define void @malloc_address_only_escape() {
; CHECK-LABEL: @malloc_address_only_escape(
; CHECK-NEXT: [[M:%.*]] = call ptr @malloc(i64 24)
; CHECK-NEXT: call void @use.ptr(ptr captures(address) [[M]])
; CHECK-NEXT: ret void
;
%m = call ptr @malloc(i64 24)
call void @use.ptr(ptr captures(address) %m)
store i8 0, ptr %m
ret void
}

define void @malloc_provenance_escape() {
; CHECK-LABEL: @malloc_provenance_escape(
; CHECK-NEXT: [[M:%.*]] = call ptr @malloc(i64 24)
; CHECK-NEXT: call void @use.ptr(ptr captures(provenance) [[M]])
; CHECK-NEXT: store i8 0, ptr [[M]], align 1
; CHECK-NEXT: ret void
;
%m = call ptr @malloc(i64 24)
call void @use.ptr(ptr captures(provenance) %m)
store i8 0, ptr %m
ret void
}

define void @malloc_read_provenance_escape() {
; CHECK-LABEL: @malloc_read_provenance_escape(
; CHECK-NEXT: [[M:%.*]] = call ptr @malloc(i64 24)
; CHECK-NEXT: call void @use.ptr(ptr captures(read_provenance) [[M]])
; CHECK-NEXT: store i8 0, ptr [[M]], align 1
; CHECK-NEXT: ret void
;
%m = call ptr @malloc(i64 24)
call void @use.ptr(ptr captures(read_provenance) %m)
store i8 0, ptr %m
ret void
}

define void @test21() {
; CHECK-LABEL: @test21(
; CHECK-NEXT: ret void
Expand Down Expand Up @@ -484,7 +536,7 @@ define i32 @test32(i1 %c, ptr %p, i32 %i, i1 %arg) {
; CHECK: bb1:
; CHECK-NEXT: store i32 [[V]], ptr [[P]], align 4
; CHECK-NEXT: call void @unknown_func()
; CHECK-NEXT: br i1 %arg, label [[BB1]], label [[BB2:%.*]]
; CHECK-NEXT: br i1 [[ARG:%.*]], label [[BB1]], label [[BB2:%.*]]
; CHECK: bb2:
; CHECK-NEXT: ret i32 0
;
Expand Down