Because of the Thanksgiving holiday in the U.S., TI E2E™ design support forum responses may be delayed the week of November 23.

F29H850TU: How to specify the alignment using #pragma

Part Number: F29H850TU

Hello,

In AUTOSAR, when we define, for example, 8 bits variables in a MEMMAP section with the alignment 32, each variable is aligned on 32 bits.

How to achieve that with the F29 compiler ?

Thank you

  • Hello Ronan,

    On F29, alignment is specified with the Clang alignment attribute:

    uint8_t myVar __attribute__((aligned(4)));   /* 4-byte (32-bit) aligned */
    

    The aligned(value) argument is in bytes — aligned(8) means 8-byte alignment — so an AUTOSAR "alignment 32" (32-bit) section maps to aligned(4).

    Applied per-object, this gives you the same effect as an AUTOSAR MEMMAP alignment class: each 8-bit variable in the section is individually padded/placed to the requested boundary.

    Please note that if an aligned object is passed into a function as a pointer, the compiler loses the alignment information. Reassert it with __builtin_assume_aligned on the pointer so the compiler can still exploit the alignment:

    uint8_t *p = (uint8_t *)__builtin_assume_aligned(ptr, 4);
    

    See reference information here.


    Best regards,
    François.

  • Hello Mr Charlot,

    In fact, this is not equivalent because

    1/ for 10 variables of the same type, instead of specifying the alignment twice (1 in START and 1 in STOP MEMMAP section), we have to specify it for each variable

    2/ MEMMAP allows to abstract this alignment (the code is invariant whatever the compiler) while __attribute__((aligned(X)) is compiler specific

    Also, the AUTOSAR specification of MEMMAP, more or less, depending on the version, specifies that a variable with a size X located in a MEMMAP section with an alignment Y, with X < Y, must be aligned on Y.

    We know that, by default, the clang does not allow to specify an alignment on #pragma clang section. But, another provider of compilers added a specific #pragma to allow to specify the alignment.

    I just discovered this:

    #pragma clang attribute push (__attribute__((<attribute_name>)), apply_to = <match_scope>)
    #pragma clang attribute pop

    Could this allow to specify the attribute aligned() on all the data (variables, constants) in a MEMMAP section ?

    Do you have an example for its usage in this specific use case ?

    Also, the documentation of the compiler references the page "">clang.llvm.org/.../LanguageExtensions.html but it seems that the site is down. Would you have a copy of its content or another location where to find the information ?

    Thank you

  • Hi Ronan,

    1/ for 10 variables of the same type, instead of specifying the alignment twice (1 in START and 1 in STOP MEMMAP section), we have to specify it for each variable

    This is correct, clang defines each aligned attribute only applies to the variable, function or struct it is assigned to so in order to ensure each variable is aligned the user must annotate each individual variable.

    I just discovered this:

    #pragma clang attribute push (__attribute__((<attribute_name>)), apply_to = <match_scope>)
    #pragma clang attribute pop

    Unfortunately, clang does not support the combination of the attribute push/pop pragma with the aligned attribute. Currently the only way to annotate the alignment of variable, function, or struct is to apply the attribute individually. The push/pop pragma is mentioned in our documentation here: https://software-dl.ti.com/codegen/docs/c29clang/rel2_2_2_LTS/compiler_manual/c_cpp_language_implementation/pragmas.html

    Also, the documentation of the compiler references the page "">clang.llvm.org/.../LanguageExtensions.html but it seems that the site is down. Would you have a copy of its content or another location where to find the information ?

    I was able to find the information following the link in the documentation, but for quicker access you will be able to view clang's documentation here: https://clang.llvm.org/docs/LanguageExtensions.html#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute.

    Regards,
    Mackay

  • Hello Mackay,

    Regarding the access to clang.llvm.org, the site was down at the time I created the post but, as you mentioned, it is back on and so I have already been able to access it.

    That is unfortunate that the aligned attribute is not supported.

    This means that your compiler is not compatible with AUTOSAR and this is very problematic for us.

    AUTOSAR specifies that when we use the section VAR or CONST with an alignment of X, all the data in this section must have a minimum alignment of X.

    Adding __attribute__((aligned(..))) on each variable is not an option especially with components that are generic and used on different microcontrollers.

    What can be done from there ?

    Thank you

  • You stated that the type of each variable is the same. You can use type attributes in typedef declarations.

    typedef int __attribute__((aligned(16))) aligned_int;
    
    aligned_int x;
    aligned_int y;
    aligned_int z;
    

    If I compile this C code into LLVM intermediate representation, I get:

    @x = dso_local global i32 0, align 16 #0
    @y = dso_local global i32 0, align 16 #0
    @z = dso_local global i32 0, align 16 #0
    

    Note that all 3 variables have been aligned with only a single application of the attribute to a type, as desired.

    For each type, you only need 1 typedef to align all variables of that type. This typedef can be changed depending on predefined macros used by different microcontrollers.

  • Hello James,

    In fact, in the AUTOSAR MEMMAP section, the alignment is not stricty linked to the type of the data.

    Let's consider that in a section with a 32 bits alignment, I put 1 byte, 1 long (32 bits) and 1 long long (64 bits), the first 1 will be forced to be aligned on 32 bits, the second one will be aligned naturally on 32 bits and the last one will be aligned naturally on 64 bits.

    So, globally, the alignment in an AUTOSAR MEMMAP section is the minimum alignment that is imposed to all the data of that section.

    As I said in a previous post, we know that the #pragma clang section does not support the alignment, that is why at least 1 provider of compiler added its own pragma to add this feature.

    So, the question now is more: do you think that you can implement such a feature ?

    Thank you

  • If that's the question, then the answer is possibly, but not before 3.0.0.LTS or likely even later. The enhancement will be prioritized and scheduled to other work. I feel like such a feature has hidden complexity or unresolvable issues because upstream clang has made no plans to support the pragma for any type attributes.

    I've started the process to create an external Jira for this. I'll share the link when that's finished.

    --

    I am trying to help outside of a possible new feature by trying to find C analogues. I am no expert on AUTOSAR, so I might not be understanding the implications of what you're referencing.

    Question 1

    Are MEMMAPs more like C's structures, or more like the linker's output sections? I'll elaborate:

    • C structures are defined by a strict layout ordering for its enclosed members, including rules for creating holes due to alignment and padding the structure to be a multiple of its largest member's size
    • A linker output section defines a region of memory into which a number of objects are placed in some unspecified order according to their own rules. That is, an output section's alignment has no effect on the alignment of anything placed inside it.

    What happens, for example, when your 3 element MEMMAP is placed at 4-byte aligned address 0x4?

    In C, using struct with forced 4-byte alignment as the analogue for MEMMAP, this is a problem:

    • The byte is 4-byte aligned at 0x4
    • The long is 4-byte aligned at 0x8
    • The long long is not 8-byte aligned at 0xc

    This is why C states that the default alignment of a struct or union is the maximum of its data members.

    Question 2

    Back in your original post:

    8 bits variables in a MEMMAP section with the alignment 32, each variable is aligned on 32 bits

    Does that means with 2 bytes in a 4-byte aligned MEMMAP, the layout looks like:

    • Byte offset 0, byte 1 (4-byte aligned)
    • 3 byte hole
    • Byte offset 4, byte 2 (4 byte aligned)
    • 3 byte hole

    If so, I don't think there's an analogue to that in C besides individually aligning each item.