nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.49k stars 1.46k forks source link

arc/orc generate invalid openMP code #15703

Open pmetras opened 3 years ago

pmetras commented 3 years ago

OpenMP code can't be compiled with --gc:arc or --gc:orc.

Example

{.passC: "-fopenmp".}
{.passL: "-fopenmp".}
{.pragma: omp, header:"omp.h".}
proc omp_get_num_threads*(): cint {.omp.}
proc omp_get_max_threads*(): cint {.omp.}
proc omp_get_thread_num*(): cint {.omp.}

proc main =
  var n: int = 0
  for i in 0 || omp_get_max_threads():
    inc(n, omp_get_thread_num())

  echo "n=" & $n
  echo "max_threads=" & $omp_get_max_threads()

main()

Current Output

$ nim c -d:openmp --stackTrace:off --gc:arc toto
Hint: used config file '.../.choosenim/toolchains/nim-1.4.0/config/nim.cfg' [Conf]
Hint: used config file '.../.choosenim/toolchains/nim-1.4.0/config/config.nims' [Conf]
....CC: stdlib_system.nim
CC: toto.nim
.../.cache/nim/toto_d/@mtoto.nim.c: In function ‘main__6Zr46bVUB8DQ9cxJjNkHcSw’:
.../.cache/nim/toto_d/@mtoto.nim.c:145:85: error: invalid branch to/from OpenMP structured block
  145 |   if (nimAddInt(n, ((NI) (T4_)), &TM__7CcE0BWqWMN0aXUZigTrnw_2)) { raiseOverflow(); goto LA1_;
      |                                                                                     ^~~~

Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3 -fopenmp   -I.../.choosenim/toolchains/nim-1.4.0/lib -I.../Development/fastparse -o .../.cache/nim/toto_d/@mtoto.nim.c.o .../.cache/nim/toto_d/@mtoto.nim.c' failed with exit code: 1

Expected Output

That the program compiles, like when using default GC.

$ nim -v
Nim Compiler Version 1.4.0 [Linux: amd64]
Compiled at 2020-10-16
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: bdcd87afca238a0a7b2c70971827cf9172817b12
active boot switches: -d:release
alaviss commented 3 years ago

Seems to be an --exceptions:goto issue. Can you try adding --exceptions:setjmp?

pmetras commented 3 years ago

I confirm that compilation with --exceptions:setjump --gc:arc is successful.

$ nim c --exceptions:setjmp --gc.arc toto
Hint: used config file '.../.choosenim/toolchains/nim-1.4.0/config/nim.cfg' [Conf]
Hint: used config file '.../.choosenim/toolchains/nim-1.4.0/config/config.nims' [Conf]
....CC: stdlib_io.nim
CC: stdlib_system.nim
CC: toto.nim

Hint:  [Link]
Hint: 22174 lines; 8.403s; 25.574MiB peakmem; Debug build; proj: .../Development/fastparse/toto; out: .../Development/fastparse/toto [SuccessX]