Skip to content

Commit fab77b9

Browse files
authored
Remove unnecessary fences from GSI output (#7308)
GlobalStructInference only optimizes gets of immutable fields, so even if those gets are seqcst, they cannot synchronize with any writes. We were being overly conservative by emitting fences in place of optimized seqcst gets before.
1 parent 379e5ec commit fab77b9

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

src/passes/GlobalStructInference.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,12 @@ struct GlobalStructInference : public Pass {
462462
// the early return above) so that only leaves 1 and 2.
463463
if (values.size() == 1) {
464464
// The case of 1 value is simple: trap if the ref is null, and
465-
// otherwise return the value. We must also fence if the get was
466-
// seqcst. No additional work is necessary for an acquire get because
467-
// there cannot have been any writes to this immutable field that it
468-
// would synchronize with.
469-
Expression* replacement =
470-
builder.makeDrop(builder.makeRefAs(RefAsNonNull, curr->ref));
471-
if (curr->order == MemoryOrder::SeqCst) {
472-
replacement =
473-
builder.blockify(replacement, builder.makeAtomicFence());
474-
}
475-
replaceCurrent(
476-
builder.blockify(replacement, getReadValue(values[0])));
465+
// otherwise return the value. Since the field is immutable, there
466+
// cannot have been any writes to it we must synchonize with, so we do
467+
// not need a fence.
468+
replaceCurrent(builder.makeSequence(
469+
builder.makeDrop(builder.makeRefAs(RefAsNonNull, curr->ref)),
470+
getReadValue(values[0])));
477471
return;
478472
}
479473
assert(values.size() == 2);
@@ -499,16 +493,10 @@ struct GlobalStructInference : public Pass {
499493
// of their execution matters (they may note globals for un-nesting).
500494
auto* left = getReadValue(values[0]);
501495
auto* right = getReadValue(values[1]);
502-
// Note that we must trap on null, so add a ref.as_non_null here. We
503-
// must also add a fence if this get is seqcst. As before, no extra work
504-
// is necessary for an acquire get because there cannot be a write it
505-
// synchronizes with.
496+
// Note that we must trap on null, so add a ref.as_non_null here. As
497+
// before, the get cannot have synchronized with anything.
506498
Expression* getGlobal =
507499
builder.makeGlobalGet(checkGlobal, wasm.getGlobal(checkGlobal)->type);
508-
if (curr->order == MemoryOrder::SeqCst) {
509-
getGlobal =
510-
builder.makeSequence(builder.makeAtomicFence(), getGlobal);
511-
}
512500
replaceCurrent(builder.makeSelect(
513501
builder.makeRefEq(builder.makeRefAs(RefAsNonNull, curr->ref),
514502
getGlobal),

test/lit/passes/gsi.wast

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,10 +2077,7 @@
20772077
;; CHECK-NEXT: (ref.as_non_null
20782078
;; CHECK-NEXT: (local.get $0)
20792079
;; CHECK-NEXT: )
2080-
;; CHECK-NEXT: (block (result (ref $two))
2081-
;; CHECK-NEXT: (atomic.fence)
2082-
;; CHECK-NEXT: (global.get $two-a)
2083-
;; CHECK-NEXT: )
2080+
;; CHECK-NEXT: (global.get $two-a)
20842081
;; CHECK-NEXT: )
20852082
;; CHECK-NEXT: )
20862083
;; CHECK-NEXT: )
@@ -2099,8 +2096,7 @@
20992096
)
21002097
)
21012098
(drop
2102-
;; This requires a fence to maintain its effect on the global order of
2103-
;; seqcst operations.
2099+
;; Even though this is seqcst, it still can't synchronize with anything.
21042100
(struct.atomic.get $two 0
21052101
(local.get 0)
21062102
)
@@ -2135,7 +2131,6 @@
21352131
;; CHECK-NEXT: (local.get $0)
21362132
;; CHECK-NEXT: )
21372133
;; CHECK-NEXT: )
2138-
;; CHECK-NEXT: (atomic.fence)
21392134
;; CHECK-NEXT: (i32.const 42)
21402135
;; CHECK-NEXT: )
21412136
;; CHECK-NEXT: )
@@ -2154,8 +2149,7 @@
21542149
)
21552150
)
21562151
(drop
2157-
;; This requires a fence to maintain its effect on the global order of
2158-
;; seqcst operations.
2152+
;; Even though this is seqcst, it still can't synchronize with anything.
21592153
(struct.atomic.get $two-same 0
21602154
(local.get 0)
21612155
)

0 commit comments

Comments
 (0)