Skip to content

Commit 24acb96

Browse files
author
Anselm Kruis
committed
Stackless issue python#283: Concentrate platform code in slp_transfer.c
Move the definition of SLP_DO_NOT_OPTIMIZE_AWAY from pycore_stackless.h to slp_transfer.c, because it is only used there.
1 parent cc159a3 commit 24acb96

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

Include/internal/pycore_stackless.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -833,51 +833,6 @@ slp_cstack_set_root(PyThreadState *tstate, const void * pstackvar);
833833
PyObject *
834834
slp_cstack_set_base_and_goodgap(PyThreadState *tstate, const void * pstackvar, PyFrameObject *f);
835835

836-
/*
837-
* Call SLP_DO_NOT_OPTIMIZE_AWAY(pointer) to ensure that pointer will be
838-
* computed even post-optimization. Use it for pointers that are computed but
839-
* otherwise are useless. The compiler tends to do a good job at eliminating
840-
* unused variables, and this macro fools it into thinking var is in fact
841-
* needed.
842-
*/
843-
844-
#ifndef SLP_DO_NOT_OPTIMIZE_AWAY
845-
846-
/* Code is based on Facebook folly
847-
* https://github.com/facebook/folly/blob/master/folly/Benchmark.h,
848-
* which has an Apache 2 license.
849-
*/
850-
#ifdef _MSC_VER
851-
852-
#pragma optimize("", off)
853-
854-
static inline void doNotOptimizeDependencySink(const void* p) {}
855-
856-
#pragma optimize("", on)
857-
858-
#define SLP_DO_NOT_OPTIMIZE_AWAY(pointer) doNotOptimizeDependencySink(pointer)
859-
#define SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS /* empty */
860-
861-
#elif (defined(__GNUC__) || defined(__clang__))
862-
/*
863-
* The "r" constraint forces the compiler to make datum available
864-
* in a register to the asm block, which means that it must have
865-
* computed/loaded it.
866-
*/
867-
#define SLP_DO_NOT_OPTIMIZE_AWAY(pointer) \
868-
do {__asm__ volatile("" ::"r"(pointer));} while(0)
869-
#define SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS /* empty */
870-
#else
871-
/*
872-
* Unknown compiler
873-
*/
874-
#define SLP_DO_NOT_OPTIMIZE_AWAY(pointer) \
875-
do { slp_do_not_opimize_away_sink = ((void*)(pointer)); } while(0)
876-
extern uint8_t* volatile slp_do_not_opimize_away_sink;
877-
#define SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS uint8_t* volatile slp_do_not_opimize_away_sink;
878-
#endif
879-
#endif /* #ifndef SLP_DO_NOT_OPTIMIZE_AWAY */
880-
881836

882837

883838
#endif /* #ifdef SLP_BUILD_CORE */

Stackless/core/slp_transfer.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,53 @@ or disable the STACKLESS flag.
5454
**********
5555
#endif
5656

57+
/*
58+
* Call SLP_DO_NOT_OPTIMIZE_AWAY(pointer) to ensure that pointer will be
59+
* computed even post-optimization. Use it for pointers that are computed but
60+
* otherwise are useless. The compiler tends to do a good job at eliminating
61+
* unused variables, and this macro fools it into thinking var is in fact
62+
* needed.
63+
*
64+
* A platform specific include may provide its own definition of
65+
* SLP_DO_NOT_OPTIMIZE_AWAY and SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS.
66+
*/
67+
#ifndef SLP_DO_NOT_OPTIMIZE_AWAY
68+
69+
/* Code is based on Facebook folly
70+
* https://github.com/facebook/folly/blob/master/folly/Benchmark.h,
71+
* which has an Apache 2 license.
72+
*/
73+
#ifdef _MSC_VER
74+
75+
#pragma optimize("", off)
76+
77+
static inline void doNotOptimizeDependencySink(const void* p) {}
78+
79+
#pragma optimize("", on)
80+
81+
#define SLP_DO_NOT_OPTIMIZE_AWAY(pointer) doNotOptimizeDependencySink(pointer)
82+
#define SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS /* empty */
83+
84+
#elif (defined(__GNUC__) || defined(__clang__))
85+
/*
86+
* The "r" constraint forces the compiler to make datum available
87+
* in a register to the asm block, which means that it must have
88+
* computed/loaded it.
89+
*/
90+
#define SLP_DO_NOT_OPTIMIZE_AWAY(pointer) \
91+
do {__asm__ volatile("" ::"r"(pointer));} while(0)
92+
#define SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS /* empty */
93+
#else
94+
/*
95+
* Unknown compiler
96+
*/
97+
#define SLP_DO_NOT_OPTIMIZE_AWAY(pointer) \
98+
do { slp_do_not_opimize_away_sink = ((void*)(pointer)); } while(0)
99+
extern uint8_t* volatile slp_do_not_opimize_away_sink;
100+
#define SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS uint8_t* volatile slp_do_not_opimize_away_sink;
101+
#endif
102+
#endif /* #ifndef SLP_DO_NOT_OPTIMIZE_AWAY */
103+
57104
SLP_DO_NOT_OPTIMIZE_AWAY_DEFINITIONS
58105

59106
#ifdef SLP_EXTERNAL_ASM

0 commit comments

Comments
 (0)