Optimiser Issue?

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
RedDave
Experienced Member
Posts: 77
Joined: Fri Oct 05, 2018 4:26 pm

Optimiser Issue?

Post by RedDave »

I have a structure that is filled by one task which notifies another with a ready() call, which in turn calls get_data() to copy that structure.
This structure then gets copied into a section of memory in order to be transmitted as a UDP packet.
In addition to the structure there is a preceding message code and a CRC byte added.

I think the optimiser is stopping the entire structure from being copied.

Here is some notified code that demonstrates the problem.

Code: Select all

        case i_sync.ready():
            sync_data sync_pulse = i_sync.get_data();
            memcpy(&sync_msg[1], &sync_pulse, 5);
            printf("A: ");
            for(int i=0; i<sizeof(sync_msg); i++)
                printf("%02X ", sync_msg[i]);
            printf("\n");
            memset(sync_msg, 0x03, sizeof(sync_msg));
            //printf("-- %d\n", (int)sync_pulse.count);
            break;
The structure is trivial.

Code: Select all

typedef struct {
    unsigned int period;
    unsigned short count;
} sync_data;
If I run the code above, but with the printf line uncommented, I get the expected result.
The notifying task is printing SYNC: <count value>, which can be seen in position [5] of the array.

Code: Select all

SYNC: 5
A: 03 2A 42 0F 00 05 03 
-- 5
SYNC: 6
A: 03 2A 42 0F 00 06 03 
-- 6
SYNC: 7
A: 03 2A 42 0F 00 07 03 
-- 7
SYNC: 8
A: 03 2A 42 0F 00 08 03 
-- 8
SYNC: 9
A: 03 2A 42 0F 00 09 03 
-- 9
SYNC: 10
A: 03 2A 42 0F 00 0A 03 
-- 10
SYNC: 11
A: 03 2A 42 0F 00 0B 03 
-- 11
SYNC: 12
A: 03 2A 42 0F 00 0C 03 
-- 12
However, with the printf commented out, and so no reference to count, it seems that the memcpy of that part of the structure is optimised out in some way. The 03 from the previous memset is not replaced.

Code: Select all

SYNC: 5
A: 03 2A 42 0F 00 03 03 
SYNC: 6
A: 03 2A 42 0F 00 03 03 
SYNC: 7
A: 03 2A 42 0F 00 03 03 
SYNC: 8
A: 03 2A 42 0F 00 03 03 
SYNC: 9
A: 03 2A 42 0F 00 03 03 
SYNC: 10
A: 03 2A 42 0F 00 03 03 
SYNC: 11
A: 03 2A 42 0F 00 03 03 
SYNC: 12
A: 03 2A 42 0F 00 03 03
So, am I doing something stupid or is this the optimiser enbugging my code?