[EXT_EP-11456] When using --opt_level=3, for a loop, compiler may emit assignment to member of a struct in the wrong place Created: 14/Sep/23  Updated: 09/May/24  Resolved: 11/Oct/23

Status: Fixed
Project: Embedded Software & Tools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Medium
Reporter: TI User Assignee: TI User
Resolution: Fixed Votes: 0
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Product: Code Generation Tools
Internal ID: CODEGEN-11523
Forum URL: https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1270340/c6000-cgt-loop-optimization-bug-on-8-3-10
Found In Release: C6000_8.3.0
MSP430_20.2.0.LTS
C7000_2.1.0.LTS
C2000_22.6.0.B1
PRU_2.3.0
C7000_3.1.0.LTS
MSP430_21.6.0.B1
Fix In Release: MSP430_20.2.8.LTS*
C7000_2.1.3.LTS*
MSP430_21.6.2.LTS*
C7000_3.1.1.LTS
C2000_22.6.2.LTS*
C6000_8.3.13
C2000_NEXT*
PRU_2.3.4*
C7000_4.0.0.STS
C2000_21.6.2.LTS*
Affected Platform/Device: default
Workaround: To fully avoid this behavior, an optimization level lower than -o2 may be used.

To avoid this behavior in specifically affected code, inserting `__asm(“ NOP”);` between the reads and/or writes of the struct member will prevent reordering the reads and/or writes. For example:
my_struct.member = ...;
__asm(“ NOP”);
... = my_struct.member;

 Description   

This attached code contains these lines ...

    QualityAndStatus& qualityAndStatus = outResults.qualityAndStatus[idx];

    qualityAndStatus.quality = 1.0F;
    qualityAndStatus.status = getResultCode(qualityAndStatus.quality, 0);

outResults is a reference to a structure defined from the caller. The value of the first parameter seen in getResultCode is 0.0F, not 1.0F.

Build it ...

$ cl6x -@options.txt -o3 file.cpp main.cpp -z -o test.out -l lnk.cmd
[file.cpp]
[main.cpp]
<Linking>

(Note lnk.cmd is from the \lib directory of the compiler installation.)

When executed see ...

Result 1 (expect 0): 1
Result 2 (expect 0): 1

Build it again with no optimization (remove -o3) and execute it to see ...

Result 1 (expect 0): 0
Result 2 (expect 0): 0

Generated at Wed Apr 09 08:00:06 CDT 2025 using Jira 9.12.17#9120017-sha1:aba4002bcd633f188b6a4bb5dd8a0e1f20b79ee4.