From owner-freebsd-toolchain@freebsd.org Wed Aug 12 14:35:25 2020 Return-Path: Delivered-To: freebsd-toolchain@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 78D653A2890 for ; Wed, 12 Aug 2020 14:35:25 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mailman.nyi.freebsd.org (mailman.nyi.freebsd.org [IPv6:2610:1c1:1:606c::50:13]) by mx1.freebsd.org (Postfix) with ESMTP id 4BRXL50M7bz40Hc for ; Wed, 12 Aug 2020 14:35:25 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: by mailman.nyi.freebsd.org (Postfix) id 0C29B3A262E; Wed, 12 Aug 2020 14:35:25 +0000 (UTC) Delivered-To: toolchain@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0BF123A2824 for ; Wed, 12 Aug 2020 14:35:25 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4BRXL44tCNz40Qh; Wed, 12 Aug 2020 14:35:24 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 07CEZHZ4052528 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 12 Aug 2020 17:35:20 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 07CEZHZ4052528 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 07CEZHkb052527; Wed, 12 Aug 2020 17:35:17 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 12 Aug 2020 17:35:17 +0300 From: Konstantin Belousov To: =?utf-8?Q?T=C4=B3l?= Coosemans Cc: Gleb Popov , toolchain@freebsd.org Subject: Re: Undefined reference to __atomic_store_8 Message-ID: <20200812143517.GZ2551@kib.kiev.ua> References: <20200808133000.GC2551@kib.kiev.ua> <20200809143742.430764e7@FreeBSD.org> <20200809154312.GH2551@kib.kiev.ua> <20200811235210.41049ad1@FreeBSD.org> <20200812134158.249dc5ca@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200812134158.249dc5ca@FreeBSD.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on tom.home X-Rspamd-Queue-Id: 4BRXL44tCNz40Qh X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US] X-BeenThere: freebsd-toolchain@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Maintenance of FreeBSD's integrated toolchain List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 Aug 2020 14:35:25 -0000 On Wed, Aug 12, 2020 at 01:41:58PM +0200, Tijl Coosemans wrote: > On Wed, 12 Aug 2020 09:44:25 +0400 Gleb Popov wrote: > > On Wed, Aug 12, 2020 at 9:21 AM Gleb Popov wrote: > >> Indeed, this looks like a culprit! When compiling using first command line > >> (the long one) I get following warnings: > >> > >> /wrkdirs/usr/ports/lang/ghc/work/ghc-8.10.1/libraries/ghc-prim/cbits/atomic.c:369:10: > >> warning: misaligned atomic operation may incur significant performance > >> penalty [-Watomic-alignment] > >> return __atomic_load_n((StgWord64 *) x, __ATOMIC_SEQ_CST); > >> ^ > >> /wrkdirs/usr/ports/lang/ghc/work/ghc-8.10.1/libraries/ghc-prim/cbits/atomic.c:417:3: > >> warning: misaligned atomic operation may incur significant performance > >> penalty [-Watomic-alignment] > >> __atomic_store_n((StgWord64 *) x, (StgWord64) val, __ATOMIC_SEQ_CST); > >> ^ > >> 2 warnings generated. > >> > >> I guess this basically means "I'm emitting a call there". So, what's the > >> correct fix in this case? > > > > I just noticed that Clang emits these warnings (and the call instruction) > > only for functions handling StgWord64 type. For the same code with > > StgWord32, like > > > > StgWord > > hs_atomicread32(StgWord x) > > { > > #if HAVE_C11_ATOMICS > > return __atomic_load_n((StgWord32 *) x, __ATOMIC_SEQ_CST); > > #else > > return __sync_add_and_fetch((StgWord32 *) x, 0); > > #endif > > } > > > > no warning is emitted as well as no call. > > > > How does clang infer alignment in these cases? What's so special about > > StgWord64? > > StgWord64 is uint64_t which is unsigned long long which is 4 byte > aligned on i386. Clang wants 8 byte alignment to use the fildll > instruction. This all is very strange. How could code use fildll to load 8 bytes as bit-value ? FILDLL converts single and double precision fp into long-double fp, so it would change the bit-value. Also, both ISA and x86 psABI only require 4-byte alignment for the double precision fp variables. If the variable memory spans over two cache lines, then SDM states that the access can be not atomic, but I believe it cannot happen for any existing CPU. It might be slow. For some future CPUs, Intel provides control which would cause such accesses to trap. > > You could change the definition of the StgWord64 type to look like: > > typedef uint64_t StgWord64 __attribute__((aligned(8))); > > But this only works if all calls to hs_atomicread64 pass a StgWord64 > as argument and not some other 64 bit value. > > > Another solution I already mentioned in a previous message: replace > HAVE_C11_ATOMICS with 0 in hs_atomicread64 so it uses > __sync_add_and_fetch instead of __atomic_load_n. That uses the > cmpxchg8b instruction which doesn't care about alignment. It's much > slower but I guess 64 bit atomic loads are rare enough that this > doesn't matter much.