Skip to content

Commit 9756ff0

Browse files
bmehta001Copilot
andcommitted
Use release/acquire ordering for GatherND error flag
Address review feedback: with relaxed ordering on separate atomics, the acquire of has_invalid_index could observe true while err_index is still at its default. Store err_index first (relaxed), then publish has_invalid_index with release. Load the flag with acquire to guarantee err_index visibility. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4921cde commit 9756ff0

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

onnxruntime/core/providers/cpu/tensor/gather_nd.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Status GatherNDBase::PrepareForCompute(const TensorShape& input_shape, const Ten
9191
// err_index == 0 to mean "no error" creates a collision when a zero-sized
9292
// dimension makes index 0 invalid — the error goes undetected.
9393
std::atomic<bool> has_invalid_index{false};
94-
int64_t err_index = 0;
94+
std::atomic<int64_t> err_index{0};
9595
p.element_bytes = bytes_per_value;
9696
p.element_count_per_slice = slice_size;
9797
p.bytes_per_slice = p.element_bytes * p.element_count_per_slice;
@@ -110,8 +110,10 @@ Status GatherNDBase::PrepareForCompute(const TensorShape& input_shape, const Ten
110110
const auto upper_limit = input_shape[SafeInt<size_t>(batch_dims_) + dim_idx];
111111
const auto lower_limit = -upper_limit;
112112
if (index < lower_limit || index >= upper_limit) {
113-
has_invalid_index.store(true, std::memory_order_relaxed);
114-
err_index = index;
113+
// Store err_index first, then publish via has_invalid_index with release
114+
// so the acquire load on the flag guarantees err_index is visible.
115+
err_index.store(index, std::memory_order_relaxed);
116+
has_invalid_index.store(true, std::memory_order_release);
115117
break;
116118
}
117119
if (index < 0) index += upper_limit;
@@ -130,9 +132,10 @@ Status GatherNDBase::PrepareForCompute(const TensorShape& input_shape, const Ten
130132
}
131133
});
132134

133-
return !has_invalid_index.load(std::memory_order_relaxed)
135+
return !has_invalid_index.load(std::memory_order_acquire)
134136
? Status::OK()
135-
: ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "invalid index found, index = ", err_index);
137+
: ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "invalid index found, index = ",
138+
err_index.load(std::memory_order_relaxed));
136139
}
137140

138141
template Status GatherNDBase::PrepareForCompute<int32_t>(const TensorShape&,

0 commit comments

Comments
 (0)