Title: [253904] trunk/Source/_javascript_Core
Revision
253904
Author
[email protected]
Date
2019-12-25 01:07:19 -0800 (Wed, 25 Dec 2019)

Log Message

[JSC] Compact Bytecodes more by emitting 1-byte Opcode
https://bugs.webkit.org/show_bug.cgi?id=205553

Reviewed by Keith Miller.

When emitting 16bit / 32bit bytecodes, we also emit 16bit / 32bit Opcode.
So the layout is the following.

        8bit        16bit        16bit       16bit
    - [op_wide16][  Opcode     ][ Operand0 ][ Operand1 ]

But this is unnecessary since Opcode must fit in 8bit. We should emit Opcode in 8bit in all cases.

        8bit       8bit     16bit       16bit
    - [op_wide16][Opcode][ Operand0 ][ Operand1 ]

* bytecode/Instruction.h:
(JSC::BaseInstruction::size const):
* bytecompiler/BytecodeGeneratorBaseInlines.h:
(JSC::BytecodeGeneratorBase<Traits>::alignWideOpcode16):
(JSC::BytecodeGeneratorBase<Traits>::alignWideOpcode32):
* generator/Argument.rb:
* generator/Opcode.rb:
* llint/LowLevelInterpreter.asm:
* llint/LowLevelInterpreter32_64.asm:
* llint/LowLevelInterpreter64.asm:
* llint/WebAssembly.asm:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (253903 => 253904)


--- trunk/Source/_javascript_Core/ChangeLog	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-12-25 09:07:19 UTC (rev 253904)
@@ -1,3 +1,33 @@
+2019-12-25  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Compact Bytecodes more by emitting 1-byte Opcode
+        https://bugs.webkit.org/show_bug.cgi?id=205553
+
+        Reviewed by Keith Miller.
+
+        When emitting 16bit / 32bit bytecodes, we also emit 16bit / 32bit Opcode.
+        So the layout is the following.
+
+                8bit        16bit        16bit       16bit
+            - [op_wide16][  Opcode     ][ Operand0 ][ Operand1 ]
+
+        But this is unnecessary since Opcode must fit in 8bit. We should emit Opcode in 8bit in all cases.
+
+                8bit       8bit     16bit       16bit
+            - [op_wide16][Opcode][ Operand0 ][ Operand1 ]
+
+        * bytecode/Instruction.h:
+        (JSC::BaseInstruction::size const):
+        * bytecompiler/BytecodeGeneratorBaseInlines.h:
+        (JSC::BytecodeGeneratorBase<Traits>::alignWideOpcode16):
+        (JSC::BytecodeGeneratorBase<Traits>::alignWideOpcode32):
+        * generator/Argument.rb:
+        * generator/Opcode.rb:
+        * llint/LowLevelInterpreter.asm:
+        * llint/LowLevelInterpreter32_64.asm:
+        * llint/LowLevelInterpreter64.asm:
+        * llint/WebAssembly.asm:
+
 2019-12-24  Keith Miller  <[email protected]>
 
         Fix ARM64E by adding missing pointer tag.

Modified: trunk/Source/_javascript_Core/bytecode/Instruction.h (253903 => 253904)


--- trunk/Source/_javascript_Core/bytecode/Instruction.h	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/bytecode/Instruction.h	2019-12-25 09:07:19 UTC (rev 253904)
@@ -66,7 +66,7 @@
         typename Traits::OpcodeID opcodeID() const { return static_cast<typename Traits::OpcodeID>(m_opcode); }
 
     private:
-        typename TypeBySize<Width>::unsignedType m_opcode;
+        typename TypeBySize<OpcodeSize::Narrow>::unsignedType m_opcode;
     };
 
 public:
