# HG changeset patch
# Parent  f20811cd2fde8357b5c51307decee8c877c1e16a
Import libcilkrts Build 4467 (PR target/68945)

diff --git a/libcilkrts/Makefile.am b/libcilkrts/Makefile.am
--- a/libcilkrts/Makefile.am
+++ b/libcilkrts/Makefile.am
@@ -54,7 +54,7 @@ GENERAL_FLAGS = -I$(top_srcdir)/include 
 # Enable Intel Cilk Plus extension
 GENERAL_FLAGS += -fcilkplus
 
-# Always generate unwind tables
+#Always generate unwind tables
 GENERAL_FLAGS += -funwind-tables
 
 AM_CFLAGS = $(XCFLAGS) $(GENERAL_FLAGS) -std=c99
diff --git a/libcilkrts/README b/libcilkrts/README
--- a/libcilkrts/README
+++ b/libcilkrts/README
@@ -1,14 +1,16 @@
-Intel(R) Cilk(TM) Plus runtime library
+Intel(R) Cilk(TM) Plus Runtime Library
 
 Index:
-1. BUILDING
-2. USING
-3. DOXYGEN DOCUMENTATION
-4. QUESTIONS OR BUGS
-5. CONTRIBUTIONS
+1. BUILDING WITH AUTOMAKE
+2. BUILDING WITH CMAKE
+3. INSTALLING TO VXWORKS
+4. USING
+5. DOXYGEN DOCUMENTATION
+6. QUESTIONS OR BUGS
+7. CONTRIBUTIONS
 
 #
-#  1. BUILDING:
+#  1. BUILDING WITH AUTOMAKE:
 #
 
 To distribute applications that use the Intel Cilk Plus language
@@ -40,22 +42,87 @@ configure script:
 
 % ./configure --prefix=/your/path/to/lib
 
-It is also possible to use CMake if the above method does not apply
-well in your environment. Instruction is available in CMakeLists.txt.
+#
+#  2. BUILDING WITH CMAKE:
+#
+
+To distribute applications that use the Intel Cilk Plus language
+extensions to non-development systems, you need to build the Intel
+Cilk Plus runtime library and distribute it with your application.
+This instruction describes the build process using CMake*, which
+supports Linux*, Windows*, and OS X*.  It is fine to use this process
+to build a Linux library, but it is highly recommended to use the
+more mature build process described above when building on Linux.
+
+You need the CMake tool and a C/C++ compiler that supports the Intel
+Cilk Plus language extensions, and the requirements for each operating
+systems are:
+
+Common:
+    CMake 3.0.0 or later
+    Make tools such as make (Linux, OS X) or nmake (Windows)
+Linux:
+    GCC* 4.9.2 or later, or Intel(R) C++ Compiler v12.1 or later
+Windows:
+    Intel C++ Compiler v12.1 or later
+    Visual Studio* 2010 or later
+OS X:
+    Cilk-enabled branch of Clang*/LLVM* (http://cilkplus.github.io),
+    or Intel C++ Compiler v12.1 or later
+
+The common steps to build the libraries are 1) invoke cmake with
+appropriate options, 2) invoke a make tool available on the system.
+The following examples show build processes on OS X and Windows.
+
+OS X:
+    % mkdir ./build && cd ./build
+    % cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
+            -DCMAKE_INSTALL_PREFIX=./install ..
+    % make && make install
+
+Windows:
+    % mkdir .\build && cd .\build
+    % cmake -G "NMake Makefiles" -DCMAKE_C_COMPILER=icl \
+            -DCMAKE_CXX_COMPILER=icl -DCMAKE_INSTALL_PREFIX=.\install ..
+    % nmake && nmake install
 
 #
-#  2. USING:
+#  3. INSTALLING TO VXWORKS OS
+#
+
+For Windows host, run VxWorks_Install.bat.
+For Linux host, run VxWorks_Install.sh.
+
+You may need to give environment variable WIND_BASE to indicate VxWorks
+installation path.
+
+Create a VSB project, and you will see a layer named "CILKPLUS_KERNEL".
+Enable it and build the project.
+
+Create a VIP project with ICC and add component INCLUDE_CILKPLUS, then you
+will get support of Intel Cilk Plus features in VxWorks.
+
+#
+#  4. USING:
 #
 
 The Intel(R) C++ Compiler will automatically try to bring in the
 Intel Cilk Plus runtime in any program that uses the relevant
