mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	gh-131798: JIT: Narrow the return type of _CALL_LEN to int (#132940)
Reduce unnecessary guards whenever `len()` is called and used after. Co-authored-by: Max Bernstein <tekknolagi@gmail.com>
This commit is contained in:
		
							parent
							
								
									f0485de43a
								
							
						
					
					
						commit
						4c20f46fa0
					
				
					 4 changed files with 18 additions and 1 deletions
				
			
		|  | @ -1911,6 +1911,18 @@ def testfunc(n): | |||
|         self.assertNotIn("_COMPARE_OP_INT", uops) | ||||
|         self.assertNotIn("_GUARD_IS_TRUE_POP", uops) | ||||
| 
 | ||||
|     def test_call_len(self): | ||||
|         def testfunc(n): | ||||
|             a = [1, 2, 3, 4] | ||||
|             for _ in range(n): | ||||
|                 _ = len(a) - 1 | ||||
| 
 | ||||
|         _, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||||
|         uops = get_opnames(ex) | ||||
|         self.assertNotIn("_GUARD_NOS_INT", uops) | ||||
|         self.assertNotIn("_GUARD_TOS_INT", uops) | ||||
|         self.assertIn("_CALL_LEN", uops) | ||||
| 
 | ||||
| 
 | ||||
| def global_identity(x): | ||||
|     return x | ||||
|  |  | |||
|  | @ -0,0 +1 @@ | |||
| Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to int. Patch by Diego Russo | ||||
|  | @ -1055,6 +1055,10 @@ dummy_func(void) { | |||
|         sym_set_const(callable, (PyObject *)&PyUnicode_Type); | ||||
|     } | ||||
| 
 | ||||
|     op(_CALL_LEN, (callable[1], self_or_null[1], args[oparg] -- res)) { | ||||
|         res = sym_new_type(ctx, &PyLong_Type); | ||||
|     } | ||||
| 
 | ||||
| // END BYTECODES //
 | ||||
| 
 | ||||
| } | ||||
|  |  | |||
							
								
								
									
										2
									
								
								Python/optimizer_cases.c.h
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Python/optimizer_cases.c.h
									
										
									
										generated
									
									
									
								
							|  | @ -2014,7 +2014,7 @@ | |||
| 
 | ||||
|         case _CALL_LEN: { | ||||
|             JitOptSymbol *res; | ||||
|             res = sym_new_not_null(ctx); | ||||
|             res = sym_new_type(ctx, &PyLong_Type); | ||||
|             stack_pointer[-2 - oparg] = res; | ||||
|             stack_pointer += -1 - oparg; | ||||
|             assert(WITHIN_STACK_BOUNDS()); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Diego Russo
						Diego Russo