[backend]本地全过

This commit is contained in:
Lixuanwang
2025-08-05 17:38:49 +08:00
parent 167c2ac2ae
commit 3ba12bf320
5 changed files with 707 additions and 70 deletions

View File

@@ -5,6 +5,8 @@
#include <iostream>
#include <sstream>
#include <cassert>
#include <chrono>
#include <thread>
namespace sysy {
@@ -56,53 +58,24 @@ bool RISCv64RegAlloc::run() {
if (DEBUG) std::cerr << "===== Running Graph Coloring Register Allocation for function: " << MFunc->getName() << " =====\n";
while (true) {
const int MAX_ITERATIONS = 50;
int iteration = 0;
while (iteration++ < MAX_ITERATIONS) {
// std::cerr << "Iteration Step: " << iteration << "\n";
// std::this_thread::sleep_for(std::chrono::seconds(1));
if (doAllocation()) {
break;
} else {
rewriteProgram();
if (DEBUG) std::cerr << "--- Spilling detected, re-running allocation ---\n";
if (DEBUG) std::cerr << "--- Spilling detected, re-running allocation (iteration " << iteration << ") ---\n";
if (iteration >= MAX_ITERATIONS) {
return false;
}
}
}
// const int MAX_ITERATIONS = 50;
// int iteration = 0;
// while (iteration++ < MAX_ITERATIONS) {
// if (doAllocation()) {
// break;
// } else {
// rewriteProgram();
// if (DEBUG) std::cerr << "--- Spilling detected, re-running allocation (iteration " << iteration << ") ---\n";
// if (iteration >= MAX_ITERATIONS) {
// std::cerr << "ERROR: Register allocation failed to converge after " << MAX_ITERATIONS << " iterations\n";
// std::cerr << " Spill worklist size: " << spillWorklist.size() << "\n";
// std::cerr << " Total nodes: " << (initial.size() + coloredNodes.size()) << "\n";
// // Emergency spill remaining nodes to break the loop
// std::cerr << " Emergency spilling remaining spill worklist nodes...\n";
// for (unsigned node : spillWorklist) {
// spilledNodes.insert(node);
// }
// // Also spill any nodes that didn't get colors
// std::set<unsigned> uncolored;
// for (unsigned node : initial) {
// if (color_map.find(node) == color_map.end()) {
// uncolored.insert(node);
// }
// }
// for (unsigned node : uncolored) {
// spilledNodes.insert(node);
// }
// // Force completion
// break;
// }
// }
// }
applyColoring();
MFunc->getFrameInfo().vreg_to_preg_map = this->color_map;
@@ -113,6 +86,8 @@ bool RISCv64RegAlloc::run() {
// 单次分配的核心流程
bool RISCv64RegAlloc::doAllocation() {
const int MAX_ITERATIONS = 50;
int iteration = 0;
initialize();
precolorByCallingConvention();
analyzeLiveness();
@@ -120,14 +95,16 @@ bool RISCv64RegAlloc::doAllocation() {
makeWorklist();
while (!simplifyWorklist.empty() || !worklistMoves.empty() || !freezeWorklist.empty() || !spillWorklist.empty()) {
if (DEEPDEBUG) dumpState("Loop Start");
// if (DEBUG) std::cerr << "Inner Iteration Step: " << ++iteration << "\n";
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
// if (DEEPDEBUG) dumpState("Loop Start");
if (!simplifyWorklist.empty()) simplify();
else if (!worklistMoves.empty()) coalesce();
else if (!freezeWorklist.empty()) freeze();
else if (!spillWorklist.empty()) selectSpill();
}
if (DEEPDEBUG) dumpState("Before AssignColors");
// if (DEEPDEBUG) dumpState("Before AssignColors");
assignColors();
return spilledNodes.empty();
}