@@ -118,9 +118,10 @@
     size_t size() const
     {
         auto sizeShiftAmount = this->sizeShiftAmount<Traits>();
-        auto padding = sizeShiftAmount ? 1 : 0;
-        auto size = 1 << sizeShiftAmount;
-        return Traits::opcodeLengths[opcodeID<Traits>()] * size + padding;
+        auto prefixSize = sizeShiftAmount ? 1 : 0;
+        auto operandSize = 1 << sizeShiftAmount;
+        size_t sizeOfBytecode = 1;
+        return sizeOfBytecode + (Traits::opcodeLengths[opcodeID<Traits>()] - 1) * operandSize + prefixSize;
     }
 
     template<class T, typename Traits = JSOpcodeTraits>

Modified: trunk/Source/_javascript_Core/bytecompiler/BytecodeGeneratorBaseInlines.h (253903 => 253904)


--- trunk/Source/_javascript_Core/bytecompiler/BytecodeGeneratorBaseInlines.h	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/bytecompiler/BytecodeGeneratorBaseInlines.h	2019-12-25 09:07:19 UTC (rev 253904)
@@ -102,7 +102,9 @@
 void BytecodeGeneratorBase<Traits>::alignWideOpcode16()
 {
 #if CPU(NEEDS_ALIGNED_ACCESS)
-    while ((m_writer.position() + 1) % OpcodeSize::Wide16)
+    size_t opcodeSize = 1;
+    size_t prefixAndOpcodeSize = 1 + PaddingBySize<OpcodeSize::Wide16>::value;
+    while ((m_writer.position() + prefixAndOpcodeSize) % OpcodeSize::Wide16)
         Traits::OpNop::template emit<OpcodeSize::Narrow>(this);
 #endif
 }
@@ -111,7 +113,9 @@
 void BytecodeGeneratorBase<Traits>::alignWideOpcode32()
 {
 #if CPU(NEEDS_ALIGNED_ACCESS)
-    while ((m_writer.position() + 1) % OpcodeSize::Wide32)
+    size_t opcodeSize = 1;
+    size_t prefixAndOpcodeSize = 1 + PaddingBySize<OpcodeSize::Wide16>::value;
+    while ((m_writer.position() + prefixAndOpcodeSize) % OpcodeSize::Wide32)
         Traits::OpNop::template emit<OpcodeSize::Narrow>(this);
 #endif
 }

Modified: trunk/Source/_javascript_Core/generator/Argument.rb (253903 => 253904)


