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.

Related Topics