Skip to content

Add static IntrinsicSizeInBytes(parameters) for parameter-dependent struct sizes#111

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/add-intrinsic-size-computation
Draft

Add static IntrinsicSizeInBytes(parameters) for parameter-dependent struct sizes#111
Copilot wants to merge 3 commits intomasterfrom
copilot/add-intrinsic-size-computation

Conversation

Copy link

Copilot AI commented Dec 6, 2025

For dynamically-sized structs where size depends only on parameters (not field values), users currently must instantiate a view to compute size. This adds a static IntrinsicSizeInBytes(params...) function to compute size from parameters alone.

Changes

  • header_generator.py:

    • _expression_only_depends_on_parameters() - detects if size expression uses only parameters/constants
    • _StaticParameterFieldRenderer - renders parameter refs as function args instead of view accessors
    • _generate_static_size_from_parameters() - generates the static function when applicable
  • generated_code_templates: Added declaration/definition templates for static parameterized size function

  • parameters_test.cc: Test coverage for Axes::IntrinsicSizeInBytes()

Example

struct Axes(axes: UInt:4):
  0 [+axes*4]  Axis(AxisType.GENERIC)[]  values

Generated:

namespace Axes {
inline constexpr ::std::size_t IntrinsicSizeInBytes(::std::int32_t axes);
}

// Usage: compute size before allocating
std::size_t size = Axes::IntrinsicSizeInBytes(3);  // Returns 12
std::vector<char> buffer(size);
auto view = MakeAxesView(3, buffer.data(), buffer.size());

Only generated when size depends solely on parameters—structs with constant size or field-dependent size are unchanged.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • releases.bazel.build
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 version (dns block)
  • www.googleapis.com
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 test //compiler/back_end/cpp/testcode:parameters_test --test_output=errors (dns block)
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 version (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>static IntrinsicSizeInBytes(parameters) to compute expected size of struct</issue_title>
<issue_description>This issue was copied from the upstream repository google/emboss.

Original issue: google#227
Original state: open, created at: 2025-08-19T18:01:51Z, updated at: 2025-08-19T18:01:51Z, by @studgeek


Original description

I have seen a common need where code has to compute a struct size outside of emboss to properly allocate that space. It then applies an emboss view on that allocated space.

For non-dynamically-sized structs the code can use static IntrinsicSizeInBytes to get the needed size. But for dynamically-sized structs (that determine size based on parameters or fields) the code usually has to do its own computation of struct size outside of emboss.

I have been thinking that for dynamically-sized structs where parameters are enough to determine the size, it would be handy if emboss generated a parameterized version of the static IntrinsicSizeInBytes function that can take the struct's parameters and use them to compute the size.

So

struct Bar(x: UInt:8, y: Int:32)

would generate something like

template <typename T>
auto MakeBarView(std::uint8_t x, std::int32_t y, T *data, size_t size);

and also generate the new

static constexpr auto IntrinsicSizeInBytes(std::uint8_t x, std::int32_t y) const;

Above assumes the parameters are enough to compute the size. If not, then no IntrinsicSizeInBytes would be created. I think recognizing these cases is probably one of the trickt parts of any implementation.
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 6, 2025 03:12
…r-dependent size

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Copilot AI changed the title [WIP] Add static IntrinsicSizeInBytes for struct size computation Add static IntrinsicSizeInBytes(parameters) for parameter-dependent struct sizes Dec 6, 2025
Copilot AI requested a review from AaronWebster December 6, 2025 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

static IntrinsicSizeInBytes(parameters) to compute expected size of struct

2 participants