Skip to content
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

Avoid handing out duplicate CORINFO_MODULE_STRUCT_* handles #94082

Merged
merged 1 commit into from
Oct 30, 2023

Conversation

MichalStrehovsky
Copy link
Member

RyuJIT depends on never seeing two different CORINFO_MODULE_STRUCT for the same thing.

Fixes #93843.

Cc @dotnet/ilc-contrib

RyuJIT depends on never seeing two different `CORINFO_MODULE_STRUCT` for the same thing.

Fixes dotnet#93843.
@ghost
Copy link

ghost commented Oct 27, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

RyuJIT depends on never seeing two different CORINFO_MODULE_STRUCT for the same thing.

Fixes #93843.

Cc @dotnet/ilc-contrib

Author: MichalStrehovsky
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

}

private Dictionary<object, IntPtr> _objectToHandle = new Dictionary<object, IntPtr>(new JitObjectComparer());
private Dictionary<MethodDesc, IntPtr> _methodILScopeToHandle = new Dictionary<MethodDesc, IntPtr>(new JitObjectComparer());
Copy link
Member

Choose a reason for hiding this comment

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

Just for my education - this is a per-method cache, as in, we're going to clear it after each method compilation. And JIT doesn't maintain any state between methods either, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's per method and there's not supposed to be extra state on the JIT side either.

@jkotas
Copy link
Member

jkotas commented Oct 27, 2023

RyuJIT depends on never seeing two different CORINFO_MODULE_STRUCT for the same thing.

Where is this dependency exactly?

@MichalStrehovsky
Copy link
Member Author

RyuJIT depends on never seeing two different CORINFO_MODULE_STRUCT for the same thing.

Where is this dependency exactly?

Per #93843 (comment) it's in GenTreeStrCon equality. I think it's this:

case GT_CNS_STR:
if ((op1->AsStrCon()->gtSconCPX == op2->AsStrCon()->gtSconCPX) &&
(op1->AsStrCon()->gtScpHnd == op2->AsStrCon()->gtScpHnd))
{
return true;
}
break;

@jkotas
Copy link
Member

jkotas commented Oct 27, 2023

     if ((op1->AsStrCon()->gtSconCPX == op2->AsStrCon()->gtSconCPX) && 
         (op1->AsStrCon()->gtScpHnd == op2->AsStrCon()->gtScpHnd)) 

This condition is not a precise check to validate whether two string literals are going produce same string object. It is going to miss on string literals from different modules with JIT, and it is going to miss on string literals from different methods with AOT/R2R. It may be better to abstract this comparison via JIT/EE interface, so that it can be more precise.

In general, JIT depending on module handle equality seems to be an anti-pattern.

@SingleAccretion
Copy link
Contributor

It may be better to abstract this comparison via JIT/EE interface, so that it can be more precise.

That was something I considered. It has the following drawbacks:

  1. GenTree::Equals becomes more expensive. It feels questionable for "simple tree equality" to depend on (potentially expensive) Jit-EE calls.
  2. VN-based optimizations such as CSE operate on the lowered form of CNS_STR, so it won't move the needle for them.

(As I said in the issue) I don't know what the "right" fix here is. This change seems like a practical option.

@jkotas
Copy link
Member

jkotas commented Oct 27, 2023

I agree that this change is a practical option. It will work ok as long as nothing depends on the comparison being precise.

@MichalStrehovsky
Copy link
Member Author

I agree that this change is a practical option. It will work ok as long as nothing depends on the comparison being precise.

@dotnet/jit-contrib anyone else has an opinion about this fix? We're working around RyuJIT making assumptions about CORINFO_MODULE_STRUCT* handles. The workaround is not great and the RyuJIT assumptions have other problems enumerated by Jan above. Would a JIT-side fix make more sense?

If not, I'll merge this, but it is quite fragile. A more robust fix on the driver side would be to never purge the ILProvider cache and eat the memory overhead (essentially turn a cache into a memory leak). That one is obviously not great either.

@jakobbotsch
Copy link
Member

This fix looks good to me. I agree with @SingleAccretion about the concerns for GenTree:: Compare. This function is meant to be a fast syntactic equality check, it is not meant to be a precise or semantic equality check.

@MichalStrehovsky MichalStrehovsky merged commit 848c729 into dotnet:main Oct 30, 2023
108 of 110 checks passed
@MichalStrehovsky MichalStrehovsky deleted the nodupemethodil branch October 30, 2023 22:07
@ghost ghost locked as resolved and limited conversation to collaborators Nov 30, 2023
@MichalStrehovsky
Copy link
Member Author

/backport to release/8.0-staging

@github-actions github-actions bot unlocked this conversation Dec 1, 2023
Copy link
Contributor

github-actions bot commented Dec 1, 2023

Started backporting to release/8.0-staging: https://github.com/dotnet/runtime/actions/runs/7057987064

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 1, 2023
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.

Non-deterministic codegen in S_P_CoreLib_System_Reflection_Runtime_TypeInfos_RuntimeTypeInfo__GetPropertyImpl
5 participants