From 79dfd0eb4f177370a14b01dac37f12ae9637f016 Mon Sep 17 00:00:00 2001 From: Leszek Swirski Date: Wed, 9 Mar 2022 14:20:15 +0100 Subject: [PATCH] [codegen] Ensure that all register types fit in an int Registers were supposed to fit in an int, but some register subclasses added extra fields which made them too big. This fiddles around with field sizes so that all the Register subclasses still fit inside of an int (by making the Register code field 8-bit etc). Change-Id: I31ffbc8f98b6a5e1f638fc59147009e5f81dff41 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3513612 Auto-Submit: Leszek Swirski Reviewed-by: Victor Gomes Commit-Queue: Victor Gomes Cr-Commit-Position: refs/heads/main@{#79421} --- src/codegen/arm/register-arm.h | 6 +++--- src/codegen/arm64/register-arm64.h | 12 +++++++++--- src/codegen/ia32/register-ia32.h | 2 +- src/codegen/ppc/register-ppc.h | 6 +++--- src/codegen/register-base.h | 14 ++++++++------ src/codegen/s390/register-s390.h | 4 ++-- src/codegen/x64/register-x64.h | 6 +++--- 7 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/codegen/arm/register-arm.h b/src/codegen/arm/register-arm.h index dd32f200aa..2ea62a1a2f 100644 --- a/src/codegen/arm/register-arm.h +++ b/src/codegen/arm/register-arm.h @@ -109,7 +109,7 @@ class Register : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(Register); -static_assert(sizeof(Register) == sizeof(int), +static_assert(sizeof(Register) <= sizeof(int), "Register can efficiently be passed by value"); // r7: context register @@ -169,7 +169,7 @@ class SwVfpRegister : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(SwVfpRegister); -static_assert(sizeof(SwVfpRegister) == sizeof(int), +static_assert(sizeof(SwVfpRegister) <= sizeof(int), "SwVfpRegister can efficiently be passed by value"); using FloatRegister = SwVfpRegister; @@ -210,7 +210,7 @@ class DwVfpRegister : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(DwVfpRegister); -static_assert(sizeof(DwVfpRegister) == sizeof(int), +static_assert(sizeof(DwVfpRegister) <= sizeof(int), "DwVfpRegister can efficiently be passed by value"); using DoubleRegister = DwVfpRegister; diff --git a/src/codegen/arm64/register-arm64.h b/src/codegen/arm64/register-arm64.h index 42b02683bc..185e330df8 100644 --- a/src/codegen/arm64/register-arm64.h +++ b/src/codegen/arm64/register-arm64.h @@ -95,7 +95,7 @@ enum RegisterCode { class CPURegister : public RegisterBase { public: - enum RegisterType { kRegister, kVRegister, kNoRegister }; + enum RegisterType : int8_t { kRegister, kVRegister, kNoRegister }; static constexpr CPURegister no_reg() { return CPURegister{kCode_no_reg, 0, kNoRegister}; @@ -188,7 +188,7 @@ class CPURegister : public RegisterBase { bool IsSameSizeAndType(const CPURegister& other) const; protected: - int reg_size_; + uint8_t reg_size_; RegisterType reg_type_; #if defined(V8_OS_WIN) && !defined(__clang__) @@ -224,6 +224,8 @@ class CPURegister : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(CPURegister); +static_assert(sizeof(CPURegister) <= sizeof(int), + "CPURegister can efficiently be passed by value"); class Register : public CPURegister { public: @@ -250,6 +252,8 @@ class Register : public CPURegister { }; ASSERT_TRIVIALLY_COPYABLE(Register); +static_assert(sizeof(Register) <= sizeof(int), + "Register can efficiently be passed by value"); // Stack frame alignment and padding. constexpr int ArgumentPaddingSlots(int argument_count) { @@ -419,7 +423,7 @@ class VRegister : public CPURegister { } private: - int lane_count_; + int8_t lane_count_; constexpr explicit VRegister(const CPURegister& r, int lane_count) : CPURegister(r), lane_count_(lane_count) {} @@ -430,6 +434,8 @@ class VRegister : public CPURegister { }; ASSERT_TRIVIALLY_COPYABLE(VRegister); +static_assert(sizeof(VRegister) <= sizeof(int), + "VRegister can efficiently be passed by value"); // No*Reg is used to indicate an unused argument, or an error case. Note that // these all compare equal. The Register and VRegister variants are provided for diff --git a/src/codegen/ia32/register-ia32.h b/src/codegen/ia32/register-ia32.h index 1c41ab5615..8a65e4e01b 100644 --- a/src/codegen/ia32/register-ia32.h +++ b/src/codegen/ia32/register-ia32.h @@ -67,7 +67,7 @@ class Register : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(Register); -static_assert(sizeof(Register) == sizeof(int), +static_assert(sizeof(Register) <= sizeof(int), "Register can efficiently be passed by value"); #define DEFINE_REGISTER(R) \ diff --git a/src/codegen/ppc/register-ppc.h b/src/codegen/ppc/register-ppc.h index 17625fea63..6a67c07e87 100644 --- a/src/codegen/ppc/register-ppc.h +++ b/src/codegen/ppc/register-ppc.h @@ -199,7 +199,7 @@ class Register : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(Register); -static_assert(sizeof(Register) == sizeof(int), +static_assert(sizeof(Register) <= sizeof(int), "Register can efficiently be passed by value"); #define DEFINE_REGISTER(R) \ @@ -249,7 +249,7 @@ class DoubleRegister : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(DoubleRegister); -static_assert(sizeof(DoubleRegister) == sizeof(int), +static_assert(sizeof(DoubleRegister) <= sizeof(int), "DoubleRegister can efficiently be passed by value"); using FloatRegister = DoubleRegister; @@ -292,7 +292,7 @@ class Simd128Register explicit constexpr Simd128Register(int code) : RegisterBase(code) {} }; ASSERT_TRIVIALLY_COPYABLE(Simd128Register); -static_assert(sizeof(Simd128Register) == sizeof(int), +static_assert(sizeof(Simd128Register) <= sizeof(int), "Simd128Register can efficiently be passed by value"); #define DECLARE_SIMD128_REGISTER(R) \ diff --git a/src/codegen/register-base.h b/src/codegen/register-base.h index fd04654db5..4f859452b1 100644 --- a/src/codegen/register-base.h +++ b/src/codegen/register-base.h @@ -8,6 +8,7 @@ #include "src/base/bits.h" #include "src/base/bounds.h" #include "src/codegen/reglist.h" +#include "src/common/globals.h" namespace v8 { @@ -28,13 +29,13 @@ namespace internal { template class RegisterBase { public: - static constexpr int kCode_no_reg = -1; - static constexpr int kNumRegisters = kAfterLastRegister; + static constexpr int8_t kCode_no_reg = -1; + static constexpr int8_t kNumRegisters = kAfterLastRegister; static constexpr SubType no_reg() { return SubType{kCode_no_reg}; } - static constexpr SubType from_code(int code) { - DCHECK(base::IsInRange(code, 0, kNumRegisters - 1)); + static constexpr SubType from_code(int8_t code) { + DCHECK(base::IsInRange(static_cast(code), 0, kNumRegisters - 1)); return SubType{code}; } @@ -45,7 +46,7 @@ class RegisterBase { constexpr bool is_valid() const { return reg_code_ != kCode_no_reg; } - constexpr int code() const { + constexpr int8_t code() const { DCHECK(is_valid()); return reg_code_; } @@ -90,7 +91,8 @@ class RegisterBase { explicit constexpr RegisterBase(int code) : reg_code_(code) {} private: - int reg_code_; + int8_t reg_code_; + STATIC_ASSERT(kAfterLastRegister <= kMaxInt8); }; template { }; ASSERT_TRIVIALLY_COPYABLE(Register); -static_assert(sizeof(Register) == sizeof(int), +static_assert(sizeof(Register) <= sizeof(int), "Register can efficiently be passed by value"); #define DEFINE_REGISTER(R) \ @@ -204,7 +204,7 @@ class DoubleRegister : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(DoubleRegister); -static_assert(sizeof(DoubleRegister) == sizeof(int), +static_assert(sizeof(DoubleRegister) <= sizeof(int), "DoubleRegister can efficiently be passed by value"); using FloatRegister = DoubleRegister; diff --git a/src/codegen/x64/register-x64.h b/src/codegen/x64/register-x64.h index c23c09be72..88aabe70b3 100644 --- a/src/codegen/x64/register-x64.h +++ b/src/codegen/x64/register-x64.h @@ -75,7 +75,7 @@ class Register : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(Register); -static_assert(sizeof(Register) == sizeof(int), +static_assert(sizeof(Register) <= sizeof(int), "Register can efficiently be passed by value"); #define DECLARE_REGISTER(R) \ @@ -212,7 +212,7 @@ class XMMRegister : public RegisterBase { }; ASSERT_TRIVIALLY_COPYABLE(XMMRegister); -static_assert(sizeof(XMMRegister) == sizeof(int), +static_assert(sizeof(XMMRegister) <= sizeof(int), "XMMRegister can efficiently be passed by value"); class YMMRegister : public XMMRegister { @@ -228,7 +228,7 @@ class YMMRegister : public XMMRegister { }; ASSERT_TRIVIALLY_COPYABLE(YMMRegister); -static_assert(sizeof(YMMRegister) == sizeof(int), +static_assert(sizeof(YMMRegister) <= sizeof(int), "YMMRegister can efficiently be passed by value"); using FloatRegister = XMMRegister;