From 1b9c8cc40d09373621eaaa0ef34cf6164f476744 Mon Sep 17 00:00:00 2001 From: lzkk <956449176@qq.com> Date: Thu, 28 May 2026 17:11:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(mir):=20=E4=BF=AE=E5=A4=8D=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=8F=98=E9=87=8F=20.bss/.data=20=E6=AE=B5=E5=8F=8D?= =?UTF-8?q?=E5=A4=8D=E5=88=87=E6=8D=A2=E2=80=94=E2=80=94=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=88=86=E7=BB=84=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前每个零初始化全局变量都会在 .bss → .data 之间切换一次, 大数组(12MB BSS)的 .data 插入可能导致链接器内存布局异常。 现在先输出全部 .bss 段,再输出 .data 段,消除无用的段切换。 这可能是 many_mat_cal-1/3 在平台 100MB 限制下 WA 的根因。 --- src/mir/AsmPrinter.cpp | 95 ++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 49 deletions(-) diff --git a/src/mir/AsmPrinter.cpp b/src/mir/AsmPrinter.cpp index dc94fa16..1aaad65c 100644 --- a/src/mir/AsmPrinter.cpp +++ b/src/mir/AsmPrinter.cpp @@ -865,74 +865,71 @@ namespace mir void PrintGlobals(const MachineModule &module, std::ostream &os) { if (module.GetGlobals().empty()) - { return; - } - os << " .data\n"; - for (const auto &global : module.GetGlobals()) - { - const std::string asm_name = NormalizeAsmSymbol(global.name); - - bool is_zero_init = false; - if (global.kind == MachineGlobal::Kind::I32Scalar && global.init_value == 0) + auto is_zero = [](const MachineGlobal &g) -> bool { + if (g.kind == MachineGlobal::Kind::I32Scalar && g.init_value == 0) + return true; + if (g.kind == MachineGlobal::Kind::I32Array) { - is_zero_init = true; - } - if (global.kind == MachineGlobal::Kind::I32Array) - { - bool all_zero = true; - for (auto v : global.init_values) - { - if (v != 0) - { - all_zero = false; - break; - } - } - if (all_zero) - { - is_zero_init = true; - } + if (g.init_values.empty()) return true; + for (auto v : g.init_values) + if (v != 0) return false; + return true; } + return false; + }; + + // 先统一输出 .bss 段 + bool has_bss = false; + for (const auto &g : module.GetGlobals()) + if (is_zero(g)) { has_bss = true; break; } - if (is_zero_init) + if (has_bss) + { + os << " .bss\n"; + for (const auto &global : module.GetGlobals()) { - os << " .bss\n"; + if (!is_zero(global)) continue; + const std::string asm_name = NormalizeAsmSymbol(global.name); os << " .globl " << asm_name << "\n"; os << " .p2align 2\n"; os << asm_name << ":\n"; if (global.kind == MachineGlobal::Kind::I32Scalar) - { os << " .space 4\n"; - } else - { os << " .space " << (global.array_size * 4) << "\n"; - } - os << " .data\n"; - continue; } + } - os << " .globl " << asm_name << "\n"; - os << " .p2align 2\n"; - os << asm_name << ":\n"; + // 再统一输出 .data 段 + bool has_data = false; + for (const auto &g : module.GetGlobals()) + if (!is_zero(g)) { has_data = true; break; } - if (global.kind == MachineGlobal::Kind::I32Scalar) + if (has_data) + { + os << " .data\n"; + for (const auto &global : module.GetGlobals()) { - os << " .word " << global.init_value << "\n"; - continue; - } + if (is_zero(global)) continue; + const std::string asm_name = NormalizeAsmSymbol(global.name); + os << " .globl " << asm_name << "\n"; + os << " .p2align 2\n"; + os << asm_name << ":\n"; - const size_t init_count = global.init_values.size(); - for (size_t i = 0; i < init_count; ++i) - { - os << " .word " << global.init_values[i] << "\n"; - } + if (global.kind == MachineGlobal::Kind::I32Scalar) + { + os << " .word " << global.init_value << "\n"; + continue; + } - if (global.array_size > init_count) - { - os << " .zero " << ((global.array_size - init_count) * 4) << "\n"; + const size_t init_count = global.init_values.size(); + for (size_t i = 0; i < init_count; ++i) + os << " .word " << global.init_values[i] << "\n"; + + if (global.array_size > init_count) + os << " .zero " << ((global.array_size - init_count) * 4) << "\n"; } } os << "\n";