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

toolchain: Add assembly-side counterpart of __aligned #85758

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

hIAhmad
Copy link
Contributor

@hIAhmad hIAhmad commented Feb 13, 2025

Zephyr toolchain abstraction provides a macro __aligned(x) to add specified alignment, in bytes, to a chosen symbol for C/C++ source files but there is no portable counterpart available for symbols defined in assembly source files. This change-set adds a new macro ALIGN(x) for this purpose.

@zephyrbot zephyrbot added the area: Toolchains Toolchains label Feb 13, 2025
@hIAhmad hIAhmad force-pushed the portable_balign branch 3 times, most recently from 4260068 to 8e0624a Compare February 14, 2025 02:43
Copy link
Collaborator

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

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

I would prefer having the macro named ALIGN.

defined(CONFIG_XTENSA) || defined(CONFIG_MIPS) || \
defined(CONFIG_ARCH_POSIX)

#define BALIGN(x) .balign x
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think ALIGN is a better term because alignment is what we want.
I know the differences between .align and .balign, but for the code that wants to use alignment, then ALIGN is a better macro name and aligns with the PERFOPT_ALIGN macros in naming (which also uses .balign when possible).


#define BALIGN(x) .balign x

#elif defined(CONFIG_ARC) || defined(CONFIG_SPARC)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically support for .balign is not an architecture thing but a toolchain supported instruction, whereas .align is generally supported across toolchains (but where the behavior is system specific 😞 ).

Copy link
Contributor Author

@hIAhmad hIAhmad Feb 18, 2025

Choose a reason for hiding this comment

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

Yes, indeed. For preprocessor conditionals, I just followed the similar scheme that the logically close existing macro PERFOPT_ALIGN is using. But theoretically, yes, this is a bit off. However, it won't cause any trouble unless we (need to) support multiple toolchains for an architecture that either treat .align differently (i.e. one interprets its param as number of bytes and other as number of lower order bits to be zeroed) and/or one supports .balign and other doesn't.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep for now.
Just wanted to point it out.

Perhaps a code comment would be appropriate so someone later don't change this by accident.

Copy link
Contributor Author

@hIAhmad hIAhmad Feb 21, 2025

Choose a reason for hiding this comment

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

Just noticed that PERFOPT_ALIGN implementation has a comment exactly for this clarification for the ARC case - I have updated the code to reuse the same comment.

@RobinKastberg
Copy link
Contributor

Currently IAR use GNU assembler, but if we start using iasmarm alignment in assembly files are specified in Power-Of-Two unfortunately.
E.g.

 alignram 6 ; Align to a 64-byte boundary

Maybe this is a shortcoming we will need to fix in our toolchain however, because I am not sure how to make a clean solution that would allow both

@bjorniuppsala any ideas?

@bjorniuppsala
Copy link
Contributor

Currently IAR use GNU assembler, but if we start using iasmarm alignment in assembly files are specified in Power-Of-Two unfortunately.
...
@bjorniuppsala any ideas?

I think fixing our assembler is the way to go.


#define BALIGN(x) .balign x

#elif defined(CONFIG_ARC) || defined(CONFIG_SPARC)
Copy link
Member

Choose a reason for hiding this comment

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

For ARC classic there are multiple compilers, gnu and MetaWare. Metaware indeed doesn't support .balign, and .align works for both gnu and MetaWare. Should the condition for this choice be architecture, or rather toolchain variant? Maybe this shouldn't be in 'common.h' but specified per toolchain?

@hIAhmad
Copy link
Contributor Author

hIAhmad commented Feb 18, 2025

I would prefer having the macro named ALIGN.

I also preferred it but unfortunately it conflicts with a (GNU/GNU-compatible) linker script directive of the same name and syntax. So instances like . = ALIGN(4); in linker scripts will get messed up after the preprocessor run, resulting in eventual link failures.

BYTE_ALIGN can be another alternative name that I initially used but then shortened it to BALIGN. If you'd prefer BYTE_ALIGN compared to BALGIN, I can revert to that.

Or we can consider some other good name, but I don't have any in my mind, as of yet.

@tejlmand
Copy link
Collaborator

I also preferred it but unfortunately it conflicts with a (GNU/GNU-compatible) linker script directive of the same name and syntax. So instances like . = ALIGN(4); in linker scripts will get messed up after the preprocessor run, resulting in eventual link failures.

You just need to properly safe guard the definition so that it's not used for linker script preprocessing, like this:

#if defined(_ASMLANGUAGE) && !defined(_LINKER)

* 'test_symbol' will get aligned to 4 bytes.
*/

#ifdef _ASMLANGUAGE
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you use:

Suggested change
#ifdef _ASMLANGUAGE
#if defined(_ASMLANGUAGE) && !defined(_LINKER)

then it will be defined for the linker script template pre-processin, and thus not conflict with the linker directive of same name and you can use ALIGN as macro name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the pointer. I have updated it.

@hIAhmad
Copy link
Contributor Author

hIAhmad commented Feb 21, 2025

@tejlmand BTW, now PERFOPT_ALIGN can be implemented in terms of this newly introduced ALIGN macro. It will consolidate the implementation and reduce the effort to add support for new architectures/toolchains.

If you agree, I can post a follow-up PR afterwards.

tejlmand
tejlmand previously approved these changes Feb 26, 2025
Copy link
Collaborator

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

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

minor nit observed, rest looks good.


#elif defined(CONFIG_ARC)

/* .align assembler directive is supposed by all ARC toolchains and it is
Copy link
Collaborator

Choose a reason for hiding this comment

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

supposed ?

Suggested change
/* .align assembler directive is supposed by all ARC toolchains and it is
/* .align assembler directive is supported by all ARC toolchains and it is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Carry over from the existing comment in PERFOPT_ALIGN that I reused. Fixed here. Will also fix the old reference comment in the follow-up PR while rebasing PERFOPT_ALIGN over this newly introduced ALIGN macro.

@tejlmand
Copy link
Collaborator

If you agree, I can post a follow-up PR afterwards.

will be appreciated 👍

Zephyr toolchain abstraction provides a macro __aligned(x) to add specified
alignment, in bytes, to a chosen symbol for C/C++ source files but there is
no portable counterpart available for symbols defined in assembly source
files. This change-set adds a new macro ALIGN(x) for this purpose.

Signed-off-by: Irfan Ahmad <irfan.ahmad@siemens.com>
@hIAhmad
Copy link
Contributor Author

hIAhmad commented Feb 26, 2025

If you agree, I can post a follow-up PR afterwards.

will be appreciated 👍

Created #86366 - will rebase and put in review, once this PR gets merged.

@hIAhmad
Copy link
Contributor Author

hIAhmad commented Mar 21, 2025

@stephanosio, @nordicjm Can you please review/re-review and give your +1 or feedback? This PR is blocking other PRs. Already has +1 from @tejlmand - needs one more.

@kartben kartben merged commit 58d5e35 into zephyrproject-rtos:main Mar 24, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants