Skip to content

Add MemoryAllocator used_size()/free_size() accessors (#20307)#20307

Open
yctwo wants to merge 1 commit into
pytorch:mainfrom
yctwo:export-D108612649
Open

Add MemoryAllocator used_size()/free_size() accessors (#20307)#20307
yctwo wants to merge 1 commit into
pytorch:mainfrom
yctwo:export-D108612649

Conversation

@yctwo

@yctwo yctwo commented Jun 16, 2026

Copy link
Copy Markdown

Summary:

Problem

executorch::runtime::MemoryAllocator is a bump allocator, but it does not
expose how much of its buffer is currently consumed: the bump cursor (cur_)
is private. Consumers that need the allocation high-water have to re-derive it
from the returned block pointer and the base address.

Duplication this removes

This exact "used offset" math is already re-implemented by multiple
MemoryAllocator subclasses:

  • [[https://www.internalfb.com/code/fbsource/xplat/executorch/examples/espressif/executor_runner/esp_memory_allocator.cpp?lines=19-20 | esp_memory_allocator.cpp]] — used_ = (uint8_t*)ret + size - base_address(), exposed via used_size() / free_size().
  • [[https://www.internalfb.com/code/fbsource/xplat/executorch/examples/arm/executor_runner/arm_memory_allocator.cpp?lines=35-61 | arm_memory_allocator.cpp]] — tracks used_ with equivalent alignment math, exposed via used_size() / free_size().

A per-op-locked shared scratch allocator on wrist firmware needs the same value
(see T274998164).

Change

Add two virtual accessors to the base class, alongside the existing virtual
size() / base_address():

  • used_size()cur_ - begin_ (bytes allocated since construction or the
    last reset()).
  • free_size()end_ - cur_ (bytes still available, not accounting for any
    alignment padding a future allocation may add).

Both return size_t -- the conventional byte-count type (allocate() already
takes size_t), matching the same-named accessors the Arm/Esp examples above
already expose.

The base provides the default bump-cursor accounting. Subclasses that track
usage differently (the Arm/Esp examples; internal heap-backed allocators) keep
their own implementations -- this diff just adds override to those existing
size_t used_size() / free_size() declarations so they bind to the new
virtuals (no behavior change; all call sites are concrete-typed today).

Differential Revision: D108612649

@pytorch-bot

pytorch-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20307

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures, 3 Unrelated Failures, 2 Unclassified Failures

As of commit 35e7930 with merge base a581673 (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 16, 2026
@meta-codesync

meta-codesync Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

@yctwo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D108612649.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 16, 2026

Copy link
Copy Markdown

CLA Not Signed

@github-actions github-actions Bot added ciflow/trunk module: arm Issues related to arm backend labels Jun 16, 2026
@pytorch-bot

pytorch-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Summary:

## Problem

`executorch::runtime::MemoryAllocator` is a bump allocator, but it does not
expose how much of its buffer is currently consumed: the bump cursor (`cur_`)
is private. Consumers that need the allocation high-water have to re-derive it
from the returned block pointer and the base address.

## Duplication this removes

This exact "used offset" math is already re-implemented by multiple
`MemoryAllocator` subclasses:

- [[https://www.internalfb.com/code/fbsource/xplat/executorch/examples/espressif/executor_runner/esp_memory_allocator.cpp?lines=19-20 | esp_memory_allocator.cpp]] — `used_ = (uint8_t*)ret + size - base_address()`, exposed via `used_size()` / `free_size()`.
- [[https://www.internalfb.com/code/fbsource/xplat/executorch/examples/arm/executor_runner/arm_memory_allocator.cpp?lines=35-61 | arm_memory_allocator.cpp]] — tracks `used_` with equivalent alignment math, exposed via `used_size()` / `free_size()`.

A per-op-locked shared scratch allocator on wrist firmware needs the same value
(see T274998164).

## Change

Add two **virtual** accessors to the base class, alongside the existing virtual
`size()` / `base_address()`:

- `used_size()` → `cur_ - begin_` (bytes allocated since construction or the
  last `reset()`).
- `free_size()` → `end_ - cur_` (bytes still available, not accounting for any
  alignment padding a future allocation may add).

Both return `size_t` -- the conventional byte-count type (`allocate()` already
takes `size_t`), matching the same-named accessors the Arm/Esp examples above
already expose.

The base provides the default bump-cursor accounting. Subclasses that track
usage differently (the Arm/Esp examples; internal heap-backed allocators) keep
their own implementations -- this diff just adds `override` to those existing
`size_t used_size()` / `free_size()` declarations so they bind to the new
virtuals (no behavior change; all call sites are concrete-typed today).

Differential Revision: D108612649
@meta-codesync meta-codesync Bot changed the title Add MemoryAllocator used_size()/free_size() accessors Add MemoryAllocator used_size()/free_size() accessors (#20307) Jun 16, 2026
@yctwo yctwo force-pushed the export-D108612649 branch from 365b155 to 35e7930 Compare June 16, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported module: arm Issues related to arm backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants