[backend]修复了栈分配空间大小不考虑数组的错误
This commit is contained in:
@@ -1348,6 +1348,7 @@ RISCv64CodeGen::RegAllocResult RISCv64CodeGen::register_allocation(Function* fun
|
|||||||
int current_stack_offset = 0;
|
int current_stack_offset = 0;
|
||||||
std::set<AllocaInst*> allocas_in_func;
|
std::set<AllocaInst*> allocas_in_func;
|
||||||
|
|
||||||
|
// 收集函数中的所有 AllocaInst
|
||||||
for (const auto& bb_ptr : func->getBasicBlocks()) {
|
for (const auto& bb_ptr : func->getBasicBlocks()) {
|
||||||
for (const auto& inst_ptr : bb_ptr->getInstructions()) {
|
for (const auto& inst_ptr : bb_ptr->getInstructions()) {
|
||||||
if (auto alloca = dynamic_cast<AllocaInst*>(inst_ptr.get())) {
|
if (auto alloca = dynamic_cast<AllocaInst*>(inst_ptr.get())) {
|
||||||
@@ -1356,10 +1357,28 @@ RISCv64CodeGen::RegAllocResult RISCv64CodeGen::register_allocation(Function* fun
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 为每个 AllocaInst 计算栈空间并分配偏移量
|
||||||
for (auto alloca : allocas_in_func) {
|
for (auto alloca : allocas_in_func) {
|
||||||
int size = 4; // 假设 i32 或 float, 依旧是4字节
|
int total_size = 4; // 基本元素大小(int 或 float)
|
||||||
|
auto dims = alloca->getDims();
|
||||||
|
if (!dims.empty()) {
|
||||||
|
int num_elements = 1;
|
||||||
|
for (const auto& dim_use : dims) {
|
||||||
|
Value* dim_value = dim_use->getValue();
|
||||||
|
if (auto const_dim = dynamic_cast<ConstantValue*>(dim_value)) {
|
||||||
|
if (const_dim->isInt()) {
|
||||||
|
num_elements *= const_dim->getInt();
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("数组维度必须是整数");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("数组维度必须是编译时常量");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total_size *= num_elements;
|
||||||
|
}
|
||||||
alloc_result.stack_map[alloca] = current_stack_offset;
|
alloc_result.stack_map[alloca] = current_stack_offset;
|
||||||
current_stack_offset += size;
|
current_stack_offset += total_size;
|
||||||
}
|
}
|
||||||
// RV64 修改: 为保存的 ra 和 s0 (各8字节) 预留16字节空间
|
// RV64 修改: 为保存的 ra 和 s0 (各8字节) 预留16字节空间
|
||||||
alloc_result.stack_size = current_stack_offset + 16;
|
alloc_result.stack_size = current_stack_offset + 16;
|
||||||
|
|||||||
Reference in New Issue
Block a user