Skip to content

[mono][jit] box+callvirt optimization - #73456

Merged
jandupej merged 22 commits into
dotnet:mainfrom
jandupej:issue32166
Sep 9, 2022
Merged

jandupej merged 22 commits into
dotnet:mainfrom
jandupej:issue32166

Conversation

@jandupej

@jandupej jandupej commented Aug 5, 2022 •

Copy link
Copy Markdown
Contributor

Addresses #32166.

Optimizes JIT of box opcode when followed by a callvirt in mono. This occurs when calling a method through an interface or a base class method and requires that the callee has no parameters. Barring pathological cases, boxing is elided, address of the boxed value is taken and the virtual call is resolved into to a direct call.

@ghost ghost added area-Codegen-JIT-mono community-contribution Indicates that the PR has been added by a community member labels Aug 5, 2022
@dnfadmin

dnfadmin commented Aug 5, 2022 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@jandupej jandupej changed the title [mono][jit] box+callvirt optimization #32166 [mono][jit] box+callvirt optimization Aug 5, 2022
Comment thread src/mono/mono/mini/method-to-ir.c Resolved
Comment thread src/mono/mono/mini/method-to-ir.c Outdated
// The method to be called may have already been resolved when handling a previous opcode. In that
// case, we ignore the operand and act as CALL, instead of CALLVIRT.
// E.g. #32166 (box+callvirt optimization)
if(cmethod_override) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wish we didn't have to use this - effectively global - variable. But I guess the call logic is so complicated that it wouldn't be reasonable to do the optimization inplace in the BOX case of the switch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll see how practical it would be to pull the call/callvirt logic out to a function. At first glance it looks like a function with lots of parameters, but I'll try.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we tried a few years ago and it regressed JIT perf. but maybe the C compilers got better...

I wouldn't recommend spending a ton of time on this. I was just making an observation

Comment thread src/mono/mono/mini/method-to-ir.c Resolved
gboolean emitted_funccall_seq_point = FALSE;
gboolean detached_before_ret = FALSE;
gboolean ins_has_side_effect;
MonoMethod* cmethod_override = NULL; // this is ised in call/callvirt handler to override the method to be called (e.g. from box handler)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: typo 'ised' instead of 'used'

Comment thread src/mono/mono/mini/method-to-ir.c Outdated
// The method to be called may have already been resolved when handling a previous opcode. In that
// case, we ignore the operand and act as CALL, instead of CALLVIRT.
// E.g. #32166 (box+callvirt optimization)
if(cmethod_override) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

formatting: space after if

Comment thread src/mono/mono/mini/method-to-ir.c Outdated
guint32 callvirt_proc_token;
if((callvirt_ip = il_read_callvirt(next_ip, end, &callvirt_proc_token)) &&
ip_in_bb(cfg, cfg->cbb, callvirt_ip) )
{

@ivanpovazan ivanpovazan Aug 5, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

formatting: there are several small issues in the PR regarding formatting, for example:

  • mono C source follows k&r indentation style
  • space between function name and braces
  • space after if

Here is a useful guideline: https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/mono-code-guide.md

// call SomeStruct::Method()
guchar* callvirt_ip;
guint32 callvirt_proc_token;
if (!((cfg->compile_aot || cfg->compile_llvm) && !mono_class_is_def(klass)) && // we cannot devirtualize in AOT when using generics

@lambdageek lambdageek Aug 13, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand this condition - together with the comment. we don't often see mono_class_is_def used. is_def will be just the non-generic class/struct/interface types - it will exclude arrays, generic definitions and generic instances, generic params, and pointer types. it's really a property of the metadata encoding, not usually something that mini cares about.

does the issue have to do with generic sharing?

@jandupej jandupej Aug 15, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was getting runtime errors under LLVM in the test JIT/opt/Devirtualization/box1. The test calls a method of a generic struct through an interface. The test passes with JIT. I noticed that calling methods of generic value types is more involved in AOT, so I disabled the optimization on LLVM if the class is generic. Do you think checking for gsharedvt instead would suffice?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yea - I would check for gsharedvt.

We can make a follow-up work item to revisit the optimization in gsharedvt contexts.

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.

There are a lot of cases here so I would suggest restricting the set of allowed cases to a simple set right now so no generics, etc.

@jandupej

Copy link
Copy Markdown
Contributor Author

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 73456 in repo dotnet/runtime

@SamMonoRT

Copy link
Copy Markdown
Member

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@akoeplinger

Copy link
Copy Markdown
Member

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jandupej

Copy link
Copy Markdown
Contributor Author

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@SamMonoRT SamMonoRT removed the community-contribution Indicates that the PR has been added by a community member label Aug 30, 2022
@jandupej
jandupej marked this pull request as ready for review August 31, 2022 10:10
@jandupej
jandupej requested a review from lambdageek September 9, 2022 11:34
Comment thread src/mono/mono/mini/method-to-ir.c Resolved
@srxqds

srxqds commented Sep 9, 2022

Copy link
Copy Markdown
Contributor

will you backport to release/7.0?

[mono] Referencing issue with a full link, instead of number.

Co-authored-by: Alexander Köplinger <[email protected]>
@jandupej
jandupej merged commit c2790d5 into dotnet:main Sep 9, 2022
@SamMonoRT

Copy link
Copy Markdown
Member

will you backport to release/7.0?

No, not going to backport.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 9, 2022
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.

8 participants