Main Content

Troubleshoot Code Replacement Misses

Use miss reason messages that appear in the Code Replacement Viewer to analyze and correct code replacement misses.

Miss Reason Messages

The Code Replacement Viewer displays miss reason messages in trace information for code replacement misses. A legend listing each message that appears in the miss report precedes the report details. A message consists of:

  • Numeric identifier, which identifies the message in the report details.

  • Message text, which in some cases includes placeholders for names of arguments, call site object values, table entry values, and property names.

For example:

1. Mismatched data types (argument name, CSO value, table entry value)

The parenthetical information represents placeholders for actual values that appear in the report details.

In the Miss Source Locations table that lists the miss details, the Reason column includes:

  • The message identifier, as listed in the legend.

  • The placeholder values for that instance of the miss reason message.

The following Reason details indicate a data type mismatch because the call site object specifies data type int8 for arguments y1, u1, and u2, while the code replacement table entry specifies uint32.

1. y1, int8, uint32
   u1, int8, uint32
   u2, int8, uint32

Depending on your situation and the reported miss reason, troubleshoot reported misses by looking for instances of the following:

  • A typo in the code replacement table entry definition or a source parameter setting.

  • Information missing from the code replacement table entry or a source parameter setting.

  • Invalid or incorrect information in the code replacement table entry definition or a source parameter setting.

  • Arguments incorrectly ordered in the code replacement table entry definition or the source being replaced with replacement code.

  • Failed algorithm classification for an addition or subtraction operation due to:

    • An ideal accumulator not being calculated because the type of an input argument is not fixed-point or the slope adjustment factors of the input arguments are not equal.

    • Input or output casts with a floating-point cast type.

    • Input or output casts with cast types that have different slope adjustment factors or biases.

    • Output casts not being convertible to a single output cast.

    • Input casts resulting in loss of bits.

Analyze and Correct Code Replacement Misses

The following example shows how to use Code Replacement Viewer trace information to troubleshoot code replacement misses. For this example use the code generated for block Sub32 in the model CRLAdditionSubtraction.

  1. Open and build the model. In the MATLAB® Command Window enter:

    openExample("CRLAdditionSubtraction");
    set_param(bdroot,GenerateReport=true);
    slbuild(bdroot);

  2. Review the generated code of block Sub32 and look for code replacements. Right-click the block and select C/C++ Code > Navigate to C/C++ Code.

    block Sub32 in model CRLAdditionSubtraction is highlighted with the menu Navigate To C/C++ Code selected

    The Code Generation Report opens to the location of the generated code for that block.

    Note that the code generator replaced code. But the replacement was for the signed version of the 32-bit subtraction operation, while you expected it to be for the unsigned version.

  3. Open the Code Replacement Viewer. In the report, in the Content pane in the left, click Code Replacement Report. Then, in the right pane, click the link to open the Code Replacement Viewer.

    Report with Code Replacement Report selected and cursor points to the link of the Code Replacement Viewer

  4. In the Code Replacement Viewer:

    1. In the left pane select the crl_table_addsub replacement entry.

    2. In the middle pane select RTW_OP_MINUS with implementation function u32_sub_u32_u32.

    3. In the right pane select the Trace Information tab.

    The Trace Information is a table that lists the following information for each miss:

    • Call site object preview. The call site object is the conceptual representation of a subtraction operator. The code generator uses this object to query the code replacement library for a match.

    • A link to the source location in the model for which the code generator considered replacing code.

    • The reasons that the miss occurred. For the list of reasons that misses occur, see Miss Reason Messages.

    In this example, the report shows misses for two blocks: Sub32 and Sub8.

  5. According to the information provided in the Trace Information table, the miss reason for the Sub32 block was type mismatch. The data type in the call site object is signed 32-bit integer for all three arguments, while the code replacement entry specifies unsigned 32-bit integer for the arguments.

    Correct the model:

    1. In the right pane of the trace report, click the link in the Source Location of the Sub32 block. The model opens with the Sub32 block highlighted.

    2. Right-click the block and select Block Parameters.

      Sub32 block highlighted with right-click menu open and cursor is on the Block Parameters option

    3. In the dialog that opens, select the Signal Attributes tab. Then, in the Output data type edit box, change the value from int32 to uint32, then click the OK button.

      Changing the output data type in the Signal Attributes dialog from int32 to uint32

    4. Right-click input port 9 and select Block Parameters. In the dialog that opens, select the Signal Attributes tab. Then, in the Data type edit box, change the value from int32 to uint32, then click the OK button.

    5. Change the Data type of input port 10 from int32 to uint32 in the same way.

  6. Regenerate the code. In the MATLAB Command Window enter:

    slbuild(bdroot);

  7. Reopen the Code Replacement Viewer trace information to verify that Sub32 block is no longer listed as containing a miss. In the following display, the trace information shows a hit for block Sub32.

Related Topics