Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions lib/TileOps/tfillpad_expand_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def template_tfillpad_expand(src: pto.Tile, dst: pto.Tile):
has_valid_expansion = (src_valid_cols < dst_valid_cols) or (src_valid_rows < dst_valid_rows)

# PadValue handling - dtype-specific
if pto.constexpr(dtype == pto.f32):
if pto.constexpr(dst.pad_value == pto.PadValue.ZERO):
if has_valid_expansion:
fill_scalar = pto.f32(_NEG1_F32)
else:
fill_scalar = pto.f32(0.0)
if pto.constexpr(dtype == pto.f32):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在TileLib已经切到PTODSL框架了,旧的TileLib不再使用,请直接更新到新的TileLib库:ptodsl/ptodsl/tilelib

if pto.constexpr(dst.pad_value == pto.PadValue.ZERO):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTODSL中对应的语法是pto.const_expr,请一并修改

if has_valid_expansion:
fill_scalar = pto.f32(_NEG1_F32)
else:
fill_scalar = pto.f32(0.0)
elif pto.constexpr(dst.pad_value != pto.PadValue.NULL):
fill_scalar = dst.pad_value.eval()
else:
Expand Down Expand Up @@ -131,15 +131,16 @@ def template_tfillpad_expand(src: pto.Tile, dst: pto.Tile):
fill_scalar = pto.i8(0)

# Phase 1: Copy aligned valid blocks
for row in range(0, src_valid_rows, 1):
remained = aligned_col
for col in range(0, aligned_col, lanes):
mask, remained = pto.make_mask(dtype, remained)
data = pto.vlds(src[row, col:])
pto.vsts(data, dst[row, col:], mask)
if aligned_col > 0:
for row in range(0, src_valid_rows, 1):
remained = aligned_col
for col in range(0, aligned_col, lanes):
mask, remained = pto.make_mask(dtype, remained)
data = pto.vlds(src[row, col:])
pto.vsts(data, dst[row, col:], mask)

# Phase 2: Fill col padding
if aligned_col < dst_valid_cols:
if aligned_col < dst_valid_cols:
for row in range(0, dst_valid_rows, 1):
Comment on lines +143 to 144

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

High Severity Correctness Issue

Issue: Potential uninitialized memory/garbage values in the destination tile when src_valid_rows < dst_valid_rows.

Explanation:
If src_valid_rows < dst_valid_rows (i.e., the destination has more valid rows than the source), the rows from src_valid_rows to dst_valid_rows are part of the expansion region and must be fully filled with fill_scalar.

However:

  1. Phase 1 and Phase 3 (copying valid blocks and tail) only loop up to src_valid_rows.
  2. Phase 2 (filling column padding) loops up to dst_valid_rows, but it only fills columns from aligned_col to dst_valid_cols.
  3. Phase 4 (filling row expansion) only runs if src_rows < dst_rows (physical shapes differ), and even then, it only fills from src_rows to dst_rows.

As a result, for any row in range(src_valid_rows, min(src_rows, dst_valid_rows)), the columns from 0 to aligned_col are never filled or copied, leaving them with uninitialized/garbage values.

Suggested Fix:

  1. Change Phase 2 to only loop up to src_valid_rows (since the remaining rows will be fully filled by Phase 4).
  2. Change Phase 4 to fill the entire row expansion from src_valid_rows to dst_rows for all columns from 0 to dst_valid_cols:
    # Phase 4: Fill row expansion
    if pto.constexpr(src_valid_rows < dst_rows):
        for row in range(src_valid_rows, dst_rows, 1):
            remained = dst_valid_cols
            for col in range(0, dst_valid_cols, lanes):
                mask, remained = pto.make_mask(dtype, remained)
                vec = pto.vdup(fill_scalar, mask)
                pto.vsts(vec, dst[row, col:], mask)
Suggested change
if aligned_col < dst_valid_cols:
for row in range(0, dst_valid_rows, 1):
if aligned_col < dst_valid_cols:
for row in range(0, src_valid_rows, 1):

remained = dst_valid_cols - aligned_col
for col in range(aligned_col, dst_valid_cols, lanes):
Expand All @@ -148,7 +149,7 @@ def template_tfillpad_expand(src: pto.Tile, dst: pto.Tile):
pto.vsts(vec, dst[row, col:], mask)

