Main Content

Improve Code Efficiency and Integration of Inlined S-Functions

The Target Language Compiler (TLC) block interface is a key component of the Simulink® code generation infrastructure. It is a set of functions and records that are used by the code generation implementation of S-Function blocks. You can generate code for S-Function blocks by using .tlc files. This top-level diagram shows how the TLC block interface fits in the code generation process. For detailed information on the translation of block diagrams into high-quality code, refer to Target Language Compiler Basics.

The image displays the comprehensive overview of transforming block diagrams into high quality code.

The TLC block interface is integrated into the back end of Simulink Coder™. Being positioned at the back end delays its execution, introducing several constraints. Its effectiveness is also limited by the fact that many of the Simulink Coder optimization transforms are carried out in the middle end.

Enhanced TLC Block Interface

The enhanced TLC block interface introduces S-Function block TLC implementation in the Simulink Coder Optimization Stage. This infrastructure change enables the S-Function block code to integrate more effectively into the generated code of the model, resulting in improved code efficiency with enhanced optimization and customization capabilities.

This illustration shows the Simulink Coder architecture with (a) the TLC block interface and (b) the enhanced TLC block interface.

The image illustrates the Simulink Coder architecture in the TLC block interface and the enhanced TLC block interface

The compatibility of the S-Function block with the Simulink Coder Optimization Stage must be specified by the .tlc file. This enhanced TLC block interface offers these benefits:

  • Improved buffer allocation for the inputs and outputs of S-Function blocks in the generated code

  • Improved inlining of the code generated for the S-Function block

  • Reuse of local variables used in for-loop iterations

  • A more explicit set of cross-release compatible TLC library functions

The enhanced TLC block interface makes use of optimizations, including better loop generation and improved buffer allocation for S-Function blocks with TLC implementation inside of Function-Call Subsystem blocks and for Simulink Functions blocks inside Stateflow®.

Enable the Enhanced TLC Block Interface

To enable the enhanced TLC block interface, make the necessary changes to the block and the target TLC files.

  • Block TLC files

    Call LibEnableBlockFcnOptimizations from BlockInstanceSetup (see Block Target File Methods) to enable the enhanced TLC block interface for an S-Function.

    %function BlockInstanceSetup(block, system) void
    %<LibEnableBlockFcnOptimizations(block)>
    %endfunction

  • Target TLC files

    Check if the target includes codegenentry.tlc.

    • Case a – If the target includes codegenentry.tlc:

      Define the global variable PreCodeGenExecCompliant, as in this example from rtw/c/tlc/grt/grt.tlc.

      %assign TargetRegistSynchroOp = 1
      %assign PreCodeGenExecCompliant = 1
      
      %include "codegenentry.tlc"

    • Case b – If the target does not include codegenentry.tlc:

      Include code to switch between the TLC program executed before and after the Simulink Coder Optimization Stage. This example shows the contents of a target TLC file without codegenentry.tlc.

      %%% START global TLC variable declaration
      ...
      %%% END global TLC variable declaration
      %%% START custom TLC target code
      ...
      %%% END custom TLC target code

      In this code, you can observe the modifications required to support the execution of the target TLC file prior to the Simulink Coder Optimization Stage as is shown in the example from rtw/c/rsim/rsim.tlc.

      %%% START global TLC variable declaration
      ...
      %%% END global TLC variable declaration
      
      %assign PreCodeGenExecCompliant = 1
      
      %if EXISTS("::CompiledModel")
      %include "codegenentrylib.tlc"
      %if SLibIsPreCodeGenPhase()
      %include "codegenentry.tlc"
      %else
      
      %%% START custom TLC target code
      ...
      %%% END custom TLC target code
      
      %endif %% SLibIsPreCodeGenPhase()
      %endif %% EXISTS("::CompiledModel")

Supported Functions

Currently, only the functions in these TLC folders are supported.

FolderDescription
rtw/c/tlc/public_apiDocumented (public) functions
rtw/c/tlc/private_apiUndocumented functions

Use only listed functions from the public_api folder in custom TLC code, or functions with a Lib prefix from the private_api folder, as these functions are scheduled to be released and documented. Avoid using functions with the SLib prefix, as they are not cross-release compatible.

Debugging and Error Handling

The enhanced TLC block interface includes support for debugging options such as the TLC debugger, TLC coverage, TLC assertion and TLC profiler; for TLC error handling; and for retaining the generated model.rtw file through the use of the Retain .rtw file configuration parameter.

The TLC block analysis phase, in the Simulink Coder Optimization Stage, functions as a separate TLC process. By enabling the specified TLC debugging options, you can independently debug the block TLC code that is executed within the TLC block analysis phase. In the Configuration Parameters dialog box, enable these parameters in the Code Generation pane under Advanced parameters:

  • Start TLC coverage when generating code – Track code execution frequency and combine coverage data from the TLC block analysis phase with the target TLC in a single report. This report provides a comprehensive view of code test coverage.

  • Retain .rtw file – Create a new file modelName_reduced.rtw containing metadata for the TLC block analysis phase while retaining the file modelName_reduced.rtw.

  • Profile TLC – Collect timing statistics for TLC code and produce the files modelName_frontend.html for block analysis and modelName.html for target TLC.

  • Start TLC debugger when generating code – Set breakpoints, inspect variables, navigate TLC code, and correct errors in the generated code without disrupting the workflow.

  • Enable TLC assertion – Halt the build process upon failed conditions within TLC code and produce a TLC stack trace that identifies errors within .tlc files.

Related Topics