Skip to content

[EarlyCSE] Fix dead store elimination for unwinding readnone calls #145287

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 1 commit into from
Jun 23, 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
10 changes: 7 additions & 3 deletions llvm/lib/Transforms/Scalar/EarlyCSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,11 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
}
}

// Make sure stores prior to a potential unwind are not removed, as the
// caller may read the memory.
if (Inst.mayThrow())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the target memory store intrinsics have nounwind, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. It's the default for intrinsics.

LastStore = nullptr;

// If this is a simple instruction that we can value number, process it.
if (SimpleValue::canHandle(&Inst)) {
if ([[maybe_unused]] auto *CI = dyn_cast<ConstrainedFPIntrinsic>(&Inst)) {
Expand Down Expand Up @@ -1616,13 +1621,12 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
continue;
}

// If this instruction may read from memory or throw (and potentially read
// from memory in the exception handler), forget LastStore. Load/store
// If this instruction may read from memory, forget LastStore. Load/store
// intrinsics will indicate both a read and a write to memory. The target
// may override this (e.g. so that a store intrinsic does not read from
// memory, and thus will be treated the same as a regular store for
// commoning purposes).
if ((Inst.mayReadFromMemory() || Inst.mayThrow()) &&
if (Inst.mayReadFromMemory() &&
!(MemInst.isValid() && !MemInst.mayReadFromMemory()))
LastStore = nullptr;

Expand Down
21 changes: 18 additions & 3 deletions llvm/test/Transforms/EarlyCSE/basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ declare i32 @func(ptr%P) readonly
;; Simple call CSE'ing.
define i32 @test5(ptr%P) {
; CHECK-LABEL: @test5(
; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]), !prof !0
; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]), !prof [[PROF0:![0-9]+]]
; CHECK-NEXT: ret i32 0
;
%V1 = call i32 @func(ptr %P), !prof !0
Expand Down Expand Up @@ -212,10 +212,25 @@ define i32 @test9(ptr%P) {
ret i32 %V1
}

;; Trivial DSE can be performed across a readnone call.
;; Trivial DSE can be performed across a readnone nounwind call.
define i32 @test10(ptr%P) {
; CHECK-LABEL: @test10(
; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]) #[[ATTR2]]
; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]) #[[ATTR3:[0-9]+]]
; CHECK-NEXT: store i32 5, ptr [[P]], align 4
; CHECK-NEXT: ret i32 [[V1]]
;
store i32 4, ptr %P
%V1 = call i32 @func(ptr %P) readnone nounwind
store i32 5, ptr %P
ret i32 %V1
}

; Trivial DSE can't be performed across a potentially unwinding readnone
; call, as the caller may read the memory on unwind.
define i32 @test_readnone_missing_nounwind(ptr %P) {
; CHECK-LABEL: @test_readnone_missing_nounwind(
; CHECK-NEXT: store i32 4, ptr [[P:%.*]], align 4
; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P]]) #[[ATTR2]]
; CHECK-NEXT: store i32 5, ptr [[P]], align 4
; CHECK-NEXT: ret i32 [[V1]]
;
Expand Down
18 changes: 0 additions & 18 deletions llvm/test/Transforms/EarlyCSE/readnone-mayunwind.ll

This file was deleted.

Loading