Main Content

Optimize Performance of Memory Access by Using Data Alignment

R2026b

For some target processors, you can improve the performance of memory access by instructing the compiler to align data objects on specific boundaries in memory. In the generated code, you can align data objects such as Simulink.Parameter, Simulink.Signal, and Simulink.Bus objects.

For information about specifying data alignment for a code replacement entry, see Data Alignment for Code Replacement.

For information about specifying data alignment for MATLAB® code, see coder.dataAlignment.

Specify Data Alignment Requirements for Data Objects

To specify the alignment boundary of a data object, set the Alignment property by using one of these methods:

  • Type Editor

  • Dialog box editor for the data object

  • Programmatic interface for the object

This example shows how to specify the data alignment requirements using the programmatic interface or the Type Editor.

Data objects that support data alignment specification include:

Select Toolchain for Data Alignment Syntax

The generated code aligns data by using the capabilities and syntax supported by your build toolchain. Set the model configuration parameter Toolchain to one of these toolchains, which support data alignment by default using the syntax __attribute((aligned(%n))).

  • Microsoft® Visual C++®

  • MinGW®

  • GCC

  • Xcode with Clang

To use data alignment for a different toolchain or to use custom data alignment syntax, create a custom specification by registering a code replacement library. For more information, see Provide Custom Data Alignment Specifications for Compilers.

Align Data for a Data Object Programmatically

This example shows how to specify the data alignment for two Simulink.Bus objects.

  1. Open the example model PreserveBusDims. The model contains two bus ports, which resolve to Simulink.Bus objects in the base workspace.

    openExample("PreserveBusDims")
  2. Set the data alignment for the data objects. For this example, set the alignment for the bus objects ImperialSpecs and MetricSpecs to 64.

    ImperialSpecs.Alignment = 64;
    MetricSpecs.Alignment = 64;

  3. Configure the model to use a toolchain that supports data alignment by setting the Toolchain parameter. For example, use the MinGW toolchain.

    set_param("PreserveBusDims",Toolchain="MinGW64 | gmake (64-bit Windows)");

  4. Generate code for the model.

    slbuild("PreserveBusDims");

  5. Check that the generated code aligns the data by using the alignment syntax that you specified. For this example, PreserveBusDims.h contains this code.

    #ifndef DEFINED_TYPEDEF_FOR_ImperialSpecs_
    #define DEFINED_TYPEDEF_FOR_ImperialSpecs_
    
    typedef struct {
      __attribute((aligned(64))) real_T DimensionsInInches[30];
      __attribute((aligned(64))) real_T WeightInPounds[10];
    } ImperialSpecs;
    
    #endif
    
    #ifndef DEFINED_TYPEDEF_FOR_MetricSpecs_
    #define DEFINED_TYPEDEF_FOR_MetricSpecs_
    
    typedef struct {
      __attribute((aligned(64))) real_T DimensionsInCms[10][3];
      __attribute((aligned(64))) real_T WeightInKilograms[10];
    } MetricSpecs;
    
    #endif

Align Data for a Data Object by Using the Type Editor

Configure Alignment Values for Data Objects

Set the Alignment property of a data object by using the Type Editor.

  1. Open the Type Editor. On the Modeling tab, from the Design gallery, click Type Editor.

  2. In the Type Editor, set the View to All.

  3. For the data object that you want to align, set the value in the Alignment column.

    Type Editor for the PreserveBusDims model. The row for the object ImperialSpecs is highlighted. The Alignment column for that row shows a value of 64.

  4. Use the Type Editor to specify the alignment for other data objects that you want to align.

Apply Alignment Specifications to the Model

  1. Configure the model to use a toolchain that supports data alignment. On the Modeling tab, click Settings. Set the model configuration parameter Toolchain to a toolchain that supports data alignment. For example, select MinGW64 | gmake (64-bit Windows) and click OK.

  2. Generate code for the model. The generated header file contains the alignment directives for the data.

Data Alignment Limitations

