Skip to content

Commit

Permalink
ssr: Add barrier instructions that polls for done bit (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
huettern committed Aug 11, 2021
1 parent 7621f44 commit 93c1ed5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ void __builtin_ssr_setup_bound_stride_3d(uint32_t DM, uint32_t b, uint32_t s);
* @param s relative stride
*/
void __builtin_ssr_setup_bound_stride_4d(uint32_t DM, uint32_t b, uint32_t s);

/**
* @brief Wait for the done bit to be set on data mover `DM`
* @details Creates a polling loop and might not exit if SSR not configured correctly
*
* @param DM data mover ID
*/
void __builtin_ssr_barrier(uint32_t DM);
```
### SDMA
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/BuiltinsRISCV.def
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SSR_BUILTIN(push, "vUid", "n", "xssr")
SSR_BUILTIN(pop, "dUi", "n", "xssr")
SSR_BUILTIN(enable, "v", "n", "xssr")
SSR_BUILTIN(disable, "v", "n", "xssr")
SSR_BUILTIN(barrier, "vUi", "n", "xssr")

// SDMA builtins

Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsRISCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,9 @@ let TargetPrefix = "riscv" in {
[llvm_i32_ty, llvm_i32_ty],
[IntrHasSideEffects, ImmArg<ArgIndex<0>>]>,
RISCVSSRIntrinsic;
def int_riscv_ssr_barrier
: GCCBuiltin<"__builtin_ssr_barrier">,
Intrinsic<[], [llvm_i32_ty], [IntrHasSideEffects, ImmArg<ArgIndex<0>>]>, RISCVSSRIntrinsic;

} // TargetPrefix = "riscv"

Expand Down
48 changes: 48 additions & 0 deletions llvm/lib/Target/RISCV/RISCVExpandSSRInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class RISCVExpandSSR : public MachineFunctionPass {
MachineBasicBlock::iterator MBBI);
bool expandSSR_SetupRep(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI);
bool expandSSR_Barrier(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
MachineBasicBlock::iterator &NextMBBI);

RISCVExpandSSR::RegisterMergingPreferences gatherRegisterMergingPreferences();
};
Expand Down Expand Up @@ -165,6 +168,8 @@ bool RISCVExpandSSR::expandMI(MachineBasicBlock &MBB,
return expandSSR_EnDis(MBB, MBBI);
case RISCV::PseudoSSRSetupRepetition:
return expandSSR_SetupRep(MBB, MBBI);
case RISCV::PseudoSSRBarrier:
return expandSSR_Barrier(MBB, MBBI, NextMBBI);
}

// Prevent excessive live-ins, they pose a problem with multiple SSR regions
Expand Down Expand Up @@ -347,6 +352,49 @@ bool RISCVExpandSSR::expandSSR_EnDis(MachineBasicBlock &MBB,
return true;
}

bool RISCVExpandSSR::expandSSR_Barrier(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
MachineBasicBlock::iterator &NextMBBI) {
DebugLoc DL = MBBI->getDebugLoc();
MachineInstr &MI = *MBBI;
MachineFunction *MF = MBB.getParent();
MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();

unsigned streamer = (unsigned)MBBI->getOperand(0).getImm();

LLVM_DEBUG(dbgs() << "-- Expanding SSR barrier on DM" << streamer << "\n");

auto LoopMBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
auto DoneMBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());

// Insert new MBBs.
MF->insert(++MBB.getIterator(), LoopMBB);
MF->insert(++LoopMBB->getIterator(), DoneMBB);

// Set up successors and transfer remaining instructions to DoneMBB.
LoopMBB->addSuccessor(LoopMBB);
LoopMBB->addSuccessor(DoneMBB);
DoneMBB->splice(DoneMBB->end(), &MBB, MI, MBB.end());
DoneMBB->transferSuccessorsAndUpdatePHIs(&MBB);
MBB.addSuccessor(LoopMBB);

// build loop: %0 = scfgri 0 | DM; srli %0, %0, 31; beq %0, zero, loop
Register R = MRI.createVirtualRegister(&RISCV::GPRRegClass);
BuildMI(LoopMBB, DL, TII->get(RISCV::SCFGRI), R).addImm(streamer);
Register Rs = MRI.createVirtualRegister(&RISCV::GPRRegClass);
BuildMI(LoopMBB, DL, TII->get(RISCV::SRLI), Rs).addReg(R, RegState::Kill).addImm(31);
BuildMI(LoopMBB, DL, TII->get(RISCV::BEQ)).addReg(Rs, RegState::Kill).addReg(RISCV::X0).addMBB(LoopMBB);

NextMBBI = MBB.end();
MI.eraseFromParent();

LivePhysRegs LiveRegs;
computeAndAddLiveIns(LiveRegs, *LoopMBB);
computeAndAddLiveIns(LiveRegs, *DoneMBB);

return true;
}

void RISCVExpandSSR::mergePushPop(MachineBasicBlock &MBB) {
SmallSet<Register, 8> virtRegs[NUM_SSR];
const TargetRegisterInfo *TRI = MBB.getParent()->getRegInfo().getTargetRegisterInfo();
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfoXssr.td
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ class SPseudoEnDis:
let usesCustomInserter = 0;
}

class SPseudoBarrier:
Pseudo<(outs), (ins uimm5:$ssr),[]> {
let hasSideEffects = 1;
let usesCustomInserter = 0;
}

let Predicates = [HasExtXssr] in {
def PseudoSSRSetup_1D_R : SPseudoSetup1D;
def PseudoSSRSetup_1D_W : SPseudoSetup1D;
Expand All @@ -144,6 +150,7 @@ let Predicates = [HasExtXssr] in {
def PseudoSSRRead : SPseudoRW;
def PseudoSSRWrite : SPseudoRW;
def PseudoSSRSetupRepetition : SPseudoSetupRepetition;
def PseudoSSRBarrier : SPseudoBarrier;

// pattern matching on intrinsic and resulting in pseudo instruction
def : Pat<(int_riscv_ssr_setup_1d_r timm:$ssr, GPR:$rep, GPR:$bound, GPR:$stride, GPR:$ptr),
Expand Down Expand Up @@ -176,4 +183,7 @@ let Predicates = [HasExtXssr] in {
def : Pat<(int_riscv_ssr_enable), (PseudoSSREnable)>;
def : Pat<(int_riscv_ssr_disable), (PseudoSSRDisable)>;

def : Pat<(int_riscv_ssr_barrier timm:$ssr),
(PseudoSSRBarrier timm:$ssr)>;

} // Predicates = [HasExtXssr]
24 changes: 24 additions & 0 deletions llvm/test/CodeGen/RISCV/ssr-pseudo-instructions.mir
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
define i32 @outline_3(i32 %a, i32 %b) { ret i32 0 }
define i32 @outline_4(i32 %a, i32 %b) { ret i32 0 }
define i32 @outline_5(i32 %a, i32 %b) { ret i32 0 }
define i32 @outline_6(i32 %a, i32 %b) { ret i32 0 }
...
---
name: outline_0
Expand Down Expand Up @@ -96,3 +97,26 @@ body: |
PseudoSSRDisable
PseudoRET
...
---
name: outline_6
tracksRegLiveness: true
body: |
bb.0:
liveins:
; RV32-SSR: bb.1
; RV32-SSR: %0:gpr = SCFGRI 0
; RV32-SSR-NEXT: %1:gpr = SRLI killed %0, 31
; RV32-SSR-NEXT: BEQ killed %1, $x0, %bb.1
PseudoSSRBarrier 0
; RV32-SSR: bb.3
; RV32-SSR: %2:gpr = SCFGRI 1
; RV32-SSR-NEXT: %3:gpr = SRLI killed %2, 31
; RV32-SSR-NEXT: BEQ killed %3, $x0, %bb.3
PseudoSSRBarrier 1
PseudoRET
...

0 comments on commit 93c1ed5

Please sign in to comment.