Code replacement is a technique to change the code that the code generator produces for functions and operators to meet application code requirements. For example, you can replace generated code to meet requirements such as:
Optimization for a specific run-time environment, including, but not limited to, specific target hardware.
Integration with existing application code.
Compliance with a standard, such as AUTOSAR.
Modification of code behavior, such as enabling or disabling nonfinite or inline support.
Application- or project-specific code requirements, such as:
Elimination of math.h
.
Elimination of system header files.
Elimination of calls to memcpy
or memset
.
Use of BLAS.
Use of a specific BLAS.
To apply this technique, configure the code generator to apply a code replacement library (CRL) during code generation. By default, the code generator does not apply a code replacement library. You can choose from the following libraries that MathWorks® provides:
GNU C99 extensions—GNU®[1]
gcc math library, which provides C99 extensions as defined
by compiler option -std=gnu99
.
AUTOSAR 4.0—Produces code that more closely aligns with the AUTOSAR standard. Requires an Embedded Coder® license.
Intel IPP for x86-64 (Windows)—Generates calls to the Intel® Performance Primitives (IPP) library for the x86-64 Windows® platform.
Intel IPP/SSE for x86-64 (Windows)—Generates calls to the IPP and Streaming SIMD Extensions (SSE) libraries for the x86-64 Windows platform.
Intel IPP for x86-64 (Windows using MinGW compiler)—Generates calls to the IPP library for the x86-64 Windows platform and MinGW compiler.
Intel IPP/SSE for x86-64 (Windows using MinGW compiler)—Generates calls to the IPP and SSE libraries for the x86-64 Windows platform and MinGW compiler.
Intel IPP for x86/Pentium (Windows)—Generates calls to the IPP library for the x86/Pentium Windows platform.
Intel IPP/SSE for x86/Pentium (Windows)—Generates calls to the Intel Performance IPP and SSE libraries for the x86/Pentium Windows platform.
Intel IPP for x86-64 (Linux)—Generates calls to the IPP library for the x86-64 Linux® platform.
Intel IPP/SSE with GNU99 extensions for x86-64 (Linux)—Generates calls to the GNU libraries for IPP and SSE, with GNU C99 extensions, for the x86-64 Linux platform.
Libraries that include GNU99 extensions are intended for use with the GCC compiler. If use one of those libraries with another compiler, generated code might not compile.
Depending on the product licenses that you have, other libraries might be available . If you have an Embedded Coder license, you can view and choose from other libraries and you can create custom code replacement libraries.
A code replacement library consists of one or more code replacement tables that specify application-specific implementations of functions and operators. For example, a library for a specific embedded processor specifies function and operator replacements that optimize generated code for that processor.
A code replacement table contains one or more code replacement entries, with each entry representing a potential replacement for a function or operator. Each entry maps a conceptual representation of a function or operator to an implementation representation and priority.
Table Entry Component | Description |
---|---|
Conceptual representation | Identifies the table entry and contains match criteria for the code generator. Consists of:
|
Implementation representation | Specifies replacement code. Consists of:
|
Priority | Defines the entry priority relative to other entries in the table. The value can range from 0 to 100, with 0 being the highest priority. If multiple entries have the same priority, the code generator uses the first match with that priority. |
When the code generator looks for a match in a code replacement library, it creates and populates a call site object with the function or operator conceptual representation. If a match exists, the code generator uses the matched code replacement entry populated with the implementation representation and uses it to generate code.
The code generator searches the tables in a code replacement library for a match in the order that the tables appear in the library. If the code generator finds multiple matches within a table, the priority determines the match. The code generator uses a higher-priority entry over a similar entry with a lower priority.
Term | Definition |
---|---|
Cache hit | A code replacement entry for a function or operator, defined in the specified code replacement library, for which the code generator finds a match. |
Cache miss | A conceptual representation of a function or operator for which the code generator does not find a match. |
Call site object | Conceptual representation of a function or operator that the code generator uses when it encounters a call site for a function or operator. The code generator uses the object to query the code replacement library for a conceptual representation match. If a match exists, the code generator returns a code replacement object, fully populated with the conceptual representation, implementation representation, and priority, and uses that object to generate replacement code. |
Code replacement library | One or more code replacement tables that specify application-specific implementations of functions and operators. When configured to use a code replacement library, the code generator uses criteria defined in the library to search for matches. If a match is found, the code generator replaces code that it generates by default with application-specific code defined in the library. |
Code replacement table | One or more code replacement table entries. Provides a way to group related or shared entries for use in different libraries. |
Code replacement entry | Represents a potential replacement for a function or operator. Maps a conceptual representation of a function or operator to an implementation representation and priority. |
Conceptual argument | Represents an input or output argument for a function or operator
being replaced. Conceptual arguments observe naming conventions ('y1' , 'u1' , 'u2' ,
...) and data types familiar to the code generator. |
Conceptual representation | Represents match criteria that the code generator uses to qualify functions and operators for replacement. Consists of:
|
Implementation argument | Represents an input or output argument for a C or C++ replacement function. Implementation arguments observe C/C++ name and data type specifications. |
Implementation representation | Specifies C or C++ replacement function prototype. Consists of:
|
Key | Identifies a function or operator that is being replaced. A
function name or key appears in the conceptual representation of a
code replacement entry. The key RTW_OP_ADD identifies
the addition operator. |
Priority | Defines the match priority for a code replacement entry relative to other entries, which have the same name and conceptual argument list, within a code replacement library. The priority can range from 0 to 100, with 0 being the highest priority. The default is 100. If a library provides two implementations for a function or operator, the implementation with the higher priority shadows the one with the lower priority. |
Code replacement verification — It is possible that code replacement behaves differently than you expect. For example, data types that you observe in code generator input might not match what the code generator uses as intermediate data types during an operation. Verify code replacements by examining generated code.
[1] GNU is a registered trademark of the Free Software Foundation.