-features.  GCC requires explicit linking of both the library and
-its dependencies (libpthread, libdl).  For example:
+features.  GCC and Clang requires an explicit compiler option,
+-fcilkplus, to enable Intel Cilk Plus language extensions.
+For example,
+
+% gcc -fcilkplus -o foo.exe foo.c
+% clang -fcilkplus -o foo.exe foo.c 
+
+Older GCC versions (e.g., 4.8 cilkplus branch) requires explicit linking
+of both the library and its dependencies (libpthread, libdl).
+For example:
 
 % gcc foo.c -lcilkrts -lpthread -ldl
 
 #
-#  3. DOXYGEN DOCUMENTATION:
+#  5. DOXYGEN DOCUMENTATION:
 #
 
 The library source has Doxygen markup.  Generate HTML documentation
@@ -64,7 +131,7 @@ based on the markup by changing director
 % doxygen doxygen.cfg
 
 #
-#  4. QUESTIONS OR BUGS:
+#  6. QUESTIONS OR BUGS:
 #
 
 Issues with the Intel Cilk Plus runtime can be addressed in the Intel
@@ -72,7 +139,7 @@ Cilk Plus forums:
 http://software.intel.com/en-us/forums/intel-cilk-plus/
 
 #
-#  5. CONTRIBUTIONS:
+#  7. CONTRIBUTIONS:
 #
 
 The Intel Cilk Plus runtime library is dual licensed. The upstream copy
@@ -85,6 +152,13 @@ contributed to the upstream version via 
 Thanks to Tobias Burnus for showing us the magic to make gcc and g++
 automatically include the Cilk Plus runtime.
 
+Thanks to Eric Olson for sharing his patch for Raspberry Pi* with us.
+
+Thanks to Rainer Orth for submitting patches for exception handling and
+enabling Cilk Plus on the SPARC* architecture.
+
 ------------------------
 Intel and Cilk are trademarks of Intel Corporation in the U.S. and/or
 other countries.
+
+*Other names and brands may be claimed as the property of others.
diff --git a/libcilkrts/configure.ac b/libcilkrts/configure.ac
--- a/libcilkrts/configure.ac
+++ b/libcilkrts/configure.ac
@@ -69,6 +69,10 @@ AC_PROG_CXX
 AC_CONFIG_FILES([Makefile libcilkrts.spec])
 AC_FUNC_ALLOCA
 
+# Check for dl functions
+AC_CHECK_LIB(dl, dladdr,
+             [AC_DEFINE(HAVE_DLADDR, 1, [Define if you have dladdr()])])
+
 # Check whether the target supports protected visibility.
 save_CFLAGS="$CFLAGS"
 CFLAGS="$CFLAGS -Werror"
@@ -141,11 +145,7 @@ esac
 # contains information on what's needed
 case "${target}" in
 
-  x86_64-*-*)
-    config_dir="x86"
-    ;;
-
-  i?86-*-*)
+  i?86-*-* | x86_64-*-*)
     config_dir="x86"
     ;;
 
@@ -153,6 +153,10 @@ case "${target}" in
     config_dir="arm"
     ;;
 
+  sparc*-*-*)
+    config_dir="sparc"
+    ;;
+
   *)
     config_dir="generic"
     ;;
diff --git a/libcilkrts/configure.tgt b/libcilkrts/configure.tgt
--- a/libcilkrts/configure.tgt
+++ b/libcilkrts/configure.tgt
@@ -44,12 +44,12 @@
 
 # Disable Cilk Runtime library for unsupported architectures.
 case "${target}" in
-  x86_64-*-*)
-    ;;
-  i?86-*-*)
+  i?86-*-* | x86_64-*-*)
     ;;
   arm-*-*)
     ;;
+  sparc*-*-*)
+    ;;
   *-*-*)
     UNSUPPORTED=1
     ;;
