Add __builtin___clear_cache to the portability headers
authorChristopher Dykes <cdykes@fb.com>
Thu, 26 Jan 2017 18:44:00 +0000 (10:44 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 26 Jan 2017 18:47:54 +0000 (10:47 -0800)
Summary:
HHVM uses this heavily in it's ARM JIT, so provide an implementation. With GCC & Clang this function is a no-op on x86_64 where the requirement doesn't exist, so do the same in the portability header.
This doesn't include the Windows portability header on x64 specifically because it is a very heavy header and this is currently very lightweight.

Reviewed By: yfeldblum

Differential Revision: D4451032

fbshipit-source-id: 0af57448577635f9cd690ea9b09ce46a244191e1

folly/Portability.h
folly/portability/Builtins.cpp [new file with mode: 0755]
folly/portability/Builtins.h

index 7a48f5332487e23c6179151dac2e75e398626670..17c064ba8a6f01d1d9771942a2c99a091cbf5241 100644 (file)
@@ -109,6 +109,12 @@ constexpr bool kHasUnalignedAccess = false;
 # define FOLLY_PPC64 0
 #endif
 
+namespace folly {
+constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
+constexpr bool kIsArchAArch64 = FOLLY_A64 == 1;
+constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
+}
+
 // packing is very ugly in msvc
 #ifdef _MSC_VER
 # define FOLLY_PACK_ATTR /**/
diff --git a/folly/portability/Builtins.cpp b/folly/portability/Builtins.cpp
new file mode 100755 (executable)
index 0000000..80f634e
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/portability/Builtins.h>
+
+#if _WIN32
+#include <folly/portability/Windows.h>
+
+namespace folly {
+namespace portability {
+namespace detail {
+void call_flush_instruction_cache_self_pid(void* begin, size_t size) {
+  FlushInstructionCache(GetCurrentProcess(), begin, size);
+}
+}
+}
+}
+#endif
index acc4ed9ab5158b97387884ab898a494f33229698..98bbb0975d19448645d2d3bde20d94b9134d4203 100755 (executable)
 
 #ifdef _WIN32
 #include <assert.h>
-#include <intrin.h>
 #include <folly/Portability.h>
+#include <intrin.h>
+#include <stdint.h>
+
+namespace folly {
+namespace portability {
+namespace detail {
+void call_flush_instruction_cache_self_pid(void* begin, size_t size);
+}
+}
+}
+
+FOLLY_ALWAYS_INLINE void __builtin___clear_cache(char* begin, char* end) {
+  if (folly::kIsArchAmd64) {
+    // x86_64 doesn't require the instruction cache to be flushed after
+    // modification.
+  } else {
+    // Default to flushing it for everything else, such as ARM.
+    folly::portability::detail::call_flush_instruction_cache_self_pid(
+        static_cast<void*>(begin), static_cast<size_t>(end - begin));
+  }
+}
 
 FOLLY_ALWAYS_INLINE int __builtin_clz(unsigned int x) {
   unsigned long index;