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)?
XC #pragma unsafe arrays Directive
-
- Respected Member
- Posts: 283
- Joined: Fri Mar 19, 2010 4:49 am
-
- XCore Addict
- Posts: 165
- Joined: Wed Feb 10, 2010 2:32 pm
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.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.
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
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.
-
- Member++
- Posts: 29
- Joined: Sat Jul 08, 2023 5:15 am
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.