diff --git a/libcilkrts/include/cilk/cilk_undocumented.h b/libcilkrts/include/cilk/cilk_undocumented.h
--- a/libcilkrts/include/cilk/cilk_undocumented.h
+++ b/libcilkrts/include/cilk/cilk_undocumented.h
@@ -106,9 +106,6 @@ size_t __cilkrts_get_stack_size(void);
 CILK_EXPORT __CILKRTS_NOTHROW
 void __cilkrts_dump_stats(void);
 
-CILK_EXPORT __CILKRTS_NOTHROW
-int __cilkrts_irml_version(void);
-
 struct __cilk_tbb_unwatch_thunk;
 struct __cilk_tbb_stack_op_thunk;
 
diff --git a/libcilkrts/include/internal/cilk_version.h b/libcilkrts/include/internal/cilk_version.h
--- a/libcilkrts/include/internal/cilk_version.h
+++ b/libcilkrts/include/internal/cilk_version.h
@@ -49,10 +49,10 @@
 
 #define VERSION_MAJOR 2
 #define VERSION_MINOR 0
-#define VERSION_BUILD 4420
+#define VERSION_BUILD 4467
 #define VERSION_REV 0
-#define VERSION_STRING "2,0,4420,0"
-#define VERSION_HASH "3b2d6aa9059c"
+#define VERSION_STRING "2,0,4467,0"
+#define VERSION_HASH "b7e54d87bd17"
 #define VERSION_BRANCH "eng"
-#define TBB_REV_NUMBER "14788"
-#define VERSION_YEAR "2015"
+#define TBB_REV_NUMBER ""
+#define VERSION_YEAR "2016"
diff --git a/libcilkrts/include/internal/rev.mk b/libcilkrts/include/internal/rev.mk
--- a/libcilkrts/include/internal/rev.mk
+++ b/libcilkrts/include/internal/rev.mk
@@ -49,4 +49,4 @@
 #
 # It was automatically generated by cilkrts/include/internal/Makefile
 
-CILK_REVISION = 4420
+CILK_REVISION = 4467
diff --git a/libcilkrts/runtime/cilk-abi.c b/libcilkrts/runtime/cilk-abi.c
--- a/libcilkrts/runtime/cilk-abi.c
+++ b/libcilkrts/runtime/cilk-abi.c
@@ -709,7 +709,6 @@ CILK_API_TBB_RETCODE
     return 0;   /* Success! */
 }
 
-
 // This function must be called only within a continuation, within the stack
 // frame of the continuation itself.
 CILK_API_INT __cilkrts_synched(void)
diff --git a/libcilkrts/runtime/cilk_fiber-unix.cpp b/libcilkrts/runtime/cilk_fiber-unix.cpp
--- a/libcilkrts/runtime/cilk_fiber-unix.cpp
+++ b/libcilkrts/runtime/cilk_fiber-unix.cpp
@@ -220,7 +220,7 @@ NORETURN cilk_fiber_sysdep::run()
         // enough extra space from the top of the stack we are
         // switching to for any temporaries required for this run()
         // function.
-        JMPBUF_SP(m_resume_jmpbuf) = m_stack_base - frame_size;
+        JMPBUF_SP(m_resume_jmpbuf) = CILK_ADJUST_SP(m_stack_base - frame_size);
 
         // GCC doesn't allow us to call __builtin_longjmp in the same function
         // that calls __builtin_setjmp, so it's been moved into it's own
diff --git a/libcilkrts/runtime/cilk_fiber.h b/libcilkrts/runtime/cilk_fiber.h
--- a/libcilkrts/runtime/cilk_fiber.h
+++ b/libcilkrts/runtime/cilk_fiber.h
@@ -73,9 +73,12 @@
  *
  * A value of 0 means no debugging.
  * Higher values generate more debugging output.
+ *
  */
+
+#ifndef FIBER_DEBUG
 #define FIBER_DEBUG 0
-
+#endif
 /**
  * @brief Flag for validating reference counts.
  * 
diff --git a/libcilkrts/runtime/config/arm/os-fence.h b/libcilkrts/runtime/config/arm/os-fence.h
--- a/libcilkrts/runtime/config/arm/os-fence.h
+++ b/libcilkrts/runtime/config/arm/os-fence.h
@@ -47,6 +47,14 @@
  *  for your assistance in helping us improve Cilk Plus.
  **************************************************************************/
 
