From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 461FE3857355; Sun, 11 Sep 2022 15:46:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 461FE3857355 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662911214; bh=lodIH3OodJAvFUuXgyoYn/xOfRY/fZNOyqybq4zbyE4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ogPVWvWtuNFgD1Uwao1A1a2i33ICZ9yQAatoBe2/rzz8ppPimxkeBD1SI5o2OgvIh OZRcRltOrDgnV35LjFdLiw/Vmn1DtyUFvMHH9jYosJGFRW9rbaWeGF6Vj8SEStHO3w lQfEn8YVF+IQukr7ACYSSRqGok5c/52TaUdo3Ebc= From: "brendandg at nyu dot edu" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/55522] -funsafe-math-optimizations is unexpectedly harmful, especially w/ -shared Date: Sun, 11 Sep 2022 15:46:53 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 4.6.3 X-Bugzilla-Keywords: documentation X-Bugzilla-Severity: major X-Bugzilla-Who: brendandg at nyu dot edu X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D55522 Brendan Dolan-Gavitt changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |brendandg at nyu dot edu --- Comment #19 from Brendan Dolan-Gavitt --- I read through the crtfastmath.c implementations for the other affected tar= gets and confirmed that they do all set flush-to-zero in this thread: https://threadreaderapp.com/thread/1567612053363347461.html I agree that there should be a way for a shared library to link crtfastmath= .o if it wants that behavior. But is there a reason -l:crtfastmath.o isn't sufficient in that case? Why does it need to be enabled automatically when -Ofast/-ffast-math/-funsafe-math optimizations are turned on? The other note I would add is that in multi-threaded applications, crtfastmath.o is already not behaving as intended: FTZ/DAZ will only be set= in the CPU state of the thread that loaded the shared library; it's hard to imagine a case where a user wants individual threads to have different FTZ/= DAZ (unless they explicitly manage that by hand). Example: $ cat baz.c #include #include #include #include void loadlib() { void *handle =3D dlopen("./gofast.so", RTLD_LAZY); if (!handle) { fprintf(stderr, "dlopen: %s\n", dlerror()); } } #define MXCSR_DAZ (1 << 6) /* Enable denormals are zero mode */ #define MXCSR_FTZ (1 << 15) /* Enable flush to zero mode */ void printftz(int i) { unsigned int mxcsr =3D __builtin_ia32_stmxcsr (); printf("[%d] mxcsr.FTZ =3D %d, mxcsr.DAZ =3D %d\n", i, !!(mxcsr & MXCSR= _FTZ), !!(mxcsr & MXCSR_DAZ)); return; } void *thread(void *arg) { // Print thread id int i =3D *(int *)arg; if (i =3D=3D 0) loadlib(); sleep(1); printftz(i); } int main(int argc, char **argv) { // Create 4 threads pthread_t threads[4]; int tids[4]; for (int i =3D 0; i < 4; i++) { tids[i] =3D i; pthread_create(&threads[i], NULL, thread, &tids[i]); } // Wait for all threads to finish for (int i =3D 0; i < 4; i++) { pthread_join(threads[i], NULL); } return 0; } $ touch gofast.c $ gcc -Ofast -fpic -shared gofast.c -o gofast.so $ gcc -pthread baz.c -o baz -ldl $ ./baz [3] mxcsr.FTZ =3D 0, mxcsr.DAZ =3D 0 [0] mxcsr.FTZ =3D 1, mxcsr.DAZ =3D 1 [2] mxcsr.FTZ =3D 0, mxcsr.DAZ =3D 0 [1] mxcsr.FTZ =3D 0, mxcsr.DAZ =3D 0=