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

Fix function call handling according to Microsoft Arm64 variadic function call ABI #37

Open
wants to merge 3 commits into
base: woarm64
Choose a base branch
from

Conversation

@Blackhex Blackhex force-pushed the fix-va-list branch 2 times, most recently from 3b18f93 to 2cb2f8a Compare November 4, 2024 10:49
@Blackhex Blackhex force-pushed the woarm64 branch 2 times, most recently from c26bfab to 266c7a4 Compare November 5, 2024 07:56
@Blackhex Blackhex force-pushed the fix-va-list branch 2 times, most recently from e65268d to caa9691 Compare November 5, 2024 21:07
@Blackhex Blackhex force-pushed the fix-va-list branch 2 times, most recently from 0321b24 to d4e43b1 Compare November 6, 2024 09:51
@Blackhex Blackhex requested review from eukarpov and vejbomar November 6, 2024 09:52
@eukarpov
Copy link
Member

eukarpov commented Nov 6, 2024

It looks like a right direction. I suggest to get CI results for the toolchain with ucrt.

Copy link
Member

@eukarpov eukarpov left a comment

Choose a reason for hiding this comment

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

The first round of reviewing is done, it is not covering all changes. This implementation deserves the patch series with 4-5 patches.

extra_headers="arm_fp16.h arm_neon.h arm_bf16.h arm_acle.h arm_sve.h
arm_sme.h arm_neon_sve_bridge.h arm_private_fp8.h
arm_private_neon_types.h
cross-stdarg.h"
Copy link
Member

Choose a reason for hiding this comment

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

all headers start with arm_ prefix and use underscore except new one. It looks like it should be moved to mingw and cygwin targets.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought so as well but the i386 implementation is in beteween the common headers as well. I think it has something to do with the fact that those valist type definitions can be relevant accross targets. I'd keep this to be resolved within the discussion with the maintainers.

Copy link
Member

Choose a reason for hiding this comment

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

ix86 implementation is very different historically, and I do not see reasons why it should not be moved to cygwin and mingw targets.

@@ -22,6 +22,10 @@ along with GCC; see the file COPYING3. If not see

extern tree mingw_handle_selectany_attribute (tree *, tree, tree, int, bool *);

#if defined (TARGET_AARCH64_MS_ABI)
extern tree aarch64_handle_ms_abi_attribute (tree *, tree, tree, int, bool *);
Copy link
Member

Choose a reason for hiding this comment

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

it should be called mingw_handle_ms_abi_attribute and declared here without the check.

Copy link
Member Author

@Blackhex Blackhex Jan 15, 2025

Choose a reason for hiding this comment

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

It's relevant only to AArch64 MinGW, aarch64_mingw_handle_ms_abi_attribute or mingw_aarch64_handle_ms_abi_attribute then?

Copy link
Member

Choose a reason for hiding this comment

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

mingw folder contains not architecture specific implementation. Based on implementation it looks it is not relaying on aarch64 directly, otherwise it should be moved out to aarch64. It looks like renaming should be enough.

Copy link
Member Author

@Blackhex Blackhex Jan 15, 2025

Choose a reason for hiding this comment

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

Should new files aarch64/winnt.h and aarch64/winnt.cc be created then?

Copy link
Member

Choose a reason for hiding this comment

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

For me it looks like mingw is right place for current implementation with renaming.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe the architecture specific methods should have the architecture in it's name. That implies that the name can be either aarch64_mingw_handle_ms_abi_attribute or mingw_aarch64_handle_ms_abi_attribute. Can you give me another expample of architecture specific function that has not an achitecture in its name?

Copy link
Member

Choose a reason for hiding this comment

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

for instance mingw_pe_file_end behaves differently based on configuration for ix86 and aarch64 targets.

Copy link
Member Author

Choose a reason for hiding this comment

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

I dont understand your point. The code of mingw_pe_file_end itself is generic so it does not have the prefix. It calls i386_find_on_wrapper_list for the architecure specific sub-logic which proves the logic that architecture specific functions should have the architecture prefix.

