From c60327ba74f4db232a2832f2c7ca4b2db43a3132 Mon Sep 17 00:00:00 2001 From: yuchengliu1 Date: Wed, 25 Jun 2025 06:14:57 +0000 Subject: [PATCH] avoid to declare an unknown bound array without any element (#156543) Fixes #153180 Pull Request resolved: https://github.com/pytorch/pytorch/pull/156543 Approved by: https://github.com/jansel Co-authored-by: Xu Han --- torch/_inductor/codegen/cpp_wrapper_cpu.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/torch/_inductor/codegen/cpp_wrapper_cpu.py b/torch/_inductor/codegen/cpp_wrapper_cpu.py index 08c6a586e986..1152bf4dce2c 100644 --- a/torch/_inductor/codegen/cpp_wrapper_cpu.py +++ b/torch/_inductor/codegen/cpp_wrapper_cpu.py @@ -1503,12 +1503,19 @@ class CppWrapperCpu(PythonWrapperCodegen): # This is why writeline needs to explicitly passed in as a parameter. var = f"int_array_{next(self.int_array_id)}" ctype = "int64_t" - if var not in self.declared_int_array_vars: - self.declared_int_array_vars.add(var) + if int_array == "{}": + # An array of unknown bound cannot be initialized with {}. if known_statically: - writeline(f"static constexpr {ctype} {var}[] = {int_array};") + writeline(f"static constexpr {ctype} *{var}=nullptr;") else: - writeline(f"const {ctype} {var}[] = {int_array};") + writeline(f"const {ctype} *{var}=nullptr;") + else: + if var not in self.declared_int_array_vars: + self.declared_int_array_vars.add(var) + if known_statically: + writeline(f"static constexpr {ctype} {var}[] = {int_array};") + else: + writeline(f"const {ctype} {var}[] = {int_array};") return var def make_buffer_allocation(self, buffer):