Skip to content
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
13 changes: 10 additions & 3 deletions llvm/lib/Target/EVM/EVMMarkRecursiveFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
// functions to detect unknown calls (as it is implemented in FunctionAttrs.cpp,
// function createSCCNodeSet), because all functions are known at compile time,
// and we don't have any indirect calls.
// This is needed during stackification, since we can't use spills for recursive
// functions, as we are using memory for spills, and not the real stack.
// The attribute value is the id of the function's call graph cycle. A call
// into a different cycle can never re-enter the caller.
// Stackification depends on this attribute. Spill slots live at fixed
// memory addresses, so every activation of a recursive function reuses
// them. For marked functions the stackifier callee-saves each spill slot
// that is live across a call into the same cycle.
//
//===----------------------------------------------------------------------===//

Expand Down Expand Up @@ -50,6 +54,7 @@ class EVMMarkRecursiveFunctions final : public ModulePass {

static bool runImpl(CallGraph &CG) {
bool Changed = false;
unsigned SCCId = 0;
for (auto SCCI = scc_begin(&CG); !SCCI.isAtEnd(); ++SCCI) {
const auto &SCC = *SCCI;
// Only mark indirect recursive functions (size > 1) or self-recursive
Expand All @@ -58,11 +63,13 @@ static bool runImpl(CallGraph &CG) {
if (SCC.size() == 1 && !SCCI.hasCycle())
continue;

// The attribute value is the id of the function's call graph cycle.
const std::string Id = std::to_string(SCCId++);
for (const auto *Node : SCC) {
Function *F = Node->getFunction();
if (!F || F->isDeclaration())
continue;
F->addFnAttr("evm-recursive");
F->addFnAttr("evm-recursive", Id);
Changed = true;
}
}
Expand Down
108 changes: 100 additions & 8 deletions llvm/lib/Target/EVM/EVMStackModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,103 @@ std::string CallerReturnSlot::toString() const {

EVMStackModel::EVMStackModel(MachineFunction &MF, const LiveIntervals &LIS,
unsigned StackDepthLimit)
: MF(MF), LIS(LIS), StackDepthLimit(StackDepthLimit) {
for (MachineBasicBlock &MBB : MF)
: MF(MF), LIS(LIS), StackDepthLimit(StackDepthLimit),
IsRecursive(MF.getFunction().hasFnAttribute("evm-recursive")),
HasReturnBlock(false),
FunctionParameters(MF.getInfo<EVMMachineFunctionInfo>()->getNumParams(),
getUnusedSlot()) {
for (MachineBasicBlock &MBB : MF) {
HasReturnBlock |= MBB.isReturnBlock();
for (const MachineInstr &MI : instructionsToProcess(&MBB))
processMI(MI);
}
}

// A function without blocks has no arguments to collect.
if (MF.empty())
return;

Stack EVMStackModel::getFunctionParameters() const {
auto *MFI = MF.getInfo<EVMMachineFunctionInfo>();
Stack Parameters(MFI->getNumParams(), EVMStackModel::getUnusedSlot());
for (const MachineInstr &MI : MF.front()) {
if (MI.getOpcode() == EVM::ARGUMENT) {
int64_t ArgIdx = MI.getOperand(1).getImm();
Parameters[ArgIdx] = getRegisterSlot(MI.getOperand(0).getReg());
FunctionParameters[ArgIdx] = getRegisterSlot(MI.getOperand(0).getReg());
}
}
return Parameters;
}

// Return true if \p Callee belongs to the same call graph cycle as \p F, so
// a call to it may create a new activation of \p F. The cycle id is the value
// of the "evm-recursive" attribute, see EVMMarkRecursiveFunctions.
static bool inSameRecursiveCycle(const Function &F, const Function &Callee) {
if (&Callee == &F)
return true;

if (!F.hasFnAttribute("evm-recursive") ||
!Callee.hasFnAttribute("evm-recursive"))
return false;

return F.getFnAttribute("evm-recursive").getValueAsString() ==
Callee.getFnAttribute("evm-recursive").getValueAsString();
}

bool EVMStackModel::crossesReentrantCall(const Register &Reg) const {
// The register crosses the call if its value is live into the call and
// is not killed by it. A register defined by the call has no live-in
// value there. A register whose last use is a call argument is killed at
// the call, and its last reload happens before the callee starts.
const LiveInterval &LI = LIS.getInterval(Reg);
return any_of(ReentrantCalls, [&LI, this](const MachineInstr *Call) {
LiveQueryResult Q = LI.Query(LIS.getInstructionIndex(*Call));
return Q.valueIn() && !Q.isKill();
});
}

void EVMStackModel::addSpillRegs(const SmallSet<Register, 4> &SpillRegs) const {
for (const auto &R : SpillRegs) {
// Note: getRegisterSlot may create the slot. A register whose only use
// is RET has no slot yet at this point, because RET is not processed by
// the model.
auto *RegSlot = getRegisterSlot(R);
assert(!RegSlot->isSpill() &&
"Register slot has already been marked as spill");
RegSlot->setIsSpill();
}

if (!spillsNeedCalleeSave())
return;

// Sort for a deterministic slot creation order.
SmallVector<Register> Sorted(SpillRegs.begin(), SpillRegs.end());
llvm::sort(Sorted, [](Register A, Register B) { return A.id() < B.id(); });
for (const auto &R : Sorted)
if (crossesReentrantCall(R))
getCalleeSavedSlot(R);
}

Stack EVMStackModel::getFunctionEntryStack() const {
Stack EntryStack;
bool IsNoReturn = MF.getFunction().hasFnAttribute(Attribute::NoReturn);

// We assume the function containing PUSHDEPLOYADDRESS instruction has the
// following properties:
// - It is unique (verified in AsmPrinter)
// - It appears first in the module layout. TODO: #778
// - It does not return and has no arguments
if (const auto *MFI = MF.getInfo<EVMMachineFunctionInfo>();
MFI->getHasPushDeployAddress()) {
MachineBasicBlock::const_iterator I = MF.front().getFirstNonDebugInstr();
assert(I != MF.front().end() && I->getOpcode() == EVM::PUSHDEPLOYADDRESS &&
"MF has no PUSHDEPLOYADDRESS");
assert(IsNoReturn && FunctionParameters.empty() &&
"PUSHDEPLOYADDRESS in a non - void/nullary function");
EntryStack.push_back(getRegisterSlot(I->getOperand(0).getReg()));
}
if (!IsNoReturn)
EntryStack.push_back(getCalleeReturnSlot(&MF));

// Calling convention: input arguments are passed in stack such that the
// first one specified in the function declaration is passed on the stack TOP.
append_range(EntryStack, reverse(FunctionParameters));
return EntryStack;
}

StackSlot *EVMStackModel::getStackSlot(const MachineOperand &MO) const {
Expand Down Expand Up @@ -116,6 +197,13 @@ void EVMStackModel::processMI(const MachineInstr &MI) {
"Unexpected explicit def or use");

if (Opc == EVM::FCALL) {
const auto *Callee =
cast<Function>(MI.explicit_uses().begin()->getGlobal());
if (Callee->hasFnAttribute("evm-recursive"))
RecursiveCalls.insert(&MI);
if (inSameRecursiveCycle(MF.getFunction(), *Callee))
ReentrantCalls.insert(&MI);

Stack Input;
if (!isNoReturnCallMI(MI))
Input.push_back(getCallerReturnSlot(&MI));
Expand Down Expand Up @@ -150,5 +238,9 @@ Stack EVMStackModel::getReturnArguments(const MachineInstr &MI) const {
// last one specified in the RET instruction is passed on the stack TOP.
std::reverse(Input.begin(), Input.end());
Input.push_back(getCalleeReturnSlot(&MF));
// The callee-saved words of the spill slots sit on top. The code emitter
// consumes each of them with a store back to its slot just before the
// return jump. This restores the caller's spill area.
append_range(Input, getCalleeSavedSlots());
return Input;
}
101 changes: 90 additions & 11 deletions llvm/lib/Target/EVM/EVMStackModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "MCTargetDesc/EVMMCTargetDesc.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/LiveIntervals.h"
Expand All @@ -39,6 +40,7 @@ class StackSlot {
SK_Symbol,
SK_CallerReturn,
SK_CalleeReturn,
SK_CalleeSaved,
SK_Unused,
SK_Unknown
};
Expand Down Expand Up @@ -161,6 +163,29 @@ class CalleeReturnSlot final : public StackSlot {
}
};

/// The previous contents of a spill slot of a recursive function.
/// The slot is not rematerializable. Once the spill slot has been
/// overwritten, the saved word exists only on the stack.
class CalleeSavedSlot final : public StackSlot {
Register SpilledReg;

public:
explicit CalleeSavedSlot(const Register &R)
: StackSlot(SK_CalleeSaved), SpilledReg(R) {}
const Register &getReg() const { return SpilledReg; }

bool isRematerializable() const override { return false; }
std::string toString() const override {
SmallString<64> S;
raw_svector_ostream OS(S);
OS << "Saved[" << printReg(SpilledReg, nullptr, 0, nullptr) << ']';
return std::string(S.str());
}
static bool classof(const StackSlot *S) {
return S->getSlotKind() == SK_CalleeSaved;
}
};

/// A slot containing an arbitrary value that is always eventually popped and
/// never used. Used to maintain stack balance on control flow joins.
class UnusedSlot final : public StackSlot {
Expand Down Expand Up @@ -228,6 +253,12 @@ class EVMStackModel {
MachineFunction &MF;
const LiveIntervals &LIS;
unsigned StackDepthLimit;
// The function carries the "evm-recursive" attribute.
const bool IsRecursive;
// The function has an internal return.
bool HasReturnBlock;
// The function parameters, collected once from the ARGUMENT instructions.
Stack FunctionParameters;

// Storage for stack slots.
mutable DenseMap<APInt, std::unique_ptr<LiteralSlot>> LiteralStorage;
Expand All @@ -241,6 +272,16 @@ class EVMStackModel {
// There should be a single CalleeReturnSlot for the MF.
mutable std::unique_ptr<CalleeReturnSlot> TheCalleeReturnSlot;

mutable DenseMap<Register, std::unique_ptr<CalleeSavedSlot>>
CalleeSavedStorage;
// Callee-saved words for the spill slots in creation order.
mutable SmallVector<const CalleeSavedSlot *, 8> CalleeSavedSlots;
// Calls to recursive functions, collected in processMI.
SmallPtrSet<const MachineInstr *, 4> RecursiveCalls;
// Calls that may re-enter this function, i.e. calls into the same call
// graph cycle. A subset of RecursiveCalls, collected in processMI.
SmallPtrSet<const MachineInstr *, 4> ReentrantCalls;

using MBBStackMap = DenseMap<const MachineBasicBlock *, Stack>;
using InstStackMap = DenseMap<const MachineInstr *, Stack>;

Expand All @@ -265,7 +306,12 @@ class EVMStackModel {
public:
EVMStackModel(MachineFunction &MF, const LiveIntervals &LIS,
unsigned StackDepthLimit);
Stack getFunctionParameters() const;
const Stack &getFunctionParameters() const { return FunctionParameters; }
/// The stack at function entry as the caller leaves it: the deploy
/// address register, if the function starts with PUSHDEPLOYADDRESS, the
/// return address, if the function returns, and the parameters with the
/// first one on top. The solver derives the entry MBB's layout from it.
Stack getFunctionEntryStack() const;
Stack getReturnArguments(const MachineInstr &MI) const;

const Stack &getMIInput(const MachineInstr &MI) const {
Expand All @@ -278,35 +324,68 @@ class EVMStackModel {
return Defs;
}

void addSpillRegs(const SmallSet<Register, 4> &SpillRegs) {
for (const auto &R : SpillRegs) {
auto *RegSlot = RegStorage.at(R).get();
assert(!RegSlot->isSpill() &&
"Register slot has already been marked as spill");
RegSlot->setIsSpill();
// Mark the registers as spilled. For a recursive function with internal
// returns, also create the callee-saved slot of each new spill.
void addSpillRegs(const SmallSet<Register, 4> &SpillRegs) const;

bool isRecursive() const { return IsRecursive; }

// Return true if the spill slots of this function must be callee-saved.
// Spill slots have fixed memory addresses, so every activation of a
// recursive function reuses them. Without an internal return nothing can
// observe the slot contents after this function, because all exits abort
// the external call frame. In that case no saving is needed.
bool spillsNeedCalleeSave() const { return IsRecursive && HasReturnBlock; }

// The callee-saved words of this function's spill slots, in creation
// order.
ArrayRef<const CalleeSavedSlot *> getCalleeSavedSlots() const {
return CalleeSavedSlots;
}

// Return true if \p Reg is live across a call that may re-enter this
// function.
bool crossesReentrantCall(const Register &Reg) const;

// Return true if \p MI is a call to a recursive function.
bool isCallToRecursiveFunction(const MachineInstr &MI) const {
return RecursiveCalls.contains(&MI);
}

// Return true if a callee-saved slot exists for the spill slot of \p R.
bool hasCalleeSavedSlot(const Register &R) const {
return CalleeSavedStorage.contains(R);
}

/// Get or create the callee-saved slot for the spill slot of \p R.
const CalleeSavedSlot *getCalleeSavedSlot(const Register &R) const {
if (!CalleeSavedStorage.contains(R)) {
CalleeSavedStorage[R] = std::make_unique<CalleeSavedSlot>(R);
CalleeSavedSlots.push_back(CalleeSavedStorage[R].get());
}
return CalleeSavedStorage[R].get();
}

// Get or create a requested stack slot.
StackSlot *getStackSlot(const MachineOperand &MO) const;
LiteralSlot *getLiteralSlot(const APInt &V) const {
if (LiteralStorage.count(V) == 0)
if (!LiteralStorage.contains(V))
LiteralStorage[V] = std::make_unique<LiteralSlot>(V);
return LiteralStorage[V].get();
}
RegisterSlot *getRegisterSlot(const Register &R) const {
if (RegStorage.count(R) == 0)
if (!RegStorage.contains(R))
RegStorage[R] = std::make_unique<RegisterSlot>(R);
return RegStorage[R].get();
}
SymbolSlot *getSymbolSlot(MCSymbol *S, const MachineInstr *MI) const {
auto Key = std::make_pair(S, MI);
if (SymbolStorage.count(Key) == 0)
if (!SymbolStorage.contains(Key))
SymbolStorage[Key] = std::make_unique<SymbolSlot>(S, MI);
return SymbolStorage[Key].get();
}
CallerReturnSlot *getCallerReturnSlot(const MachineInstr *Call) const {
if (CallerReturnStorage.count(Call) == 0)
if (!CallerReturnStorage.contains(Call))
CallerReturnStorage[Call] = std::make_unique<CallerReturnSlot>(Call);
return CallerReturnStorage[Call].get();
}
Expand Down
Loading
Loading