Out of memory when compiling a function with many goto labels (50k > )

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi, all

gcc 4.6.3 compiler(running on top of ubuntu 12.04 32-bit) failed to output an
object for a function when it comes with extremely large number of goto
labels.

Attached is a simple script that generates the test source file. Here's how to
reproduce a failure with the script.

~$./goto_gen.py 60000 t.c

~$ time cc -O1 -o t t.c

cc1: out of memory allocating 4064 bytes after a total of 2309808128 bytes

real	11m44.371s
user	11m42.592s
sys	0m1.876s

~$ time cc -O0 -o t t.c

real	22m5.106s
user	22m3.539s
sys	0m1.640s


As you can see from the above, -O1 option trigger the problem while -O0
doesn't.

Could you anyone tell me how to workaround this situation? Or which specific
optimization option causes the issue?

Thanks a lot for your help in advance.

Regards, Kangkook

=== goto_gen.py ===


#!/usr/bin/env python
import sys
from random import shuffle

funcHeader = """
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define MAX_VISIT 0xffffff
#define NUM_LABEL %d

void *labels[NUM_LABEL];
uint32_t setCnt = 0, visitCnt = 0;

void goto_test() {
  uint32_t idx;
"""

funcBody = """
  LBL_%d:
  if (!labels[%d]) {
    labels[%d] = && LBL_%d;
    setCnt++;
  }

    visitCnt ++;
    if (visitCnt >= MAX_VISIT)
      return;

  idx = random() %% NUM_LABEL;
  if (labels[idx] == 0) {
    goto LBL_%d;
  } else {
    goto *labels[idx];
  }
"""

funcFooter = """
}

int main(int argc, char* argv[]) {
  srandom(0);
  memset(labels, 0, NUM_LABEL * sizeof(void *));

  goto_test();
  return setCnt;
}
"""


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print >> sys.stderr, "Usage: %s #-labels c-source-name" % sys.argv[0]
        sys.exit(-1)

    numLabel = int(sys.argv[1])
    targetLst = range(numLabel)
    shuffle(targetLst)

    with file(sys.argv[2], "w") as gotoFile:
        print >> gotoFile, funcHeader % numLabel
        for num in range(numLabel):
            print num
            print >> gotoFile, funcBody % \
            (num, num, num, num, targetLst.pop())
        else:
            print "DBG:", num

        print >> gotoFile, funcFooter




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux