Please note as of Wednesday, August 15th, 2018 this wiki has been set to read only. If you are a TI Employee and require Edit ability please contact x0211426 from the company directory.

Restrict Inlining Problem

From Texas Instruments Wiki
Jump to: navigation, search

Description of the Restrict Inlining Problem

A loop which contains an inlined function call and also makes memory accesses may not achieve the expected performance, even if the appropriate pointers are declared "restrict." The reason is that the C standard defines "restrict" to operate within a single lexical scope, and each function body exists in a separate lexical scope. As a result, the effect of "restrict" in the caller does not extend to memory accesses in the called function (inlined or not) and the compiler cannot exploit it to know that accesses in the caller do not overlap (alias) accesses in the called function.

Example

In the code below, the compiler will be unable to determine if "testArray" and "out" overlap in memory. This is because the C standard specifies specific scoping rules for restrict and so the restrict qualifier on "out" does not apply in funcA. Therefore, the compiler cannot know that "testArray" and "out" do not alias (overlap) and so the software pipelining performance suffers.

  extern int * testArray;

  static __inline int funcA(int i)
  {
    return testArray[i*i];
  }

  void testFunc(int * restrict out, int thresh)
  {
    int i;
    for(i=0; i<100; i++)
    {
      if( funcA(i) > thresh )
      {
        out[0] = i;
        out[1] = i*i;
      }
    }
    return;
  }

Possible Workarounds

Use a Macro

Turn the inlined function ("funcA" in our example above) into a macro. "funcA" and "out" will appear in the same scope and so the compiler can use the "restrictness" of out to determine that "funcA" does not alias out.

In the example above, the programmer could turn funcA into:

 #define funcA(i) testArray[i*i]

Hand-inline the function

The programmer could hand-inline the function. "funcA" and "out" will appear in the same scope and so the compiler can use the "restrictness" of out to determine that "funcA" does not alias out.

Historical Notes

In the 6.0.x version of the C6000 compiler, certain aspects of the restrict scoping rules were incorrectly implemented. This means that in some situations, incorrect code would be produced by the compiler when restrict was used. In the 6.1.x and later versions of the compiler, the restrict scoping rules are properly implemented. When users move from 6.0.x to 6.1.x or later, users may see decreased performance because of this issue with the restrict keyword and inlined functions.

References

Restrict Type Qualifier