From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 09DE814E38C9; Mon, 25 Aug 2025 18:26:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 09DE814E38C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1756135601; bh=erGiQS3a3tLJa816bQdWjMUblocE8j4K/038bMgI52E=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Nggn0NtyVKpfS9gcJSfb47qFpzugfznPUChGId2W3+KgeR+fo9C+XT/gSBHniv/Mp OoUtmlNtMT7CKwlKaAUkixLRo0eUc4Lzxf8wATCOx5E4RDNoirA4/XUEZ4ZDq1aBtL VR2gZ8l+f5OXqdz9HVJAuMSuJBIl9ItozQHOfnos= Received: from send59.i.mail.ru (send59.i.mail.ru [89.221.237.154]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4CAC814E38C9 for ; Mon, 25 Aug 2025 18:26:39 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4CAC814E38C9 Received: by exim-smtp-6944cbb85b-hmbdr with esmtpa (envelope-from ) id 1uqZ5i-00000000Gek-1yaX; Mon, 25 Aug 2025 18:26:38 +0300 Content-Type: multipart/alternative; boundary="------------SvFCITGDVyEcfmnGk0lS2ist" Message-ID: <5be3820a-2060-40b0-a30c-c6935c8200a0@tarantool.org> Date: Mon, 25 Aug 2025 18:26:38 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org References: In-Reply-To: X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9F7647FA575010556B5A1E406A4D1B053CF82201E7FF298B9182A05F538085040E2E7B59C3929F1663DE06ABAFEAF6705072B4FA6DB4EDE54CE9A42D256B6A2551BB5EE8A55BF1B9C X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77633BACAB33B9508C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6759CC434672EE6371C2A783ECEC0211ADC4224003CC836476D5A39DEEDB180909611E41BBFE2FEB2B833B909B0DBCC822E72387B83BF3B273A9CCF7FAD9BBF4571FDBAE9A32ECE6AB9FA2833FD35BB23D9E625A9149C048EE33AC447995A7AD1828451B159A507268D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B55B19328CBC4F849A471835C12D1D977C4224003CC836476EB9C4185024447017B076A6E789B0E975F5C1EE8F4F765FC5613D04DEA810176D81D268191BDAD3DBD4B6F7A4D31EC0BE2F48590F00D11D6D81D268191BDAD3D78DA827A17800CE7EEFE94893D2DF0F4CD04E86FAF290E2DB606B96278B59C421DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6BF3059D42242344A089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A538DF98FA91C72AEC5002B1117B3ED6965DCBDC5B837DFD45886DC9BC01168B20823CB91A9FED034534781492E4B8EEAD05E80F4396618BB2BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADBF74143AD284FC7177DD89D51EBB7742424CF958EAFF5D571004E42C50DC4CA955A7F0CF078B5EC49A30900B95165D349DAEDEF7DE8FDCFAA68345C5E028038CA6108A3981121A1E1E7DB93D17076692AF7696CBD54658001D7E09C32AA3244C99F3A2A009D2AC5077DD89D51EBB7742254AA609D689B7D8EA455F16B58544A2E30DDF7C44BCB90DA5AE236DF995FB59978A700BF655EAEEED6A17656DB59BCAD427812AF56FC65B X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVXNcwk+fZooKl/wGswRJ1OM= X-Mailru-Sender: 811C44EDE0507D1F797560C68D020EBD79464AE9DBA5B8E7125AE33D5AC58CD44EF91FAF76386722BAFD280175AB1E28645D15D82EE4B272BD6E4642A116CA93524AA66B5ACBE6721EF430B9A63E2A504198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit 1/3] ARM64: Improve generation of immediates. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This is a multi-part message in MIME format. --------------SvFCITGDVyEcfmnGk0lS2ist Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, Sergey! thanks for the patch! LGTM Sergey On 7/24/25 12:03, Sergey Kaplun wrote: > From: Mike Pall > > (cherry picked from commit ebc4919affbc0f9e8adfb5dede378017c7dd1fdd) > > This patch improves the generation of immediates, which are used in the > cdata alignment or arguments to the function calls. Before this patch, > these immediates are rematerialized to the registers like the following > (if they may be encoded in K13 format for logical data processing > instructions): > | orr w1, wzr, 0x3 > > Instead of recommended [1][2]: > | mov x1, 0x3 > > Since for the case when a constant may be placed in the register with > the single `mov` [3] instruction, it is preferable to encode the `mov` > instruction instead (which is an alias to `orr`, in most cases). > > For the cases when the constant needs at least `mov` and `mov[kn]` > instructions, it is still preferable to try short emitting via `orr` if > possible. > > Sergey Kaplun: > * added the description for the patch > > [1]:https://developer.arm.com/documentation/ddi0602/2025-06/Shared-Pseudocode/aarch64-functions-movwpreferred > [2]:https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/ORR--immediate---Bitwise-OR--immediate-- > [3]:https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/MOV--register---Move-register-value--an-alias-of-ORR--shifted-register-- > > Part of tarantool/tarantool#11691 > --- > src/lj_emit_arm64.h | 64 +++++++++++++++++++++++---------------------- > 1 file changed, 33 insertions(+), 31 deletions(-) > > diff --git a/src/lj_emit_arm64.h b/src/lj_emit_arm64.h > index 5c1bc372..2bb93dd9 100644 > --- a/src/lj_emit_arm64.h > +++ b/src/lj_emit_arm64.h > @@ -194,39 +194,41 @@ static int emit_kdelta(ASMState *as, Reg rd, uint64_t k, int lim) > > static void emit_loadk(ASMState *as, Reg rd, uint64_t u64, int is64) > { > - uint32_t k13 = emit_isk13(u64, is64); > - if (k13) { /* Can the constant be represented as a bitmask immediate? */ > - emit_dn(as, (is64|A64I_ORRw)^k13, rd, RID_ZERO); > - } else { > - int i, zeros = 0, ones = 0, neg; > - if (!is64) u64 = (int64_t)(int32_t)u64; /* Sign-extend. */ > - /* Count homogeneous 16 bit fragments. */ > - for (i = 0; i < 4; i++) { > - uint64_t frag = (u64 >> i*16) & 0xffff; > - zeros += (frag == 0); > - ones += (frag == 0xffff); > + int i, zeros = 0, ones = 0, neg; > + if (!is64) u64 = (int64_t)(int32_t)u64; /* Sign-extend. */ > + /* Count homogeneous 16 bit fragments. */ > + for (i = 0; i < 4; i++) { > + uint64_t frag = (u64 >> i*16) & 0xffff; > + zeros += (frag == 0); > + ones += (frag == 0xffff); > + } > + neg = ones > zeros; /* Use MOVN if it pays off. */ > + if ((neg ? ones : zeros) < 3) { /* Need 2+ ins. Try shorter K13 encoding. */ > + uint32_t k13 = emit_isk13(u64, is64); > + if (k13) { > + emit_dn(as, (is64|A64I_ORRw)^k13, rd, RID_ZERO); > + return; > } > - neg = ones > zeros; /* Use MOVN if it pays off. */ > - if (!emit_kdelta(as, rd, u64, 4 - (neg ? ones : zeros))) { > - int shift = 0, lshift = 0; > - uint64_t n64 = neg ? ~u64 : u64; > - if (n64 != 0) { > - /* Find first/last fragment to be filled. */ > - shift = (63-emit_clz64(n64)) & ~15; > - lshift = emit_ctz64(n64) & ~15; > - } > - /* MOVK requires the original value (u64). */ > - while (shift > lshift) { > - uint32_t u16 = (u64 >> shift) & 0xffff; > - /* Skip fragments that are correctly filled by MOVN/MOVZ. */ > - if (u16 != (neg ? 0xffff : 0)) > - emit_d(as, is64 | A64I_MOVKw | A64F_U16(u16) | A64F_LSL16(shift), rd); > - shift -= 16; > - } > - /* But MOVN needs an inverted value (n64). */ > - emit_d(as, (neg ? A64I_MOVNx : A64I_MOVZx) | > - A64F_U16((n64 >> lshift) & 0xffff) | A64F_LSL16(lshift), rd); > + } > + if (!emit_kdelta(as, rd, u64, 4 - (neg ? ones : zeros))) { > + int shift = 0, lshift = 0; > + uint64_t n64 = neg ? ~u64 : u64; > + if (n64 != 0) { > + /* Find first/last fragment to be filled. */ > + shift = (63-emit_clz64(n64)) & ~15; > + lshift = emit_ctz64(n64) & ~15; > + } > + /* MOVK requires the original value (u64). */ > + while (shift > lshift) { > + uint32_t u16 = (u64 >> shift) & 0xffff; > + /* Skip fragments that are correctly filled by MOVN/MOVZ. */ > + if (u16 != (neg ? 0xffff : 0)) > + emit_d(as, is64 | A64I_MOVKw | A64F_U16(u16) | A64F_LSL16(shift), rd); > + shift -= 16; > } > + /* But MOVN needs an inverted value (n64). */ > + emit_d(as, (neg ? A64I_MOVNx : A64I_MOVZx) | > + A64F_U16((n64 >> lshift) & 0xffff) | A64F_LSL16(lshift), rd); > } > } > --------------SvFCITGDVyEcfmnGk0lS2ist Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hi, Sergey!

