public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug jit/103562] New: Jitted code produces incorrect result when returning 3-member struct from internal function
@ 2021-12-05  4:24 andy.pj.hanson at gmail dot com
  2021-12-06 13:46 ` [Bug jit/103562] " marxin at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: andy.pj.hanson at gmail dot com @ 2021-12-05  4:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103562

            Bug ID: 103562
           Summary: Jitted code produces incorrect result when returning
                    3-member struct from internal function
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: jit
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: andy.pj.hanson at gmail dot com
  Target Milestone: ---

Created attachment 51927
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51927&action=edit
File that reproduces the bug

See the attached program `jit_error.c`. To reproduce, run: `gcc jit_error.c
-lgccjit && ./a.out`. This will output: `get_a(&s) is 140730936729392` (or some
other large number); The correct output would be 1.

The file should JIT a program like this:
```
struct my_struct { long a; long b; long c; };
struct my_struct deref(struct my_struct *ptr) { return *ptr; } 
long get_a(struct my_struct *s) { return deref(s).a; }
```

For some reason, the function `deref` is optimized in an invalid way.

The bug goes away (and the program correctly outputs 1) if you do any one of
the following:

* Set GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL to 0.
* Remove the `c` field.
* Change `func_deref` from `GCC_JIT_FUNCTION_INTERNAL` to
`GCC_JIT_FUNCTION_EXPORTED`.
* Inline the call using `gcc_jit_rvalue *callDeref =
gcc_jit_lvalue_as_rvalue(gcc_jit_rvalue_dereference(gcc_jit_param_as_rvalue(param_get_a),
NULL));`.
* Use `GCC_JIT_TYPE_INT` instead of `GCC_JIT_TYPE_LONG`.

I've tested this the GCC master branch as of 20211204, and with OpenSUSE's GCC
version.

My `gcc -v` is:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/andy/temp/gccjitrepro2/install/libexec/gcc/x86_64-pc-linux-gnu/12.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../src/configure --enable-host-shared
--enable-languages=jit,c++ --disable-bootstrap --disable-shared
--prefix=/home/andy/temp/gccjitrepro2/install
--with-gmp=/home/andy/temp/gccjitrepro2/build-gmp
--with-isl==/home/andy/temp/gccjitrepro2/build-isl
--with-mpfr=/home/andy/temp/gccjitrepro2/build-mpfr
--with-mpc=/home/andy/temp/gccjitrepro2/build-mpc
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.0.0 20211204 (experimental) (GCC)

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-06-01 17:30 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-05  4:24 [Bug jit/103562] New: Jitted code produces incorrect result when returning 3-member struct from internal function andy.pj.hanson at gmail dot com
2021-12-06 13:46 ` [Bug jit/103562] " marxin at gcc dot gnu.org
2021-12-06 22:13 ` pinskia at gcc dot gnu.org
2021-12-09 13:25 ` marxin at gcc dot gnu.org
2021-12-10 10:15 ` marxin at gcc dot gnu.org
2021-12-10 18:13 ` dmalcolm at gcc dot gnu.org
2021-12-10 22:52 ` cvs-commit at gcc dot gnu.org
2021-12-10 22:57 ` dmalcolm at gcc dot gnu.org
2021-12-11  2:56 ` cvs-commit at gcc dot gnu.org
2024-06-01 17:30 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).