Skip to content

Commit

Permalink
Now in all new[] expressions the size is now explicitly casted to i…
Browse files Browse the repository at this point in the history
…nteger.

The reason is if `pow` is used as size.
  • Loading branch information
philip-paul-mueller committed Nov 19, 2024
1 parent 4f8eb92 commit 5a5c4c2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dace/codegen/instrumentation/data/data_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _generate_copy_to_host(self, node: nodes.AccessNode, desc: dt.Array, ptr: st
# Emit synchronous memcpy
preamble = f'''
{{
{new_desc.as_arg(name=new_ptr)} = new {desc.dtype.ctype}[{csize}];
{new_desc.as_arg(name=new_ptr)} = new {desc.dtype.ctype}[std::size_t({csize})];
{self.backend}Memcpy({new_ptr}, {ptr}, sizeof({desc.dtype.ctype}) * ({csize}), {self.backend}MemcpyDeviceToHost);
'''

Expand All @@ -65,7 +65,7 @@ def _generate_copy_to_device(self, node: nodes.AccessNode, desc: dt.Array, ptr:
# Emit synchronous memcpy
preamble = f'''
{{
{new_desc.as_arg(name=new_ptr)} = new {desc.dtype.ctype}[{csize}];
{new_desc.as_arg(name=new_ptr)} = new {desc.dtype.ctype}[std::size_t({csize})];
'''

postamble = f'''
Expand Down
4 changes: 2 additions & 2 deletions dace/codegen/targets/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def allocate_array(self, sdfg: SDFG, cfg: ControlFlowRegion, dfg: StateSubgraphV
if not declared:
declaration_stream.write(f'{nodedesc.dtype.ctype} *{name};\n', cfg, state_id, node)
allocation_stream.write(
"%s = new %s DACE_ALIGN(64)[%s];\n" % (alloc_name, nodedesc.dtype.ctype, cpp.sym2cpp(arrsize)), cfg,
"%s = new %s DACE_ALIGN(64)[std::size_t(%s)];\n" % (alloc_name, nodedesc.dtype.ctype, cpp.sym2cpp(arrsize)), cfg,
state_id, node)
define_var(name, DefinedType.Pointer, ctypedef)

Expand Down Expand Up @@ -548,7 +548,7 @@ def allocate_array(self, sdfg: SDFG, cfg: ControlFlowRegion, dfg: StateSubgraphV
"""
#pragma omp parallel
{{
{name} = new {ctype} DACE_ALIGN(64)[{arrsize}];""".format(ctype=nodedesc.dtype.ctype,
{name} = new {ctype} DACE_ALIGN(64)[std::size_t({arrsize})];""".format(ctype=nodedesc.dtype.ctype,
name=alloc_name,
arrsize=cpp.sym2cpp(arrsize)),
cfg,
Expand Down
3 changes: 2 additions & 1 deletion dace/codegen/targets/snitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def allocate_array(self, sdfg: SDFG, cfg: ControlFlowRegion, dfg: StateSubgraphV
#pragma omp parallel
{{
#error "malloc is not threadsafe"
{name} = new {ctype} [{arrsize}];""".format(ctype=nodedesc.dtype.ctype,
{name} = new {ctype} [std::size_t({arrsize})];""".format(ctype=nodedesc.dtype.ctype,
name=alloc_name,
arrsize=cpp.sym2cpp(arrsize)),
cfg,
Expand Down Expand Up @@ -1108,6 +1108,7 @@ def gen_code_snitch(sdfg):

# change new/delete to malloc/free
code._code = re.sub(r"new (.+) \[(\d*)\];", r"(\1*)malloc(\2*sizeof(\1));", code._code)
code._code = re.sub(r"new (.+) \[std::size_t\((\d*)\)\];", r"(\1*)malloc(\2*sizeof(\1));", code._code)
code._code = re.sub(r"new ([a-zA-Z0-9 _]*);", r"(\1*)malloc(sizeof(\1));", code._code)
code._code = re.sub(r"delete (.*);", r"free(\1);", code._code)
code._code = re.sub(r"delete\[\] (.*);", r"free(\1);", code._code)
Expand Down

0 comments on commit 5a5c4c2

Please sign in to comment.