diff --git a/src/coreclr/jit/codegenarm64.cpp b/src/coreclr/jit/codegenarm64.cpp index db45c98b68e63c..9cb1620968c627 100644 --- a/src/coreclr/jit/codegenarm64.cpp +++ b/src/coreclr/jit/codegenarm64.cpp @@ -3280,6 +3280,9 @@ void CodeGen::genCodeForStoreInd(GenTreeStoreInd* tree) else { // issue a full memory barrier before a volatile StInd + // Note: We cannot issue store barrier ishst because it is a weaker barrier. + // The loads can get rearranged around the barrier causing to read wrong + // value. instGen_MemoryBarrier(); } } diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 00c58d70bae1d2..6bc30ca68389c0 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -4083,6 +4083,14 @@ unsigned Compiler::gtSetEvalOrder(GenTree* tree) } } } +#ifdef TARGET_ARM64 + if (tree->gtFlags & GTF_IND_VOLATILE) + { + // For volatile store/loads when address is contained we always emit `dmb` + // if it's not - we emit one-way barriers i.e. ldar/stlr + doAddrMode = false; + } +#endif // TARGET_ARM64 if (doAddrMode && gtMarkAddrMode(addr, &costEx, &costSz, tree->TypeGet())) { goto DONE;