+// __atomic_* intrinsics are available since GCC 4.7.
+#define HAVE_ATOMIC_INTRINSICS defined(__GNUC__) && \
+                               (__GNUC__ * 10 + __GNUC_MINOR__ >= 47)
+
+// GCC before 4.4 does not implement __sync_synchronize properly
+#define HAVE_SYNC_INTRINSICS defined(__GNUC__) && \
+                             (__GNUC__ * 10 + __GNUC_MINOR__ >= 44)
+
 /*
  * void __cilkrts_fence(void)
  *
@@ -60,5 +68,12 @@
  * the CPUID instruction).
  */
 
-// COMMON_SYSDEP void __cilkrts_fence(void); ///< MFENCE instruction
-# define __cilkrts_fence() __asm__ __volatile__ ("mcr   p15,0,%[t],c7,c10,4\n" :: [t] "r" (0) : "memory");
+#if HAVE_ATOMIC_INTRINSICS
+#   define __cilkrts_fence() __atomic_thread_fence(__ATOMIC_SEQ_CST)
+#elif HAVE_SYNC_INTRINSICS
+#   define __cilkrts_fence() __sync_synchronize()
+#else
+#   define __cilkrts_fence()
+// Leaving this code just in case.
+//# define __cilkrts_fence() __asm__ __volatile__ ("mcr   p15,0,%[t],c7,c10,4\n" :: [t] "r" (0) : "memory");
+#endif
diff --git a/libcilkrts/runtime/except-gcc.cpp b/libcilkrts/runtime/except-gcc.cpp
--- a/libcilkrts/runtime/except-gcc.cpp
+++ b/libcilkrts/runtime/except-gcc.cpp
@@ -60,7 +60,9 @@
 #include <stdint.h>
 #include <typeinfo>
 
+#ifndef DEBUG_EXCEPTIONS
 #define DEBUG_EXCEPTIONS 0
