Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EVM] Refactor StackSlot std::variant -> LLVM style RTTI #760

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 10 additions & 64 deletions llvm/lib/Target/EVM/EVMStackDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,82 +24,28 @@ template <class... Ts> struct Overload : Ts... {
};
template <class... Ts> Overload(Ts...) -> Overload<Ts...>;

static std::string getInstName(const MachineInstr *MI) {
const MachineFunction *MF = MI->getParent()->getParent();
const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
return TII->getName(MI->getOpcode()).str();
}

const Function *llvm::getCalledFunction(const MachineInstr &MI) {
for (const MachineOperand &MO : MI.operands()) {
if (!MO.isGlobal())
continue;
const Function *Func = dyn_cast<Function>(MO.getGlobal());
if (Func != nullptr)
return Func;
}
return nullptr;
}

std::string llvm::stackToString(const Stack &S) {
std::string Result("[ ");
for (auto const &Slot : S)
Result += stackSlotToString(Slot) + ' ';
for (const auto *Slot : S)
Result += Slot->toString() + ' ';
Result += ']';
return Result;
}

std::string llvm::stackSlotToString(const StackSlot &Slot) {
return std::visit(
Overload{
[](const FunctionCallReturnLabelSlot &Ret) -> std::string {
return "RET[" +
std::string(getCalledFunction(*Ret.Call)->getName()) + "]";
},
[](const FunctionReturnLabelSlot &) -> std::string { return "RET"; },
[](const VariableSlot &Var) -> std::string {
SmallString<64> S;
raw_svector_ostream OS(S);
OS << printReg(Var.VirtualReg, nullptr, 0, nullptr);
return std::string(S);
;
},
[](const LiteralSlot &Literal) -> std::string {
SmallString<64> S;
Literal.Value.toStringSigned(S);
return std::string(S);
},
[](const SymbolSlot &Symbol) -> std::string {
return getInstName(Symbol.MI) + ":" +
std::string(Symbol.Symbol->getName());
},
[](const TemporarySlot &Tmp) -> std::string {
SmallString<128> S;
raw_svector_ostream OS(S);
OS << "TMP[" << getInstName(Tmp.MI) << ", ";
OS << std::to_string(Tmp.Index) + "]";
return std::string(S);
},
[](const JunkSlot &Junk) -> std::string { return "JUNK"; }},
Slot);
;
}

#ifndef NDEBUG

void StackLayoutPrinter::operator()() {
OS << "Function: " << MF.getName() << "(";
for (const StackSlot &ParamSlot : StackModel.getFunctionParameters()) {
if (const auto *Slot = std::get_if<VariableSlot>(&ParamSlot))
OS << printReg(Slot->VirtualReg, nullptr, 0, nullptr) << ' ';
else if (std::holds_alternative<JunkSlot>(ParamSlot))
for (const StackSlot *ParamSlot : StackModel.getFunctionParameters()) {
if (const auto *Slot = dyn_cast<VariableSlot>(ParamSlot))
OS << printReg(Slot->getReg(), nullptr, 0, nullptr) << ' ';
else if (isa<JunkSlot>(ParamSlot))
OS << "[unused param] ";
else
llvm_unreachable("Unexpected stack slot");
}
OS << ");\n";
OS << "FunctionEntry "
<< " -> Block" << getBlockId(MF.front()) << ";\n";
OS << "FunctionEntry " << " -> Block" << getBlockId(MF.front()) << ";\n";

for (const auto &MBB : MF) {
printBlock(MBB);
Expand All @@ -123,8 +69,8 @@ void StackLayoutPrinter::printBlock(MachineBasicBlock const &Block) {
},
[&](Assignment const &Assignment) {
OS << "Assignment(";
for (const auto &Var : Assignment.Variables)
OS << printReg(Var.VirtualReg, nullptr, 0, nullptr)
for (const auto *Var : Assignment.Variables)
OS << printReg(Var->getReg(), nullptr, 0, nullptr)
<< ", ";
OS << ")";
}},
Expand Down Expand Up @@ -153,7 +99,7 @@ void StackLayoutPrinter::printBlock(MachineBasicBlock const &Block) {
auto [CondBr, UncondBr, TrueBB, FalseBB, Condition] =
TermInfo->getConditionalBranch();
OS << "Block" << getBlockId(Block) << "Exit [label=\"{ ";
OS << stackSlotToString(StackModel.getStackSlot(*Condition));
OS << StackModel.getStackSlot(*Condition)->toString();
OS << "| { <0> Zero | <1> NonZero }}\"];\n";
OS << "Block" << getBlockId(Block);
OS << "Exit:0 -> Block" << getBlockId(*FalseBB) << ";\n";
Expand Down
6 changes: 0 additions & 6 deletions llvm/lib/Target/EVM/EVMStackDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@
#include "EVMMachineCFGInfo.h"
#include "EVMStackModel.h"
#include "llvm/CodeGen/MachineFunction.h"
#include <cassert>
#include <map>
#include <numeric>
#include <set>
#include <variant>

namespace llvm {

class EVMStackLayout;

const Function *getCalledFunction(const MachineInstr &MI);
std::string stackSlotToString(const StackSlot &Slot);
std::string stackToString(Stack const &S);

#ifndef NDEBUG
Expand Down
Loading