When you try to perform data alignment in a case that is not supported:

  • If you specify the data alignment by using only a data object, the code generator produces an error.

  • If you specify the data alignment by using a code replacement entry, replacement does not occur. The code generator also produces an error for some cases.

Data Alignment Unsupported Cases

Data alignment is not supported for:

  • Software-in-the-loop (SIL) simulation

  • Model reference parameters

  • Exported functions in Stateflow® charts

  • Functions that are generated with C function prototype control or C++ class I/O arguments step method and that use root-level I/O variables

  • Functions that are generated with the AUTOSAR system target file and that use root-level I/O or AUTOSAR inter-runnable access functions

Data Alignment Requiring Generated Main Function

If the following conditions exist, the code generator includes data alignment directives for root-level I/O variables in the ert_main.c or ert_main.cpp file it produces. In this case, if you discard the generated example main program, align used root-level I/O variables correctly. If you choose not to generate an example main program in this case, data alignment is not supported.

  • Compiler supports global variable alignment

  • Generate an example main program (select Configuration Parameters > Generate an example main program)

  • Generate a reusable function interface for the model (set Configuration Parameters > Code Generation > Interface > Code interface packaging to Reusable function)

  • Function uses root-level I/O variables that are passed in as individual arguments (set Configuration Parameters > Code Generation > Interface > Pass root-level I/O asto Individual arguments)

  • Function uses a root-level I/O variable

  • Function imposes alignment requirements

Code Replacement Limitations

If a replacement imposes alignment requirements on the shared utility interface arguments, the code generator does not honor data alignment. Under these conditions, replacement does not occur. Replacement is allowed if the registered data alignment type specification supports alignment of local variables, and the replacement involves only local variables.

Bus Object Limitations

For Simulink.Bus objects:

  • If user registered alignment specifications do not support structure field alignment, aligning Simulink.Bus objects is not supported unless the Simulink.Bus object is imported.

  • When you align a Simulink.Bus data object, the elements in the bus object are aligned on the same boundary. The boundary is the lowest common multiple of the alignment requirements for each individual bus element.

Model Reference Limitations

When you specify alignment for functions that occur in a model reference hierarchy, and multiple models in the hierarchy operate on the same function data, the bottommost model dictates alignment for the rest of the hierarchy. If the alignment requirement for a function in a model higher in the hierarchy cannot be honored due to the alignment set by a model lower in the hierarchy, data alignment is not supported. To work around this issue, if the shared data is represented by a bus or signal object, manually set the alignment property on the shared data by setting the alignment property of the Simulink.Bus or Simulink.Signal object.

Storage Class Limitations

Data that is associated with storage classes supports alignment only under certain conditions. This table shows the built-in storage classes that support data alignment and the required conditions.

Storage ClassConditions
DefaultNone
ExportedGlobalNone
ImportedExternSpecify the data alignment by using a data object. Specification by using a code replacement entry is not supported.
ImportedExternPointerSpecify the data alignment by using a data object. Specification by using a code replacement entry is not supported.
VolatileNone
ConstNone
ConstVolatileNone
ExportToFileNone
ImportedFromFileSpecify the data alignment by using a data object. Specification by using a code replacement entry is not supported.
LocalizableNot supported for root input ports
GetSetNot supported
MultiInstanceNone
FileScopeNot supported when used from an Embedded Coder Dictionary
StructNone
ReusableNone
BitfieldNot supported
DefineNot supported
ImportedDefineSpecify the data alignment by using a data object. Specification by using a code replacement entry is not supported.
CompilerFlagSpecify the data alignment by using a data object. Specification by using a code replacement entry is not supported.

For storage classes that you create by using the Embedded Coder Dictionary or the Custom Storage Class Designer, data alignment is not supported if the storage class has:

  • Data Access set to Pointer or Function

  • Data Scope set to Exported and:

    • Data Initialization set to Macro

    • Bit-Pack Boolean selected (available in Custom Storage Class Designer only)

    • Type set to Other (available in Custom Storage Class Designer only)

Data Alignment Limitations Removed

These capabilities remove data alignment limitations.

  • In R2026b: PIL simulations support data alignment.

See Also

Topics