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

[mono] Use bitshift instead of multiply in Unsafe.Add intrinsic if possible #103775

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mono/mono/mini/interp/mintops.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ typedef enum {
#define MINT_LDELEM_I MINT_LDELEM_I8
#define MINT_STELEM_I MINT_STELEM_I8
#define MINT_MUL_P_IMM MINT_MUL_I8_IMM
#define MINT_SHL_P_IMM MINT_SHL_I8_IMM
#define MINT_ADD_MUL_P_IMM MINT_ADD_MUL_I8_IMM
#else
#define MINT_MOV_P MINT_MOV_4
Expand All @@ -204,6 +205,7 @@ typedef enum {
#define MINT_LDELEM_I MINT_LDELEM_I4
#define MINT_STELEM_I MINT_STELEM_I4
#define MINT_MUL_P_IMM MINT_MUL_I4_IMM
#define MINT_SHL_P_IMM MINT_SHL_I4_IMM
#define MINT_ADD_MUL_P_IMM MINT_ADD_MUL_I4_IMM
#endif

Expand Down
5 changes: 3 additions & 2 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -2287,9 +2287,10 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas

esize = mono_type_size (t, &align);
if (esize != 1) {
int power2 = mono_is_power_of_two ((guint32)esize);
g_assert (esize <= 32767);
interp_add_ins (td, MINT_MUL_P_IMM);
td->last_ins->data [0] = (gint16)esize;
interp_add_ins (td, power2 > 0 ? MINT_SHL_P_IMM : MINT_MUL_P_IMM);
td->last_ins->data [0] = (gint16)(power2 > 0 ? power2 : esize);
Comment on lines +2292 to +2293
Copy link
Member

Choose a reason for hiding this comment

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

Can't this just be a general optimization done against MINT_MUL_P_IMM rather than one done specifically here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Optimizations like that will only happen when the method is being optimized, so it makes the intrinsic lower value to punt it to the superoptimizer pass. We could add it to the superoptimizer

Copy link
Member

Choose a reason for hiding this comment

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

I think this is one of those "we should always do this, even if we aren't optimizing" type transforms.


That is, in RyuJIT we have different knobs that determine what type of optimizations can be done and there's some simple optimizations we do always regardless.

In particular we have a general OptimizationDisabled which returns true if we are in Tier 0 or we are marked as needing to produce debuggable code (such as if a debugger is attached). This (and its logical inverse OptimizationEnabled) are used in most code to determine if we should optimize or not.

However, we also recognize that there's places where it is fundamentally beneficial to perform basic transforms regardless. For example, in gtFoldExpr rather than checking OptimizationDisabled we instead manually check if we need to produce debuggable code or if we are marked as JIT_FLAG_MIN_OPT, which is typically only set if the first compilation pass hit some internal limitation and failed to produce code (so it disables any and all optimizations as a fail safe so things still work, even if slow): https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/gentree.cpp#L13487-L13501 -- This is called unconditionally from places like import: https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/importer.cpp#L7206

This same general consideration also applies to more general optimizations, such as in Morph (https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L13104), where we then skip additional constant folding if optimizations are disabled (https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L8288-L8292, unlike import which did this same bit unconditionally), but we do some other core optimizations such as replacing multiplication with shift regardless of optimization level: https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L10456-L10520

This also allows code to do basic optimizations such as dead code elimination where trivially possible, leading to better throughput and handling, as we can differentiate between the levels of optimization and whether we need to be debuggable or not.


I imagine that Mono (JIT, LLVM, and Interpreter) would benefit from the same type of considerations and would see better perf and throughput if it mirrored the general premise

Copy link
Member Author

Choose a reason for hiding this comment

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

Vlad can correct me on this, but iirc we don't do any dead code elimination in non-optimized mode right now. I've been thinking recently that it might be beneficial to always do DCE. That would require some amount of constant folding or similar analysis though, I think. We have some methods in the BCL that are full of dead code, and we burn cycles processing all that dead code during codegen for unoptimized methods.

Copy link
Member

Choose a reason for hiding this comment

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

That would require some amount of constant folding or similar analysis though

Agreed. That's exactly why we do the basic constant folding during import in RyuJIT (only skipping them if we're in the true minopts mode or have a debugger attached) and then similarly do basic DCE based on this as well under similar circumstances.

interp_ins_set_sreg (td->last_ins, offset_var);
interp_ins_set_dreg (td->last_ins, offset_var);
}
Expand Down
Loading