Skip to content

Implement sizeof() in the interpreter #115745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged

Implement sizeof() in the interpreter #115745

merged 1 commit into from
May 20, 2025

Conversation

kg
Copy link
Member

@kg kg commented May 19, 2025

No description provided.

@Copilot Copilot AI review requested due to automatic review settings May 19, 2025 21:34
@kg kg requested review from BrzVlad and janvorli as code owners May 19, 2025 21:34
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request implements the sizeof() functionality within the interpreter and introduces a corresponding test.

  • Added a new test method (TestSizeof) in the interpreter tests.
  • Introduced a new case for CEE_SIZEOF in the JIT interpreter compiler.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/tests/JIT/interpreter/Interpreter.cs Added TestSizeof() and integrated its execution with FailFast.
src/coreclr/interpreter/compiler.cpp Added a new switch-case branch for the CEE_SIZEOF instruction.

@@ -416,6 +416,9 @@ public static void RunInterpreterTests()
if (!TestArray())
Environment.FailFast(null);

if (!TestSizeof())
Environment.FailFast(null);
Copy link
Preview

Copilot AI May 19, 2025

Choose a reason for hiding this comment

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

Consider providing a descriptive error message in Environment.FailFast to aid debugging if TestSizeof fails.

Suggested change
Environment.FailFast(null);
Environment.FailFast("TestSizeof failed.");

Copilot uses AI. Check for mistakes.

return false;
if (sizeof(double) != 8)
return false;
if (sizeof(MyStruct) != 4)
Copy link
Preview

Copilot AI May 19, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment explaining the assumptions behind MyStruct's size (e.g. packing or field types) to clarify why the expected size is 4.

Copilot uses AI. Check for mistakes.

AddIns(INTOP_LDC_I4);
m_pLastNewIns->data[0] = m_compHnd->getClassSize(clsHnd);
PushStackType(StackTypeI4, NULL);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
Copy link
Preview

Copilot AI May 19, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment to explain the reason for incrementing m_ip by 5, which can help clarify the instruction length and avoid confusion.

Suggested change
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
// Increment the instruction pointer by 5 to account for the 1-byte opcode
// and the 4-byte operand (e.g., a token or offset) of the CEE_SIZEOF instruction.

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@@ -3500,6 +3500,16 @@ int InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
m_ip++;
break;
case CEE_SIZEOF:
{
CORINFO_CLASS_HANDLE clsHnd = ResolveClassToken(getU4LittleEndian(m_ip + 1));
Copy link
Member

Choose a reason for hiding this comment

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

It seems it would make sense to add CHECK_STACK(1);

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, I thought CHECK_STACK was "make sure this many items are on the stack"

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I guess I am wrong in this case, I haven't realized the token is not on the stack.

@kg kg merged commit 36e8537 into dotnet:main May 20, 2025
102 checks passed
@kg kg mentioned this pull request May 22, 2025
61 tasks
SimaTian pushed a commit that referenced this pull request May 27, 2025
Implements CEE_SIZEOF in the interpreter
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants