On Mon, 18 Jan 2021, Zebediah Figura (she/her) wrote:

On 1/18/21 12:34 PM, Martin Storsjö wrote:
On Mon, 18 Jan 2021, Zebediah Figura (she/her) wrote:

On 1/17/21 2:05 PM, Martin Storsjö wrote:
Signed-off-by: Martin Storsjö <[email protected]>
---
Added memory clobbers for MemoryBarrier().
---
 mingw-w64-headers/crt/_mingw.h.in | 8 ++++++++
 mingw-w64-headers/include/winnt.h | 6 ++++++
 2 files changed, 14 insertions(+)

diff --git a/mingw-w64-headers/crt/_mingw.h.in
b/mingw-w64-headers/crt/_mingw.h.in
index 01eff66fa..25513d7da 100644
--- a/mingw-w64-headers/crt/_mingw.h.in
+++ b/mingw-w64-headers/crt/_mingw.h.in
@@ -580,7 +580,15 @@ extern "C" {
 void __cdecl __debugbreak(void);
 __MINGW_INTRIN_INLINE void __cdecl __debugbreak(void)
 {
+#if defined(__i386__) || defined(__x86_64__)
   __asm__ __volatile__("int {$}3":);
+#elif defined(__arm__)
+  __asm__ __volatile__("udf #1");
+#elif defined(__aarch64__)
+  __asm__ __volatile__("brk #0xf000");
+#else
+  __asm__ __volatile__("unimplemented");
+#endif
 }
 #endif
 #endif
diff --git a/mingw-w64-headers/include/winnt.h
b/mingw-w64-headers/include/winnt.h
index 0decf495a..558423745 100644
--- a/mingw-w64-headers/include/winnt.h
+++ b/mingw-w64-headers/include/winnt.h
@@ -1846,6 +1846,9 @@ extern "C" {
 #define InterlockedCompareExchangePointerAcquire
_InterlockedCompareExchangePointer
 #define InterlockedCompareExchangePointerRelease
_InterlockedCompareExchangePointer

+#define YieldProcessor() __asm__ __volatile__("dmb ishst\n\tyield")
+#define MemoryBarrier() __asm__ __volatile__("dmb":::"memory")
+
 #ifdef __cplusplus
   }
 #endif
@@ -2055,6 +2058,9 @@ extern "C" {
 #define InterlockedCompareExchangePointerAcquire
_InterlockedCompareExchangePointer
 #define InterlockedCompareExchangePointerRelease
_InterlockedCompareExchangePointer

+#define YieldProcessor() __asm__ __volatile__("dmb ishst\n\tyield")
+#define MemoryBarrier() __asm__ __volatile__("dmb sy":::"memory")
+
 #ifdef __cplusplus
   }
 #endif


Should YieldProcessor() have a compiler barrier? Unfortunately I can't
seem to find any reference as to whether it should, and I don't have
Visual Studio to test with.

I have Visual Studio - how do you suggest to investigate it?

My best guess is to play around with code and optimization until I can
find a sequence that the compiler reorders, and see if YieldProcessor()
prevents that. But of course that could be difficult, and may not prove
anything for certain.

I guess I ask because it's not clear to me whether it should be
necessary either. It doesn't seem to be present for x86, but there might
be code out there that does something like

"while (!var) YieldProcessor();"

and expects it to work. I'm assuming the "dmb ishst" would prevent CPU
reordering enough for that to work, but it's not clear to me that the
compiler wouldn't optimize out the loop somehow...

Right, I guess it would be safest with such clobbers just in case. It's not like one should have a concern of utmost optimization at the point where one is intentionally yielding anyway.

Technically, maybe the __debugbreak inline asm snippet also should have that as well, for the same reasons? Although there, the existing x86 inline asm version doesn't have such clobbers either.

// Martin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to