--- trunk/Source/_javascript_Core/generator/Argument.rb	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/generator/Argument.rb	2019-12-25 09:07:19 UTC (rev 253904)
@@ -84,7 +84,7 @@
     {
         if (!#{Fits::check "size", "value", @type})
             value = func();
-        auto* stream = bitwise_cast<typename TypeBySize<size>::unsignedType*>(reinterpret_cast<uint8_t*>(this) + #{@index} * size + PaddingBySize<size>::value);
+        auto* stream = bitwise_cast<typename TypeBySize<size>::unsignedType*>(reinterpret_cast<uint8_t*>(this) + #{@index} * size + PaddingBySize<size>::value + /* Opcode size */ 1);
         *stream = #{Fits::convert "size", "value", @type};
     }
 EOF

Modified: trunk/Source/_javascript_Core/generator/Opcode.rb (253903 => 253904)


--- trunk/Source/_javascript_Core/generator/Opcode.rb	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/generator/Opcode.rb	2019-12-25 09:07:19 UTC (rev 253904)
@@ -51,7 +51,7 @@
         @name = name
         @extras = extras || {}
         @metadata = Metadata.new metadata, metadata_initializers
-        @args = args.map.with_index { |(arg_name, type), index| Argument.new arg_name, type, index + 1 } unless args.nil?
+        @args = args.map.with_index { |(arg_name, type), index| Argument.new arg_name, type, index } unless args.nil?
         @tmps = tmps
         @checkpoints = checkpoints.map { |(checkpoint, _)| checkpoint } unless checkpoints.nil?
     end
@@ -117,6 +117,15 @@
         args.map { |arg| "#{prefix}#{block.call(arg, size)}" }
     end
 
+    def map_operands_with_size(prefix, size, &block)
+        args = []
+        args += @args.dup if @args
+        unless @metadata.empty?
+            args << @metadata.emitter_local
+        end
+        args.map { |arg| "#{prefix}#{block.call(arg, size)}" }
+    end
+
     def struct
         <<-EOF
 struct #{capitalized_name} : public Instruction {
@@ -229,7 +238,8 @@
                 #{op_wide16.fits_write Size::Narrow}
             else if (__size == OpcodeSize::Wide32)
                 #{op_wide32.fits_write Size::Narrow}
-#{map_fields_with_size("            ", "__size", &:fits_write).join "\n"}
+            #{Argument.new("opcodeID", opcodeIDType, 0).fits_write Size::Narrow}
+#{map_operands_with_size("            ", "__size", &:fits_write).join "\n"}
             return true;
         }
         return false;
@@ -246,7 +256,7 @@
         dumper->printLocationAndOp(__location, &"**#{@name}"[2 - __sizeShiftAmount]);
 #{print_args { |arg|
 <<-EOF.chomp
-        dumper->dumpOperand(#{arg.field_name}, #{arg.index == 1});
+        dumper->dumpOperand(#{arg.field_name}, #{arg.index == 0});
 EOF
     }}
     }
@@ -255,19 +265,19 @@
 
     def constructors
         fields = (@args || []) + (@metadata.empty? ? [] : [@metadata])
-        init = ->(size) { fields.empty?  ? "" : ": #{fields.map.with_index { |arg, i| arg.load_from_stream(i + 1, size) }.join "\n        , " }" }
+        init = ->(size) { fields.empty?  ? "" : ": #{fields.map.with_index { |arg, i| arg.load_from_stream(i, size) }.join "\n        , " }" }
 
         <<-EOF
     #{capitalized_name}(const uint8_t* stream)
         #{init.call("OpcodeSize::Narrow")}
     {
-        ASSERT_UNUSED(stream, stream[0] == opcodeID);
+        ASSERT_UNUSED(stream, bitwise_cast<const uint8_t*>(stream)[-1] == opcodeID);
     }
 
     #{capitalized_name}(const uint16_t* stream)
         #{init.call("OpcodeSize::Wide16")}
     {
-        ASSERT_UNUSED(stream, stream[0] == opcodeID);
+        ASSERT_UNUSED(stream, bitwise_cast<const uint8_t*>(stream)[-1] == opcodeID);
     }
 
 
@@ -274,16 +284,17 @@
     #{capitalized_name}(const uint32_t* stream)
         #{init.call("OpcodeSize::Wide32")}
     {
-        ASSERT_UNUSED(stream, stream[0] == opcodeID);
+        ASSERT_UNUSED(stream, bitwise_cast<const uint8_t*>(stream)[-1] == opcodeID);
     }
 
     static #{capitalized_name} decode(const uint8_t* stream)
     {
+        // A pointer is pointing to the first operand (opcode and prefix are not included).
         if (*stream == #{wide32})
-            return { bitwise_cast<const uint32_t*>(stream + 1) };
+            return { bitwise_cast<const uint32_t*>(stream + 2) };
         if (*stream == #{wide16})
-            return { bitwise_cast<const uint16_t*>(stream + 1) };
-        return { stream };
+            return { bitwise_cast<const uint16_t*>(stream + 2) };
+        return { stream + 1 };
     }
 EOF
     end
@@ -321,7 +332,7 @@
         out += @args.map(&:field_name) unless @args.nil?
         out << Metadata.field_name unless @metadata.empty?
         out.map.with_index do |name, index|
-            "const unsigned #{capitalized_name}_#{name}_index = #{index + 1};"
+            "const unsigned #{capitalized_name}_#{name}_index = #{index};"
         end
     end
 

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm (253903 => 253904)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm	2019-12-25 09:07:19 UTC (rev 253904)
@@ -214,8 +214,6 @@
     const StructureEntropyBitsShift = constexpr StructureIDTable::s_entropyBitsShiftForStructurePointer
 end
 
-const CallOpCodeSize = constexpr op_call_length
-
 const maxFrameExtentForSlowPathCall = constexpr maxFrameExtentForSlowPathCall
 
 if X86_64 or X86_64_WIN or ARM64 or ARM64E
@@ -306,6 +304,12 @@
     const GigacageJSValueBasePtrOffset = constexpr Gigacage::offsetOfJSValueGigacageBasePtr
 end
 
+# Opcode offsets
+const OpcodeIDNarrowSize = 1 # OpcodeID
+const OpcodeIDWide16Size = 2 # Wide16 Prefix + OpcodeID
+const OpcodeIDWide32Size = 2 # Wide32 Prefix + OpcodeID
+
+
 macro dispatch(advanceReg)
     addp advanceReg, PC
     nextInstruction()
@@ -317,15 +321,15 @@
 
 macro genericDispatchOp(dispatch, size, opcodeName)
     macro dispatchNarrow()
-        dispatch(constexpr %opcodeName%_length)
+        dispatch((constexpr %opcodeName%_length - 1) * 1 + OpcodeIDNarrowSize)
     end
 
     macro dispatchWide16()
-        dispatch(constexpr %opcodeName%_length * 2 + 1)
+        dispatch((constexpr %opcodeName%_length - 1) * 2 + OpcodeIDWide16Size)
     end
 
     macro dispatchWide32()
-        dispatch(constexpr %opcodeName%_length * 4 + 1)
+        dispatch((constexpr %opcodeName%_length - 1) * 4 + OpcodeIDWide32Size)
     end
 
     size(dispatchNarrow, dispatchWide16, dispatchWide32, macro (dispatch) dispatch() end)

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm (253903 => 253904)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2019-12-25 09:07:19 UTC (rev 253904)
@@ -30,39 +30,39 @@
 end
 
 macro nextInstructionWide16()
-    loadh 1[PC], t0
+    loadb OpcodeIDNarrowSize[PC], t0
     leap _g_opcodeMapWide16, t1
     jmp [t1, t0, 4], BytecodePtrTag
 end
 
 macro nextInstructionWide32()
-    loadi 1[PC], t0
+    loadb OpcodeIDNarrowSize[PC], t0
     leap _g_opcodeMapWide32, t1
     jmp [t1, t0, 4], BytecodePtrTag
 end
 
 macro getuOperandNarrow(opcodeStruct, fieldName, dst)
-    loadb constexpr %opcodeStruct%_%fieldName%_index[PC], dst
+    loadb constexpr %opcodeStruct%_%fieldName%_index + OpcodeIDNarrowSize[PC], dst
 end
 
 macro getOperandNarrow(opcodeStruct, fieldName, dst)
-    loadbsi constexpr %opcodeStruct%_%fieldName%_index[PC], dst
+    loadbsi constexpr %opcodeStruct%_%fieldName%_index + OpcodeIDNarrowSize[PC], dst
 end
 
 macro getuOperandWide16(opcodeStruct, fieldName, dst)
-    loadh constexpr %opcodeStruct%_%fieldName%_index * 2 + 1[PC], dst
+    loadh constexpr %opcodeStruct%_%fieldName%_index * 2 + OpcodeIDWide16Size[PC], dst
 end
 
 macro getOperandWide16(opcodeStruct, fieldName, dst)
-    loadhsi constexpr %opcodeStruct%_%fieldName%_index * 2 + 1[PC], dst
+    loadhsi constexpr %opcodeStruct%_%fieldName%_index * 2 + OpcodeIDWide16Size[PC], dst
 end
 
 macro getuOperandWide32(opcodeStruct, fieldName, dst)
-    loadi constexpr %opcodeStruct%_%fieldName%_index * 4 + 1[PC], dst
+    loadi constexpr %opcodeStruct%_%fieldName%_index * 4 + OpcodeIDWide32Size[PC], dst
 end
 
 macro getOperandWide32(opcodeStruct, fieldName, dst)
-    loadis constexpr %opcodeStruct%_%fieldName%_index * 4 + 1[PC], dst
+    loadis constexpr %opcodeStruct%_%fieldName%_index * 4 + OpcodeIDWide32Size[PC], dst
 end
 
 macro makeReturn(get, dispatch, fn)

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm (253903 => 253904)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2019-12-25 09:07:19 UTC (rev 253904)
@@ -31,13 +31,13 @@
 end
 
 macro nextInstructionWide16()
-    loadh 1[PB, PC, 1], t0
+    loadb OpcodeIDNarrowSize[PB, PC, 1], t0
     leap _g_opcodeMapWide16, t1
     jmp [t1, t0, PtrSize], BytecodePtrTag
 end
 
 macro nextInstructionWide32()
-    loadi 1[PB, PC, 1], t0
+    loadb OpcodeIDNarrowSize[PB, PC, 1], t0
     leap _g_opcodeMapWide32, t1
     jmp [t1, t0, PtrSize], BytecodePtrTag
 end
@@ -51,27 +51,27 @@
 end
 
 macro getuOperandNarrow(opcodeStruct, fieldName, dst)
-    loadb constexpr %opcodeStruct%_%fieldName%_index[PB, PC, 1], dst
+    loadb constexpr %opcodeStruct%_%fieldName%_index + OpcodeIDNarrowSize[PB, PC, 1], dst
 end
 
 macro getOperandNarrow(opcodeStruct, fieldName, dst)
-    loadbsq constexpr %opcodeStruct%_%fieldName%_index[PB, PC, 1], dst
+    loadbsq constexpr %opcodeStruct%_%fieldName%_index + OpcodeIDNarrowSize[PB, PC, 1], dst
 end
 
 macro getuOperandWide16(opcodeStruct, fieldName, dst)
-    loadh constexpr %opcodeStruct%_%fieldName%_index * 2 + 1[PB, PC, 1], dst
+    loadh constexpr %opcodeStruct%_%fieldName%_index * 2 + OpcodeIDWide16Size[PB, PC, 1], dst
 end
 
 macro getOperandWide16(opcodeStruct, fieldName, dst)
-    loadhsq constexpr %opcodeStruct%_%fieldName%_index * 2 + 1[PB, PC, 1], dst
+    loadhsq constexpr %opcodeStruct%_%fieldName%_index * 2 + OpcodeIDWide16Size[PB, PC, 1], dst
 end
 
 macro getuOperandWide32(opcodeStruct, fieldName, dst)
-    loadi constexpr %opcodeStruct%_%fieldName%_index * 4 + 1[PB, PC, 1], dst
+    loadi constexpr %opcodeStruct%_%fieldName%_index * 4 + OpcodeIDWide32Size[PB, PC, 1], dst
 end
 
 macro getOperandWide32(opcodeStruct, fieldName, dst)
-    loadis constexpr %opcodeStruct%_%fieldName%_index * 4 + 1[PB, PC, 1], dst
+    loadis constexpr %opcodeStruct%_%fieldName%_index * 4 + OpcodeIDWide32Size[PB, PC, 1], dst
 end
 
 macro makeReturn(get, dispatch, fn)

Modified: trunk/Source/_javascript_Core/llint/WebAssembly.asm (253903 => 253904)


--- trunk/Source/_javascript_Core/llint/WebAssembly.asm	2019-12-24 20:05:21 UTC (rev 253903)
+++ trunk/Source/_javascript_Core/llint/WebAssembly.asm	2019-12-25 09:07:19 UTC (rev 253904)
@@ -100,13 +100,13 @@
 end
 
 macro wasmNextInstructionWide16()
-    loadh 1[PB, PC, 1], t0
+    loadb OpcodeIDNarrowSize[PB, PC, 1], t0
     leap _g_opcodeMapWide16, t1
     jmp NumberOfJSOpcodeIDs * PtrSize[t1, t0, PtrSize], BytecodePtrTag
 end
 
 macro wasmNextInstructionWide32()
-    loadi 1[PB, PC, 1], t0
+    loadb OpcodeIDNarrowSize[PB, PC, 1], t0
     leap _g_opcodeMapWide32, t1
     jmp NumberOfJSOpcodeIDs * PtrSize[t1, t0, PtrSize], BytecodePtrTag
 end
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to