Do I understand correctly that you are suggesting to refactor of i386 implementation so the ix86_handle_abi_attribute and aarch64_handle_ms_abi_attribute would be merged, containing #if defined (TARGET_AARCH64_MS_ABI) conditions inside?

Copy link
Member

Choose a reason for hiding this comment

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

yes, ix86_handle_abi_attribute looks alike and should be moved to mingw with renaming to mingw_handle_abi_attribute.
TARGET_AARCH64_MS_ABI or other configuration could be used to adjust the function for aarch64.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, thank you. Now, I see your point.

attribute_spec.handler. */

tree
aarch64_handle_ms_abi_attribute (tree *node, tree name, tree, int,
Copy link
Member

Choose a reason for hiding this comment

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

it looks like TARGET_AARCH64_MS_ABI is not needed here and it brings common implementation not arch specific which could be reused. It should be renamed to mingw_handle_ms_abi_attribute.

Copy link
Member Author

@Blackhex Blackhex Jan 15, 2025

Choose a reason for hiding this comment

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

This is architecture-specific implementation i386 has its own in i386/winnt.cc.

Copy link
Member

Choose a reason for hiding this comment

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

the same here
#37 (comment)

int arg_count = 0;
for (tree arg = TYPE_ARG_TYPES (fntype); arg; arg = TREE_CHAIN (arg))
{
if (TREE_VALUE (arg) == void_type_node)
Copy link
Member

Choose a reason for hiding this comment

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

Should it not be enough to check only first arg for void_type_node?

arg_count++;
}

return arg_count > 0;
Copy link
Member

Choose a reason for hiding this comment

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

arg_count 1 is still variadic?

return aarch64_ms_variadic_abi ();
#endif

if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
Copy link
Member

Choose a reason for hiding this comment

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

what exactly is happening here?

Copy link
Member Author

@Blackhex Blackhex Jan 15, 2025

Choose a reason for hiding this comment

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

This logic was re-used form Zac's branch and verified by comparing it with i386 implementation. I believe it has something to do with the ability to explicitly specify that some function call should follow the Microsoft ABI using the attribute explicity. This might be needed for ensuring ABI compatibility accross hybrid environments like wine, WSL, QEMU, etc. Albeit those changes are not necessary for our use-cases, I'd keep them in the patch so we can initiate discussions with people that can bring better understanding for which exactly this is needed and then we can decide whether we should keep and test this feature or remove.

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 exclude this change until the moment it is known what is changing/fixing. It could be a separate discussion on the mailing list if needed.

Copy link
Member Author

@Blackhex Blackhex Jan 15, 2025

Choose a reason for hiding this comment

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

OK, I can move the relevant changes into a separate commit here and submit it as a separate (dependent) patch to the maiiling list.

ncrn = pcum->aapcs_ncrn;
nregs = size / UNITS_PER_WORD;

if (ncrn < NUM_ARG_REGS)
Copy link
Member

Choose a reason for hiding this comment

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

too much branching, i would change to do while(false) with breaks and move bottom branch to the top.

Copy link
Member Author

@Blackhex Blackhex Jan 15, 2025

Choose a reason for hiding this comment

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

Yes, I agree this can be written using guard-clauses. My intention was to keep this code as similar to the AAPCS one as possible to help reviewers to see the differences better.

mingw_handle_selectany_attribute, NULL }, \
{ "ms_abi", 0, 0, false, true, true, true, \
aarch64_handle_ms_abi_attribute, NULL }, \
{ "ms_abi va_list", 0, 0, false, false, false, false, NULL, NULL }
Copy link
Member

Choose a reason for hiding this comment

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

Why is it needed and how it helps without a handler?

nregs -= pcum->aapcs_stack_words;
}

/* Generate load arg to registers intructions. */
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/* Generate load arg to registers intructions. */
/* Generate load arg to registers instructions. */

Just a typo I noticed yesterday

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.

3 participants