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
29 changes: 29 additions & 0 deletions docs/changes/2026-11-29-cgir-lowering-storage/README.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions docs/modules/compiler/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
42 changes: 20 additions & 22 deletions src/compiler/cgir/lowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,8 +27,8 @@ template <typename T> 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() {
Expand Down Expand Up @@ -59,7 +60,9 @@ template <typename T> 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:
Expand Down Expand Up @@ -286,16 +289,10 @@ template <typename T> 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) {
Expand Down Expand Up @@ -771,20 +768,20 @@ template <typename T> 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);
}

Expand All @@ -801,8 +798,9 @@ template <typename T> class CgLowering {

CgRegisterInfo &MRI;
CgBasicBlock *CurBB = nullptr;
CompileUnorderedMap<const MInstruction *, CgRegister> _expr_reg_map;
CompileUnorderedMap<uint32_t, CgRegister> _var_reg_map;
llvm::DenseMap<const MInstruction *, CgRegister> _expr_reg_map;
// MIR parameters and locals share one contiguous variable index space.
CompileVector<CgRegister> VariableRegisters;
// Map from MIR BB to CgIR BB; the key is the index of MIR BB
CompileVector<CgBasicBlock *> BBMap;
};
Expand Down
1 change: 0 additions & 1 deletion src/compiler/cgir/pass/cg_register_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
38 changes: 25 additions & 13 deletions src/compiler/cgir/pass/cg_register_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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<Register, Register> getRegAllocationHint(Register VReg) const {
assert(VReg.isVirtual());
Register BestHint = (RegAllocHints[VReg.id()].second.size()
? RegAllocHints[VReg.id()].second[0]
: Register());
return std::pair<Register, Register>(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
Expand All @@ -824,11 +823,24 @@ class CgRegisterInfo : public NonCopyable {
/// register allocation hints for VReg.
const std::pair<Register, SmallVector<Register, 4>> &
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<Register, SmallVector<Register, 4>> Empty;
return Empty;
}

private:
std::pair<Register, SmallVector<Register, 4>> &
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;

Expand Down
Loading