IBM Support

IY87090: POBLEMS WITH UNIONS WHEN USING -O OR ANY OTHER HIGHER OPTIMIZATION LEVEL.

Subscribe

You can track all active APARs for this component.

 

APAR status

  • Closed as program error.

Error description

  • Statements need to be declared twice when using the -O option.
    Otherwise, an incorrect output is obtained.
    
    When compiled with xlC -O test.cpp the actual output is obtained
    only if "buffer[0] = ..." are stated twice.
    
    -------------output-------------
    
    exp: 0 1 a5 a5
    act: be ef a5 a5
    
    -------------end of output----
    
    
    
    ========Test Case===========
    
    extern "C" {
      int printf(const char *,...);
    }
    
    inline void putInt16(char *buffer, int   value) {
        typedef const union {
            int d_variable;
            char d_bytes[1];
        }& T;
    
        buffer[0] = T(value).d_bytes[sizeof value - 2];
        buffer[1] = T(value).d_bytes[sizeof value - 1];
    
        buffer[0] = T(value).d_bytes[sizeof value - 2];
        buffer[1] = T(value).d_bytes[sizeof value - 1];
    
    }
    
    
    int main() {
      const unsigned short unsignedNumber = 1;
    
      for (int di = 0; di < 1; ++di) {
        char buffer[4] = {0xa5,0xa5,0xa5,0xa5};
        putInt16(buffer , unsignedNumber);
        printf("act: %01x %01x %01x %01x\n", buffer[0], buffer[1],
    buffer[2], buffer[3]);
      }
      return 0;
    }
    
    =========End of Test case =========
    

Local fix

  • Modify the union and variable T such that T.d_variable holds the
    value of 'value'.
    
    for example:
    
     union{
            int d_variable;
            char d_bytes[1];
        }T;
    
        T.d_variable = value;
    
        buffer[0] = T.d_bytes[sizeof value - 2];
        buffer[1] = T.d_bytes[sizeof value - 1];
    

Problem summary

  • Anyone optimizing and using inlining.
    
    A function that is being inlined is taking the address of a
    parameter.  At the call site the parameter is a literal.  The
    generated code is trying to take the address of a constant.
    

Problem conclusion

  • We changed the compiler to store the literal into a
    temporary since the parameter is address taken.
    

Temporary fix

Comments

APAR Information

  • APAR number

    IY87090

  • Reported component name

    XLC C++ AIX

  • Reported component ID

    5724M1200

  • Reported release

    800

  • Status

    CLOSED PER

  • PE

    NoPE

  • HIPER

    NoHIPER

  • Special Attention

    NoSpecatt

  • Submitted date

    2006-07-19

  • Closed date

    2006-08-30

  • Last modified date

    2007-09-13

Fix information

  • Fixed component name

    XLC C++ AIX

  • Fixed component ID

    5724M1200

Applicable component levels

  • R800 PSY U813114

       UP07/09/13 I 1000

[{"Line of Business":{"code":"LOB08","label":"Cognitive Systems"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"8.0"}]

Document Information

Modified date:
03 October 2021