[dynamo][compile-time] Cache method on load builtin (#153524)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/153524
Approved by: https://github.com/StrongerXi, https://github.com/jansel
ghstack dependencies: #153522
This commit is contained in:
Animesh Jain 2025-05-14 15:02:38 -07:00 committed by PyTorch MergeBot
parent b47be23461
commit 9839ec1383

View File

@ -1651,6 +1651,9 @@ class InstructionTranslatorBase(
self.DUP_TOP(inst)
self._load_attr(inst)
# Cache note: This cache only exists for the duration of this
# InstructionTranslator - so it should be safe to do.
@cache_method
def load_builtin_from_argval(self, argval):
if argval not in self.f_builtins:
raise Unsupported(f"name '{argval}' is not defined")
@ -1661,13 +1664,13 @@ class InstructionTranslatorBase(
self.output.name_of_builtins_dict_key_in_fglobals
)
var_source = DictGetItemSource(builtins_source, argval)
self.push(VariableTracker.build(self, val, var_source))
return VariableTracker.build(self, val, var_source)
else:
assert is_builtin_constant(val)
self.push(ConstantVariable.create(value=val))
return ConstantVariable.create(value=val)
def load_builtin(self, inst):
self.load_builtin_from_argval(inst.argval)
self.push(self.load_builtin_from_argval(inst.argval))
def jump(self, inst):
assert self.instruction_pointer is not None
@ -2809,7 +2812,7 @@ class InstructionTranslatorBase(
self.push(ConstantVariable.create(False))
def LOAD_ASSERTION_ERROR(self, inst):
self.load_builtin_from_argval("AssertionError")
self.push(self.load_builtin_from_argval("AssertionError"))
def LOAD_BUILD_CLASS(self, inst):
unimplemented_v2(