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

Add --trace-dispatch #55848

Merged
merged 9 commits into from
Sep 30, 2024
Merged

Add --trace-dispatch #55848

merged 9 commits into from
Sep 30, 2024

Conversation

kpamnany
Copy link
Contributor

@kpamnany kpamnany commented Sep 23, 2024

Similar to --trace-compile, emit the precompile statement for a method once, but only when it is dynamically dispatched.

For this, we add a dispatched field into jl_method_instance_t that is initialized to 0, and when the method is dispatched, it is set to 1 and the precompile statement is emitted (once).

TODO:

  • Add NEWS entry.
  • Update manual.
  • Add test.

Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

I think you also need to cover the invoke codepath in this same file to get all dispatches, but looks like a good start overall to implementing this

src/method.c Outdated Show resolved Hide resolved
}
else {
if (ios_file(&f_dispatch, t, 1, 1, 1, 1) == NULL)
jl_errorf("cannot open dispatch statement file \"%s\" for writing", t);
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Very awkward (and slightly unsound) to throw errors from here. We should try to move this into the jloptions file so we can detect any issues earlier

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I essentially duplicated record_precompile_statement from right above this, which does the same thing.

Copy link
Sponsor Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Clearly needed in conjunction with #55047, thanks for doing this.

src/jltypes.c Outdated Show resolved Hide resolved
@kpamnany
Copy link
Contributor Author

I think you also need to cover the invoke codepath in this same file to get all dispatches, but looks like a good start overall to implementing this

I added this; does that look right?

Is there anywhere else where this should be recorded?

Similar to `--trace-compile`, emit the `precompile` statement for a method
once, but only when it is dynamically dispatched.

For this, we add a `dispatched` field into `jl_method_instance_t` that is
initialized to 0, and when the method is dispatched, it is set to 1 and the
precompile statement is emitted (once).
- Rename `precompiled` field in `jl_method_instance_t` to `flags`
  and use bit 0 as `precompiled` and bit 1 as `dispatched`.
- Use relaxed atomic operations to access `flags`.
- Add setting of `dispatched` to `jl_gf_invoke_by_method`.
Also add a test for `--trace-compile`.
@kpamnany
Copy link
Contributor Author

Added a test.

@kpamnany kpamnany marked this pull request as ready for review September 25, 2024 19:53
@kpamnany kpamnany added the status:merge me PR is reviewed. Merge when all tests are passing label Sep 25, 2024
src/gf.c Outdated
@@ -3360,6 +3396,13 @@ JL_DLLEXPORT jl_value_t *jl_apply_generic(jl_value_t *F, jl_value_t **args, uint
jl_int32hash_fast(jl_return_address()),
world);
JL_GC_PROMISE_ROOTED(mfunc);
uint8_t miflags = jl_atomic_load_relaxed(&mfunc->flags);
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Are we sure this has no overhead? Could it go in a slow path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, not sure. I think the cost should be negligible. Thoughts @vtjnash?

Could add an outer check for jloptions.trace_dispatch? Remove the check from inside the record... function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved this atomic load off the fast path.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This is probably better but I think it should go in a slower lookup path in jl_lookup_generic; at least only when the fast cache misses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see. Going by the comments in jl_lookup_generic_, there is a lookup in an associative cache, and if that misses, a lookup in the full cache. Can a method instance be in either of those caches if it hasn't been dispatched? If not, then I could move the call here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved it here; please take a look?

@kpamnany kpamnany removed the status:merge me PR is reviewed. Merge when all tests are passing label Sep 25, 2024
@kpamnany

This comment was marked as resolved.

@IanButterworth

This comment was marked as resolved.

@IanButterworth
Copy link
Sponsor Member

Just sharing a thought that if the user wants both --trace-compile and --trace-dispatch and to be able to tell which is which they can send them to differently named files.
If they wanted to use stderr they could do --trace-compile=stderr --trace-compile-timing --trace-dispatch=stderr and the compiled ones would show with the timing comment first.

Maybe that's overthinking it but at least it's possible if desired.

@IanButterworth
Copy link
Sponsor Member

Merge? @JeffBezanson

@IanButterworth IanButterworth merged commit bb25910 into master Sep 30, 2024
7 checks passed
@IanButterworth IanButterworth deleted the kp/add-trace-dispatch branch September 30, 2024 19:41
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.

5 participants