Skip to content

Commit 79abb43

Browse files
author
Joe Valenzuela
committed
Fix for ldloc/ldfld of ref to valuetype
When we have the following sequence: ldloc n ldfld valuetype [Unity.Mathematics]Unity.Mathematics.int2 Foo::bar and local n is a reference to value, when walking the instructions in mono_method_to_ir, we generally try to avoid loading a whole value type just to load one of the fields. I believe this is an optimization. The test here is not quite complete, though, as we have ref valuetypes where the object stored at the location is actually an address, in which case we still need to treat it as we would a normal reference. This optimization isn't present in the ldloc.n coded forms and is written in a slightly more robust way in ldloc.s, so no changes are necessary there. As an alternative to this commit, we might adopt the ldloc.s test for ldloc as well, or merge in mono@29428d9/mono/mini/method-to-ir.c from master which encapsulates much the same intent.
1 parent f4a8a66 commit 79abb43

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

mono/mini/method-to-ir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12458,7 +12458,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
1245812458
CHECK_OPSIZE (4);
1245912459
n = read16 (ip + 2);
1246012460
CHECK_LOCAL (n);
12461-
if ((ip [4] == CEE_LDFLD) && ip_in_bb (cfg, cfg->cbb, ip + 4) && header->locals [n]->type == MONO_TYPE_VALUETYPE) {
12461+
if ((ip [4] == CEE_LDFLD) && ip_in_bb (cfg, cfg->cbb, ip + 4) && header->locals [n]->type == MONO_TYPE_VALUETYPE && !header->locals [n]->byref) {
1246212462
/* Avoid loading a struct just to load one of its fields */
1246312463
EMIT_NEW_LOCLOADA (cfg, ins, n);
1246412464
} else {

0 commit comments

Comments
 (0)