This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] RISCV vector calling convention (1/2)
Needs ReviewPublic

Authored by 4vtomat on Jul 6 2023, 1:04 AM.

Details

Summary

This is the vector calling convention based on
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/389,
the idea is to split between "scalar" callee-saved registers
and "vector" callee-saved registers. "scalar" ones remain the
original strategy, however, "vector" ones are handled together
with RVV objects.

The stack layout would be:

|--------------------------| <-- FP
| callee-allocated save    |
| area for register varargs|
|--------------------------|
| callee-saved registers   | <-- scalar callee-saved
|        (scalar)          |
|--------------------------|
| RVV alignment padding    |
|--------------------------|
| callee-saved registers   | <-- vector callee-saved
|        (vector)          |
|--------------------------|
| RVV objects              |
|--------------------------|
| padding before RVV       |
|--------------------------|
| scalar local variables   |
|--------------------------| <-- BP
| variable size objects    |
|--------------------------| <-- SP

Note: This patch doesn't contain "tuple" type, e.g. vint32m1x2. It will be handled in future patch once the PR389 is ready.

The calling convention can be turned on by using

__attribute__((riscv_vector_cc))

Diff Detail

Event Timeline

4vtomat created this revision.Jul 6 2023, 1:04 AM
Herald added a project: Restricted Project. · View Herald Transcript
4vtomat requested review of this revision.Jul 6 2023, 1:04 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 6 2023, 1:04 AM

Does this only change the calling convention when __attribute__((riscv_vector_cc)) is used? The attribute should be mentioned in the patch description.

4vtomat added a comment.EditedJul 6 2023, 1:22 AM

Does this only change the calling convention when __attribute__((riscv_vector_cc)) is used? The attribute should be mentioned in the patch description.

Yes it only changes when __attribute__((riscv_vector_cc)) is used, thanks for reminding, I will mention in the description.

4vtomat edited the summary of this revision. (Show Details)Jul 6 2023, 1:22 AM
4vtomat edited the summary of this revision. (Show Details)
4vtomat edited the summary of this revision. (Show Details)Jul 6 2023, 1:25 AM
lhtin added a subscriber: lhtin.Jul 9 2023, 6:24 PM

Does this only change the calling convention when __attribute__((riscv_vector_cc)) is used? The attribute should be mentioned in the patch description.

Yes it only changes when __attribute__((riscv_vector_cc)) is used, thanks for reminding, I will mention in the description.

In addition to using attributes, functions that use vector registers to pass arguments or return values also need to comply with the new calling convention.

aaron.ballman added inline comments.Jul 13 2023, 9:21 AM
clang/lib/AST/ItaniumMangle.cpp
3267

Is it possible to use this calling convention on Windows where we'd hit the Microsoft name mangler?

clang/test/CodeGen/RISCV/riscv-vector-cc-attr.c
1

You should also have Sema tests for various things (attribute accepts no arguments, only applies to functions, does/doesn't silently convert to cdecl, etc). You should also have some tests using the attribute as a type attribute instead of a declaration attribute.

You should also have C++ tests for Sema and codegen for things like putting the calling convention on a member function or a lambda to ensure those do reasonable things with the convention.

Does this only change the calling convention when __attribute__((riscv_vector_cc)) is used? The attribute should be mentioned in the patch description.

Yes it only changes when __attribute__((riscv_vector_cc)) is used, thanks for reminding, I will mention in the description.

In addition to using attributes, functions that use vector registers to pass arguments or return values also need to comply with the new calling convention.

Thanks for pointing this out! However, since it would make a tons of test cases change, I will make it in another patch!

4vtomat marked 2 inline comments as done.Jul 30 2023, 12:13 AM
4vtomat added inline comments.
clang/lib/AST/ItaniumMangle.cpp
3267

Thanks for reviewing. However since RISCV target currently only supports elf, so maybe we will do this in the future!

clang/test/CodeGen/RISCV/riscv-vector-cc-attr.c
1

Good idea, thanks for your review, I will add some tests for Sema!

4vtomat updated this revision to Diff 545413.Jul 30 2023, 12:42 AM
4vtomat marked 2 inline comments as done.

Resolved Aaron's comments.
Also group some code and refactoring some code to make it much clear.

The clang bits LGTM, but I'll leave final sign-off to the LLVM reviewers.

asb added a comment.Aug 9 2023, 4:05 AM

Putting this behind an attribute for now makes sense given that we don't seem to have consensus on https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/389 let alone how to manage any migration. I'm slightly wary of merging this with a generic attribute (currently 'riscv_vector_cc') and the proposed ABI changing, causing further confusion.

4vtomat updated this revision to Diff 556995.Sep 18 2023, 9:50 PM

Add VRM2, VRM4 and VRM8 to CalleeSaved register list.

wangpc added inline comments.Sep 18 2023, 10:19 PM
llvm/lib/Target/RISCV/RISCVCallingConv.td
64–65

Should we add CSRs for interrupt functions? And Should we save vtype, vstart, vxrm, vxsat, etc. registers?

4vtomat added inline comments.Sep 19 2023, 9:37 PM
llvm/lib/Target/RISCV/RISCVCallingConv.td
64–65

Yeah, we should also handle these in interrupt function, but seems it's not related to this patch. Am I right? @kito-cheng