r258506 - [MSVC Compat] Don't provide /volatile:ms semantics to types > pointer

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 22 08:36:44 PST 2016


Author: majnemer
Date: Fri Jan 22 10:36:44 2016
New Revision: 258506

URL: http://llvm.org/viewvc/llvm-project?rev=258506&view=rev
Log:
[MSVC Compat] Don't provide /volatile:ms semantics to types > pointer

Volatile loads of type wider than a pointer get split by MSVC because
the base x86 ISA doesn't provide loads which are wider than pointer
width.  LLVM assumes that it can emit an cmpxchg8b but this is
problematic if the memory is in a CONST memory segment.

Instead, provide behavior compatible with MSVC: split loads wider than a
pointer.

Modified:
    cfe/trunk/lib/CodeGen/CGAtomic.cpp
    cfe/trunk/test/CodeGen/ms-volatile.c

Modified: cfe/trunk/lib/CodeGen/CGAtomic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGAtomic.cpp?rev=258506&r1=258505&r2=258506&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGAtomic.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGAtomic.cpp Fri Jan 22 10:36:44 2016
@@ -1295,10 +1295,23 @@ bool CodeGenFunction::LValueIsSuitableFo
 /// performing such an operation can be performed without a libcall.
 bool CodeGenFunction::typeIsSuitableForInlineAtomic(QualType Ty,
                                                     bool IsVolatile) const {
+  // The operation must be volatile for us to make it atomic.
+  if (!IsVolatile)
+    return false;
+  // The -fms-volatile flag must be passed for us to adopt this behavior.
+  if (!CGM.getCodeGenOpts().MSVolatile)
+    return false;
+
   // An atomic is inline if we don't need to use a libcall (e.g. it is builtin).
-  bool AtomicIsInline = getContext().getTargetInfo().hasBuiltinAtomic(
-      getContext().getTypeSize(Ty), getContext().getTypeAlign(Ty));
-  return CGM.getCodeGenOpts().MSVolatile && IsVolatile && AtomicIsInline;
+  if (!getContext().getTargetInfo().hasBuiltinAtomic(
+          getContext().getTypeSize(Ty), getContext().getTypeAlign(Ty)))
+    return false;
+
+  // MSVC doesn't seem to do this for types wider than a pointer.
+  if (getContext().getTypeSize(Ty) >
+      getContext().getTypeSize(getContext().getIntPtrType()))
+    return false;
+  return true;
 }
 
 RValue CodeGenFunction::EmitAtomicLoad(LValue LV, SourceLocation SL,

Modified: cfe/trunk/test/CodeGen/ms-volatile.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/ms-volatile.c?rev=258506&r1=258505&r2=258506&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/ms-volatile.c (original)
+++ cfe/trunk/test/CodeGen/ms-volatile.c Fri Jan 22 10:36:44 2016
@@ -52,11 +52,23 @@ void test7(volatile struct bar *p, volat
 void test8(volatile double *p, volatile double *q) {
   *p = *q;
   // CHECK-LABEL: @test8
-  // CHECK: load atomic volatile {{.*}} acquire
-  // CHECK: store atomic volatile {{.*}}, {{.*}} release
+  // CHECK: load volatile {{.*}}
+  // CHECK: store volatile {{.*}}, {{.*}}
 }
 void test9(volatile baz *p, baz *q) {
   *p = *q;
   // CHECK-LABEL: @test9
   // CHECK: store atomic volatile {{.*}}, {{.*}} release
 }
+void test10(volatile long long *p, volatile long long *q) {
+  *p = *q;
+  // CHECK-LABEL: @test10
+  // CHECK: load volatile {{.*}}
+  // CHECK: store volatile {{.*}}, {{.*}}
+}
+void test11(volatile float *p, volatile float *q) {
+  *p = *q;
+  // CHECK-LABEL: @test11
+  // CHECK: load atomic volatile {{.*}} acquire
+  // CHECK: store atomic volatile {{.*}}, {{.*}} release
+}




More information about the cfe-commits mailing list