Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only)#146397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
eendebakpt wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromeendebakpt:jit_float_truediv
Draft
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,440 changes: 1,224 additions & 1,216 deletionsInclude/internal/pycore_uop_ids.h
View file
Open in desktop

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletionsInclude/internal/pycore_uop_metadata.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

70 changes: 70 additions & 0 deletionsLib/test/test_capi/test_opt.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3237,6 +3237,76 @@ def testfunc(args):
uops=get_opnames(ex)
self.assertNotIn("_UNARY_NEGATIVE_FLOAT_INPLACE",uops)

deftest_float_truediv_inplace_unique_lhs(self):
# (a + b) produces a unique float; dividing by c reuses it
deftestfunc(args):
a,b,c,n=args
total=0.0
for_inrange(n):
total+= (a+b)/c
returntotal

res,ex=self._run_with_optimizer(testfunc, (2.0,3.0,4.0,TIER2_THRESHOLD))
self.assertAlmostEqual(res,TIER2_THRESHOLD*1.25)
self.assertIsNotNone(ex)
uops=get_opnames(ex)
self.assertIn("_BINARY_OP_TRUEDIV_FLOAT_INPLACE",uops)

deftest_float_truediv_inplace_unique_rhs(self):
# (a + b) produces a unique float on the right side of /
deftestfunc(args):
a,b,c,n=args
total=0.0
for_inrange(n):
total+=c/ (a+b)
returntotal

res,ex=self._run_with_optimizer(testfunc, (2.0,3.0,4.0,TIER2_THRESHOLD))
self.assertAlmostEqual(res,TIER2_THRESHOLD*0.8)
self.assertIsNotNone(ex)
uops=get_opnames(ex)
self.assertIn("_BINARY_OP_TRUEDIV_FLOAT_INPLACE_RIGHT",uops)

deftest_float_truediv_type_propagation(self):
# (a/b) + (c/d): inner divisions are generic _BINARY_OP but
# type propagation marks their results as float, so the +
# is specialized and the += uses inplace on the unique result
deftestfunc(args):
a,b,c,d,n=args
total=0.0
for_inrange(n):
total+= (a/b)+ (c/d)
returntotal

res,ex=self._run_with_optimizer(testfunc, (10.0,3.0,4.0,5.0,TIER2_THRESHOLD))
expected=TIER2_THRESHOLD* (10.0/3.0+4.0/5.0)
self.assertAlmostEqual(res,expected)
self.assertIsNotNone(ex)
uops=get_opnames(ex)
# The + between the two division results should use inplace
# (the a/b result is unique from type propagation)
self.assertIn("_BINARY_OP_ADD_FLOAT_INPLACE",uops)
# The += should also use inplace (the + result is unique)
self.assertIn("_BINARY_OP_ADD_FLOAT_INPLACE_RIGHT",uops)

deftest_float_truediv_unique_result_enables_inplace_add(self):
# a / b: the generic division result is marked as unique float
# by type propagation, so total += (a / b) uses inplace add
deftestfunc(args):
a,b,n=args
total=0.0
for_inrange(n):
total+=a/b
returntotal

res,ex=self._run_with_optimizer(testfunc, (10.0,3.0,TIER2_THRESHOLD))
expected=TIER2_THRESHOLD* (10.0/3.0)
self.assertAlmostEqual(res,expected)
self.assertIsNotNone(ex)
uops=get_opnames(ex)
# The += uses inplace because the division result is unique
self.assertIn("_BINARY_OP_ADD_FLOAT_INPLACE_RIGHT",uops)

deftest_load_attr_instance_value(self):
deftestfunc(n):
classC():
Expand Down
22 changes: 22 additions & 0 deletionsPython/bytecodes.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -838,6 +838,28 @@ dummy_func(
INPUTS_DEAD();
}

tier2op(_BINARY_OP_TRUEDIV_FLOAT_INPLACE, (left,right--res,l,r)) {
FLOAT_INPLACE_DIVOP(left,right,left);
if (_divop_err) {
ERROR_NO_POP();
}
res=left;
l=PyStackRef_NULL;
r=right;
INPUTS_DEAD();
}

tier2op(_BINARY_OP_TRUEDIV_FLOAT_INPLACE_RIGHT, (left,right--res,l,r)) {
FLOAT_INPLACE_DIVOP(left,right,right);
if (_divop_err) {
ERROR_NO_POP();
}
res=right;
l=left;
r=PyStackRef_NULL;
INPUTS_DEAD();
}

pureop(_BINARY_OP_ADD_UNICODE, (left,right--res,l,r)) {
PyObject*left_o=PyStackRef_AsPyObjectBorrow(left);
PyObject*right_o=PyStackRef_AsPyObjectBorrow(right);
Expand Down
24 changes: 24 additions & 0 deletionsPython/ceval_macros.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -562,3 +562,27 @@ gen_try_set_executing(PyGenObject *gen)
((PyFloatObject *)PyStackRef_AsPyObjectBorrow(TARGET)) \
->ob_fval = _dres; \
} while (0)

// Inplace float true division. Sets _divop_err to 1 on zero division.
// Caller must check _divop_err and call ERROR_NO_POP() if set.
#defineFLOAT_INPLACE_DIVOP(left,right,TARGET) \
int _divop_err = 0; \
do { \
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left); \
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right); \
assert(PyFloat_CheckExact(left_o)); \
assert(PyFloat_CheckExact(right_o)); \
assert(_PyObject_IsUniquelyReferenced( \
PyStackRef_AsPyObjectBorrow(TARGET))); \
STAT_INC(BINARY_OP, hit); \
double _divisor = ((PyFloatObject *)right_o)->ob_fval; \
if (_divisor == 0.0) { \
PyErr_SetString(PyExc_ZeroDivisionError, \
"float division by zero"); \
_divop_err = 1; \
break; \
} \
double _dres = ((PyFloatObject *)left_o)->ob_fval / _divisor; \
((PyFloatObject *)PyStackRef_AsPyObjectBorrow(TARGET)) \
->ob_fval = _dres; \
} while (0)
184 changes: 184 additions & 0 deletionsPython/executor_cases.c.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading
Loading

[8]ページ先頭

©2009-2026 Movatter.jp