From 8f1340823790bf84d31cfe402b42cc22615d1d08 Mon Sep 17 00:00:00 2001 From: z277zhu Date: Tue, 5 Dec 2023 13:43:56 -0500 Subject: [PATCH] Added proto2BB conversion with interfered register lists --- gematria/basic_block/basic_block.cc | 3 ++- gematria/basic_block/basic_block.h | 8 +++++++- gematria/basic_block/basic_block_protos.cc | 16 +++++++++++++--- gematria/basic_block/basic_block_protos_test.cc | 8 ++++++-- gematria/basic_block/python/basic_block.cc | 2 +- gematria/llvm/canonicalizer.cc | 6 +----- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/gematria/basic_block/basic_block.cc b/gematria/basic_block/basic_block.cc index 2a65bafb..4047a61c 100644 --- a/gematria/basic_block/basic_block.cc +++ b/gematria/basic_block/basic_block.cc @@ -108,11 +108,12 @@ bool InstructionOperand::operator==(const InstructionOperand& other) const { } InstructionOperand InstructionOperand::VirtualRegister( - const std::string register_name, size_t size) { + const std::string register_name, size_t size, const std::vector& interfered_registers) { InstructionOperand result; result.type_ = OperandType::kVirtualRegister; result.register_name_ = std::move(register_name); result.size_ = size; + result.interfered_registers_ = std::move(interfered_registers); return result; } diff --git a/gematria/basic_block/basic_block.h b/gematria/basic_block/basic_block.h index e7a1de86..9dd8741f 100644 --- a/gematria/basic_block/basic_block.h +++ b/gematria/basic_block/basic_block.h @@ -147,7 +147,7 @@ class InstructionOperand { // The operands must be created through one of the factory functions. static InstructionOperand VirtualRegister(std::string register_name, - size_t size); + size_t size, const std::vector& interfered_registers); static InstructionOperand Register(std::string register_name); static InstructionOperand ImmediateValue(uint64_t immediate_value); static InstructionOperand FpImmediateValue(double fp_immediate_value); @@ -169,6 +169,11 @@ class InstructionOperand { // Returns the list of tokens representing this instruction. std::vector AsTokenList() const; + std::vector getInterferedRegisters() const { + assert(type_ == OperandType::kVirtualRegister); + return interfered_registers_; + } + // Returns a human-readable representation of the operand. // // This method implements the __str__() and __repr__() methods in the Python @@ -225,6 +230,7 @@ class InstructionOperand { double fp_immediate_value_ = 0.0; AddressTuple address_; int alias_group_id_ = 0; + std::vector interfered_registers_; }; std::ostream& operator<<(std::ostream& os, const InstructionOperand& operand); diff --git a/gematria/basic_block/basic_block_protos.cc b/gematria/basic_block/basic_block_protos.cc index 98c5ef56..f8f88742 100644 --- a/gematria/basic_block/basic_block_protos.cc +++ b/gematria/basic_block/basic_block_protos.cc @@ -25,6 +25,13 @@ namespace gematria { +namespace { + std::vector ToVector( + const google::protobuf::RepeatedPtrField& protos) { + return std::vector(protos.begin(), protos.end()); + } +} + AddressTuple AddressTupleFromProto( const CanonicalizedOperandProto::AddressTuple& proto) { return AddressTuple( @@ -64,8 +71,12 @@ InstructionOperand InstructionOperandFromProto( return InstructionOperand::MemoryLocation( proto.memory().alias_group_id()); case CanonicalizedOperandProto::kVirtualRegister: - return InstructionOperand::VirtualRegister( - proto.virtual_register().name(), proto.virtual_register().size()); + { + std::vector interfered_registers = ToVector(proto.intefered_register()); + return InstructionOperand::VirtualRegister( + proto.virtual_register().name(), proto.virtual_register().size(), interfered_registers); + } + } } @@ -102,7 +113,6 @@ CanonicalizedOperandProto ProtoFromInstructionOperand( } namespace { - std::vector ToVector( const google::protobuf::RepeatedPtrField& protos) { diff --git a/gematria/basic_block/basic_block_protos_test.cc b/gematria/basic_block/basic_block_protos_test.cc index 60d9424e..006dec19 100644 --- a/gematria/basic_block/basic_block_protos_test.cc +++ b/gematria/basic_block/basic_block_protos_test.cc @@ -239,7 +239,11 @@ TEST(BasicBlockFromProtoTest, VRegInstructions) { canonicalized_instructions { mnemonic: "CMP64RI32" llvm_mnemonic: "CMP64ri32" - input_operands { virtual_register { name: "%60" size: 64 } } + input_operands { + virtual_register { name: "%60" size: 64 } + intefered_register: "%61" + intefered_register: "%62" + } input_operands { immediate_value: 0 } implicit_output_operands { register_name: "EFLAGS" } } @@ -250,7 +254,7 @@ TEST(BasicBlockFromProtoTest, VRegInstructions) { /* mnemonic = */ "CMP64RI32", /* llvm_mnemonic = */ "CMP64ri32", /* prefixes = */ {}, /* input_operands = */ - {InstructionOperand::VirtualRegister("%60", 64), + {InstructionOperand::VirtualRegister("%60", 64, {"%61, %62"}), InstructionOperand::ImmediateValue(0)}, /* implicit_input_operands = */ {}, /* output_operands = */ {}, diff --git a/gematria/basic_block/python/basic_block.cc b/gematria/basic_block/python/basic_block.cc index ad231734..fb4da159 100644 --- a/gematria/basic_block/python/basic_block.cc +++ b/gematria/basic_block/python/basic_block.cc @@ -156,7 +156,7 @@ PYBIND11_MODULE(basic_block, m) { py::arg("fp_immediate_value")) .def_static("from_virtual_register", &InstructionOperand::VirtualRegister, - py::arg("register_name"), py::arg("size") = 0) + py::arg("register_name"), py::arg("size"), py::arg("interfered_registers")) .def_static