Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Mono] [Arm64] Add basic Vector3 SIMD intrinsics #97416

Merged
merged 9 commits into from
Feb 5, 2024

Conversation

jkurdek
Copy link
Member

@jkurdek jkurdek commented Jan 23, 2024

Basic SIMD support for System.Numerics.Vector3. Should fix #91456

@ghost
Copy link

ghost commented Jan 23, 2024

Tagging subscribers to this area: @SamMonoRT, @fanyang-mono
See info in area-owners.md if you want to be subscribed.

Issue Details

Basic SIMD support for System.Numerics.Vector3. Should fix #91456

Author: jkurdek
Assignees: jkurdek
Labels:

area-Codegen-Intrinsics-mono

Milestone: -

@jkurdek
Copy link
Member Author

jkurdek commented Jan 23, 2024

/azp run runtime-extra-platforms

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@vargaz
Copy link
Contributor

vargaz commented Jan 23, 2024

Probably needs llvm support as well.

@jkurdek jkurdek force-pushed the vector3-arm64-intrinsics branch from a0aa8fe to 1e30142 Compare January 29, 2024 20:20
@vargaz
Copy link
Contributor

vargaz commented Jan 29, 2024

The llvm changes look ok as well, just the formatting needs to be cleaned up to conform to the mono coding convs.

@jkurdek
Copy link
Member Author

jkurdek commented Jan 29, 2024

/azp run runtime-extra-platforms

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Comment on lines 6217 to 6219

if (mono_class_value_size (klass, NULL) == 12)
len--;
Copy link
Member

Choose a reason for hiding this comment

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

Why are we decrementing len here?

Copy link
Member Author

@jkurdek jkurdek Jan 30, 2024

Choose a reason for hiding this comment

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

I encountered case that lhs would be V3 represented as V4(three elements + zero). And the retval would be V3.

The code tried to extract the fourth value of lhs and insert it into retval which caused an error, as retval had only three elements.

The change made it only extract and insert the first three elms if V3 was encoutered

Copy link
Member

Choose a reason for hiding this comment

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

I would add comment explaining this special case for easier comprehension.

Copy link
Member

Choose a reason for hiding this comment

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

@jkurdek thank you for the explanation.

Just for my understanding, so it basically means that in such cases LLVMGetReturnType (ctx->lmethod_type) and LLVMTypeOf (lhs) are returning a different type (as we end up having different vector sizes) ?

If we have these kind of workarounds across the mini-llvm code, I think it is ok to manually adjust the len however to be less cryptic, it might be better to have a comment explaining the special-casing (as @matouskozak suggested) and something like this instead:

// TODO: Here add explanation why handling Vector3 requires special treatment
int len = mono_class_value_size (klass, NULL) == 12 ? 3 : LLVMGetVectorSize (LLVMTypeOf (lhs));

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, LLVMGetReturnType (ctx->lmethod_type) is a vector of three elements, whereas LLVMTypeOf (lhs) is a vector of four elements (three + zero)

Copy link
Contributor

Choose a reason for hiding this comment

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

Since 3 element vectors don't exist in the hardware, it might be easier to use 4 elements ones in return types etc. and only handle 3 element ones in loads/stores etc.

@jkurdek
Copy link
Member Author

jkurdek commented Jan 30, 2024

/azp run runtime-extra-platforms

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkurdek
Copy link
Member Author

jkurdek commented Jan 30, 2024

/azp run runtime-extra-platforms

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkurdek
Copy link
Member Author

jkurdek commented Jan 31, 2024

/azp run runtime-extra-platforms

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkurdek
Copy link
Member Author

jkurdek commented Jan 31, 2024

/azp run runtime-extra-platforms

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkurdek
Copy link
Member Author

jkurdek commented Feb 1, 2024

Test failures seem unrelated

@jkurdek jkurdek requested a review from ivanpovazan February 1, 2024 09:34
Copy link
Member

@matouskozak matouskozak left a comment

Choose a reason for hiding this comment

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

The changes look good to me, as a follow up we could intrisify more Vector3/4 constructors.

@@ -2721,6 +2727,17 @@ emit_vector_2_3_4 (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *f
for (int i = 1; i < fsig->param_count; ++i)
ins = emit_vector_insert_element (cfg, klass, ins, MONO_TYPE_R4, args [i + 1], i, FALSE);

if (len == 3) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this also handle the Vector3 constructor: Vector3 (Vector2, float)? As a follow up we could enable this block

#if 0
} else if (len == 3 && fsig->param_count == 2 && fsig->params [0]->type == MONO_TYPE_VALUETYPE && fsig->params [1]->type == etype->type) {
/* Vector3 (Vector2, float) */
int dreg = load_simd_vreg (cfg, cmethod, args [0], NULL);
ins = emit_simd_ins (cfg, klass, OP_INSERT_R4, args [1]->dreg, args [2]->dreg);
ins->inst_c0 = 2;
ins->dreg = dreg;
return ins;
} else if (len == 4 && fsig->param_count == 2 && fsig->params [0]->type == MONO_TYPE_VALUETYPE && fsig->params [1]->type == etype->type) {
/* Vector4 (Vector3, float) */
int dreg = load_simd_vreg (cfg, cmethod, args [0], NULL);
ins = emit_simd_ins (cfg, klass, OP_INSERT_R4, args [1]->dreg, args [2]->dreg);
ins->inst_c0 = 3;
ins->dreg = dreg;
return ins;
} else if (len == 4 && fsig->param_count == 3 && fsig->params [0]->type == MONO_TYPE_VALUETYPE && fsig->params [1]->type == etype->type && fsig->params [2]->type == etype->type) {
/* Vector4 (Vector2, float, float) */
int dreg = load_simd_vreg (cfg, cmethod, args [0], NULL);
int sreg = args [1]->dreg;
ins = emit_simd_ins (cfg, klass, OP_INSERT_R4, sreg, args [2]->dreg);
ins->inst_c0 = 2;
ins = emit_simd_ins (cfg, klass, OP_INSERT_R4, ins->dreg, args [3]->dreg);
ins->inst_c0 = 3;
ins->dreg = dreg;
return ins;
} else {
g_assert_not_reached ();
}
#endif
to intrinsify constructors that take another vector as a parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not supported yet, I created a new issue for the rest of vector constructors as it seems they don't work out of the box.

#97815

Comment on lines 6217 to 6219

if (mono_class_value_size (klass, NULL) == 12)
len--;
Copy link
Member

Choose a reason for hiding this comment

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

I would add comment explaining this special case for easier comprehension.

jkurdek and others added 2 commits February 1, 2024 15:05
Co-authored-by: Ivan Povazan <55002338+ivanpovazan@users.noreply.github.com>
Copy link
Member

@ivanpovazan ivanpovazan left a comment

Choose a reason for hiding this comment

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

LGTM, Thank you!

@jkurdek jkurdek merged commit bdc031f into dotnet:main Feb 5, 2024
108 of 111 checks passed
@jkurdek jkurdek deleted the vector3-arm64-intrinsics branch February 5, 2024 10:00
@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Mono] Add basic SIMD support for System.Numerics.Vector3 on Arm64
4 participants