Skip to content

Commit

Permalink
[SOL] Use ADD for subtracting stack pointer (anza-xyz#70)
Browse files Browse the repository at this point in the history
This PR replaces sub r11, imm by add r11, -imm and is the equivalent of solana-labs/rbpf#488 for the SBF target in LLVM.

This is the first task in solana-labs/solana#34250
  • Loading branch information
LucasSte committed Jan 30, 2024
1 parent e7b77b0 commit 8d0422c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
12 changes: 5 additions & 7 deletions llvm/lib/Target/SBF/SBFFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"

using namespace llvm;

namespace {

void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator &MBBI,
unsigned int Opcode) {
MachineBasicBlock::iterator &MBBI, bool IsSubtract) {
MachineFrameInfo &MFI = MF.getFrameInfo();
int NumBytes = (int)MFI.getStackSize();
if (NumBytes) {
DebugLoc Dl;
const SBFInstrInfo &TII =
*static_cast<const SBFInstrInfo *>(MF.getSubtarget().getInstrInfo());
BuildMI(MBB, MBBI, Dl, TII.get(Opcode), SBF::R11)
BuildMI(MBB, MBBI, Dl, TII.get(SBF::ADD_ri), SBF::R11)
.addReg(SBF::R11)
.addImm(NumBytes);
.addImm(IsSubtract ? -NumBytes : NumBytes);
}
}

Expand All @@ -47,7 +45,7 @@ void SBFFrameLowering::emitPrologue(MachineFunction &MF,
return;
}
MachineBasicBlock::iterator MBBI = MBB.begin();
adjustStackPointer(MF, MBB, MBBI, SBF::SUB_ri);
adjustStackPointer(MF, MBB, MBBI, true);
}

void SBFFrameLowering::emitEpilogue(MachineFunction &MF,
Expand All @@ -56,7 +54,7 @@ void SBFFrameLowering::emitEpilogue(MachineFunction &MF,
return;
}
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
adjustStackPointer(MF, MBB, MBBI, SBF::ADD_ri);
adjustStackPointer(MF, MBB, MBBI, false);
}

void SBFFrameLowering::determineCalleeSaves(MachineFunction &MF,
Expand Down
31 changes: 31 additions & 0 deletions llvm/test/CodeGen/SBF/dynamic_stack_frame_add_not_sub.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; RUN: llc < %s -march=sbf --mattr=+dynamic-frames | FileCheck %s
;
; Source:
; int test_func(int * vec, int idx) {
; vec[idx] = idx-1;
; return idx;
; }
; Compilation flag:
; clang -S -emit-llvm test.c


; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
define i32 @test_func(ptr noundef %vec, i32 noundef %idx) #0 {
; CHECK-LABEL: test_func:
; CHECK: add64 r11, -16
; CHECK: add64 r11, 16
entry:
%vec.addr = alloca ptr, align 8
%idx.addr = alloca i32, align 4
store ptr %vec, ptr %vec.addr, align 8
store i32 %idx, ptr %idx.addr, align 4
%0 = load i32, ptr %idx.addr, align 4
%sub = sub nsw i32 %0, 1
%1 = load ptr, ptr %vec.addr, align 8
%2 = load i32, ptr %idx.addr, align 4
%idxprom = sext i32 %2 to i64
%arrayidx = getelementptr inbounds i32, ptr %1, i64 %idxprom
store i32 %sub, ptr %arrayidx, align 4
%3 = load i32, ptr %idx.addr, align 4
ret i32 %3
}

0 comments on commit 8d0422c

Please sign in to comment.