# Phase 3: Copy tail valid lanes
if has_tail:
if has_tail:
for row in range(0, src_valid_rows, 1):
remained = src_valid_cols - aligned_col
mask_copy, remained = pto.make_mask(dtype, remained)
Expand All @@ -164,4 +165,4 @@ def template_tfillpad_expand(src: pto.Tile, dst: pto.Tile):
vec = pto.vdup(fill_scalar, mask)
pto.vsts(vec, dst[row, col:], mask)

return
return
31 changes: 16 additions & 15 deletions lib/TileOps/tfillpad_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def template_tfillpad(src: pto.Tile, dst: pto.Tile):
has_valid_expansion = (src_valid_cols < dst_valid_cols) or (src_valid_rows < dst_valid_rows)

# PadValue handling - dtype-specific (inline to avoid external call)
if pto.constexpr(dtype == pto.f32):
if pto.constexpr(dst.pad_value == pto.PadValue.ZERO):
if has_valid_expansion:
fill_scalar = pto.f32(_NEG1_F32)
else:
fill_scalar = pto.f32(0.0)
if pto.constexpr(dtype == pto.f32):
if pto.constexpr(dst.pad_value == pto.PadValue.ZERO):
if has_valid_expansion:
fill_scalar = pto.f32(_NEG1_F32)
else:
fill_scalar = pto.f32(0.0)
elif pto.constexpr(dst.pad_value != pto.PadValue.NULL):
fill_scalar = dst.pad_value.eval()
else:
Expand Down Expand Up @@ -137,15 +137,16 @@ def template_tfillpad(src: pto.Tile, dst: pto.Tile):
fill_scalar = pto.i8(0)

# Phase 1: Copy aligned valid blocks
for row in range(0, src_valid_rows, 1):
remained = aligned_col
for col in range(0, aligned_col, lanes):
mask, remained = pto.make_mask(dtype, remained)
data = pto.vlds(src[row, col:])
pto.vsts(data, dst[row, col:], mask)
if aligned_col > 0:
for row in range(0, src_valid_rows, 1):
remained = aligned_col
for col in range(0, aligned_col, lanes):
mask, remained = pto.make_mask(dtype, remained)
data = pto.vlds(src[row, col:])
pto.vsts(data, dst[row, col:], mask)

# Phase 2: Fill cols from aligned_col to dst_cols-1
if aligned_col < dst_cols:
if aligned_col < dst_cols:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

High Severity Correctness Issue

Issue: Potential uninitialized memory/garbage values in the destination tile when src_valid_rows < dst_valid_rows.

Explanation:
In tfillpad_template.py, if src_valid_rows < dst_valid_rows (i.e., the destination has more valid rows than the source), the rows from src_valid_rows to dst_valid_rows are part of the expansion region and must be fully filled with fill_scalar.

However:

  1. Phase 1 and Phase 3 (copying valid blocks and tail) only loop up to src_valid_rows.
  2. Phase 2 (filling column padding) only loops up to src_valid_rows.
  3. Phase 4 (filling row expansion) only runs if src_rows < dst_rows. Since tfillpad requires src.shape == dst.shape (same physical size), src_rows < dst_rows is always False, so Phase 4 never runs.

As a result, any rows from src_valid_rows to dst_rows are never filled or copied, leaving them with uninitialized/garbage values.

Suggested Fix:
Change Phase 4 to fill the entire row expansion from src_valid_rows to dst_rows for all columns from 0 to dst_cols:

    # Phase 4: Fill row expansion
    if pto.constexpr(src_valid_rows < dst_rows):
        for row in range(src_valid_rows, dst_rows, 1):
            remained = dst_cols
            for col in range(0, dst_cols, lanes):
                mask, remained = pto.make_mask(dtype, remained)
                vec = pto.vdup(fill_scalar, mask)
                pto.vsts(vec, dst[row, col:], mask)

for row in range(0, src_valid_rows, 1):
remained = dst_cols - aligned_col
for col in range(aligned_col, dst_cols, lanes):
Expand All @@ -154,7 +155,7 @@ def template_tfillpad(src: pto.Tile, dst: pto.Tile):
pto.vsts(vec, dst[row, col:], mask)

# Phase 3: Copy tail valid lanes
if has_tail:
if has_tail:
for row in range(0, src_valid_rows, 1):
remained = src_valid_cols - aligned_col
mask_copy, remained = pto.make_mask(dtype, remained)
Expand All @@ -170,4 +171,4 @@ def template_tfillpad(src: pto.Tile, dst: pto.Tile):
vec = pto.vdup(fill_scalar, mask)
pto.vsts(vec, dst[row, col:], mask)

return
return
Loading