[前][次][番号順一覧][スレッド一覧]

ruby-changes:73321

From: Kevin <ko1@a...>
Date: Tue, 30 Aug 2022 01:10:22 +0900 (JST)
Subject: [ruby-changes:73321] d694f320e4 (master): Fixed width immediates (https://github.com/Shopify/ruby/pull/437)

https://git.ruby-lang.org/ruby.git/commit/?id=d694f320e4

From d694f320e40e77ab432f4d21575251ac0ab4ab76 Mon Sep 17 00:00:00 2001
From: Kevin Newton <kddnewton@g...>
Date: Fri, 26 Aug 2022 19:21:45 -0400
Subject: Fixed width immediates (https://github.com/Shopify/ruby/pull/437)

There are a lot of times when encoding AArch64 instructions that we
need to represent an integer value with a custom fixed width. For
example, the offset for a B instruction is 26 bits, so we store an
i32 on the instruction struct and then mask it when we encode.

We've been doing this masking everywhere, which has worked, but
it's getting a bit copy-pasty all over the place. This commit
centralizes that logic to make sure we stay consistent.
---
 yjit/src/asm/arm64/arg/mod.rs           |  2 +
 yjit/src/asm/arm64/arg/truncate.rs      | 66 +++++++++++++++++++++++++++++++++
 yjit/src/asm/arm64/inst/branch_cond.rs  | 18 +++++----
 yjit/src/asm/arm64/inst/call.rs         | 14 +++----
 yjit/src/asm/arm64/inst/data_reg.rs     |  6 +--
 yjit/src/asm/arm64/inst/halfword_imm.rs |  7 +++-
 yjit/src/asm/arm64/inst/load_literal.rs |  6 +--
 yjit/src/asm/arm64/inst/load_store.rs   |  6 +--
 yjit/src/asm/arm64/inst/logical_reg.rs  |  6 +--
 yjit/src/asm/arm64/inst/reg_pair.rs     | 10 ++---
 yjit/src/asm/arm64/inst/sbfm.rs         |  9 ++---
 yjit/src/asm/arm64/inst/test_bit.rs     |  8 ++--
 12 files changed, 110 insertions(+), 48 deletions(-)
 create mode 100644 yjit/src/asm/arm64/arg/truncate.rs

diff --git a/yjit/src/asm/arm64/arg/mod.rs b/yjit/src/asm/arm64/arg/mod.rs
index 30f3cc3dfe..9bf4a8ea13 100644
--- a/yjit/src/asm/arm64/arg/mod.rs
+++ b/yjit/src/asm/arm64/arg/mod.rs
@@ -6,9 +6,11 @@ mod condition; https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/arg/mod.rs#L6
 mod sf;
 mod shifted_imm;
 mod sys_reg;
+mod truncate;
 
 pub use bitmask_imm::BitmaskImmediate;
 pub use condition::Condition;
 pub use sf::Sf;
 pub use shifted_imm::ShiftedImmediate;
 pub use sys_reg::SystemRegister;
+pub use truncate::{truncate_imm, truncate_uimm};
diff --git a/yjit/src/asm/arm64/arg/truncate.rs b/yjit/src/asm/arm64/arg/truncate.rs
new file mode 100644
index 0000000000..52f2c012cb
--- /dev/null
+++ b/yjit/src/asm/arm64/arg/truncate.rs
@@ -0,0 +1,66 @@ https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/arg/truncate.rs#L1
+// There are many instances in AArch64 instruction encoding where you represent
+// an integer value with a particular bit width that isn't a power of 2. These
+// functions represent truncating those integer values down to the appropriate
+// number of bits.
+
+/// Truncate a signed immediate to fit into a compile-time known width. It is
+/// assumed before calling this function that the value fits into the correct
+/// size. If it doesn't, then this function will panic.
+/// 
+/// When the value is positive, this should effectively be a no-op since we're
+/// just dropping leading zeroes. When the value is negative we should only be
+/// dropping leading ones.
+pub fn truncate_imm<T: Into<i32>, const WIDTH: usize>(imm: T) -> u32 {
+    let value: i32 = imm.into();
+    let masked = (value as u32) & ((1 << WIDTH) - 1);
+
+    // Assert that we didn't drop any bits by truncating.
+    if value >= 0 {
+        assert_eq!(value as u32, masked);
+    } else {
+        assert_eq!(value as u32, masked | (u32::MAX << WIDTH));
+    }
+
+    masked
+}
+
+/// Truncate an unsigned immediate to fit into a compile-time known width. It is
+/// assumed before calling this function that the value fits into the correct
+/// size. If it doesn't, then this function will panic.
+/// 
+/// This should effectively be a no-op since we're just dropping leading zeroes.
+pub fn truncate_uimm<T: Into<u32>, const WIDTH: usize>(uimm: T) -> u32 {
+    let value: u32 = uimm.into();
+    let masked = (value & ((1 << WIDTH) - 1));
+
+    // Assert that we didn't drop any bits by truncating.
+    assert_eq!(value, masked);
+
+    masked
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_truncate_imm_positive() {
+        let inst = truncate_imm::<i32, 4>(5);
+        let result: u32 = inst.into();
+        assert_eq!(0b0101, result);
+    }
+
+    #[test]
+    fn test_truncate_imm_negative() {
+        let inst = truncate_imm::<i32, 4>(-5);
+        let result: u32 = inst.into();
+        assert_eq!(0b1011, result);
+    }
+
+    #[test]
+    fn test_truncate_uimm() {
+        let inst = truncate_uimm::<u32, 4>(5);
+        let result: u32 = inst.into();
+        assert_eq!(0b0101, result);
+    }
+}
diff --git a/yjit/src/asm/arm64/inst/branch_cond.rs b/yjit/src/asm/arm64/inst/branch_cond.rs
index 33cc9c3649..a6bc79dffe 100644
--- a/yjit/src/asm/arm64/inst/branch_cond.rs
+++ b/yjit/src/asm/arm64/inst/branch_cond.rs
@@ -1,4 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/branch_cond.rs#L1
-use super::super::arg::Condition;
+use super::super::arg::{Condition, truncate_imm};
 
 /// The struct that represents an A64 conditional branch instruction that can be
 /// encoded.
@@ -31,12 +31,10 @@ const FAMILY: u32 = 0b101; https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/branch_cond.rs#L31
 impl From<BranchCond> for u32 {
     /// Convert an instruction into a 32-bit value.
     fn from(inst: BranchCond) -> Self {
-        let imm19 = (inst.imm19 as u32) & ((1 << 19) - 1);
-
         0
         | (1 << 30)
         | (FAMILY << 26)
-        | (imm19 << 5)
+        | (truncate_imm::<_, 19>(inst.imm19) << 5)
         | (inst.cond as u32)
     }
 }
@@ -66,8 +64,14 @@ mod tests { https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/branch_cond.rs#L64
     }
 
     #[test]
-    fn test_b_ne_neg() {
-        let result: u32 = BranchCond::bcond(Condition::NE, -128).into();
-        assert_eq!(0x54fffc01, result);
+    fn test_b_eq_max() {
+        let result: u32 = BranchCond::bcond(Condition::EQ, (1 << 20) - 4).into();
+        assert_eq!(0x547fffe0, result);
+    }
+
+    #[test]
+    fn test_b_eq_min() {
+        let result: u32 = BranchCond::bcond(Condition::EQ, -(1 << 20)).into();
+        assert_eq!(0x54800000, result);
     }
 }
diff --git a/yjit/src/asm/arm64/inst/call.rs b/yjit/src/asm/arm64/inst/call.rs
index 8d65359f77..32d924f799 100644
--- a/yjit/src/asm/arm64/inst/call.rs
+++ b/yjit/src/asm/arm64/inst/call.rs
@@ -1,3 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/call.rs#L1
+use super::super::arg::truncate_imm;
+
 /// The operation to perform for this instruction.
 enum Op {
     /// Branch directly, with a hint that this is not a subroutine call or
@@ -45,12 +47,10 @@ const FAMILY: u32 = 0b101; https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/call.rs#L47
 impl From<Call> for u32 {
     /// Convert an instruction into a 32-bit value.
     fn from(inst: Call) -> Self {
-        let imm26 = (inst.imm26 as u32) & ((1 << 26) - 1);
-
         0
         | ((inst.op as u32) << 31)
         | (FAMILY << 26)
-        | imm26
+        | truncate_imm::<_, 26>(inst.imm26)
     }
 }
 
@@ -92,13 +92,13 @@ mod tests { https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/call.rs#L92
 
     #[test]
     fn test_b_positive() {
-        let result: u32 = Call::b(256).into();
-        assert_eq!(0x14000100, result);
+        let result: u32 = Call::b((1 << 25) - 1).into();
+        assert_eq!(0x15ffffff, result);
     }
 
     #[test]
     fn test_b_negative() {
-        let result: u32 = Call::b(-256).into();
-        assert_eq!(0x17ffff00, result);
+        let result: u32 = Call::b(-(1 << 25)).into();
+        assert_eq!(0x16000000, result);
     }
 }
diff --git a/yjit/src/asm/arm64/inst/data_reg.rs b/yjit/src/asm/arm64/inst/data_reg.rs
index e2c2723fcf..a742121f1f 100644
--- a/yjit/src/asm/arm64/inst/data_reg.rs
+++ b/yjit/src/asm/arm64/inst/data_reg.rs
@@ -1,4 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/data_reg.rs#L1
-use super::super::arg::Sf;
+use super::super::arg::{Sf, truncate_uimm};
 
 /// The operation being performed by this instruction.
 enum Op {
@@ -129,8 +129,6 @@ const FAMILY: u32 = 0b0101; https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/data_reg.rs#L129
 impl From<DataReg> for u32 {
     /// Convert an instruction into a 32-bit value.
     fn from(inst: DataReg) -> Self {
-        let imm6 = (inst.imm6 as u32) & ((1 << 6) - 1);
-
         0
         | ((inst.sf as u32) << 31)
         | ((inst.op as u32) << 30)
@@ -139,7 +137,7 @@ impl From<DataReg> for u32 { https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/data_reg.rs#L137
         | (1 << 24)
         | ((inst.shift as u32) << 22)
         | ((inst.rm as u32) << 16)
-        | (imm6 << 10)
+        | (truncate_uimm::<_, 6>(inst.imm6) << 10)
         | ((inst.rn as u32) << 5)
         | inst.rd as u32
     }
diff --git a/yjit/src/asm/arm64/inst/halfword_imm.rs b/yjit/src/asm/arm64/inst/halfword_imm.rs
index 675e33d4a8..c31d1f8945 100644
--- a/yjit/src/asm/arm64/inst/halfword_imm.rs
+++ b/yjit/src/asm/arm64/inst/halfword_imm.rs
@@ -1,3 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/halfword_imm.rs#L1
+use super::super::arg::truncate_imm;
+
 /// Whether this is a load or a store.
 enum Op {
     Load = 1,
@@ -95,11 +97,12 @@ impl From<HalfwordImm> for u32 { https://github.com/ruby/ruby/blob/trunk/yjit/src/asm/arm64/inst/halfword_imm.rs#L97
     fn from(inst: HalfwordImm) -> Self {
         let (mut opc, imm) = match inst.index {
             Index::None => {
-                let mut imm12 = ((inst.imm / 2) as u32) & ((1 << 12) - 1);
+                assert_eq!(inst.imm & 1, 0, "immediate offset must be even");
+                let imm12 = truncate_imm::<_, 12>(inst.imm / 2);
                 (0b100, imm12)
             },
             Index::PreIndex | Index::PostIndex => {
-                let mut imm9 = (inst.imm as u32) & ((1 << 9) - 1);
+                let imm9 = truncate_imm::<_, 9>(inst.imm);
                 (0b000, (imm9 << 2) | (inst.index as u (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]