Skip to content

Commit

Permalink
Added proto2BB conversion with interfered register lists
Browse files Browse the repository at this point in the history
  • Loading branch information
9Tempest committed Dec 5, 2023
1 parent 29cc7a7 commit 8f13408
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion gematria/basic_block/basic_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& 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;
}

Expand Down
8 changes: 7 additions & 1 deletion gematria/basic_block/basic_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& interfered_registers);
static InstructionOperand Register(std::string register_name);
static InstructionOperand ImmediateValue(uint64_t immediate_value);
static InstructionOperand FpImmediateValue(double fp_immediate_value);
Expand All @@ -169,6 +169,11 @@ class InstructionOperand {
// Returns the list of tokens representing this instruction.
std::vector<std::string> AsTokenList() const;

std::vector<std::string> 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
Expand Down Expand Up @@ -225,6 +230,7 @@ class InstructionOperand {
double fp_immediate_value_ = 0.0;
AddressTuple address_;
int alias_group_id_ = 0;
std::vector<std::string> interfered_registers_;
};

std::ostream& operator<<(std::ostream& os, const InstructionOperand& operand);
Expand Down
16 changes: 13 additions & 3 deletions gematria/basic_block/basic_block_protos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@

namespace gematria {

namespace {
std::vector<std::string> ToVector(
const google::protobuf::RepeatedPtrField<std::string>& protos) {
return std::vector<std::string>(protos.begin(), protos.end());
}
}

AddressTuple AddressTupleFromProto(
const CanonicalizedOperandProto::AddressTuple& proto) {
return AddressTuple(
Expand Down Expand Up @@ -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<std::string> interfered_registers = ToVector(proto.intefered_register());
return InstructionOperand::VirtualRegister(
proto.virtual_register().name(), proto.virtual_register().size(), interfered_registers);
}

}
}

Expand Down Expand Up @@ -102,7 +113,6 @@ CanonicalizedOperandProto ProtoFromInstructionOperand(
}

namespace {

std::vector<InstructionOperand> ToVector(
const google::protobuf::RepeatedPtrField<CanonicalizedOperandProto>&
protos) {
Expand Down
8 changes: 6 additions & 2 deletions gematria/basic_block/basic_block_protos_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
Expand All @@ -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 = */ {},
Expand Down
2 changes: 1 addition & 1 deletion gematria/basic_block/python/basic_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstructionOperand (*)(
std::string /* base_register */, int64_t /* displacement */,
std::string /* index_register */, int /* scaling */,
Expand Down
6 changes: 1 addition & 5 deletions gematria/llvm/canonicalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ void ReplaceExprOperands(llvm::MachineInstr& instruction) {
for (int i = 0; i < instruction.getNumOperands(); ++i) {
llvm::MachineOperand& operand = instruction.getOperand(i);
if (operand.isSymbol() || operand.isGlobal() || operand.isCPI()) {
// TODO(ondrasej): In some cases the value may change the binary encoding
// of the instruction, e.g. switch between an 8-bit or a 32-bit encoding
// of the displacement and 0 might have a special meaning (e.g. do not use
// displacement at all).
operand = llvm::MachineOperand::CreateImm(1);
}
}
Expand Down Expand Up @@ -508,7 +504,7 @@ void X86Canonicalizer::AddOperand(const llvm::MachineInstr& mi, int operand_inde
InstructionOperand::Register(name));
} else {
operand_list.push_back(
InstructionOperand::VirtualRegister(name, size));
InstructionOperand::VirtualRegister(name, size, {}));
}
} else if (operand.isImm()) {
operand_list.push_back(
Expand Down

0 comments on commit 8f13408

Please sign in to comment.