+#endif
 
 struct pending_exception_info
 {
diff --git a/libcilkrts/runtime/global_state.cpp b/libcilkrts/runtime/global_state.cpp
--- a/libcilkrts/runtime/global_state.cpp
+++ b/libcilkrts/runtime/global_state.cpp
@@ -579,7 +579,6 @@ global_state_t* cilkg_init_global_state(
     g->stack_size = cilkos_validate_stack_size(g->stack_size);
     g->failure_to_allocate_stack = 0;
 
-
     return g;
 }
 
diff --git a/libcilkrts/runtime/global_state.h b/libcilkrts/runtime/global_state.h
--- a/libcilkrts/runtime/global_state.h
+++ b/libcilkrts/runtime/global_state.h
@@ -215,7 +215,6 @@ struct global_state_t { /* COMMON_PORTAB
     /// Global fiber pool
     cilk_fiber_pool fiber_pool;
 
-
     /**
      * @brief Track whether the runtime has failed to allocate a
      * stack.
diff --git a/libcilkrts/runtime/jmpbuf.h b/libcilkrts/runtime/jmpbuf.h
--- a/libcilkrts/runtime/jmpbuf.h
+++ b/libcilkrts/runtime/jmpbuf.h
@@ -107,6 +107,18 @@
  */
 #define SP(SF) JMPBUF_SP((SF)->ctx)
 
+/**
+ * @brief Some architecture-dependent stack adjustment.
+ */
+#if defined(__sparcv9)
+    // Subtract sparc v9 stack bias so the actual stack starts at the
+    // allocated area.
+#   define CILK_ADJUST_SP(SP) ((SP) - 2047)
+#   define CILK_UNADJUST_SP(SP) ((SP) + 2047)
+#else
+#   define CILK_ADJUST_SP(SP) (SP)
+#   define CILK_UNADJUST_SP(SP) (SP)
+#endif
 
 __CILKRTS_BEGIN_EXTERN_C
 
@@ -120,7 +132,7 @@
  */
 inline char *__cilkrts_get_sp(__cilkrts_stack_frame *sf)
 {
-    return (char *)SP(sf);
+    return (char *)CILK_UNADJUST_SP(SP(sf));
 }
 
 /**
diff --git a/libcilkrts/runtime/linux-symbols.ver b/libcilkrts/runtime/linux-symbols.ver
--- a/libcilkrts/runtime/linux-symbols.ver
+++ b/libcilkrts/runtime/linux-symbols.ver
@@ -71,7 +71,6 @@ CILKABI0
     __cilkrts_hyperobject_dealloc;
     __cilkrts_hyperobject_noop_destroy;
     __cilkrts_init;
-    __cilkrts_irml_version;
     __cilkrts_leave_frame;
     __cilkrts_metacall;
     __cilkrts_rethrow;
diff --git a/libcilkrts/runtime/local_state.h b/libcilkrts/runtime/local_state.h
--- a/libcilkrts/runtime/local_state.h
+++ b/libcilkrts/runtime/local_state.h
@@ -360,6 +360,13 @@ struct local_state  /* COMMON_PORTABLE *
     unsigned int steal_failure_count;
 
     /**
+     * Record indicating that the worker stole work at least once.
+     *
+     * [local read/write]
+     */
+    int has_stolen;
+
+    /**
      * 1 if work was stolen from another worker.  When true, this will flag
      * setup_for_execution_pedigree to increment the pedigree when we resume
      * execution to match the increment that would have been done on a return
diff --git a/libcilkrts/runtime/mac-symbols.txt b/libcilkrts/runtime/mac-symbols.txt
--- a/libcilkrts/runtime/mac-symbols.txt
+++ b/libcilkrts/runtime/mac-symbols.txt
@@ -33,7 +33,6 @@
 ___cilkrts_hyperobject_dealloc
 ___cilkrts_hyperobject_noop_destroy
 ___cilkrts_init
-
 ___cilkrts_leave_frame
 ___cilkrts_metacall
 ___cilkrts_resume
diff --git a/libcilkrts/runtime/os-unix.c b/libcilkrts/runtime/os-unix.c
--- a/libcilkrts/runtime/os-unix.c
+++ b/libcilkrts/runtime/os-unix.c
@@ -432,7 +432,9 @@ COMMON_SYSDEP void __cilkrts_idle(void)
 #elif defined(__MIC__)
     _mm_delay_32(1024);
 #elif defined(__linux__) || \
-      defined(__APPLE__)
+      defined(__APPLE__) || \
+      defined(__CYGWIN__)
+      
     usleep(10000);
 #else
 # error "Unsupported architecture"
@@ -452,6 +454,7 @@ COMMON_SYSDEP void __cilkrts_yield(void)
 {
 #if defined(__ANDROID__)  || \
     defined(__APPLE__)    || \
+    defined(__CYGWIN__)   || \
     defined(__FreeBSD__)  || \
     defined(__VXWORKS__)  || \
     (defined(__sun__) && defined(__svr4__))
diff --git a/libcilkrts/runtime/record-replay.cpp b/libcilkrts/runtime/record-replay.cpp
--- a/libcilkrts/runtime/record-replay.cpp
+++ b/libcilkrts/runtime/record-replay.cpp
@@ -262,7 +262,7 @@ char * walk_pedigree_nodes(char *p, cons
     if (pnode->parent)
     {
         p = walk_pedigree_nodes(p, pnode->parent);
-        p += cilk_snprintf_s(p, PEDIGREE_BUFF_SIZE, "%s", (char *) "_");
+        p += cilk_snprintf_s(p, PEDIGREE_BUFF_SIZE, "%s", (char *)"_");
     }
     return p + cilk_snprintf_l(p, PEDIGREE_BUFF_SIZE, "%" PRIu64, pnode->rank);
 }
diff --git a/libcilkrts/runtime/scheduler.c b/libcilkrts/runtime/scheduler.c
--- a/libcilkrts/runtime/scheduler.c
+++ b/libcilkrts/runtime/scheduler.c
@@ -1789,20 +1789,27 @@ static full_frame* check_for_work(__cilk
         if (NULL == ff) {
             // Punish the worker for failing to steal.
             // No quantum for you!
-            if (w->l->steal_failure_count > 30000) {
-                // Punish more if the worker has been doing unsuccessful steals
-                // for a long time. After return from the idle state, it will
-                // be given a grace period to react quickly.
+            unsigned int max_fails = w->g->max_steal_failures << 1;
+            if (w->l->has_stolen == 0 &&
+                w->l->steal_failure_count % max_fails == max_fails - 1) {
+                // Idle briefly if the worker has never stolen anything for
+                // the given grace period
                 __cilkrts_idle();
-                w->l->steal_failure_count -= 300;
             } else {
                 __cilkrts_yield();
             }
             w->l->steal_failure_count++;
+            if (w->l->steal_failure_count > (max_fails << 8)) {
+                // Reset the flag after certain amount of failures
+                // - This will reduce cpu time in top-level synched regions
+                // - max_fails can be controlled by user (CILK_STEAL_FAILURES)
+                w->l->has_stolen = 0;
+            }
         } else {
             // Reset steal_failure_count since there is obviously still work to
             // be done.
             w->l->steal_failure_count = 0;
+            w->l->has_stolen = 1;
         }
     }
     return ff;
@@ -2912,6 +2919,7 @@ void __cilkrts_restore_stealing(
     w->l->stats = NULL;
 #endif    
     w->l->steal_failure_count = 0;
+    w->l->has_stolen = 0;
 
     w->l->work_stolen = 0;
 
diff --git a/libcilkrts/runtime/scheduler.h b/libcilkrts/runtime/scheduler.h
--- a/libcilkrts/runtime/scheduler.h
+++ b/libcilkrts/runtime/scheduler.h
@@ -85,7 +85,10 @@
  * Print debugging messages and assertions for parallel reducers. 0 is
  * no debugging.  A higher value generates more output.
  */
+
+#ifndef REDPAR_DEBUG
 #define REDPAR_DEBUG 0
+#endif
 
 /**
  * @brief Lock the worker mutex to allow exclusive access to the
diff --git a/libcilkrts/runtime/sysdep-unix.c b/libcilkrts/runtime/sysdep-unix.c
--- a/libcilkrts/runtime/sysdep-unix.c
+++ b/libcilkrts/runtime/sysdep-unix.c
@@ -465,7 +465,7 @@ char* sysdep_reset_jump_buffers_for_resu
 
     CILK_ASSERT(fiber);
     void* sp = (void*)get_sp_for_executing_sf(cilk_fiber_get_stack_base(fiber), ff, sf);
-    SP(sf) = sp;
+    SP(sf) = CILK_ADJUST_SP(sp);
 
     /* Debugging: make sure stack is accessible. */
     ((volatile char *)sp)[-1];
@@ -495,7 +495,7 @@ NORETURN sysdep_longjmp_to_sf(char* new_
 #endif
 
     // Set the stack pointer.
-    SP(sf) = new_sp;
+    SP(sf) = CILK_ADJUST_SP(new_sp);
 
 #ifdef RESTORE_X86_FP_STATE
     if (CILK_FRAME_VERSION_VALUE(sf->flags) >= 1) {
@@ -568,7 +568,7 @@ static const char *get_runtime_path ()
 {
     // dladdr is a glibc extension. If it's available, use it to find the path
     // for libcilkrts.so
-#ifdef _GNU_SOURCE
+#if HAVE_DLADDR
     Dl_info info;
     if (0 != dladdr(dummy_function, &info))
         return info.dli_fname;
@@ -689,7 +689,6 @@ static void write_version_file (global_s
     // ==================
     // System cores: 8
     // Cilk workers requested: 8
-    // Thread creator: Private
 
     fprintf(fp, "\nThread information\n");
     fprintf(fp, "==================\n");
@@ -699,11 +698,6 @@ static void write_version_file (global_s
     fprintf(fp, "System cores: %d\n", (int)sysconf(_SC_NPROCESSORS_ONLN));
 #endif    
     fprintf(fp, "Cilk workers requested: %d\n", n);
-#if (PARALLEL_THREAD_CREATE)
-        fprintf(fp, "Thread creator: Private (parallel)\n");
-#else
-        fprintf(fp, "Thread creator: Private\n");
-#endif
 
     if (fp != stderr && fp != stdout) fclose(fp);
     else fflush(fp); // flush the handle buffer if it is stdout or stderr.