thanks for the patch! LGTM

Sergey

On 7/24/25 12:03, Sergey Kaplun wrote:
From: Mike Pall <mike>

(cherry picked from commit ebc4919affbc0f9e8adfb5dede378017c7dd1fdd)

This patch improves the generation of immediates, which are used in the
cdata alignment or arguments to the function calls. Before this patch,
these immediates are rematerialized to the registers like the following
(if they may be encoded in K13 format for logical data processing
instructions):
| orr w1, wzr, 0x3

Instead of recommended [1][2]:
| mov x1, 0x3

Since for the case when a constant may be placed in the register with
the single `mov` [3] instruction, it is preferable to encode the `mov`
instruction instead (which is an alias to `orr`, in most cases).

For the cases when the constant needs at least `mov` and `mov[kn]`
instructions, it is still preferable to try short emitting via `orr` if
possible.

Sergey Kaplun:
* added the description for the patch

[1]: https://developer.arm.com/documentation/ddi0602/2025-06/Shared-Pseudocode/aarch64-functions-movwpreferred
[2]: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/ORR--immediate---Bitwise-OR--immediate--
[3]: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/MOV--register---Move-register-value--an-alias-of-ORR--shifted-register--

Part of tarantool/tarantool#11691
---
 src/lj_emit_arm64.h | 64 +++++++++++++++++++++++----------------------
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/src/lj_emit_arm64.h b/src/lj_emit_arm64.h
index 5c1bc372..2bb93dd9 100644
--- a/src/lj_emit_arm64.h
+++ b/src/lj_emit_arm64.h
@@ -194,39 +194,41 @@ static int emit_kdelta(ASMState *as, Reg rd, uint64_t k, int lim)
 
 static void emit_loadk(ASMState *as, Reg rd, uint64_t u64, int is64)
 {
-  uint32_t k13 = emit_isk13(u64, is64);
-  if (k13) {  /* Can the constant be represented as a bitmask immediate? */
-    emit_dn(as, (is64|A64I_ORRw)^k13, rd, RID_ZERO);
-  } else {
-    int i, zeros = 0, ones = 0, neg;
-    if (!is64) u64 = (int64_t)(int32_t)u64;  /* Sign-extend. */
-    /* Count homogeneous 16 bit fragments. */
-    for (i = 0; i < 4; i++) {
-      uint64_t frag = (u64 >> i*16) & 0xffff;
-      zeros += (frag == 0);
-      ones += (frag == 0xffff);
+  int i, zeros = 0, ones = 0, neg;
+  if (!is64) u64 = (int64_t)(int32_t)u64;  /* Sign-extend. */
+  /* Count homogeneous 16 bit fragments. */
+  for (i = 0; i < 4; i++) {
+    uint64_t frag = (u64 >> i*16) & 0xffff;
+    zeros += (frag == 0);
+    ones += (frag == 0xffff);
+  }
+  neg = ones > zeros;  /* Use MOVN if it pays off. */
+  if ((neg ? ones : zeros) < 3) {  /* Need 2+ ins. Try shorter K13 encoding. */
+    uint32_t k13 = emit_isk13(u64, is64);
+    if (k13) {
+      emit_dn(as, (is64|A64I_ORRw)^k13, rd, RID_ZERO);
+      return;
     }
-    neg = ones > zeros;  /* Use MOVN if it pays off. */
-    if (!emit_kdelta(as, rd, u64, 4 - (neg ? ones : zeros))) {
-      int shift = 0, lshift = 0;
-      uint64_t n64 = neg ? ~u64 : u64;
-      if (n64 != 0) {
-	/* Find first/last fragment to be filled. */
-	shift = (63-emit_clz64(n64)) & ~15;
-	lshift = emit_ctz64(n64) & ~15;
-      }
-      /* MOVK requires the original value (u64). */
-      while (shift > lshift) {
-	uint32_t u16 = (u64 >> shift) & 0xffff;
-	/* Skip fragments that are correctly filled by MOVN/MOVZ. */
-	if (u16 != (neg ? 0xffff : 0))
-	  emit_d(as, is64 | A64I_MOVKw | A64F_U16(u16) | A64F_LSL16(shift), rd);
-	shift -= 16;
-      }
-      /* But MOVN needs an inverted value (n64). */
-      emit_d(as, (neg ? A64I_MOVNx : A64I_MOVZx) |
-		 A64F_U16((n64 >> lshift) & 0xffff) | A64F_LSL16(lshift), rd);
+  }
+  if (!emit_kdelta(as, rd, u64, 4 - (neg ? ones : zeros))) {
+    int shift = 0, lshift = 0;
+    uint64_t n64 = neg ? ~u64 : u64;
+    if (n64 != 0) {
+      /* Find first/last fragment to be filled. */
+      shift = (63-emit_clz64(n64)) & ~15;
+      lshift = emit_ctz64(n64) & ~15;
+    }
+    /* MOVK requires the original value (u64). */
+    while (shift > lshift) {
+      uint32_t u16 = (u64 >> shift) & 0xffff;
+      /* Skip fragments that are correctly filled by MOVN/MOVZ. */
+      if (u16 != (neg ? 0xffff : 0))
+	emit_d(as, is64 | A64I_MOVKw | A64F_U16(u16) | A64F_LSL16(shift), rd);
+      shift -= 16;
     }
+    /* But MOVN needs an inverted value (n64). */
+    emit_d(as, (neg ? A64I_MOVNx : A64I_MOVZx) |
+	       A64F_U16((n64 >> lshift) & 0xffff) | A64F_LSL16(lshift), rd);
   }
 }
 
--------------SvFCITGDVyEcfmnGk0lS2ist--