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 <xu.han@outlook.com>
This commit is contained in:
yuchengliu1
2025-06-25 06:14:57 +00:00
committed by PyTorch MergeBot
parent 4237ee3c33
commit c60327ba74

View File

@ -1503,12 +1503,19 @@ class CppWrapperCpu(PythonWrapperCodegen):
# This is why writeline needs to explicitly passed in as a parameter. # This is why writeline needs to explicitly passed in as a parameter.
var = f"int_array_{next(self.int_array_id)}" var = f"int_array_{next(self.int_array_id)}"
ctype = "int64_t" ctype = "int64_t"
if var not in self.declared_int_array_vars: if int_array == "{}":
self.declared_int_array_vars.add(var) # An array of unknown bound cannot be initialized with {}.
if known_statically: if known_statically:
writeline(f"static constexpr {ctype} {var}[] = {int_array};") writeline(f"static constexpr {ctype} *{var}=nullptr;")
else: 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 return var
def make_buffer_allocation(self, buffer): def make_buffer_allocation(self, buffer):