XC #pragma unsafe arrays Directive

Technical questions regarding the XTC tools and programming with XMOS.
bearcat
Respected Member
Posts: 283
Joined: Fri Mar 19, 2010 4:49 am

XC #pragma unsafe arrays Directive

Post by bearcat »

I noticed that newer code from XMOS no longer uses this directive. I never really understood what the directive did in the first place.

So, is there a need for this directive with the lastest tools (presumably to increase debugged code speed)?
User avatar
Woody
XCore Addict
Posts: 165
Joined: Wed Feb 10, 2010 2:32 pm

Post by Woody »

This directive is still in use, however some improvements in the compiler mean that for certain circumstances the directive is no longer required.

A quick reminder of what this directive does. Compiled XC code often performs checks that when you access an array the index is within the boundary of the array and the code is not trying to access a location outside of this. E.g.

Code: Select all

int data[3];
nextData = data[4]; // This causes an array bounds error 
If the compiler hasn't worked out that the index will always be valid it will perform this check. However these array bounds checks take time to execute. If you are convinced that the index will always be valid you can prevent the array bounds check with the 'unsafe arrays' directive. This can cause loops with critical timing to execute fast enough to meet the required timing.

So what has changed is that the compiler has got better at working out if it needs to do these checks, but you may still want to consider it's use if you are failing timing.
User avatar
lukehatpadl
Member++
Posts: 25
Joined: Sat Jul 08, 2023 5:15 am

Post by lukehatpadl »

I ran into this yesterday. Hand-unrolling a loop met the timing constraint, but #pragma loop unroll did not (even though bounds were known at compile time). Looking at the assembly, appeared to be a bounds check; #pragma unsafe arrays fixed it. Surprised the compiler didn't elide it itself but, at least there's a way to turn it off.