No public description PiperOrigin-RevId: 1021050796 Change-Id: I798c4a4138b7ba457a60abd50a239d3c798579e6
diff --git a/riscv/riscv_register.cc b/riscv/riscv_register.cc index 41ee74a..e760826 100644 --- a/riscv/riscv_register.cc +++ b/riscv/riscv_register.cc
@@ -14,6 +14,7 @@ #include "riscv/riscv_register.h" +#include <algorithm> #include <any> #include <cstdint> #include <limits> @@ -78,6 +79,7 @@ bool RV32VectorSourceOperand::AsBool(int i) { int offset = i % vector_byte_size_; int group = i / vector_byte_size_; + group = std::min(group, group_size_ - 1); return static_cast<bool>( registers_[group]->data_buffer()->Get<uint8_t>(offset)); } @@ -85,48 +87,56 @@ int8_t RV32VectorSourceOperand::AsInt8(int i) { int offset = i % vector_byte_size_; int group = i / vector_byte_size_; + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<int8_t>(offset); } uint8_t RV32VectorSourceOperand::AsUint8(int i) { int offset = i % vector_byte_size_; int group = i / vector_byte_size_; + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<uint8_t>(offset); } int16_t RV32VectorSourceOperand::AsInt16(int i) { int offset = i % (vector_byte_size_ >> 1); int group = i / (vector_byte_size_ >> 1); + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<int16_t>(offset); } uint16_t RV32VectorSourceOperand::AsUint16(int i) { int offset = i % (vector_byte_size_ >> 1); int group = i / (vector_byte_size_ >> 1); + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<uint16_t>(offset); } int32_t RV32VectorSourceOperand::AsInt32(int i) { int offset = i % (vector_byte_size_ >> 2); int group = i / (vector_byte_size_ >> 2); + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<int32_t>(offset); } uint32_t RV32VectorSourceOperand::AsUint32(int i) { int offset = i % (vector_byte_size_ >> 2); int group = i / (vector_byte_size_ >> 2); + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<uint32_t>(offset); } int64_t RV32VectorSourceOperand::AsInt64(int i) { int offset = i % (vector_byte_size_ >> 3); int group = i / (vector_byte_size_ >> 3); + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<int64_t>(offset); } uint64_t RV32VectorSourceOperand::AsUint64(int i) { int offset = i % (vector_byte_size_ >> 3); int group = i / (vector_byte_size_ >> 3); + group = std::min(group, group_size_ - 1); return registers_[group]->data_buffer()->Get<uint64_t>(offset); }
diff --git a/riscv/riscv_register.h b/riscv/riscv_register.h index 1048b19..7730d44 100644 --- a/riscv/riscv_register.h +++ b/riscv/riscv_register.h
@@ -15,6 +15,7 @@ #ifndef MPACT_RISCV_RISCV_RISCV_REGISTER_H_ #define MPACT_RISCV_RISCV_RISCV_REGISTER_H_ +#include <algorithm> #include <any> #include <cstdint> #include <string> @@ -73,7 +74,10 @@ // Returns the RegisterBase object wrapped in absl::any. std::any GetObject() const override { return std::any(registers_[0]); } // Non-inherited method to get the register object. - generic::RegisterBase* GetRegister(int i) const { return registers_[i]; } + generic::RegisterBase* GetRegister(int i) const { + i = std::min(i, static_cast<int>(registers_.size()) - 1); + return registers_[i]; + } // Returns the shape of the register. std::vector<int> shape() const override { return registers_[0]->shape(); } std::string AsString() const override { return registers_[0]->name(); }
diff --git a/riscv/test/riscv_vector_opi_instructions_test.cc b/riscv/test/riscv_vector_opi_instructions_test.cc index 9d957b6..d174f02 100644 --- a/riscv/test/riscv_vector_opi_instructions_test.cc +++ b/riscv/test/riscv_vector_opi_instructions_test.cc
@@ -83,8 +83,13 @@ using ::mpact::sim::riscv::Vsub; using ::mpact::sim::riscv::Vxor; +using ::mpact::sim::riscv::test::kLmulSettings; +using ::mpact::sim::riscv::test::kSewSettingsByByteSize; using ::mpact::sim::riscv::test::kVd; using ::mpact::sim::riscv::test::kVectorLengthInBytes; +using ::mpact::sim::riscv::test::kVmask; +using ::mpact::sim::riscv::test::kVmaskName; +using ::mpact::sim::riscv::test::kVs1; using ::mpact::sim::riscv::test::kVs2; using RVScalarRegister = RV32Register; @@ -124,6 +129,61 @@ [](uint64_t val0, uint64_t val1) -> uint64_t { return val0 + val1; }); } +TEST_F(RiscVVectorInstructionsTest, Vadd64VVMisalignedVs2OutOfBounds) { + using ::mpact::sim::riscv::RV32VectorSourceOperand; + SetSemanticFunction(&Vadd); + // Construct a 1-register source operand for misaligned vs2 = v3. + auto* v3_base = RV32VectorSourceOperand(vreg_[3], "v3").GetRegister(0); + decltype(v3_base) vs2_regs[] = {v3_base}; + instruction_->AppendSource(new RV32VectorSourceOperand(vs2_regs, "v3")); + AppendVectorRegisterOperands({kVs1, kVmask}, {kVd}); + + constexpr int kElementsPerReg = kVectorLengthInBytes / sizeof(uint64_t); + uint64_t v3_values[kElementsPerReg]; + uint64_t sentinel_values[kElementsPerReg]; + for (int i = 0; i < kElementsPerReg; ++i) { + v3_values[i] = 100 + i; + sentinel_values[i] = 0xdeadbeef'deadbeefULL; + } + SetVectorRegisterValues<uint64_t>({{"v3", v3_values}}); + for (int reg = 4; reg < 11; ++reg) { + SetVectorRegisterValues<uint64_t>( + {{absl::StrCat("v", reg), sentinel_values}}); + } + for (int reg = 0; reg < 8; ++reg) { + uint64_t vs1_reg_values[kElementsPerReg]; + for (int i = 0; i < kElementsPerReg; ++i) { + vs1_reg_values[i] = 1000 + reg * kElementsPerReg + i; + } + SetVectorRegisterValues<uint64_t>( + {{absl::StrCat("v", kVs1 + reg), vs1_reg_values}}); + } + uint8_t all_ones_mask[kVectorLengthInBytes]; + for (int i = 0; i < kVectorLengthInBytes; ++i) { + all_ones_mask[i] = 0xff; + } + SetVectorRegisterValues<uint8_t>({{kVmaskName, all_ones_mask}}); + + // Configure SEW=64 (8 bytes), LMUL=8 (index 6), vl=64. + uint32_t vtype = (kSewSettingsByByteSize[8] << 3) | kLmulSettings[6]; + ConfigureVectorUnit(vtype, /*vlen=*/64); + rv_vector_->set_vstart(0); + ClearVectorRegisterGroup(kVd, 8); + + instruction_->Execute(); + EXPECT_FALSE(rv_vector_->vector_exception()); + + for (int reg = 0; reg < 8; ++reg) { + for (int i = 0; i < kElementsPerReg; ++i) { + uint64_t vs2_expected = v3_values[i]; + uint64_t vs1_expected = 1000 + reg * kElementsPerReg + i; + EXPECT_EQ(vreg_[kVd + reg]->data_buffer()->Get<uint64_t>(i), + vs2_expected + vs1_expected) + << "reg=" << reg << " i=" << i; + } + } +} + // Vector-scalar. TEST_F(RiscVVectorInstructionsTest, Vadd8VX) { SetSemanticFunction(&Vadd);
diff --git a/riscv/test/riscv_vector_true_test.cc b/riscv/test/riscv_vector_true_test.cc index 06cd3bf..eb9803d 100644 --- a/riscv/test/riscv_vector_true_test.cc +++ b/riscv/test/riscv_vector_true_test.cc
@@ -26,7 +26,9 @@ using ::mpact::sim::riscv::RiscVState; using ::mpact::sim::riscv::RiscVVectorState; using ::mpact::sim::riscv::RiscVXlen; +using ::mpact::sim::riscv::RV32VectorSourceOperand; using ::mpact::sim::riscv::RV32VectorTrueOperand; +using ::mpact::sim::riscv::RVVectorRegister; using ::mpact::sim::util::FlatDemandMemory; constexpr int kVLengthInBytes = 64; @@ -65,4 +67,57 @@ delete op; } +TEST_F(RV32VectorTrueTest, SourceOperandOutOfBoundsAccessClampsToLastGroup) { + auto* v2 = state_->GetRegister<RVVectorRegister>("v2").first; + auto* v3 = state_->GetRegister<RVVectorRegister>("v3").first; + auto v2_span = v2->data_buffer()->Get<uint8_t>(); + auto v3_span = v3->data_buffer()->Get<uint8_t>(); + for (int i = 0; i < kVLengthInBytes; ++i) { + v2_span[i] = 0x00; + v3_span[i] = 0xff; + } + auto* v2_base = RV32VectorSourceOperand(v2, "v2").GetRegister(0); + auto* v3_base = RV32VectorSourceOperand(v3, "v3").GetRegister(0); + decltype(v2_base) regs[] = {v2_base, v3_base}; + RV32VectorSourceOperand op(regs, "v2"); + + // Group 0 reads from v2 (all zeros). + EXPECT_FALSE(op.AsBool(0)); + EXPECT_EQ(op.AsInt8(0), 0); + EXPECT_EQ(op.AsUint8(0), 0U); + EXPECT_EQ(op.AsInt16(0), 0); + EXPECT_EQ(op.AsUint16(0), 0U); + EXPECT_EQ(op.AsInt32(0), 0); + EXPECT_EQ(op.AsUint32(0), 0U); + EXPECT_EQ(op.AsInt64(0), 0); + EXPECT_EQ(op.AsUint64(0), 0ULL); + EXPECT_EQ(op.GetRegister(0), v2); + + // Group 1 reads from v3 (all ones). + EXPECT_TRUE(op.AsBool(kVLengthInBytes)); + EXPECT_EQ(op.AsInt8(kVLengthInBytes), -1); + EXPECT_EQ(op.AsUint8(kVLengthInBytes), 0xffU); + EXPECT_EQ(op.AsInt16(kVLengthInBytes / 2), -1); + EXPECT_EQ(op.AsUint16(kVLengthInBytes / 2), 0xffffU); + EXPECT_EQ(op.AsInt32(kVLengthInBytes / 4), -1); + EXPECT_EQ(op.AsUint32(kVLengthInBytes / 4), 0xffff'ffffU); + EXPECT_EQ(op.AsInt64(kVLengthInBytes / 8), -1); + EXPECT_EQ(op.AsUint64(kVLengthInBytes / 8), 0xffff'ffff'ffff'ffffULL); + EXPECT_EQ(op.GetRegister(1), v3); + + // Out-of-bounds accesses (group >= 2) clamp to the last register in the + // group (v3, index 1) rather than reading v2 or past the end of registers_. + EXPECT_TRUE(op.AsBool(2 * kVLengthInBytes)); + EXPECT_EQ(op.AsInt8(2 * kVLengthInBytes), -1); + EXPECT_EQ(op.AsUint8(2 * kVLengthInBytes), 0xffU); + EXPECT_EQ(op.AsInt16(2 * (kVLengthInBytes / 2)), -1); + EXPECT_EQ(op.AsUint16(2 * (kVLengthInBytes / 2)), 0xffffU); + EXPECT_EQ(op.AsInt32(2 * (kVLengthInBytes / 4)), -1); + EXPECT_EQ(op.AsUint32(2 * (kVLengthInBytes / 4)), 0xffff'ffffU); + EXPECT_EQ(op.AsInt64(2 * (kVLengthInBytes / 8)), -1); + EXPECT_EQ(op.AsUint64(2 * (kVLengthInBytes / 8)), 0xffff'ffff'ffff'ffffULL); + EXPECT_EQ(op.GetRegister(2), v3); + EXPECT_EQ(op.GetRegister(7), v3); +} + } // namespace