From cdbd3f9ac80fec2aace8d79a85a6def08ddbf0c1 Mon Sep 17 00:00:00 2001 From: "shaoding.zw" Date: Sun, 29 Nov 2026 08:28:12 +0000 Subject: [PATCH] perf(compiler): reduce CGIR lowering overhead --- .../README.md | 29 +++++++++++++ docs/modules/compiler/spec.md | 11 +++++ src/compiler/cgir/lowering.h | 42 +++++++++---------- src/compiler/cgir/pass/cg_register_info.cpp | 1 - src/compiler/cgir/pass/cg_register_info.h | 38 +++++++++++------ 5 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 docs/changes/2026-11-29-cgir-lowering-storage/README.md diff --git a/docs/changes/2026-11-29-cgir-lowering-storage/README.md b/docs/changes/2026-11-29-cgir-lowering-storage/README.md new file mode 100644 index 000000000..74bd318f6 --- /dev/null +++ b/docs/changes/2026-11-29-cgir-lowering-storage/README.md @@ -0,0 +1,29 @@ +# Change: Reduce CGIR lowering storage overhead + +- **Status**: Implemented +- **Date**: 2026-11-29 +- **Tier**: Light + +## Overview + +Index variable registers by their contiguous MIR variable IDs and use LLVM's +dense map for the expression-to-register cache. Materialize the register +allocation hint table when a caller first sets a hint. + +## Motivation + +MIR lowering runs on the first invocation path of Lazy Multipass. Variable +and expression lookup currently allocate per-entry hash nodes. FastRA also +grows a vector of register allocation hints that it never reads. + +## Impact + +Keep the lookup order, on-demand virtual register creation, expression cache +semantics and register allocation algorithms. Greedy retains its ordered hints. +An unwritten hint reads as an empty hint. + +## Checklist + +- [x] Implementation complete +- [x] Build and tests pass +- [x] Benchmark against the main branch diff --git a/docs/modules/compiler/spec.md b/docs/modules/compiler/spec.md index ba95e21ca..548d05504 100644 --- a/docs/modules/compiler/spec.md +++ b/docs/modules/compiler/spec.md @@ -380,6 +380,17 @@ OOB). Load/store Scale stays 1. See memory operations remain unchanged. This does not enable FMA or globally enable AVX target features. +### CGIR register lookup + +Lowering indexes variable registers by the MIR parameter and local IDs. +Create a virtual register on first use. The separate expression cache +preserves whether a particular MIR instruction was already materialized. +This distinction also covers expressions with no result register. + +Allocate the register allocation hint table on the first hint write. An +unwritten entry returns an empty hint. Greedy uses the same hint types and +insertion order. FastRA can allocate registers without populating this table. + ### Scalar x86 conversion dependency breaking At final x86 MC emission, register forms `CVTSI2SSrr`, `CVTSI2SDrr`, diff --git a/src/compiler/cgir/lowering.h b/src/compiler/cgir/lowering.h index 3bbf80010..1ab01f3ca 100644 --- a/src/compiler/cgir/lowering.h +++ b/src/compiler/cgir/lowering.h @@ -9,6 +9,7 @@ #include "compiler/llvm-prebuild/Target/X86/X86Subtarget.h" #include "compiler/mir/instructions.h" #include "compiler/mir/opcode.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/CodeGen/TargetLowering.h" #include "llvm/CodeGen/TargetOpcodes.h" @@ -26,8 +27,8 @@ template class CgLowering { TRI(cg_func.getRegisterInfo()), TII(cg_func.getTargetInstrInfo()), TLI(*cg_func.getContext().getSubtargetInfo().getTargetLowering()), MRI(cg_func.getRegInfo()), - _expr_reg_map(_mir_func.getNumInstructions(), _mpool), - _var_reg_map(_mir_func.getNumVariables(), _mpool), + _expr_reg_map(_mir_func.getNumInstructions()), + VariableRegisters(_mir_func.getNumVariables(), _mpool), BBMap(_mir_func.getNumBasicBlocks(), nullptr, _mpool) {} void lower() { @@ -59,7 +60,9 @@ template class CgLowering { } CgRegister getVarReg(uint32_t VarIdx) const { - return _var_reg_map.at(VarIdx); + CgRegister Reg = VariableRegisters.at(VarIdx); + ZEN_ASSERT(Reg.isValid()); + return Reg; } protected: @@ -286,16 +289,10 @@ template class CgLowering { _expr_reg_map[&Inst] = ResultReg; } - CgRegister lowerDreadExpr(const DreadInstruction &inst) { - CgRegister ret; - auto var_idx = inst.getVarIdx(); - auto it = _var_reg_map.find(var_idx); - if (it != _var_reg_map.end()) { - ret = it->second; - } else { - ret = SELF.lowerVariable(var_idx); - } - return ret; + CgRegister lowerDreadExpr(const DreadInstruction &Inst) { + const uint32_t VarIdx = Inst.getVarIdx(); + CgRegister Reg = VariableRegisters[VarIdx]; + return Reg.isValid() ? Reg : SELF.lowerVariable(VarIdx); } CgRegister lowerUnaryOpExpr(const MInstruction &inst, Opcode Opcode) { @@ -771,20 +768,20 @@ template class CgLowering { MF->createCgInstruction(*CurBB, II, Operands); } - CgRegister getOrCreateVarReg(uint32_t var_idx, - const llvm::TargetRegisterClass *reg_class) { - auto pair = _var_reg_map.emplace(var_idx, 0); - if (pair.second) { - pair.first->second = createReg(reg_class); + CgRegister getOrCreateVarReg(uint32_t VarIdx, + const llvm::TargetRegisterClass *RegClass) { + CgRegister &Reg = VariableRegisters[VarIdx]; + if (!Reg.isValid()) { + Reg = createReg(RegClass); } - return pair.first->second; + return Reg; } CgRegister createReg(const llvm::TargetRegisterClass *reg_class) { return MRI.createVirtualRegister(reg_class); } void updateVarReg(uint32_t var_idx, CgRegister reg, const llvm::TargetRegisterClass *reg_class) { - _var_reg_map[var_idx] = reg; + VariableRegisters[var_idx] = reg; MRI.setRegClass(reg, reg_class); } @@ -801,8 +798,9 @@ template class CgLowering { CgRegisterInfo &MRI; CgBasicBlock *CurBB = nullptr; - CompileUnorderedMap _expr_reg_map; - CompileUnorderedMap _var_reg_map; + llvm::DenseMap _expr_reg_map; + // MIR parameters and locals share one contiguous variable index space. + CompileVector VariableRegisters; // Map from MIR BB to CgIR BB; the key is the index of MIR BB CompileVector BBMap; }; diff --git a/src/compiler/cgir/pass/cg_register_info.cpp b/src/compiler/cgir/pass/cg_register_info.cpp index 06fda093d..58010feb2 100644 --- a/src/compiler/cgir/pass/cg_register_info.cpp +++ b/src/compiler/cgir/pass/cg_register_info.cpp @@ -26,7 +26,6 @@ CgRegisterInfo::CgRegisterInfo(CgFunction &mf) : MF(mf), TracksSubRegLiveness(MF.getSubtarget().enableSubRegLiveness()) { unsigned NumRegs = MF.getSubtarget().getRegisterInfo()->getNumRegs(); VRegInfo.reserve(256); - RegAllocHints.reserve(256); PhysRegUseDefLists.reset(new CgOperand *[NumRegs]()); UsedPhysRegMask.resize(NumRegs); diff --git a/src/compiler/cgir/pass/cg_register_info.h b/src/compiler/cgir/pass/cg_register_info.h index 2a06980d4..df2651440 100644 --- a/src/compiler/cgir/pass/cg_register_info.h +++ b/src/compiler/cgir/pass/cg_register_info.h @@ -409,7 +409,6 @@ class CgRegisterInfo : public NonCopyable { Register createIncompleteVirtualRegister() { Register Reg = Register::index2VirtReg(getNumVirtRegs()); VRegInfo.grow(Reg); - RegAllocHints.grow(Reg); return Reg; } bool recomputeRegClass(Register Reg); @@ -776,16 +775,17 @@ class CgRegisterInfo : public NonCopyable { /// case of an earlier hint it will be overwritten. void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg) { assert(VReg.isVirtual()); - RegAllocHints[VReg].first = Type; - RegAllocHints[VReg].second.clear(); - RegAllocHints[VReg].second.push_back(PrefReg); + auto &Hint = getOrCreateRegAllocationHints(VReg); + Hint.first = Type; + Hint.second.clear(); + Hint.second.push_back(PrefReg); } /// addRegAllocationHint - Add a register allocation hint to the hints /// vector for VReg. void addRegAllocationHint(Register VReg, Register PrefReg) { assert(Register::isVirtualRegister(VReg)); - RegAllocHints[VReg].second.push_back(PrefReg); + getOrCreateRegAllocationHints(VReg).second.push_back(PrefReg); } /// Specify the preferred (target independent) register allocation hint @@ -795,6 +795,9 @@ class CgRegisterInfo : public NonCopyable { } void clearSimpleHint(Register VReg) { + assert(VRegInfo.inBounds(VReg)); + if (!RegAllocHints.inBounds(VReg)) + return; assert(!RegAllocHints[VReg].first && "Expected to clear a non-target hint!"); RegAllocHints[VReg].second.clear(); @@ -804,12 +807,8 @@ class CgRegisterInfo : public NonCopyable { /// specified virtual register. If there are many hints, this returns /// the one with the greatest weight. std::pair getRegAllocationHint(Register VReg) const { - assert(VReg.isVirtual()); - Register BestHint = (RegAllocHints[VReg.id()].second.size() - ? RegAllocHints[VReg.id()].second[0] - : Register()); - return std::pair(RegAllocHints[VReg.id()].first, - BestHint); + const auto &Hint = getRegAllocationHints(VReg); + return {Hint.first, Hint.second.empty() ? Register() : Hint.second.front()}; } /// getSimpleHint - same as getRegAllocationHint except it will only @@ -824,11 +823,24 @@ class CgRegisterInfo : public NonCopyable { /// register allocation hints for VReg. const std::pair> & getRegAllocationHints(Register VReg) const { - assert(VReg.isVirtual()); - return RegAllocHints[VReg]; + assert(VReg.isVirtual() && VRegInfo.inBounds(VReg)); + if (RegAllocHints.inBounds(VReg)) + return RegAllocHints[VReg]; + static const std::pair> Empty; + return Empty; } private: + std::pair> & + getOrCreateRegAllocationHints(Register VReg) { + assert(VRegInfo.inBounds(VReg)); + // FastRA does not use this table. Size it when a caller first sets a hint. + if (RegAllocHints.size() == 0) + RegAllocHints.resize(getNumVirtRegs()); + RegAllocHints.grow(VReg); + return RegAllocHints[VReg]; + } + const llvm::MCPhysReg * getCalleeSavedRegs(const llvm::TargetRegisterInfo &target_reg_info) const;