Skip to content

Commit

Permalink
Handle atomics in GTO (#7160)
Browse files Browse the repository at this point in the history
GTO removes fields that are never read and also removes sets to those
fields. Update the pass to add a seqcst fence when removing a seqcst set
to preserve its effect on the global order of seqcst operations.
  • Loading branch information
tlively authored Dec 18, 2024
1 parent 0090789 commit 0b37831
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/passes/GlobalTypeOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,23 @@ struct GlobalTypeOptimization : public Pass {
// operations here: the trap on a null ref happens after the value,
// which might have side effects.
Builder builder(*getModule());
auto flipped = getResultOfFirst(curr->ref,
builder.makeDrop(curr->value),
getFunction(),
getModule(),
getPassOptions());
replaceCurrent(
builder.makeDrop(builder.makeRefAs(RefAsNonNull, flipped)));
auto* flipped = getResultOfFirst(curr->ref,
builder.makeDrop(curr->value),
getFunction(),
getModule(),
getPassOptions());
Expression* replacement =
builder.makeDrop(builder.makeRefAs(RefAsNonNull, flipped));
if (curr->order == MemoryOrder::SeqCst) {
// If the removed set is sequentially consistent, we must insert a
// seqcst fence to preserve the effect on the global order of seqcst
// operations. No fence is necessary for release sets because there
// are no reads for them to synchronize with given that we are
// removing the field.
replacement =
builder.makeSequence(replacement, builder.makeAtomicFence());
}
replaceCurrent(replacement);
}
}

Expand Down
63 changes: 63 additions & 0 deletions test/lit/passes/gto-removals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1564,3 +1564,66 @@
(export "globalB" (global $globalB))
)

;; Removed atomic sets needs special handling.
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $A (shared (struct)))
(type $A (shared (struct (mut i32))))

;; CHECK: (type $1 (func (param (ref $A))))

;; CHECK: (func $sets (type $1) (param $0 (ref $A))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (block (result (ref $A))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (block (result (ref $A))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (block (result (ref $A))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (atomic.fence)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $sets (param (ref $A))
;; Normal set is optimizable.
(struct.set $A 0
(local.get 0)
(i32.const 1)
)
;; Release set is optimizable without a fence because there is no get to
;; synchronize with.
(struct.atomic.set acqrel $A 0
(local.get 0)
(i32.const 1)
)
;; This requires a fence to keep the effect on the global order of seqcst
;; operations.
(struct.atomic.set $A 0
(local.get 0)
(i32.const 1)
)
)
)

0 comments on commit 0b37831

Please sign in to comment.