From nobody Fri Dec 19 08:07:53 2025 Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91C358F6E for ; Sun, 28 Jan 2024 05:06:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418371; cv=none; b=S6U8U9Y3Nv46/vs2qZ9rABTDpigGp4H6X5mOcaSkvYCMw/5jeffNkNpYx77/9r1LAUhdvv7z/k9yCQS98nmDaQEyZKr/i2PUueMQKM09vDQulvjExV7UrG35NoZU3hsQi9FkLhuj0z/gJqZF5tuhF21Bzion7O6Dld3XGGxu8dY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418371; c=relaxed/simple; bh=qP1S1rXP0UMtjHp5qx9OgsIHVeo/JJwGD+kMiT1E4bU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NvTh+tmoyfWnAjp80a1gK8rfw+cSFCIsdHEM47YUhtHgf9XVzSAk1fbFJKU/FaQFP49hgaY9S9BAiCOR8fF558KuU8+S3+zRPzzSQHVrT833itiDumrHHefPuFLqXl0O0tYxXV55qgNLR0EgavW1dcFmcLhFLlXj183Qn71avas= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Rgazrkj6; arc=none smtp.client-ip=209.85.167.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Rgazrkj6" Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3bd5c4cffefso2162478b6e.1 for ; Sat, 27 Jan 2024 21:06:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706418368; x=1707023168; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=19sNIGW0pghbkIABBLpZ8u04puBnH22vL76jF4wAKJM=; b=Rgazrkj6j04Hl5jIA2RNOaZRpPUlXrsO9yjGn682flVLQnc7lcjNQpYJB237DFfKY9 ImVmQxWpJh5zxTfZ4rm/VzY03twK1cyFYwVQoYLHzEtvJ7B615HeTuu7cBDkJgZGkpts 2eId0sSAiKEA3Xx58FIfUGUs0yliTIVHJn4nc+pvHdWJDnNZ/Rkto2XyxMsIboxzFLQp nqQh12vs/icWZubTKl+gj1ZtjzSjRvAPThOQkOL3iuzrsb3Yn9BxWJ8RiLwf1p+5kx2F Mw8Gb1oszTubPBRH+eb6EAFh78v3z7YTxeh7m3kTLJGdjEddFmBDemcNXzBhF/cJfC++ 8VGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706418368; x=1707023168; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=19sNIGW0pghbkIABBLpZ8u04puBnH22vL76jF4wAKJM=; b=I0T35BB2sApoC+89+joHmF8HjQJTdOPdXJ2XRsfiLDo3trRETdXyIQ1ucSkU4UKW8s YTBhG2HItlFd/Q0WV8LWT5E5TPy8+bb08N2YQZF76jQCu5EorpVNSTtUFkn9syF9pjZW lI8klBaWPt1FQMLk76e6Pimbb2pQCkh+kCMG6T0xnIDmy11S+Rni8Ud28AJPr1vmgnAx 0SPvh52F2nb2hWwzMsx6ko5gYaam3eF1ItbDcB6qUUAHB0SmYI+ZyYEltgN+nBmXL1GI +4cTi4WLLnInFfGs2Firwv3b2h4AXdcdbMB7cg5qzA9beNMXywMn+akNXbV69PUOxf83 q1Eg== X-Gm-Message-State: AOJu0YwoCbwHYsH6MhITt7wxkym3idfFgUeuhKAKPXwKRQ5Ls2CyTF3m qNJQVIUYpIPgDxxsdydQIzOOHgRI/wyL15Jy/VNZY71XzfPwzXv9 X-Google-Smtp-Source: AGHT+IGBTtbhRceHwTa0x3eZf1sJmCivqoJmPoyAxwOiobLNypPD+byqKoFmrztSRgvKweXB+qAZ3Q== X-Received: by 2002:a05:6808:1815:b0:3be:2175:98e9 with SMTP id bh21-20020a056808181500b003be217598e9mr2912252oib.18.1706418368391; Sat, 27 Jan 2024 21:06:08 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id lp17-20020a056a003d5100b006ddd182bf1csm3550372pfb.46.2024.01.27.21.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 21:06:07 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org Cc: Yury Norov , Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v4 1/5] m68k/bitops: force inlining of all bit-find functions Date: Sun, 28 Jan 2024 14:00:07 +0900 Message-ID: <20240128050449.1332798-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The inline keyword actually does not guarantee that the compiler will inline a functions. Whenever the goal is to actually inline a function, __always_inline should always be preferred instead. __always_inline is also needed for further optimizations which will come up in a follow-up patch. Inline all the bit-find function which have a custom m68k assembly implementation, namely: __ffs(), ffs(), ffz(), __fls(), fls(). On linux v6.7 allyesconfig with GCC 13.2.1, it does not impact the final size, meaning that, overall, those function were already inlined on modern GCCs: $ size --format=3DGNU vmlinux.before vmlinux.after text data bss total filename 60457956 70953665 2288644 133700265 vmlinux.before 60457964 70953697 2288644 133700305 vmlinux.after Reference: commit 8dd5032d9c54 ("x86/asm/bitops: Force inlining of test_and= _set_bit and friends") Link: https://git.kernel.org/torvalds/c/8dd5032d9c54 Signed-off-by: Vincent Mailhol --- arch/m68k/include/asm/bitops.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/m68k/include/asm/bitops.h b/arch/m68k/include/asm/bitops.h index 14c64a6f1217..a8b23f897f24 100644 --- a/arch/m68k/include/asm/bitops.h +++ b/arch/m68k/include/asm/bitops.h @@ -465,7 +465,7 @@ static inline int find_next_bit(const unsigned long *va= ddr, int size, * ffz =3D Find First Zero in word. Undefined if no zero exists, * so code should check against ~0UL first.. */ -static inline unsigned long ffz(unsigned long word) +static __always_inline unsigned long ffz(unsigned long word) { int res; =20 @@ -488,7 +488,7 @@ static inline unsigned long ffz(unsigned long word) */ #if (defined(__mcfisaaplus__) || defined(__mcfisac__)) && \ !defined(CONFIG_M68000) -static inline unsigned long __ffs(unsigned long x) +static __always_inline unsigned long __ffs(unsigned long x) { __asm__ __volatile__ ("bitrev %0; ff1 %0" : "=3Dd" (x) @@ -496,7 +496,7 @@ static inline unsigned long __ffs(unsigned long x) return x; } =20 -static inline int ffs(int x) +static __always_inline int ffs(int x) { if (!x) return 0; @@ -518,7 +518,7 @@ static inline int ffs(int x) * the libc and compiler builtin ffs routines, therefore * differs in spirit from the above ffz (man ffs). */ -static inline int ffs(int x) +static __always_inline int ffs(int x) { int cnt; =20 @@ -528,7 +528,7 @@ static inline int ffs(int x) return 32 - cnt; } =20 -static inline unsigned long __ffs(unsigned long x) +static __always_inline unsigned long __ffs(unsigned long x) { return ffs(x) - 1; } @@ -536,7 +536,7 @@ static inline unsigned long __ffs(unsigned long x) /* * fls: find last bit set. */ -static inline int fls(unsigned int x) +static __always_inline int fls(unsigned int x) { int cnt; =20 @@ -546,7 +546,7 @@ static inline int fls(unsigned int x) return 32 - cnt; } =20 -static inline unsigned long __fls(unsigned long x) +static __always_inline unsigned long __fls(unsigned long x) { return fls(x) - 1; } --=20 2.43.0 From nobody Fri Dec 19 08:07:53 2025 Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E010D527 for ; Sun, 28 Jan 2024 05:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418374; cv=none; b=Hg0+yB07XDcohxxdGbASLYDFBNsyQXImdqAYPjx9lIvHFO73EOoXtTO1WiYjDDowAvcDMbz9QABFQOn00zyN7ZFU2AYXq0LSJS3+gHzHY76Z0ftEd8NtybuwlGkcPHrzycM8nihFkIYYcJYIxulRT1hjNbgCbY0QyrLumB13wVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418374; c=relaxed/simple; bh=ExuY3mPusE/FWbLSFzM/M9SA0b5v24qHPt/95eQI/Gg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=asnBv/nWq0niiAz7ja7ToC8bpHi5ZtaQjG63025AWwTqQ0HUEo3/wpyaAgmt5kI1V4Byzgb9N8zauO08t8QOs3uPkUKEDcg8i2dztetm45bTJ68cxwEDExY9yDo1wLVMDXSEuEfcjKPYAUxbgSAdg+jo49wkVvGjkfKhDqVboao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=A3SpdN5X; arc=none smtp.client-ip=209.85.167.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A3SpdN5X" Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-3bd5c4cffefso2162519b6e.1 for ; Sat, 27 Jan 2024 21:06:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706418372; x=1707023172; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Afm8icB/y+Dcgg++6LXAA0U3DRuhdcifD/F9kyq+8Bw=; b=A3SpdN5XK3K1mTaGdxFBRPKefIXvQFS5O8e2a6a+brw8qALDde5KG1JK/2plQ9EdHb hUIEP/8h9PYPf+cNM9to8lIOLM9nyBMNkxfZQc78UGzIbIiDycJDq2E1lvFlIY8GWLPR YGBq8jST8hNo5jphplFViUVJueaBfAkIhLEfO2Q6wYbBJFqDl+ONKlY9m1atA0TAF/om ODLPXue4UFk20DnZKcZzh/iOCOP5SQkC8GKoRRQCIoyYiIaD2CCetWG6wPEsVhEivAwu pm0qJjVSqaoufugs7ngmF8bTEohKdMM20AK82O/eH3tlpbRSbD95ujgsAKAdh5xNpU5b YXGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706418372; x=1707023172; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Afm8icB/y+Dcgg++6LXAA0U3DRuhdcifD/F9kyq+8Bw=; b=WDt4y/DrTyjf/SnadLGfjzJxmVcSWOUvumlmt1idlzMnFg9zLezfM6VwLk0hM7frFQ w0ayRj9uxnimBKinvFI8jYkKXT0oV6D1rB8bPgb2cIY8Nz/KT5WZei3gcKm0WH6x5q/u H8ZDVhUcTP4gj858yk26XyDdM5ACNmyJ4mcrWJkm11sm+EyXOeThirXZYzqKIaQSmcYk uJkne0kzlz7+nQfqZ+9KnyRRqWV61fYpwTvE8ny1aMToCPc1lS3lNWvbcxLYJXG6F8cH o7aJWq14WQctaWSZ943FuhKZwIt5leEXDxXtKj5II23fdtBG3C0CPmi+WHmni1DLHQZT pFJg== X-Gm-Message-State: AOJu0YwVh0br0zON9urQRZoCbAOBKn4+5wGedU41dc519vZZ08j5w0uG 09hOGYa30Ml2jrtW/4fPBRBAmCSO7Z49OiQmWqpP+N6GkjUXJLEg X-Google-Smtp-Source: AGHT+IFX51ILylxaSYnl18EoW6kL48m7lSuyH/v3viP4NEjjW8STalhGnaCEAi2BeLIKKaIi3ElmTQ== X-Received: by 2002:a05:6808:1189:b0:3bd:a71f:9bf0 with SMTP id j9-20020a056808118900b003bda71f9bf0mr3657759oil.39.1706418372492; Sat, 27 Jan 2024 21:06:12 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id lp17-20020a056a003d5100b006ddd182bf1csm3550372pfb.46.2024.01.27.21.06.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 21:06:11 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org Cc: Yury Norov , Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v4 2/5] m68k/bitops: use __builtin_{clz,ctzl,ffs} to evaluate constant expressions Date: Sun, 28 Jan 2024 14:00:08 +0900 Message-ID: <20240128050449.1332798-3-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler is not able to do constant folding on "asm volatile" code. Evaluate whether or not the function argument is a constant expression and if this is the case, return an equivalent builtin expression. On linux 6.7 with an allyesconfig and GCC 13.2.1, it saves roughly 11 KB. $ size --format=3DGNU vmlinux.before vmlinux.after text data bss total filename 60457964 70953697 2288644 133700305 vmlinux.before 60441196 70957057 2290724 133688977 vmlinux.after Reference: commit fdb6649ab7c1 ("x86/asm/bitops: Use __builtin_ctzl() to ev= aluate constant expressions") Link: https://git.kernel.org/torvalds/c/fdb6649ab7c1 Reviewed-by: Geert Uytterhoeven Signed-off-by: Vincent Mailhol --- arch/m68k/include/asm/bitops.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/m68k/include/asm/bitops.h b/arch/m68k/include/asm/bitops.h index a8b23f897f24..02ec8a193b96 100644 --- a/arch/m68k/include/asm/bitops.h +++ b/arch/m68k/include/asm/bitops.h @@ -469,6 +469,9 @@ static __always_inline unsigned long ffz(unsigned long = word) { int res; =20 + if (__builtin_constant_p(word)) + return __builtin_ctzl(~word); + __asm__ __volatile__ ("bfffo %1{#0,#0},%0" : "=3Dd" (res) : "d" (~word & -~word)); return res ^ 31; @@ -490,6 +493,9 @@ static __always_inline unsigned long ffz(unsigned long = word) !defined(CONFIG_M68000) static __always_inline unsigned long __ffs(unsigned long x) { + if (__builtin_constant_p(x)) + return __builtin_ctzl(x); + __asm__ __volatile__ ("bitrev %0; ff1 %0" : "=3Dd" (x) : "0" (x)); @@ -522,6 +528,9 @@ static __always_inline int ffs(int x) { int cnt; =20 + if (__builtin_constant_p(x)) + return __builtin_ffs(x); + __asm__ ("bfffo %1{#0:#0},%0" : "=3Dd" (cnt) : "dm" (x & -x)); @@ -540,6 +549,9 @@ static __always_inline int fls(unsigned int x) { int cnt; =20 + if (__builtin_constant_p(x)) + return x ? BITS_PER_TYPE(x) - __builtin_clz(x) : 0; + __asm__ ("bfffo %1{#0,#0},%0" : "=3Dd" (cnt) : "dm" (x)); --=20 2.43.0 From nobody Fri Dec 19 08:07:53 2025 Received: from mail-oo1-f53.google.com (mail-oo1-f53.google.com [209.85.161.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA2111D524 for ; Sun, 28 Jan 2024 05:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418379; cv=none; b=tFkCBcwCt/vA3YeObCegpIGOeM6Cqc7cBJ9j1mOpJcXdshzq4hhmKScdOjfeF2dgCrhsMVl50+Xn4QBNVYY19IvMi94kfSdAcqpB188XvePAfisCFsGYeEVdya8/XUrhlpnpt2vYoDskYFsdqdyl79w5ZpjwPs4nHlpA/rbZb28= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418379; c=relaxed/simple; bh=EUVQyICOpBqVWh6g3IN6x5mNM2NGmg0g/sEBs5QBZ1U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PJHXbdEcRWGVgsATfihIXtR4IcHMytJkv3Xn1fIraktH5lI5AXjnYn6QzFRoCGkxr1YybMEIUi1QFsHwBXalfbQsAuLoOmPq+p+RjHVKjr6NaQENiYqnH3N8R8sCdy9vb9uyXIaVIrztJzzB86waG8sDxMNDgJyAxcumEnlG0SY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jLuDGVyx; arc=none smtp.client-ip=209.85.161.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jLuDGVyx" Received: by mail-oo1-f53.google.com with SMTP id 006d021491bc7-5986d902ae6so1121787eaf.3 for ; Sat, 27 Jan 2024 21:06:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706418377; x=1707023177; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=GLlJCmhI5JZIPAQYj2G74sUtNrIAZ9HIcrgV/zcgKnA=; b=jLuDGVyx+/IBfX94fH9WjaYxXHjJCisAspgSf50Qno8VdqkSFBAg7oIiP12o16JXg/ j4TIGBapzuWkRHsrgGD7UplJHADGD1ZLFh/2F6Zo0dxV9DrxbdUOnww+wEqQHRaV4p+u RdJzEI34HM+dUGHCbs94WHyrxAZtgwlYCLbfjmX6XWV+YpTcppcXiXwEw2jAmetIgepL iPWEwK899Gti8sdylX8/KfHquqmImdMkOMQ8AjRQpct37zsHi5zXWBuNgGhBbH6brLGU xgIxKeHHop1G4Bp8GaGqFLRGi6/zf/NHdRelSGzmzEe746tF3qRIpKV/Ge3fO9I9FyOV /JVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706418377; x=1707023177; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=GLlJCmhI5JZIPAQYj2G74sUtNrIAZ9HIcrgV/zcgKnA=; b=fir4/zoKYP4sOgAc7TeTMrOuMRUr/+LwsAgYzhsMMrqOevq6zhKn5soRC60I9YLY1v lcN8hCvI2tR5v1gWFA3z/y71XCAuvUWyoiiPGDwF0dEDcFtCLVYyL8sc3j/svQkvljXi O/z0U7DEDx/Bs2IAA1N4H6HcI2x1cFVhiiHi4/ePo6gHjf4nzzoxhGlEBMstTAO5C+1e JNfZdjGACa69/z7GF8UQObmPQ/X6WgAGxOF8SE7hlx7RRhw8ymcbDJrbIJZnTZKOrAN3 /CL8vem2x72KkyajWyR/AUq7TiffzrO3bmJXJ+XQYOfKMROTsHD1jiqV3kWsB4qmeWfU 9vAA== X-Gm-Message-State: AOJu0YxlPzHH1t6GjVLfv7BFiyAbOfeW+9yBDG7L/jLEySnocsMl+mf7 ezA8JOuG2gehLb2XSqLHJjj+l/Tv4HQSO68g1sKdq+z+xfQSL0CI X-Google-Smtp-Source: AGHT+IHoQbPQZIJSI+buf7XdlQZCHJPSEHdvtWV2n+RU3qSWFQbxHhbebOSg/814TJZcUf51VrOmCA== X-Received: by 2002:a05:6359:4114:b0:177:2e7:cd54 with SMTP id kh20-20020a056359411400b0017702e7cd54mr3368594rwc.32.1706418376523; Sat, 27 Jan 2024 21:06:16 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id lp17-20020a056a003d5100b006ddd182bf1csm3550372pfb.46.2024.01.27.21.06.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 21:06:15 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org Cc: Yury Norov , Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v4 3/5] hexagon/bitops: force inlining of all bit-find functions Date: Sun, 28 Jan 2024 14:00:09 +0900 Message-ID: <20240128050449.1332798-4-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The inline keyword actually does not guarantee that the compiler will inline a functions. Whenever the goal is to actually inline a function, __always_inline should always be preferred instead. __always_inline is also needed for further optimizations which will come up in a follow-up patch. Inline all the bit-find function which have a custom hexagon assembly implementation, namely: __ffs(), ffs(), ffz(), __fls(), fls(). On linux v6.7 defconfig with clang 17.0.6, it does not impact the final size, meaning that, overall, those function were already inlined on modern clangs: $ size --format=3DGNU vmlinux.before vmlinux.after vmlinux.final text data bss total filename 4827900 1798340 364057 6990297 vmlinux.before 4827900 1798340 364057 6990297 vmlinux.after Reference: commit 8dd5032d9c54 ("x86/asm/bitops: Force inlining of test_and= _set_bit and friends") Link: https://git.kernel.org/torvalds/c/8dd5032d9c54 Signed-off-by: Vincent Mailhol --- arch/hexagon/include/asm/bitops.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/hexagon/include/asm/bitops.h b/arch/hexagon/include/asm/b= itops.h index 160d8f37fa1a..e856d6dbfe16 100644 --- a/arch/hexagon/include/asm/bitops.h +++ b/arch/hexagon/include/asm/bitops.h @@ -200,7 +200,7 @@ arch_test_bit_acquire(unsigned long nr, const volatile = unsigned long *addr) * * Undefined if no zero exists, so code should check against ~0UL first. */ -static inline long ffz(int x) +static __always_inline long ffz(int x) { int r; =20 @@ -217,7 +217,7 @@ static inline long ffz(int x) * This is defined the same way as ffs. * Note fls(0) =3D 0, fls(1) =3D 1, fls(0x80000000) =3D 32. */ -static inline int fls(unsigned int x) +static __always_inline int fls(unsigned int x) { int r; =20 @@ -238,7 +238,7 @@ static inline int fls(unsigned int x) * the libc and compiler builtin ffs routines, therefore * differs in spirit from the above ffz (man ffs). */ -static inline int ffs(int x) +static __always_inline int ffs(int x) { int r; =20 @@ -260,7 +260,7 @@ static inline int ffs(int x) * bits_per_long assumed to be 32 * numbering starts at 0 I think (instead of 1 like ffs) */ -static inline unsigned long __ffs(unsigned long word) +static __always_inline unsigned long __ffs(unsigned long word) { int num; =20 @@ -278,7 +278,7 @@ static inline unsigned long __ffs(unsigned long word) * Undefined if no set bit exists, so code should check against 0 first. * bits_per_long assumed to be 32 */ -static inline unsigned long __fls(unsigned long word) +static __always_inline unsigned long __fls(unsigned long word) { int num; =20 --=20 2.43.0 From nobody Fri Dec 19 08:07:53 2025 Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E67B81EB27 for ; Sun, 28 Jan 2024 05:06:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418383; cv=none; b=UNfSTETIPKMDH3C9xomKSbNjHZtIFwL45sLv89VN7qxDes82RS/ss8ZSpEoHml1vA0kROcbgW7wu7BQLCsLEu/wy7nHgIBDfGNUGOTorPAdDelrCnqEnTuR4Stl4hbQP8K0OobIu28rtYs5tXdrb2XLzr4QeFEy+xqQtC6y1i2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418383; c=relaxed/simple; bh=R1pbFLI03IlZKTp/TgcUS71vFBkjz46xTjfOd1hIJ0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XO7PV3s41/qIV6wzoHClEkHmVdhLUreOc4L42sx4cbJbt2aa7TK9FJEan0NIM+UaN/pqhnoe6UUzvK6E7bIkR0/7vhnl5uuvv4PwoTTCT0ukwgdhIjB0VlagtJcIfNY3gMD3lw/lkO4vFq8SvDAVDDcvuJcMkZXCjBcbTgKBymM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=V0wmCgX7; arc=none smtp.client-ip=209.85.161.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="V0wmCgX7" Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-59927972125so992648eaf.3 for ; Sat, 27 Jan 2024 21:06:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706418381; x=1707023181; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=FQBsNQfE0P3AMOj6B4RUDeOxeQtA43qqvQ0BuQwr7rw=; b=V0wmCgX7E5aOxkoYKhW0gCF7B0ohqszlzAy+69baGiBHa8s/hLMMSdb3WW4tEtXcG+ mJTa5/7br8eWyEBDBl8zA+qVhBCGgujAjX7DnF1DanyQew/VjLtekiCPu7CBbsHQaKCY ROX4BvdcadezTVoL8RoaEst18MiUIDMEejp96ok5gJ1C0wiz08Hy4ysR1DJLeVLfuthv IyG3RKP7sMgPL0Ug15DBiyLdlq/ltC8TUuBpYO4XL7SjAh4IOz41URZl46VyaTjPZbdO PC1fxweQV2WDwHHVtxUD1JNqDflu39rphFDIP0C4gqAtpmLbgdccdkR7CaB1QS0kflBc H4mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706418381; x=1707023181; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=FQBsNQfE0P3AMOj6B4RUDeOxeQtA43qqvQ0BuQwr7rw=; b=saeVgFsZZdqvLOzpZ1pQbrWBLvuDhmAxtqOYaWV4neUHkQqHeikGxnynBtDrTjBnwU YEcbVZBvXbVIrixCQJYgMVZ1ofmDOYaWorlOb+nicfxIRja3tidGUrUsfCqOKwuEpNyj dV3o92qbyPRijO6QGScJFPqAi4e1OvN9FeAn66iwzaLIy05/k0JCn6jZILOoEMwpsFwn TRfsOUYawy2DOJlVc1GIUQ+mkU4a9SphyzndDYg/RwzV286Nuxa5DX0UbD2/0O+rwsXx tgvGD+9tjktmW/bdpr+1htMdX5+/mSb5lwaLgMK1zR8FUPA5Ljq23lk40yaDH3GoVPJa mrxg== X-Gm-Message-State: AOJu0Yydo9b4cCx61Lf2C0aH/YMGOG8vKTLPKER6wf6geSAU32w9RSr1 yE4EU7CFl/ZaYyq6TqEhJ63Evud+zoH0OLBUqbsbN3jc9c3x/xyn X-Google-Smtp-Source: AGHT+IEG7nBxPu1DZAQ3x4bvDDnlqVoOR9wWy15hrjJ//5bm1LQ2RgGqogRvkweP/oDf95b7NCqmJQ== X-Received: by 2002:a05:6830:4106:b0:6db:b046:2189 with SMTP id w6-20020a056830410600b006dbb0462189mr3439811ott.24.1706418380989; Sat, 27 Jan 2024 21:06:20 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id lp17-20020a056a003d5100b006ddd182bf1csm3550372pfb.46.2024.01.27.21.06.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 21:06:20 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org Cc: Yury Norov , Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v4 4/5] hexagon/bitops: use __builtin_{clz,ctzl,ffs} to evaluate constant expressions Date: Sun, 28 Jan 2024 14:00:10 +0900 Message-ID: <20240128050449.1332798-5-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler is not able to do constant folding on "asm volatile" code. Evaluate whether or not the function argument is a constant expression and if this is the case, return an equivalent builtin expression. On linux 6.7 with an allyesconfig and clang 17.0.6, it saves roughly 4 KB. $ size --format=3DGNU vmlinux.before vmlinux.after text data bss total filename 4827900 1798340 364057 6990297 vmlinux.before 4827072 1795060 364057 6986189 vmlinux.after Reference: commit fdb6649ab7c1 ("x86/asm/bitops: Use __builtin_ctzl() to ev= aluate constant expressions") Link: https://git.kernel.org/torvalds/c/fdb6649ab7c1 Signed-off-by: Vincent Mailhol --- arch/hexagon/include/asm/bitops.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/hexagon/include/asm/bitops.h b/arch/hexagon/include/asm/b= itops.h index e856d6dbfe16..c74a639c84f3 100644 --- a/arch/hexagon/include/asm/bitops.h +++ b/arch/hexagon/include/asm/bitops.h @@ -204,6 +204,9 @@ static __always_inline long ffz(int x) { int r; =20 + if (__builtin_constant_p(x)) + return __builtin_ctzl(~x); + asm("%0 =3D ct1(%1);\n" : "=3D&r" (r) : "r" (x)); @@ -221,6 +224,9 @@ static __always_inline int fls(unsigned int x) { int r; =20 + if (__builtin_constant_p(x)) + return x ? BITS_PER_TYPE(x) - __builtin_clz(x) : 0; + asm("{ %0 =3D cl0(%1);}\n" "%0 =3D sub(#32,%0);\n" : "=3D&r" (r) @@ -242,6 +248,9 @@ static __always_inline int ffs(int x) { int r; =20 + if (__builtin_constant_p(x)) + return __builtin_ffs(x); + asm("{ P0 =3D cmp.eq(%1,#0); %0 =3D ct0(%1);}\n" "{ if (P0) %0 =3D #0; if (!P0) %0 =3D add(%0,#1);}\n" : "=3D&r" (r) @@ -264,6 +273,9 @@ static __always_inline unsigned long __ffs(unsigned lon= g word) { int num; =20 + if (__builtin_constant_p(word)) + return __builtin_ctzl(word); + asm("%0 =3D ct0(%1);\n" : "=3D&r" (num) : "r" (word)); @@ -282,6 +294,9 @@ static __always_inline unsigned long __fls(unsigned lon= g word) { int num; =20 + if (__builtin_constant_p(word)) + return BITS_PER_LONG - 1 - __builtin_clzl(word); + asm("%0 =3D cl0(%1);\n" "%0 =3D sub(#31,%0);\n" : "=3D&r" (num) --=20 2.43.0 From nobody Fri Dec 19 08:07:53 2025 Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0ED31208B4 for ; Sun, 28 Jan 2024 05:06:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418387; cv=none; b=tva42E15bZuSai/K9puEcuxR1ynmGKfcOAEHoXu5TQIIch+Gq5reKsZYyWouDAtrmKsxdW1nUSE7tLnBT5ooRG0chRl78VspVUaM/zs5xVFRY2Bnm+1830y2QFMLicWSMqBtV3Y/iqnr+CJMrWS+ifH3u2cENOJMR3P604p9e/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706418387; c=relaxed/simple; bh=sqYZMDlKDRUfnljor6SGKDrcquN71PTrtDOFGVVMCyI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kScuWIbZS/MDr6Y4O0+XdVqoeuTLgpowE/5frefYdC4KJUdvdSl0vx3XWRroAPDa9MXOtERHyMqJ5vu8hWFtBf1QvFTjiWylQEYkQ7QHUzNmesxcFgWXwntyAElH4gWtYOl0zJAE7IFd/GWf9dZVeI4tz5KzWXOX1GPh3mDZCYc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=grpLTiGS; arc=none smtp.client-ip=209.85.167.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="grpLTiGS" Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3bd6581bc66so1568747b6e.1 for ; Sat, 27 Jan 2024 21:06:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706418385; x=1707023185; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Klq863pFaGoDv/8+20XN12p6t4dw+6WO92EMrH2yao8=; b=grpLTiGSYbX1nqjDPil48wSclML50UBppOs5Z7oYohbns2Vj3FGn3ktoR0d8WaAZNV v3Vnl6JxI6Fc/AeRcuX/2F1ig/zaUhQW0L8/i2RSYTDOWjfKeuFeXFpIdxmiqhFdESpO /rMzCo5mJ9lKq9AefbVVNLLKiYozv1h1huUA4Z98nk9IIl8aCiEhHJneJ/K970ZqyXOR f28xmcFtivFSWuN2VXQjjWl/8QiMYGsEhgVbFQkc6W/wTtWjR1xXEwQ+2QUjzUlXbGhS T0MwsmBa7ohy13f1rm8k8FtZBp4pv5dlo0QejgnYsfyigpn7/bjxY9NqUz1Bd6asYeVa NX/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706418385; x=1707023185; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Klq863pFaGoDv/8+20XN12p6t4dw+6WO92EMrH2yao8=; b=g5ZX7o8fRysor6ySubqGX4K4JV0U7eobPpgM43JrxBb6IGDlvN+G2AMMZDXUCdMpYH ZB5lskDh+1Agzqd/yZyo+MO5ifrdtFZH9LJKD013ueu8Szvcb3JySR5TFhGFb4XeUtyV Dlk2hCAkKfZkOFEdRSPgK70PhXeRxv7hwiwtOzg1WN8XLIswkaUZhngmMjAd+9qTTOqq 26Eq2UV8iwupQJg7CGVYCL74qHAVb2RguxPPKOyFi8K9uHbdhoQvUmrt1o3UULEH/XHP ZHobPlZvbqVHiNvxJNdNYG2sUUwrLsaZUPpafz63cnRiYpx+HF9zVQLGZvMcDnuDUeO2 aesw== X-Gm-Message-State: AOJu0YybPLkV7zU8WB7KJzqeAaF7NOTAph8YT9dLSDUVeW9jfgnZqDx0 h8eTee2Eyx8Frq0Jo7WjsI205+WgKCbE28edu3PKOoxEvKaCnhH7 X-Google-Smtp-Source: AGHT+IFJKmp+bAvhbdfzCBdTHyX1Zr9Yd3+YnQ7Hr1irLdsB79h6IjRdALDmguFKws+ptaAXM7qdlQ== X-Received: by 2002:a05:6808:d49:b0:3be:3a9e:5cd6 with SMTP id w9-20020a0568080d4900b003be3a9e5cd6mr804691oik.34.1706418385049; Sat, 27 Jan 2024 21:06:25 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id lp17-20020a056a003d5100b006ddd182bf1csm3550372pfb.46.2024.01.27.21.06.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 21:06:24 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org Cc: Yury Norov , Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v4 5/5] lib: test_bitops: add compile-time optimization/evaluations assertions Date: Sun, 28 Jan 2024 14:00:11 +0900 Message-ID: <20240128050449.1332798-6-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20240128050449.1332798-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a function in the bitops test suite to assert that the bitops helper functions correctly fold into constant expressions (or trigger a build bug otherwise). This should work on all the optimization levels supported by Kbuild. The added function does not perform any runtime tests and gets optimized out to nothing after passing the build assertions. Suggested-by: Yury Norov Signed-off-by: Vincent Mailhol --- lib/Kconfig.debug | 4 ++++ lib/test_bitops.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 4405f81248fb..85f8638b3ae6 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2439,6 +2439,10 @@ config TEST_BITOPS compilations. It has no dependencies and doesn't run or load unless explicitly requested by name. for example: modprobe test_bitops. =20 + In addition, check that the compiler is able to fold the bitops + function into a compile-time constant (given that the argument is also + a compile-time constant) and trigger a build bug otherwise. + If unsure, say N. =20 config TEST_VMALLOC diff --git a/lib/test_bitops.c b/lib/test_bitops.c index 3b7bcbee84db..99b612515eb6 100644 --- a/lib/test_bitops.c +++ b/lib/test_bitops.c @@ -50,6 +50,34 @@ static unsigned long order_comb_long[][2] =3D { }; #endif =20 +/* Assert that a boolean expression can be folded in a constant and is tru= e. */ +#define test_const_eval(test_expr) \ +({ \ + /* Evaluate once so that compiler can fold it. */ \ + bool __test_expr =3D test_expr; \ + \ + BUILD_BUG_ON(!__builtin_constant_p(__test_expr)); \ + BUILD_BUG_ON(!__test_expr); \ +}) + +/* + * On any supported optimization level (-O2, -Os) and if invoked with + * a compile-time constant argument, the compiler must be able to fold + * into a constant expression all the bit find functions. Namely: + * __ffs(), ffs(), ffz(), __fls(), fls() and fls64(). Otherwise, + * trigger a build bug. + */ +static __always_inline void test_bitops_const_eval(unsigned int n) +{ + test_const_eval(__ffs(BIT(n)) =3D=3D n); + test_const_eval(ffs(BIT(n)) =3D=3D n + 1); + test_const_eval(ffz(~BIT(n)) =3D=3D n); + test_const_eval(__fls(BIT(n)) =3D=3D n); + test_const_eval(fls(BIT(n)) =3D=3D n + 1); + test_const_eval(fls64(BIT_ULL(n)) =3D=3D n + 1); + test_const_eval(fls64(BIT_ULL(n + 32)) =3D=3D n + 33); +} + static int __init test_bitops_startup(void) { int i, bit_set; @@ -94,6 +122,10 @@ static int __init test_bitops_startup(void) if (bit_set !=3D BITOPS_LAST) pr_err("ERROR: FOUND SET BIT %d\n", bit_set); =20 + test_bitops_const_eval(0); + test_bitops_const_eval(10); + test_bitops_const_eval(31); + pr_info("Completed bitops test\n"); =20 return 0; --=20 2.43.0