[llvm] [RISCV][GlobalISel] Legalize readcyclecounter/readsteadycounter (PR #217535)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 21 10:29:31 PDT 2026
================
@@ -889,6 +896,106 @@ bool RISCVLegalizerInfo::legalizeVAStart(MachineInstr &MI,
return true;
}
+bool RISCVLegalizerInfo::legalizeReadCounter(
+ MachineInstr &MI, MachineIRBuilder &MIRBuilder,
+ GISelChangeObserver &Observer) const {
+ assert((MI.getOpcode() == TargetOpcode::G_READCYCLECOUNTER ||
+ MI.getOpcode() == TargetOpcode::G_READSTEADYCOUNTER) &&
+ "Unexpected opcode");
+ assert(!STI.is64Bit() && "READCYCLECOUNTER/READSTEADYCOUNTER only "
+ "has custom type legalization on riscv32");
+
+ // On RV32 a 64-bit counter CSR must be read as two 32-bit halves. Because
+ // the count may wrap between the two reads, re-read the high half and loop
+ // until the two high reads agree.
+ int64_t LoCounter, HiCounter;
+ if (MI.getOpcode() == TargetOpcode::G_READCYCLECOUNTER) {
+ LoCounter = RISCVSysReg::cycle;
+ HiCounter = RISCVSysReg::cycleh;
+ } else {
+ LoCounter = RISCVSysReg::time;
+ HiCounter = RISCVSysReg::timeh;
+ }
+
+ MachineBasicBlock *BB = MI.getParent();
+ MachineFunction &MF = *BB->getParent();
+ const BasicBlock *LLVMBB = BB->getBasicBlock();
+ DebugLoc DL = MI.getDebugLoc();
+ MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
+
+ // Split BB into an entry that falls through into a loop block, and a done
+ // block that receives the remainder of BB and its original successors.
+ MachineFunction::iterator It = std::next(BB->getIterator());
+ MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVMBB);
+ MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(LLVMBB);
+ MF.insert(It, LoopMBB);
+ MF.insert(It, DoneMBB);
+
+ // Splice the instructions after the readcyclecounter into DoneMBB, notifying
+ // the observer about each moved instruction so CSEInfo stays consistent.
+ SmallVector<MachineInstr *, 4> MovedInstrs;
+ for (MachineBasicBlock::iterator I = std::next(MI.getIterator()),
+ E = BB->end();
+ I != E; ++I)
+ MovedInstrs.push_back(&*I);
+ for (MachineInstr *MovedMI : MovedInstrs)
+ Observer.changingInstr(*MovedMI);
+ DoneMBB->splice(DoneMBB->begin(), BB,
+ std::next(MachineBasicBlock::iterator(MI)), BB->end());
+ for (MachineInstr *MovedMI : MovedInstrs)
+ Observer.changedInstr(*MovedMI);
+ DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
+ BB->addSuccessor(LoopMBB);
+
+ LLT S32 = LLT::scalar(32);
+ // Generic vregs carry the s32 type for G_MERGE_VALUES below, but are also
+ // constrained to GPR so the target CSRRS/BNE instructions satisfy the
+ // verifier's register-class constraints.
+ auto CreateGPR = [&]() {
+ Register R = MRI.createGenericVirtualRegister(S32);
+ MRI.setRegClass(R, &RISCV::GPRRegClass);
+ return R;
+ };
+ Register LoReg = CreateGPR();
+ Register HiReg = CreateGPR();
+ Register ReadAgainReg = CreateGPR();
+
+ // read:
+ // csrrs HiReg, counterh # high word
+ // csrrs LoReg, counter # low word
+ // csrrs ReadAgainReg, counterh
+ // bne HiReg, ReadAgainReg, read
+ // Emit the target instructions directly with BuildMI.
+ const RISCVInstrInfo *TII = STI.getInstrInfo();
+ BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), HiReg)
----------------
arsenm wrote:
IIRC I tried to allow block splitting during InstructionSelect but decided it wasn't worth the complexity, if you want control flow it's better to do it in the legalizer. Having a G_TARGET_* pseudo is a convenience if you don't want to have to deal with the regbankselect stuff yourself here
https://github.com/llvm/llvm-project/pull/217535
More information about the llvm-commits
mailing list