From ce4d4b5f5b9035639bb75442864be858cbde6233 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Tue, 19 Aug 2025 01:08:05 +0800 Subject: [PATCH 01/15] =?UTF-8?q?[midend-phielimination]=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?phi=E6=8C=87=E4=BB=A4=E6=B6=88=E9=99=A4=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../midend/Pass/Optimize/SysYIROptUtils.h | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/include/midend/Pass/Optimize/SysYIROptUtils.h b/src/include/midend/Pass/Optimize/SysYIROptUtils.h index 48d2f26..71c0532 100644 --- a/src/include/midend/Pass/Optimize/SysYIROptUtils.h +++ b/src/include/midend/Pass/Optimize/SysYIROptUtils.h @@ -109,6 +109,28 @@ public: } + // PHI指令消除相关方法 + static void eliminateRedundantPhisInFunction(Function* func){ + std::vector toDelete; + for (auto &bb : func->getBasicBlocks()) { + for (auto &inst : bb->getInstructions()) { + if (auto phi = dynamic_cast(inst.get())) { + auto incoming = phi->getIncomingValues(); + if (incoming.size() == 1) { + Value *singleVal = incoming[0].first; + inst->replaceAllUsesWith(singleVal); + toDelete.push_back(inst.get()); + } + } + else + break; // 只处理Phi指令 + } + } + for (auto *phi : toDelete) { + usedelete(phi); + } + } + //该实现参考了libdivide的算法 static std::pair computeMulhMagicNumbers(int divisor) { From db122cabbdbfb4ff5b49b79efb5e9d4322c26db0 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Tue, 19 Aug 2025 08:27:18 +0800 Subject: [PATCH 02/15] =?UTF-8?q?[midend-phielimination]=E6=B6=88=E9=99=A4?= =?UTF-8?q?=E5=8F=AA=E6=9C=89=E4=B8=80=E4=B8=AAincomingvalue=E7=9A=84phi?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/include/midend/Pass/Optimize/SysYIROptUtils.h | 10 ++++++++-- src/midend/Pass/Optimize/DCE.cpp | 1 + src/midend/Pass/Optimize/GVN.cpp | 2 +- .../Pass/Optimize/InductionVariableElimination.cpp | 1 + src/midend/Pass/Optimize/SCCP.cpp | 5 ++--- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/include/midend/Pass/Optimize/SysYIROptUtils.h b/src/include/midend/Pass/Optimize/SysYIROptUtils.h index 71c0532..81062e1 100644 --- a/src/include/midend/Pass/Optimize/SysYIROptUtils.h +++ b/src/include/midend/Pass/Optimize/SysYIROptUtils.h @@ -110,14 +110,18 @@ public: // PHI指令消除相关方法 - static void eliminateRedundantPhisInFunction(Function* func){ + static bool eliminateRedundantPhisInFunction(Function* func){ + bool changed = false; std::vector toDelete; for (auto &bb : func->getBasicBlocks()) { for (auto &inst : bb->getInstructions()) { if (auto phi = dynamic_cast(inst.get())) { auto incoming = phi->getIncomingValues(); + if(DEBUG){ + std::cout << "Checking Phi: " << phi->getName() << " with " << incoming.size() << " incoming values." << std::endl; + } if (incoming.size() == 1) { - Value *singleVal = incoming[0].first; + Value *singleVal = incoming[0].second; inst->replaceAllUsesWith(singleVal); toDelete.push_back(inst.get()); } @@ -128,7 +132,9 @@ public: } for (auto *phi : toDelete) { usedelete(phi); + changed = true; // 标记为已更改 } + return changed; // 返回是否有删除发生 } //该实现参考了libdivide的算法 diff --git a/src/midend/Pass/Optimize/DCE.cpp b/src/midend/Pass/Optimize/DCE.cpp index 06a4822..f89781e 100644 --- a/src/midend/Pass/Optimize/DCE.cpp +++ b/src/midend/Pass/Optimize/DCE.cpp @@ -74,6 +74,7 @@ void DCEContext::run(Function *func, AnalysisManager *AM, bool &changed) { } } } + changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func); // 如果有活跃指令,则标记为已更改 } // 判断指令是否是"天然活跃"的实现 diff --git a/src/midend/Pass/Optimize/GVN.cpp b/src/midend/Pass/Optimize/GVN.cpp index 09b67a1..047ae52 100644 --- a/src/midend/Pass/Optimize/GVN.cpp +++ b/src/midend/Pass/Optimize/GVN.cpp @@ -39,7 +39,7 @@ bool GVN::runOnFunction(Function *func, AnalysisManager &AM) { } std::cout << "=== GVN completed for function: " << func->getName() << " ===" << std::endl; } - + changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func); return changed; } diff --git a/src/midend/Pass/Optimize/InductionVariableElimination.cpp b/src/midend/Pass/Optimize/InductionVariableElimination.cpp index 8055efa..56bb22a 100644 --- a/src/midend/Pass/Optimize/InductionVariableElimination.cpp +++ b/src/midend/Pass/Optimize/InductionVariableElimination.cpp @@ -133,6 +133,7 @@ bool InductionVariableEliminationContext::run(Function* F, AnalysisManager& AM) printDebugInfo(); } + modified |= SysYIROptUtils::eliminateRedundantPhisInFunction(F); return modified; } diff --git a/src/midend/Pass/Optimize/SCCP.cpp b/src/midend/Pass/Optimize/SCCP.cpp index 8fbda0b..d0fa138 100644 --- a/src/midend/Pass/Optimize/SCCP.cpp +++ b/src/midend/Pass/Optimize/SCCP.cpp @@ -1357,9 +1357,8 @@ void SCCPContext::run(Function *func, AnalysisManager &AM) { bool changed_control_flow = SimplifyControlFlow(func); // 如果任何一个阶段修改了 IR,标记分析结果为失效 - if (changed_constant_propagation || changed_control_flow) { - // AM.invalidate(); // 假设有这样的方法来使所有分析结果失效 - } + bool changed = changed_constant_propagation || changed_control_flow; + changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func); } // SCCP Pass methods From 1d59e9e2566f69d76a162bd22cce00e39536b2f1 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Tue, 19 Aug 2025 08:29:43 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E8=BE=93=E5=87=BA=E6=88=AA=E6=96=AD?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/runit-single.sh | 31 ++++++++++++++++++++++--------- script/runit.sh | 33 ++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/script/runit-single.sh b/script/runit-single.sh index bfbbcdc..786986f 100644 --- a/script/runit-single.sh +++ b/script/runit-single.sh @@ -20,18 +20,19 @@ QEMU_RISCV64="qemu-riscv64" # --- 初始化变量 --- EXECUTE_MODE=false -IR_EXECUTE_MODE=false # 新增 +IR_EXECUTE_MODE=false CLEAN_MODE=false OPTIMIZE_FLAG="" SYSYC_TIMEOUT=30 -LLC_TIMEOUT=10 # 新增 +LLC_TIMEOUT=10 GCC_TIMEOUT=10 EXEC_TIMEOUT=30 MAX_OUTPUT_LINES=20 +MAX_OUTPUT_CHARS=1000 SY_FILES=() PASSED_CASES=0 FAILED_CASES_LIST="" -INTERRUPTED=false # 新增 +INTERRUPTED=false # ================================================================= # --- 函数定义 --- @@ -50,22 +51,31 @@ show_help() { echo " -gct N 设置 gcc 交叉编译超时为 N 秒 (默认: 10)。" echo " -et N 设置 qemu 自动化执行超时为 N 秒 (默认: 30)。" echo " -ml N, --max-lines N 当输出对比失败时,最多显示 N 行内容 (默认: 20)。" + echo " -mc N, --max-chars N 当输出对比失败时,最多显示 N 个字符 (默认: 1000)。" echo " -h, --help 显示此帮助信息并退出。" echo "" echo "可在任何时候按 Ctrl+C 来中断测试并显示当前已完成的测例总结。" } +# 显示文件内容并根据行数和字符数截断的函数 display_file_content() { local file_path="$1" local title="$2" local max_lines="$3" + local max_chars="$4" # 新增参数 if [ ! -f "$file_path" ]; then return; fi echo -e "$title" local line_count + local char_count line_count=$(wc -l < "$file_path") + char_count=$(wc -c < "$file_path") + if [ "$line_count" -gt "$max_lines" ]; then head -n "$max_lines" "$file_path" - echo -e "\e[33m[... 输出已截断,共 ${line_count} 行 ...]\e[0m" + echo -e "\e[33m[... 输出因行数过多 (共 ${line_count} 行) 而截断 ...]\e[0m" + elif [ "$char_count" -gt "$max_chars" ]; then + head -c "$max_chars" "$file_path" + echo -e "\n\e[33m[... 输出因字符数过多 (共 ${char_count} 字符) 而截断 ...]\e[0m" else cat "$file_path" fi @@ -131,6 +141,7 @@ while [[ "$#" -gt 0 ]]; do -gct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift 2; else echo "错误: -gct 需要一个正整数参数。" >&2; exit 1; fi ;; -et) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift 2; else echo "错误: -et 需要一个正整数参数。" >&2; exit 1; fi ;; -ml|--max-lines) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_LINES="$2"; shift 2; else echo "错误: --max-lines 需要一个正整数参数。" >&2; exit 1; fi ;; + -mc|--max-chars) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_CHARS="$2"; shift 2; else echo "错误: --max-chars 需要一个正整数参数。" >&2; exit 1; fi ;; -h|--help) show_help; exit 0 ;; -*) echo "未知选项: $1"; show_help; exit 1 ;; *) @@ -180,6 +191,8 @@ TOTAL_CASES=${#SY_FILES[@]} echo "SysY 单例测试运行器启动..." if [ -n "$OPTIMIZE_FLAG" ]; then echo "优化等级: ${OPTIMIZE_FLAG}"; fi echo "超时设置: sysyc=${SYSYC_TIMEOUT}s, llc=${LLC_TIMEOUT}s, gcc=${GCC_TIMEOUT}s, qemu=${EXEC_TIMEOUT}s" +echo "失败输出最大行数: ${MAX_OUTPUT_LINES}" +echo "失败输出最大字符数: ${MAX_OUTPUT_CHARS}" echo "" for sy_file in "${SY_FILES[@]}"; do @@ -260,8 +273,8 @@ for sy_file in "${SY_FILES[@]}"; do out_ok=1 if ! diff -q <(tr -d '[:space:]' < "${output_actual_file}") <(tr -d '[:space:]' < "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then echo -e "\e[31m 标准输出测试失败。\e[0m"; out_ok=0 - display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" - display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" + display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" fi if [ "$ret_ok" -eq 1 ] && [ "$out_ok" -eq 1 ]; then echo -e "\e[32m 返回码与标准输出测试成功。\e[0m"; else is_passed=0; fi @@ -271,8 +284,8 @@ for sy_file in "${SY_FILES[@]}"; do echo -e "\e[32m 标准输出测试成功。\e[0m" else echo -e "\e[31m 标准输出测试失败。\e[0m"; is_passed=0 - display_file_content "${output_reference_file}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" - display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" + display_file_content "${output_reference_file}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" fi fi else @@ -301,4 +314,4 @@ for sy_file in "${SY_FILES[@]}"; do done # --- 打印最终总结 --- -print_summary \ No newline at end of file +print_summary diff --git a/script/runit.sh b/script/runit.sh index e27c905..c090415 100644 --- a/script/runit.sh +++ b/script/runit.sh @@ -27,11 +27,12 @@ LLC_TIMEOUT=10 GCC_TIMEOUT=10 EXEC_TIMEOUT=30 MAX_OUTPUT_LINES=20 +MAX_OUTPUT_CHARS=1000 TEST_SETS=() TOTAL_CASES=0 PASSED_CASES=0 FAILED_CASES_LIST="" -INTERRUPTED=false # 新增:用于标记是否被中断 +INTERRUPTED=false # ================================================================= # --- 函数定义 --- @@ -53,6 +54,7 @@ show_help() { echo " -gct N 设置 gcc 交叉编译超时为 N 秒 (默认: 10)。" echo " -et N 设置 qemu 执行超时为 N 秒 (默认: 30)。" echo " -ml N, --max-lines N 当输出对比失败时,最多显示 N 行内容 (默认: 20)。" + echo " -mc N, --max-chars N 当输出对比失败时,最多显示 N 个字符 (默认: 1000)。" echo " -h, --help 显示此帮助信息并退出。" echo "" echo "注意: 默认行为 (无 -e 或 -eir) 是将 .sy 文件同时编译为 .s (汇编) 和 .ll (IR),不执行。" @@ -60,18 +62,25 @@ show_help() { } -# 显示文件内容并根据行数截断的函数 +# 显示文件内容并根据行数和字符数截断的函数 display_file_content() { local file_path="$1" local title="$2" local max_lines="$3" + local max_chars="$4" # 新增参数 if [ ! -f "$file_path" ]; then return; fi echo -e "$title" local line_count + local char_count line_count=$(wc -l < "$file_path") + char_count=$(wc -c < "$file_path") + if [ "$line_count" -gt "$max_lines" ]; then head -n "$max_lines" "$file_path" - echo -e "\e[33m[... 输出已截断,共 ${line_count} 行 ...]\e[0m" + echo -e "\e[33m[... 输出因行数过多 (共 ${line_count} 行) 而截断 ...]\e[0m" + elif [ "$char_count" -gt "$max_chars" ]; then + head -c "$max_chars" "$file_path" + echo -e "\n\e[33m[... 输出因字符数过多 (共 ${char_count} 字符) 而截断 ...]\e[0m" else cat "$file_path" fi @@ -151,6 +160,7 @@ while [[ "$#" -gt 0 ]]; do -gct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift 2; else echo "错误: -gct 需要一个正整数参数。" >&2; exit 1; fi ;; -et) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift 2; else echo "错误: -et 需要一个正整数参数。" >&2; exit 1; fi ;; -ml|--max-lines) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_LINES="$2"; shift 2; else echo "错误: --max-lines 需要一个正整数参数。" >&2; exit 1; fi ;; + -mc|--max-chars) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_CHARS="$2"; shift 2; else echo "错误: --max-chars 需要一个正整数参数。" >&2; exit 1; fi ;; -h|--help) show_help; exit 0 ;; *) echo "未知选项: $1"; show_help; exit 1 ;; esac @@ -204,6 +214,7 @@ echo "运行模式: ${RUN_MODE_INFO}" echo "${TIMEOUT_INFO}" if ${EXECUTE_MODE} || ${IR_EXECUTE_MODE}; then echo "失败输出最大行数: ${MAX_OUTPUT_LINES}" + echo "失败输出最大字符数: ${MAX_OUTPUT_CHARS}" fi echo "" @@ -298,8 +309,8 @@ while IFS= read -r sy_file; do [ "$test_logic_passed" -eq 1 ] && echo -e "\e[32m 标准输出测试成功\e[0m" else echo -e "\e[31m 标准输出测试失败\e[0m" - display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" - display_file_content "${output_actual_file_from_ir}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" + display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_from_ir}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" test_logic_passed=0 fi else @@ -308,8 +319,8 @@ while IFS= read -r sy_file; do echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m" else echo -e "\e[31m 失败: 输出不匹配\e[0m" - display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" - display_file_content "${output_actual_file_from_ir}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" + display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_from_ir}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" test_logic_passed=0 fi fi @@ -375,8 +386,8 @@ while IFS= read -r sy_file; do [ "$test_logic_passed" -eq 1 ] && echo -e "\e[32m 标准输出测试成功\e[0m" else echo -e "\e[31m 标准输出测试失败\e[0m" - display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" - display_file_content "${output_actual_file_S}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" + display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_S}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" test_logic_passed=0 fi else @@ -385,8 +396,8 @@ while IFS= read -r sy_file; do echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m" else echo -e "\e[31m 失败: 输出不匹配\e[0m" - display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" - display_file_content "${output_actual_file_S}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" + display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_S}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" test_logic_passed=0 fi fi From ad5f35c1a0816a142caaab8a242836ca23a3ec20 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Tue, 19 Aug 2025 08:56:51 +0800 Subject: [PATCH 04/15] =?UTF-8?q?[midend]=E6=9A=82=E6=97=B6=E4=BB=85?= =?UTF-8?q?=E7=94=A8=E4=BA=86=E9=AD=94=E6=95=B0=E4=BC=98=E5=8C=96=E9=99=A4?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E6=89=8B=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pass/Optimize/GlobalStrengthReduction.cpp | 14 +++++----- .../Pass/Optimize/LoopStrengthReduction.cpp | 26 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp b/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp index e8254a2..404cf7b 100644 --- a/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp +++ b/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp @@ -671,13 +671,13 @@ bool GlobalStrengthReductionContext::reduceDivision(BinaryInst *inst) { } // x / c = x * magic_number (魔数乘法优化 - 使用libdivide算法) - if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) { - // auto magicPair = computeMulhMagicNumbers(static_cast(constVal)); - Value* magicResult = createMagicDivisionLibdivide(inst, static_cast(constVal)); - replaceWithOptimized(inst, magicResult); - divisionOptCount++; - return true; - } + // if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) { + // // auto magicPair = computeMulhMagicNumbers(static_cast(constVal)); + // Value* magicResult = createMagicDivisionLibdivide(inst, static_cast(constVal)); + // replaceWithOptimized(inst, magicResult); + // divisionOptCount++; + // return true; + // } return false; } diff --git a/src/midend/Pass/Optimize/LoopStrengthReduction.cpp b/src/midend/Pass/Optimize/LoopStrengthReduction.cpp index 0edbed4..33751df 100644 --- a/src/midend/Pass/Optimize/LoopStrengthReduction.cpp +++ b/src/midend/Pass/Optimize/LoopStrengthReduction.cpp @@ -661,9 +661,9 @@ bool StrengthReductionContext::replaceOriginalInstruction(StrengthReductionCandi case StrengthReductionCandidate::DIVIDE_CONST: { // 任意常数除法 - builder->setPosition(candidate->containingBlock, - candidate->containingBlock->findInstIterator(candidate->originalInst)); - replacementValue = generateConstantDivisionReplacement(candidate, builder); + // builder->setPosition(candidate->containingBlock, + // candidate->containingBlock->findInstIterator(candidate->originalInst)); + // replacementValue = generateConstantDivisionReplacement(candidate, builder); break; } @@ -683,17 +683,19 @@ bool StrengthReductionContext::replaceOriginalInstruction(StrengthReductionCandi ); // 检查原值是否为负数 - Value* zero = ConstantInteger::get(0); - Value* isNegative = builder->createICmpLTInst(candidate->inductionVar, zero); + Value* shift31condidata = builder->createBinaryInst( + Instruction::Kind::kSra, candidate->inductionVar->getType(), + candidate->inductionVar, ConstantInteger::get(31) + ); // 如果为负数,需要调整结果 - Value* adjustment = ConstantInteger::get(candidate->multiplier); - Value* adjustedTemp = builder->createAddInst(temp, adjustment); - - // 使用条件分支来模拟select操作 - // 为简化起见,这里先用一个更复杂但可工作的方式 - // 实际应该创建条件分支,但这里先简化处理 - replacementValue = temp; // 简化版本,假设大多数情况下不是负数 + Value* adjustment = builder->createAndInst(shift31condidata, maskConstant); + Value* adjustedTemp = builder->createAddInst(candidate->inductionVar, adjustment); + Value* adjustedResult = builder->createBinaryInst( + Instruction::Kind::kAnd, candidate->inductionVar->getType(), + adjustedTemp, maskConstant + ); + replacementValue = adjustedResult; } else { // 非负数的取模,直接使用位与 replacementValue = builder->createBinaryInst( From 8094fd57051edb080cfec6e925829c40f60b8cf9 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Tue, 19 Aug 2025 09:45:42 +0800 Subject: [PATCH 05/15] =?UTF-8?q?[midend]=E5=87=8F=E5=B0=91tmp=5Fcond?= =?UTF-8?q?=E7=9A=84=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/include/midend/IR.h | 1 + src/midend/IR.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/include/midend/IR.h b/src/include/midend/IR.h index 92539dc..9148edc 100644 --- a/src/include/midend/IR.h +++ b/src/include/midend/IR.h @@ -1007,6 +1007,7 @@ class PhiInst : public Instruction { void replaceIncomingBlock(BasicBlock *oldBlock, BasicBlock *newBlock, Value *newValue); void refreshMap() { blk2val.clear(); + vsize = getNumOperands() / 2; for (unsigned i = 0; i < vsize; ++i) { blk2val[getIncomingBlock(i)] = getIncomingValue(i); } diff --git a/src/midend/IR.cpp b/src/midend/IR.cpp index d35e16b..5ed1777 100644 --- a/src/midend/IR.cpp +++ b/src/midend/IR.cpp @@ -757,7 +757,7 @@ void BinaryInst::print(std::ostream &os) const { auto lhs_hash = std::hash{}(static_cast(getLhs())); auto rhs_hash = std::hash{}(static_cast(getRhs())); size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2); - std::string tmpName = "tmp_icmp_" + std::to_string(combined_hash % 1000000); + std::string tmpName = "tmp_icmp_" + std::to_string(combined_hash % 1000000007); os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " "; printOperand(os, getLhs()); os << ", "; @@ -772,7 +772,7 @@ void BinaryInst::print(std::ostream &os) const { auto lhs_hash = std::hash{}(static_cast(getLhs())); auto rhs_hash = std::hash{}(static_cast(getRhs())); size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2); - std::string tmpName = "tmp_fcmp_" + std::to_string(combined_hash % 1000000); + std::string tmpName = "tmp_fcmp_" + std::to_string(combined_hash % 1000000007); os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " "; printOperand(os, getLhs()); os << ", "; @@ -834,7 +834,7 @@ void CondBrInst::print(std::ostream &os) const { if (condName.empty()) { // 使用条件值地址的哈希值作为唯一标识 auto ptr_hash = std::hash{}(static_cast(condition)); - condName = "const_" + std::to_string(ptr_hash % 100000); + condName = "const_" + std::to_string(ptr_hash % 1000000007); } // 组合指令地址、条件地址和目标块地址的哈希来确保唯一性 @@ -843,7 +843,7 @@ void CondBrInst::print(std::ostream &os) const { auto then_hash = std::hash{}(static_cast(getThenBlock())); auto else_hash = std::hash{}(static_cast(getElseBlock())); size_t combined_hash = inst_hash ^ (cond_hash << 1) ^ (then_hash << 2) ^ (else_hash << 3); - std::string uniqueSuffix = std::to_string(combined_hash % 1000000); + std::string uniqueSuffix = std::to_string(combined_hash % 1000000007); os << "%tmp_cond_" << condName << "_" << uniqueSuffix << " = icmp ne i32 "; printOperand(os, condition); From b13d81353ac9d9cc4228df3662481cb3720ab8c9 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Tue, 19 Aug 2025 15:08:00 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=B7=9F=E8=B8=AAperfo?= =?UTF-8?q?rmance=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c10e61c..32dccde 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,7 @@ doxygen !/testdata/functional/*.out !/testdata/h_functional/*.out -!/testdata/performance/*.out +testdata/performance/ build/ .antlr .vscode/ From d72601d9dbdbd1af73db62ea432e98c0152010bb Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Tue, 19 Aug 2025 15:08:00 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=B7=9F=E8=B8=AAperfo?= =?UTF-8?q?rmance=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c10e61c..32dccde 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,7 @@ doxygen !/testdata/functional/*.out !/testdata/h_functional/*.out -!/testdata/performance/*.out +testdata/performance/ build/ .antlr .vscode/ From 7ada4c193fe1e57178041a50b457c07003339a45 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Tue, 19 Aug 2025 17:42:45 +0800 Subject: [PATCH 08/15] =?UTF-8?q?[backend]=E5=B0=86=E5=86=85=E8=81=94MEMSE?= =?UTF-8?q?T=E5=87=BD=E6=95=B0=E7=9A=84=E6=AD=A5=E9=95=BF=E6=94=B9?= =?UTF-8?q?=E4=B8=BA4=E5=AD=97=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index a0ad2f1..a3ba19d 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -1215,15 +1215,13 @@ void RISCv64ISel::selectNode(DAGNode* node) { std::string loop_end_label = MFunc->getName() + "_memset_loop_end_" + std::to_string(unique_id); std::string remainder_label = MFunc->getName() + "_memset_remainder_" + std::to_string(unique_id); std::string done_label = MFunc->getName() + "_memset_done_" + std::to_string(unique_id); - - // 构造64位的填充值 - addi_instr(RVOpcodes::ANDI, r_temp_val, r_value_byte, 255); - addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 8); - add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); - addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 16); - add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); - addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 32); - add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); + + // 构造32位的填充值 (将一个字节复制4次) + addi_instr(RVOpcodes::ANDI, r_temp_val, r_value_byte, 255); // 提取低8位: 000000XX + addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 8); // 左移8位: 0000XX00 + add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); // 合并得到: 0000XXXX + addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 16); // 左移16位: XXXX0000 + add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); // 合并得到完整的32位值: XXXXXXXX // 计算循环边界 add_instr(RVOpcodes::ADD, r_end_addr, r_dest_addr, r_num_bytes); @@ -1231,16 +1229,18 @@ void RISCv64ISel::selectNode(DAGNode* node) { mv->addOperand(std::make_unique(r_current_addr)); mv->addOperand(std::make_unique(r_dest_addr)); CurMBB->addInstruction(std::move(mv)); - addi_instr(RVOpcodes::ANDI, r_counter, r_num_bytes, -8); + // 计算主循环部分的总字节数 (向下舍入到4的倍数) + addi_instr(RVOpcodes::ANDI, r_counter, r_num_bytes, -4); + // 计算主循环的结束地址 add_instr(RVOpcodes::ADD, r_counter, r_dest_addr, r_counter); - // 8字节主循环 + // 4字节主循环 label_instr(loop_start_label); branch_instr(RVOpcodes::BGEU, r_current_addr, r_counter, loop_end_label); - store_instr(RVOpcodes::SD, r_temp_val, r_current_addr, 0); - addi_instr(RVOpcodes::ADDI, r_current_addr, r_current_addr, 8); + store_instr(RVOpcodes::SW, r_temp_val, r_current_addr, 0); // 使用 sw (存储字) + addi_instr(RVOpcodes::ADDI, r_current_addr, r_current_addr, 4); // 步长改为4 jump_instr(loop_start_label); - + // 1字节收尾循环 label_instr(loop_end_label); label_instr(remainder_label); From 33388496d5a7327e433322da9d2e338bea8b72b2 Mon Sep 17 00:00:00 2001 From: CGH0S7 <776459475@qq.com> Date: Tue, 19 Aug 2025 22:42:30 +0800 Subject: [PATCH 09/15] =?UTF-8?q?[backend-O1]RISCv64ISel=E5=AF=B9-O1?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=93=E9=97=A8=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 101 ++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index 22ccc22..9fccba6 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -1311,6 +1311,7 @@ void RISCv64ISel::selectNode(DAGNode* node) { auto gep = dynamic_cast(node->value); auto result_vreg = getVReg(gep); + if (optLevel == 0) { // --- Step 1: 获取基地址 (此部分逻辑正确,保持不变) --- auto base_ptr_node = node->operands[0]; auto current_addr_vreg = getNewVReg(gep->getType()); @@ -1417,6 +1418,106 @@ void RISCv64ISel::selectNode(DAGNode* node) { final_mv->addOperand(std::make_unique(current_addr_vreg)); CurMBB->addInstruction(std::move(final_mv)); break; + } else { + // 对于-O1时的处理逻辑 + // --- Step 1: 获取基地址 --- + auto base_ptr_node = node->operands[0]; + auto base_ptr_val = base_ptr_node->value; + + // last_step_addr_vreg 保存上一步计算的结果。 + // 它首先被初始化为GEP的初始基地址。 + unsigned last_step_addr_vreg; + + if (auto alloca_base = dynamic_cast(base_ptr_val)) { + last_step_addr_vreg = getNewVReg(gep->getType()); + auto frame_addr_instr = std::make_unique(RVOpcodes::FRAME_ADDR); + frame_addr_instr->addOperand(std::make_unique(last_step_addr_vreg)); + frame_addr_instr->addOperand(std::make_unique(getVReg(alloca_base))); + CurMBB->addInstruction(std::move(frame_addr_instr)); + } else if (auto global_base = dynamic_cast(base_ptr_val)) { + last_step_addr_vreg = getNewVReg(gep->getType()); + auto la_instr = std::make_unique(RVOpcodes::LA); + la_instr->addOperand(std::make_unique(last_step_addr_vreg)); + la_instr->addOperand(std::make_unique(global_base->getName())); + CurMBB->addInstruction(std::move(la_instr)); + } else { + // 对于函数参数或来自其他指令的指针,直接获取其vreg。 + // 这个vreg必须被保护,不能在计算中被修改。 + last_step_addr_vreg = getVReg(base_ptr_val); + } + + // --- Step 2: 遵循LLVM GEP语义迭代计算地址 --- + Type* current_type = gep->getBasePointer()->getType()->as()->getBaseType(); + + for (size_t i = 0; i < gep->getNumIndices(); ++i) { + Value* indexValue = gep->getIndex(i); + unsigned stride = getTypeSizeInBytes(current_type); + + if (stride != 0) { + // --- 为当前索引和步长生成偏移计算指令 --- + auto offset_vreg = getNewVReg(Type::getIntType()); + + unsigned index_vreg; + if (auto const_index = dynamic_cast(indexValue)) { + index_vreg = getNewVReg(Type::getIntType()); + auto li = std::make_unique(RVOpcodes::LI); + li->addOperand(std::make_unique(index_vreg)); + li->addOperand(std::make_unique(const_index->getInt())); + CurMBB->addInstruction(std::move(li)); + } else { + index_vreg = getVReg(indexValue); + } + + if (stride == 1) { + auto mv = std::make_unique(RVOpcodes::MV); + mv->addOperand(std::make_unique(offset_vreg)); + mv->addOperand(std::make_unique(index_vreg)); + CurMBB->addInstruction(std::move(mv)); + } else { + auto size_vreg = getNewVReg(Type::getIntType()); + auto li_size = std::make_unique(RVOpcodes::LI); + li_size->addOperand(std::make_unique(size_vreg)); + li_size->addOperand(std::make_unique(stride)); + CurMBB->addInstruction(std::move(li_size)); + + auto mul = std::make_unique(RVOpcodes::MULW); + mul->addOperand(std::make_unique(offset_vreg)); + mul->addOperand(std::make_unique(index_vreg)); + mul->addOperand(std::make_unique(size_vreg)); + CurMBB->addInstruction(std::move(mul)); + } + + // --- 关键修复点 --- + // 创建一个新的vreg来保存本次加法的结果。 + unsigned current_step_addr_vreg = getNewVReg(gep->getType()); + + // 执行 add current_step, last_step, offset + // 这确保了 last_step_addr_vreg (输入) 永远不会被直接修改。 + auto add = std::make_unique(RVOpcodes::ADD); + add->addOperand(std::make_unique(current_step_addr_vreg)); + add->addOperand(std::make_unique(last_step_addr_vreg)); + add->addOperand(std::make_unique(offset_vreg)); + CurMBB->addInstruction(std::move(add)); + + // 本次的结果成为下一次计算的输入。 + last_step_addr_vreg = current_step_addr_vreg; + } + + // --- 为下一次迭代更新类型 --- + if (auto array_type = current_type->as()) { + current_type = array_type->getElementType(); + } else if (auto ptr_type = current_type->as()) { + current_type = ptr_type->getBaseType(); + } + } + + // --- Step 3: 将最终计算出的地址存入GEP的目标虚拟寄存器 --- + auto final_mv = std::make_unique(RVOpcodes::MV); + final_mv->addOperand(std::make_unique(result_vreg)); + final_mv->addOperand(std::make_unique(last_step_addr_vreg)); + CurMBB->addInstruction(std::move(final_mv)); + break; + } } default: From 5cf411680ef35c51ba966fb3079a8082e032f812 Mon Sep 17 00:00:00 2001 From: CGH0S7 <776459475@qq.com> Date: Wed, 20 Aug 2025 01:31:51 +0800 Subject: [PATCH 10/15] =?UTF-8?q?[backend-optpatch]=E5=AF=B9RISCv64ISel.cp?= =?UTF-8?q?p=E4=BC=98=E5=8C=96=E5=88=A4=E6=96=AD=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E8=A1=A5=E4=B8=81=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index 9fccba6..6af4311 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -103,7 +103,29 @@ void RISCv64ISel::select() { } } - if (optLevel > 0) { + // 仅当函数满足特定条件时,才需要保存参数寄存器,应用更精细的过滤规则 + // 1. 函数包含call指令 (非叶子函数): 参数寄存器(a0-a7)是调用者保存的, + // call指令可能会覆盖这些寄存器,因此必须保存。 + // 2. 函数包含alloca指令 (需要栈分配)。 + // 3. 函数的指令数量超过一个阈值(如20),意味着它是一个复杂的叶子函数, + // 为安全起见,保存其参数。 + // 简单的叶子函数 (如min) 则可以跳过这个步骤进行优化。 + auto shouldSaveArgs = [](Function* func) { + if (!func) return false; + int instruction_count = 0; + for (const auto& bb : func->getBasicBlocks()) { + for (const auto& inst : bb->getInstructions()) { + if (dynamic_cast(inst.get()) || dynamic_cast(inst.get())) { + return true; // 发现call或alloca,立即返回true + } + instruction_count++; + } + } + // 如果没有call或alloca,则检查指令数量 + return instruction_count > 45; + }; + + if (optLevel > 0 && shouldSaveArgs(F)) { if (F && !F->getBasicBlocks().empty()) { // 定位到第一个MachineBasicBlock,也就是函数入口 BasicBlock* first_ir_block = F->getBasicBlocks_NoRange().front().get(); From dd2725796a2a79d2361c53e2da7a979a01943d1a Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Wed, 20 Aug 2025 01:37:59 +0800 Subject: [PATCH 11/15] =?UTF-8?q?[backend-O1-1]=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E5=AF=84=E5=AD=98=E5=99=A8=E5=88=86=E9=85=8D=E5=99=A8=E5=9C=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0=E6=97=B6?= =?UTF-8?q?=E4=B8=8D=E5=81=A5=E5=A3=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 5 +- src/backend/RISCv64/RISCv64RegAlloc.cpp | 162 +++++++++++++++--- src/include/backend/RISCv64/RISCv64LLIR.h | 7 + src/include/backend/RISCv64/RISCv64RegAlloc.h | 5 +- 4 files changed, 147 insertions(+), 32 deletions(-) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index 22ccc22..f84a887 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -129,11 +129,11 @@ void RISCv64ISel::select() { mv->addOperand(std::make_unique(original_vreg)); CurMBB->addInstruction(std::move(mv)); + MFunc->addProtectedArgumentVReg(saved_vreg); // 4.【关键】更新vreg映射表,将arg的vreg指向新的、安全的vreg // 这样,后续所有对该参数的 getVReg(arg) 调用都会自动获得 saved_vreg, // 使得函数体内的代码都使用这个被保存过的值。 vreg_map[arg] = saved_vreg; - int_arg_idx++; } // --- 处理浮点参数 --- @@ -147,9 +147,8 @@ void RISCv64ISel::select() { fmv->addOperand(std::make_unique(original_vreg)); CurMBB->addInstruction(std::move(fmv)); - // 同样更新映射 + MFunc->addProtectedArgumentVReg(saved_vreg); vreg_map[arg] = saved_vreg; - fp_arg_idx++; } // 对于栈传递的参数,则无需处理 diff --git a/src/backend/RISCv64/RISCv64RegAlloc.cpp b/src/backend/RISCv64/RISCv64RegAlloc.cpp index ee40c00..b3affd2 100644 --- a/src/backend/RISCv64/RISCv64RegAlloc.cpp +++ b/src/backend/RISCv64/RISCv64RegAlloc.cpp @@ -98,6 +98,7 @@ bool RISCv64RegAlloc::doAllocation() { precolorByCallingConvention(); analyzeLiveness(); build(); + protectCrossCallVRegs(); makeWorklist(); while (!simplifyWorklist.empty() || !worklistMoves.empty() || !freezeWorklist.empty() || !spillWorklist.empty()) { @@ -185,6 +186,57 @@ void RISCv64RegAlloc::precolorByCallingConvention() { } } +void RISCv64RegAlloc::protectCrossCallVRegs() { + // 从ISel获取被标记为需要保护的参数副本vreg集合 + const auto& vregs_to_protect_potentially = MFunc->getProtectedArgumentVRegs(); + if (vregs_to_protect_potentially.empty()) { + return; // 如果没有需要保护的vreg,直接返回 + } + + VRegSet live_across_call_vregs; + // 遍历所有指令,找出哪些被标记的vreg其生命周期确实跨越了call指令 + for (const auto& mbb_ptr : MFunc->getBlocks()) { + for (const auto& instr_ptr : mbb_ptr->getInstructions()) { + if (instr_ptr->getOpcode() == RVOpcodes::CALL) { + const VRegSet& live_out_after_call = live_out_map.at(instr_ptr.get()); + for (unsigned vreg : vregs_to_protect_potentially) { + if (live_out_after_call.count(vreg)) { + live_across_call_vregs.insert(vreg); + } + } + } + } + } + + if (live_across_call_vregs.empty()) { + return; // 如果被标记的vreg没有一个跨越call,也无需操作 + } + + if (DEEPDEBUG) { + std::cerr << "--- [FIX] Applying protection for argument vregs that live across calls: "; + for(unsigned v : live_across_call_vregs) std::cerr << regIdToString(v) << " "; + std::cerr << "\n"; + } + + // 获取所有调用者保存寄存器 + const auto& caller_saved_int = getCallerSavedIntRegs(); + const auto& caller_saved_fp = getCallerSavedFpRegs(); + const unsigned offset = static_cast(PhysicalReg::PHYS_REG_START_ID); + + // 为每个确认跨越call的vreg,添加与所有调用者保存寄存器的冲突 + for (unsigned vreg : live_across_call_vregs) { + if (isFPVReg(vreg)) { // 如果是浮点vreg + for (auto preg : caller_saved_fp) { + addEdge(vreg, offset + static_cast(preg)); + } + } else { // 如果是整数vreg + for (auto preg : caller_saved_int) { + addEdge(vreg, offset + static_cast(preg)); + } + } + } +} + // 初始化/重置所有数据结构 void RISCv64RegAlloc::initialize() { initial.clear(); @@ -504,12 +556,20 @@ void RISCv64RegAlloc::coalesce() { unsigned y = getAlias(*use.begin()); unsigned u, v; - // 进一步修正:标准化u和v的逻辑,必须同时考虑物理寄存器和已预着色的虚拟寄存器。 - // 目标是确保如果两个操作数中有一个是预着色的,它一定会被赋给 u。 - if (precolored.count(y) || coloredNodes.count(y)) { - u = y; v = x; - } else { - u = x; v = y; + // 总是将待合并的虚拟寄存器赋给 v,将合并目标赋给 u。 + // 优先级: 物理寄存器 (precolored) > 已着色的虚拟寄存器 (coloredNodes) > 普通虚拟寄存器。 + if (precolored.count(y)) { + u = y; + v = x; + } else if (precolored.count(x)) { + u = x; + v = y; + } else if (coloredNodes.count(y)) { + u = y; + v = x; + } else { + u = x; + v = y; } // 防御性检查,处理物理寄存器之间的传送指令 @@ -528,7 +588,75 @@ void RISCv64RegAlloc::coalesce() { addWorklist(u); return; } + + bool is_conflicting = false; + // 检查1:u 和 v 在冲突图中是否直接相连 + if ((adjList.count(v) && adjList.at(v).count(u)) || (adjList.count(u) && adjList.at(u).count(v))) { + if (DEEPERDEBUG) std::cerr << " -> [Check] Nodes interfere directly.\n"; + is_conflicting = true; + } + // 检查2:如果节点不直接相连,则检查是否存在间接的颜色冲突 + else { + // 获取 u 和 v 的颜色(如果它们有的话) + unsigned u_color_id = 0, v_color_id = 0; + if (precolored.count(u)) { + u_color_id = u; + } else if (coloredNodes.count(u) || color_map.count(u)) { // color_map.count(u) 是更可靠的检查 + u_color_id = static_cast(PhysicalReg::PHYS_REG_START_ID) + static_cast(color_map.at(u)); + } + if (precolored.count(v)) { + v_color_id = v; + } else if (coloredNodes.count(v) || color_map.count(v)) { + v_color_id = static_cast(PhysicalReg::PHYS_REG_START_ID) + static_cast(color_map.at(v)); + } + + // 如果 u 有颜色,检查 v 是否与该颜色代表的物理寄存器冲突 + if (u_color_id != 0 && adjList.count(v) && adjList.at(v).count(u_color_id)) { + if (DEEPERDEBUG) std::cerr << " -> [Check] Node " << regIdToString(v) << " interferes with the color of " << regIdToString(u) << " (" << regIdToString(u_color_id) << ").\n"; + is_conflicting = true; + } + // 如果 v 有颜色,检查 u 是否与该颜色代表的物理寄存器冲突 + else if (v_color_id != 0 && adjList.count(u) && adjList.at(u).count(v_color_id)) { + if (DEEPERDEBUG) std::cerr << " -> [Check] Node " << regIdToString(u) << " interferes with the color of " << regIdToString(v) << " (" << regIdToString(v_color_id) << ").\n"; + is_conflicting = true; + } + } + + if (is_conflicting) { + if (DEEPERDEBUG) std::cerr << " -> Constrained (nodes interfere directly or via pre-coloring).\n"; + constrainedMoves.insert(move); + addWorklist(u); + addWorklist(v); + return; + } + + bool u_is_colored = precolored.count(u) || coloredNodes.count(u); + bool v_is_colored = precolored.count(v) || coloredNodes.count(v); + + if (u_is_colored && v_is_colored) { + PhysicalReg u_color = precolored.count(u) + ? static_cast(u - static_cast(PhysicalReg::PHYS_REG_START_ID)) + : color_map.at(u); + PhysicalReg v_color = precolored.count(v) + ? static_cast(v - static_cast(PhysicalReg::PHYS_REG_START_ID)) + : color_map.at(v); + + if (u_color != v_color) { + if (DEEPERDEBUG) std::cerr << " -> Constrained (move between two different precolored nodes: " + << regToString(u_color) << " and " << regToString(v_color) << ").\n"; + constrainedMoves.insert(move); + return; + } else { + if (DEEPERDEBUG) std::cerr << " -> Trivial coalesce (move between same precolored nodes).\n"; + coalescedMoves.insert(move); + combine(u, v); + addWorklist(u); + return; + } + } + + // 类型检查 if (isFPVReg(u) != isFPVReg(v)) { if (DEEPERDEBUG) std::cerr << " -> Constrained (type mismatch: " << regIdToString(u) << " is " << (isFPVReg(u) ? "float" : "int") << ", " << regIdToString(v) << " is " @@ -539,25 +667,11 @@ void RISCv64RegAlloc::coalesce() { return; } - // 注意:如果v已经是u的邻居, pre_interfere 会为true。 - // 但如果v不在adjList中(例如v是预着色节点),我们需要检查u是否在v的邻居中。 - // 为了简化,我们假设adjList包含了所有虚拟寄存器。对于(Phys, Virt)对,冲突信息存储在Virt节点的邻接表中。 - bool pre_interfere = (adjList.count(v) && adjList.at(v).count(u)) || (adjList.count(u) && adjList.at(u).count(v)); - - if (pre_interfere) { - if (DEEPERDEBUG) std::cerr << " -> Constrained (nodes already interfere).\n"; - constrainedMoves.insert(move); - addWorklist(u); - addWorklist(v); - return; - } - - // 考虑物理寄存器和已预着色的虚拟寄存器 + // 启发式判断逻辑 bool u_is_effectively_precolored = precolored.count(u) || coloredNodes.count(u); bool can_coalesce = false; if (u_is_effectively_precolored) { - // --- 场景1:u是物理寄存器或已预着色虚拟寄存器,使用 George 启发式 --- if (DEEPERDEBUG) std::cerr << " -> Trying George Heuristic (u is effectively precolored)...\n"; VRegSet neighbors_of_v = adjacent(v); @@ -1227,11 +1341,7 @@ bool RISCv64RegAlloc::georgeHeuristic(unsigned t, unsigned u) { int K = isFPVReg(t) ? K_fp : K_int; - // 缺陷 #2 修正: 移除了致命的 || precolored.count(u) 条件。 - // 在此函数的上下文中,u 总是预着色的物理寄存器ID,导致旧的条件永远为true,使整个启发式失效。 - // 正确的逻辑是检查:邻居t的度数是否小于K,或者t是否已经与u冲突。 - // return degree.at(t) < K || adjList.at(t).count(u); - return degree.at(t) < K || !adjList.at(t).count(u); + return degree.at(t) < K || adjList.at(t).count(u); } void RISCv64RegAlloc::combine(unsigned u, unsigned v) { diff --git a/src/include/backend/RISCv64/RISCv64LLIR.h b/src/include/backend/RISCv64/RISCv64LLIR.h index b021f04..16d344a 100644 --- a/src/include/backend/RISCv64/RISCv64LLIR.h +++ b/src/include/backend/RISCv64/RISCv64LLIR.h @@ -326,12 +326,19 @@ public: void addBlock(std::unique_ptr block) { blocks.push_back(std::move(block)); } + void addProtectedArgumentVReg(unsigned vreg) { + protected_argument_vregs.insert(vreg); + } + const std::set& getProtectedArgumentVRegs() const { + return protected_argument_vregs; + } private: Function* F; RISCv64ISel* isel; // 指向创建它的ISel,用于获取vreg映射等信息 std::string name; std::vector> blocks; StackFrameInfo frame_info; + std::set protected_argument_vregs; }; inline bool isMemoryOp(RVOpcodes opcode) { switch (opcode) { diff --git a/src/include/backend/RISCv64/RISCv64RegAlloc.h b/src/include/backend/RISCv64/RISCv64RegAlloc.h index 123c403..8ba4e2b 100644 --- a/src/include/backend/RISCv64/RISCv64RegAlloc.h +++ b/src/include/backend/RISCv64/RISCv64RegAlloc.h @@ -45,12 +45,11 @@ private: void rewriteProgram(); bool doAllocation(); void applyColoring(); - - void dumpState(const std::string &stage); - void precolorByCallingConvention(); + void protectCrossCallVRegs(); // --- 辅助函数 --- + void dumpState(const std::string &stage); void getInstrUseDef(const MachineInstr* instr, VRegSet& use, VRegSet& def); void getInstrUseDef_Liveness(const MachineInstr *instr, VRegSet &use, VRegSet &def); void addEdge(unsigned u, unsigned v); From 9ff1ace10efeebd25527b66a6fa74ade8c3e2642 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Wed, 20 Aug 2025 02:13:23 +0800 Subject: [PATCH 12/15] =?UTF-8?q?[backend-O1-1]=E8=B0=83=E6=95=B4=E5=AF=84?= =?UTF-8?q?=E5=AD=98=E5=99=A8=E9=A2=84=E7=9D=80=E8=89=B2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 8 +++++ src/backend/RISCv64/RISCv64RegAlloc.cpp | 46 ++++++++++++------------- src/midend/Pass/Pass.cpp | 14 ++++---- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index f84a887..fb9cbd6 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -554,6 +554,14 @@ void RISCv64ISel::selectNode(DAGNode* node) { CurMBB->addInstruction(std::move(instr)); break; } + case BinaryInst::kMulh: { + auto instr = std::make_unique(RVOpcodes::MULH); + instr->addOperand(std::make_unique(dest_vreg)); + instr->addOperand(std::make_unique(lhs_vreg)); + instr->addOperand(std::make_unique(rhs_vreg)); + CurMBB->addInstruction(std::move(instr)); + break; + } case Instruction::kDiv: { auto instr = std::make_unique(RVOpcodes::DIVW); instr->addOperand(std::make_unique(dest_vreg)); diff --git a/src/backend/RISCv64/RISCv64RegAlloc.cpp b/src/backend/RISCv64/RISCv64RegAlloc.cpp index b3affd2..4bdb9ef 100644 --- a/src/backend/RISCv64/RISCv64RegAlloc.cpp +++ b/src/backend/RISCv64/RISCv64RegAlloc.cpp @@ -193,30 +193,30 @@ void RISCv64RegAlloc::protectCrossCallVRegs() { return; // 如果没有需要保护的vreg,直接返回 } - VRegSet live_across_call_vregs; - // 遍历所有指令,找出哪些被标记的vreg其生命周期确实跨越了call指令 - for (const auto& mbb_ptr : MFunc->getBlocks()) { - for (const auto& instr_ptr : mbb_ptr->getInstructions()) { - if (instr_ptr->getOpcode() == RVOpcodes::CALL) { - const VRegSet& live_out_after_call = live_out_map.at(instr_ptr.get()); - for (unsigned vreg : vregs_to_protect_potentially) { - if (live_out_after_call.count(vreg)) { - live_across_call_vregs.insert(vreg); - } - } - } - } - } + // VRegSet live_across_call_vregs; + // // 遍历所有指令,找出哪些被标记的vreg其生命周期确实跨越了call指令 + // for (const auto& mbb_ptr : MFunc->getBlocks()) { + // for (const auto& instr_ptr : mbb_ptr->getInstructions()) { + // if (instr_ptr->getOpcode() == RVOpcodes::CALL) { + // const VRegSet& live_out_after_call = live_out_map.at(instr_ptr.get()); + // for (unsigned vreg : vregs_to_protect_potentially) { + // if (live_out_after_call.count(vreg)) { + // live_across_call_vregs.insert(vreg); + // } + // } + // } + // } + // } - if (live_across_call_vregs.empty()) { - return; // 如果被标记的vreg没有一个跨越call,也无需操作 - } + // if (live_across_call_vregs.empty()) { + // return; // 如果被标记的vreg没有一个跨越call,也无需操作 + // } - if (DEEPDEBUG) { - std::cerr << "--- [FIX] Applying protection for argument vregs that live across calls: "; - for(unsigned v : live_across_call_vregs) std::cerr << regIdToString(v) << " "; - std::cerr << "\n"; - } + // if (DEEPDEBUG) { + // std::cerr << "--- [FIX] Applying protection for argument vregs that live across calls: "; + // for(unsigned v : live_across_call_vregs) std::cerr << regIdToString(v) << " "; + // std::cerr << "\n"; + // } // 获取所有调用者保存寄存器 const auto& caller_saved_int = getCallerSavedIntRegs(); @@ -224,7 +224,7 @@ void RISCv64RegAlloc::protectCrossCallVRegs() { const unsigned offset = static_cast(PhysicalReg::PHYS_REG_START_ID); // 为每个确认跨越call的vreg,添加与所有调用者保存寄存器的冲突 - for (unsigned vreg : live_across_call_vregs) { + for (unsigned vreg : vregs_to_protect_potentially) { if (isFPVReg(vreg)) { // 如果是浮点vreg for (auto preg : caller_saved_fp) { addEdge(vreg, offset + static_cast(preg)); diff --git a/src/midend/Pass/Pass.cpp b/src/midend/Pass/Pass.cpp index 0678e4e..a077645 100644 --- a/src/midend/Pass/Pass.cpp +++ b/src/midend/Pass/Pass.cpp @@ -181,19 +181,19 @@ void PassManager::runOptimizationPipeline(Module* moduleIR, IRBuilder* builderIR printPasses(); } - // this->clearPasses(); - // this->addPass(&LoopStrengthReduction::ID); - // this->run(); + this->clearPasses(); + this->addPass(&LoopStrengthReduction::ID); + this->run(); if(DEBUG) { std::cout << "=== IR After Loop Normalization, and Strength Reduction Optimizations ===\n"; printPasses(); } - // // 全局强度削弱优化,包括代数优化和魔数除法 - // this->clearPasses(); - // this->addPass(&GlobalStrengthReduction::ID); - // this->run(); + // 全局强度削弱优化,包括代数优化和魔数除法 + this->clearPasses(); + this->addPass(&GlobalStrengthReduction::ID); + this->run(); if(DEBUG) { std::cout << "=== IR After Global Strength Reduction Optimizations ===\n"; From b014efe1834c205bb30166983240868202acfb33 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Wed, 20 Aug 2025 02:46:15 +0800 Subject: [PATCH 13/15] =?UTF-8?q?[backend-O1-1]=E5=9C=A8=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0kAnd=E5=92=8CkOr=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index fb9cbd6..0d1a201 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -673,6 +673,22 @@ void RISCv64ISel::selectNode(DAGNode* node) { CurMBB->addInstruction(std::move(xori)); break; } + case BinaryInst::kAnd: { + auto instr = std::make_unique(RVOpcodes::AND); + instr->addOperand(std::make_unique(dest_vreg)); + instr->addOperand(std::make_unique(lhs_vreg)); + instr->addOperand(std::make_unique(rhs_vreg)); + CurMBB->addInstruction(std::move(instr)); + break; + } + case BinaryInst::kOr: { + auto instr = std::make_unique(RVOpcodes::OR); + instr->addOperand(std::make_unique(dest_vreg)); + instr->addOperand(std::make_unique(lhs_vreg)); + instr->addOperand(std::make_unique(rhs_vreg)); + CurMBB->addInstruction(std::move(instr)); + break; + } default: throw std::runtime_error("Unsupported binary instruction in ISel"); } From 7db7dd087688f044bed4be179571eedac2e79c30 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Wed, 20 Aug 2025 03:08:38 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=20[backend]=E5=90=8E=E7=AB=AF=E6=B5=81?= =?UTF-8?q?=E6=B0=B4=E7=BA=BF=E5=BC=95=E5=85=A5-O1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64Backend.cpp | 28 +++++++++++--------- src/include/backend/RISCv64/RISCv64Backend.h | 1 + 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/backend/RISCv64/RISCv64Backend.cpp b/src/backend/RISCv64/RISCv64Backend.cpp index f6e4515..183d1a4 100644 --- a/src/backend/RISCv64/RISCv64Backend.cpp +++ b/src/backend/RISCv64/RISCv64Backend.cpp @@ -227,13 +227,15 @@ std::string RISCv64CodeGen::function_gen(Function* func) { << ss_after_eli.str(); } - // 阶段 2.1: 除法强度削弱优化 (Division Strength Reduction) - DivStrengthReduction div_strength_reduction; - div_strength_reduction.runOnMachineFunction(mfunc.get()); + if (optLevel > 0) { + // 阶段 2.1: 除法强度削弱优化 (Division Strength Reduction) + DivStrengthReduction div_strength_reduction; + div_strength_reduction.runOnMachineFunction(mfunc.get()); - // // 阶段 2.2: 指令调度 (Instruction Scheduling) - // PreRA_Scheduler scheduler; - // scheduler.runOnMachineFunction(mfunc.get()); + // 阶段 2.2: 指令调度 (Instruction Scheduling) + PreRA_Scheduler scheduler; + scheduler.runOnMachineFunction(mfunc.get()); + } // 阶段 3: 物理寄存器分配 (Register Allocation) bool allocation_succeeded = false; @@ -341,13 +343,15 @@ std::string RISCv64CodeGen::function_gen(Function* func) { mfunc->dumpStackFrameInfo(std::cerr); } - // 阶段 4: 窥孔优化 (Peephole Optimization) - PeepholeOptimizer peephole; - peephole.runOnMachineFunction(mfunc.get()); + if (optLevel > 0) { + // 阶段 4: 窥孔优化 (Peephole Optimization) + PeepholeOptimizer peephole; + peephole.runOnMachineFunction(mfunc.get()); - // // 阶段 5: 局部指令调度 (Local Scheduling) - // PostRA_Scheduler local_scheduler; - // local_scheduler.runOnMachineFunction(mfunc.get()); + // 阶段 5: 局部指令调度 (Local Scheduling) + PostRA_Scheduler local_scheduler; + local_scheduler.runOnMachineFunction(mfunc.get()); + } // 阶段 3.2: 插入序言和尾声 PrologueEpilogueInsertionPass pei_pass; diff --git a/src/include/backend/RISCv64/RISCv64Backend.h b/src/include/backend/RISCv64/RISCv64Backend.h index 8f02e5b..ce9073b 100644 --- a/src/include/backend/RISCv64/RISCv64Backend.h +++ b/src/include/backend/RISCv64/RISCv64Backend.h @@ -6,6 +6,7 @@ extern int DEBUG; extern int DEEPDEBUG; +extern int optLevel; namespace sysy { From 4864a3078773b6fda2c86e0813b0a8f8322c406f Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Wed, 20 Aug 2025 10:49:36 +0800 Subject: [PATCH 15/15] =?UTF-8?q?[backend]=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=84=9A=E6=9C=AC=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E7=BB=93=E6=9E=9C=E5=86=99=E5=85=A5=E5=88=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/runit.sh | 257 ++++++++++++++----------- src/backend/RISCv64/RISCv64Backend.cpp | 28 ++- 2 files changed, 159 insertions(+), 126 deletions(-) diff --git a/script/runit.sh b/script/runit.sh index c090415..e1d4398 100644 --- a/script/runit.sh +++ b/script/runit.sh @@ -29,10 +29,12 @@ EXEC_TIMEOUT=30 MAX_OUTPUT_LINES=20 MAX_OUTPUT_CHARS=1000 TEST_SETS=() +PERF_RUN_COUNT=1 # 新增: 性能测试运行次数 TOTAL_CASES=0 PASSED_CASES=0 FAILED_CASES_LIST="" INTERRUPTED=false +PERFORMANCE_MODE=false # 新增: 标记是否进行性能测试 # ================================================================= # --- 函数定义 --- @@ -49,6 +51,8 @@ show_help() { echo " -c, --clean 清理 'tmp' 目录下的所有生成文件。" echo " -O1 启用 sysyc 的 -O1 优化。" echo " -set [f|h|p|all]... 指定要运行的测试集 (functional, h_functional, performance)。可多选,默认为 all。" + echo " 当包含 'p' 时,会自动记录性能数据到 ${TMP_DIR}/performance_time.csv。" + echo " -pt N 设置 performance 测试集的每个用例运行 N 次取平均值 (默认: 1)。" echo " -sct N 设置 sysyc 编译超时为 N 秒 (默认: 30)。" echo " -lct N 设置 llc-19 编译超时为 N 秒 (默认: 10)。" echo " -gct N 设置 gcc 交叉编译超时为 N 秒 (默认: 10)。" @@ -104,7 +108,6 @@ print_summary() { local failed_count if [ -n "$FAILED_CASES_LIST" ]; then - # `wc -l` 计算由换行符分隔的列表项数 failed_count=$(echo -e -n "${FAILED_CASES_LIST}" | wc -l) else failed_count=0 @@ -116,10 +119,27 @@ print_summary() { if [ -n "$FAILED_CASES_LIST" ]; then echo "" echo -e "\e[31m未通过的测例:\e[0m" - # 使用 printf 保证原样输出 printf "%b" "${FAILED_CASES_LIST}" fi + # --- 本次修改点: 提示性能测试结果文件 --- + if ${PERFORMANCE_MODE}; then + # --- 本次修改点: 计算并添加总计行 --- + if [ -f "${PERFORMANCE_CSV_FILE}" ] && [ $(wc -l < "${PERFORMANCE_CSV_FILE}") -gt 1 ]; then + local total_seconds_sum + total_seconds_sum=$(awk -F, 'NR > 1 {sum += $3} END {printf "%.5f", sum}' "${PERFORMANCE_CSV_FILE}") + + local total_s_int=${total_seconds_sum%.*} + [[ -z "$total_s_int" ]] && total_s_int=0 # 处理小于1秒的情况 + local total_us_int=$(echo "(${total_seconds_sum} - ${total_s_int}) * 1000000" | bc | cut -d. -f1) + local total_time_str="${total_s_int}s${total_us_int}us" + + echo "all,${total_time_str},${total_seconds_sum}" >> "${PERFORMANCE_CSV_FILE}" + fi + echo "" + echo -e "\e[32m性能测试数据已保存到: ${PERFORMANCE_CSV_FILE}\e[0m" + fi + echo "========================================" if [ "$failed_count" -gt 0 ]; then @@ -139,12 +159,9 @@ handle_sigint() { # --- 主逻辑开始 --- # ================================================================= -# --- 新增:设置 trap 来捕获 SIGINT --- trap handle_sigint SIGINT - mkdir -p "${TMP_DIR}" -# 解析命令行参数 while [[ "$#" -gt 0 ]]; do case "$1" in -e|--executable) EXECUTE_MODE=true; shift ;; @@ -155,6 +172,7 @@ while [[ "$#" -gt 0 ]]; do shift while [[ "$#" -gt 0 && ! "$1" =~ ^- ]]; do TEST_SETS+=("$1"); shift; done ;; + -pt) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then PERF_RUN_COUNT="$2"; shift 2; else echo "错误: -pt 需要一个正整数参数。" >&2; exit 1; fi ;; -sct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then SYSYC_TIMEOUT="$2"; shift 2; else echo "错误: -sct 需要一个正整数参数。" >&2; exit 1; fi ;; -lct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then LLC_TIMEOUT="$2"; shift 2; else echo "错误: -lct 需要一个正整数参数。" >&2; exit 1; fi ;; -gct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift 2; else echo "错误: -gct 需要一个正整数参数。" >&2; exit 1; fi ;; @@ -179,10 +197,14 @@ SET_MAP[p]="performance" SEARCH_PATHS=() if [ ${#TEST_SETS[@]} -eq 0 ] || [[ " ${TEST_SETS[@]} " =~ " all " ]]; then SEARCH_PATHS+=("${TESTDATA_DIR}") + if [ -d "${TESTDATA_DIR}/performance" ]; then PERFORMANCE_MODE=true; fi else for set in "${TEST_SETS[@]}"; do if [[ -v SET_MAP[$set] ]]; then SEARCH_PATHS+=("${TESTDATA_DIR}/${SET_MAP[$set]}") + if [[ "$set" == "p" ]]; then + PERFORMANCE_MODE=true + fi else echo -e "\e[33m警告: 未知的测试集 '$set',已忽略。\e[0m" fi @@ -212,6 +234,9 @@ else fi echo "运行模式: ${RUN_MODE_INFO}" echo "${TIMEOUT_INFO}" +if ${PERFORMANCE_MODE} && ([ ${EXECUTE_MODE} = true ] || [ ${IR_EXECUTE_MODE} = true ]) && [ ${PERF_RUN_COUNT} -gt 1 ]; then + echo "性能测试运行次数: ${PERF_RUN_COUNT}" +fi if ${EXECUTE_MODE} || ${IR_EXECUTE_MODE}; then echo "失败输出最大行数: ${MAX_OUTPUT_LINES}" echo "失败输出最大字符数: ${MAX_OUTPUT_CHARS}" @@ -225,6 +250,11 @@ if [ -z "$sy_files" ]; then fi TOTAL_CASES=$(echo "$sy_files" | wc -w) +PERFORMANCE_CSV_FILE="${TMP_DIR}/performance_time.csv" +if ${PERFORMANCE_MODE}; then + echo "Case,Time_String,Time_Seconds" > "${PERFORMANCE_CSV_FILE}" +fi + while IFS= read -r sy_file; do is_passed=0 # 0 表示失败, 1 表示通过 @@ -234,11 +264,13 @@ while IFS= read -r sy_file; do assembly_file_S="${TMP_DIR}/${output_base_name}_sysyc_S.s" executable_file_S="${TMP_DIR}/${output_base_name}_sysyc_S" output_actual_file_S="${TMP_DIR}/${output_base_name}_sysyc_S.actual_out" + stderr_file_S="${TMP_DIR}/${output_base_name}_sysyc_S.stderr" ir_file="${TMP_DIR}/${output_base_name}_sysyc_ir.ll" assembly_file_from_ir="${TMP_DIR}/${output_base_name}_from_ir.s" executable_file_from_ir="${TMP_DIR}/${output_base_name}_from_ir" output_actual_file_from_ir="${TMP_DIR}/${output_base_name}_from_ir.actual_out" + stderr_file_from_ir="${TMP_DIR}/${output_base_name}_from_ir.stderr" input_file="${sy_file%.*}.in" output_reference_file="${sy_file%.*}.out" @@ -249,165 +281,170 @@ while IFS= read -r sy_file; do if ${IR_EXECUTE_MODE}; then step_failed=0 test_logic_passed=0 - + total_time_us=0 + echo " [1/4] 使用 sysyc 编译为 IR (超时 ${SYSYC_TIMEOUT}s)..." - timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -s ir "${sy_file}" -o "${ir_file}" ${OPTIMIZE_FLAG} - SYSYC_STATUS=$? - if [ $SYSYC_STATUS -ne 0 ]; then - [ $SYSYC_STATUS -eq 124 ] && echo -e "\e[31m错误: SysY (IR) 编译超时\e[0m" || echo -e "\e[31m错误: SysY (IR) 编译失败,退出码: ${SYSYC_STATUS}\e[0m" - step_failed=1 - fi + timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -s ir "${sy_file}" -o "${ir_file}" ${OPTIMIZE_FLAG}; if [ $? -ne 0 ]; then echo -e "\e[31m错误: SysY (IR) 编译失败或超时\e[0m"; step_failed=1; fi if [ "$step_failed" -eq 0 ]; then echo " [2/4] 使用 llc-19 编译为汇编 (超时 ${LLC_TIMEOUT}s)..." - timeout -s KILL ${LLC_TIMEOUT} "${LLC_CMD}" -march=riscv64 -mcpu=generic-rv64 -mattr=+m,+a,+f,+d,+c -filetype=asm "${ir_file}" -o "${assembly_file_from_ir}" - LLC_STATUS=$? - if [ $LLC_STATUS -ne 0 ]; then - [ $LLC_STATUS -eq 124 ] && echo -e "\e[31m错误: llc-19 编译超时\e[0m" || echo -e "\e[31m错误: llc-19 编译失败,退出码: ${LLC_STATUS}\e[0m" - step_failed=1 - fi + timeout -s KILL ${LLC_TIMEOUT} ${LLC_CMD} -march=riscv64 -mcpu=generic-rv64 -mattr=+m,+a,+f,+d,+c -filetype=asm "${ir_file}" -o "${assembly_file_from_ir}"; if [ $? -ne 0 ]; then echo -e "\e[31m错误: llc-19 编译失败或超时\e[0m"; step_failed=1; fi fi if [ "$step_failed" -eq 0 ]; then echo " [3/4] 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..." - timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file_from_ir}" -o "${executable_file_from_ir}" -L"${LIB_DIR}" -lsysy_riscv -static - GCC_STATUS=$? - if [ $GCC_STATUS -ne 0 ]; then - [ $GCC_STATUS -eq 124 ] && echo -e "\e[31m错误: GCC 编译超时\e[0m" || echo -e "\e[31m错误: GCC 编译失败,退出码: ${GCC_STATUS}\e[0m" - step_failed=1 - fi + timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file_from_ir}" -o "${executable_file_from_ir}" -L"${LIB_DIR}" -lsysy_riscv -static; if [ $? -ne 0 ]; then echo -e "\e[31m错误: GCC 编译失败或超时\e[0m"; step_failed=1; fi fi if [ "$step_failed" -eq 0 ]; then echo " [4/4] 正在执行 (超时 ${EXEC_TIMEOUT}s)..." - exec_cmd="${QEMU_RISCV64} \"${executable_file_from_ir}\"" - [ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\"" - exec_cmd+=" > \"${output_actual_file_from_ir}\"" - - eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}" - ACTUAL_RETURN_CODE=$? - - if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then - echo -e "\e[31m 执行超时: 运行超过 ${EXEC_TIMEOUT} 秒\e[0m" - else + current_run_failed=0 + for (( i=1; i<=PERF_RUN_COUNT; i++ )); do + if [ ${PERF_RUN_COUNT} -gt 1 ]; then echo -n " 第 $i/${PERF_RUN_COUNT} 次运行... "; fi + exec_cmd="${QEMU_RISCV64} \"${executable_file_from_ir}\"" + [ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\"" + exec_cmd+=" > \"${output_actual_file_from_ir}\" 2> \"${stderr_file_from_ir}\"" + eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}" + ACTUAL_RETURN_CODE=$? + + if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then echo -e "\e[31m超时\e[0m"; current_run_failed=1; break; fi + if ${PERFORMANCE_MODE}; then + TIME_LINE=$(grep "TOTAL:" "${stderr_file_from_ir}") + if [ -n "$TIME_LINE" ]; then + H=$(echo "$TIME_LINE" | sed -E 's/TOTAL: ([0-9]+)H-.*/\1/') + M=$(echo "$TIME_LINE" | sed -E 's/.*-([0-9]+)M-.*/\1/') + S=$(echo "$TIME_LINE" | sed -E 's/.*-([0-9]+)S-.*/\1/') + US=$(echo "$TIME_LINE" | sed -E 's/.*-([0-9]+)us/\1/') + run_time_us=$(( H * 3600000000 + M * 60000000 + S * 1000000 + US )) + total_time_us=$(( total_time_us + run_time_us )) + if [ ${PERF_RUN_COUNT} -gt 1 ]; then echo "耗时: ${run_time_us}us"; fi + else + echo -e "\e[31m未找到时间信息\e[0m"; current_run_failed=1; break + fi + fi + done + + if [ "$current_run_failed" -eq 0 ]; then + test_logic_passed=1 if [ -f "${output_reference_file}" ]; then LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]') - test_logic_passed=1 if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED" EXPECTED_STDOUT_FILE="${TMP_DIR}/${output_base_name}_from_ir.expected_stdout" head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}" - - if [ "$ACTUAL_RETURN_CODE" -eq "$EXPECTED_RETURN_CODE" ]; then - echo -e "\e[32m 返回码测试成功: (${ACTUAL_RETURN_CODE}) 与期望值 (${EXPECTED_RETURN_CODE}) 匹配\e[0m" - else - echo -e "\e[31m 返回码测试失败: 期望: ${EXPECTED_RETURN_CODE}, 实际: ${ACTUAL_RETURN_CODE}\e[0m" - test_logic_passed=0 - fi - - if diff -q <(tr -d '[:space:]' < "${output_actual_file_from_ir}") <(tr -d '[:space:]' < "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then - [ "$test_logic_passed" -eq 1 ] && echo -e "\e[32m 标准输出测试成功\e[0m" - else - echo -e "\e[31m 标准输出测试失败\e[0m" - display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - display_file_content "${output_actual_file_from_ir}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - test_logic_passed=0 + if [ "$ACTUAL_RETURN_CODE" -ne "$EXPECTED_RETURN_CODE" ]; then echo -e "\e[31m 返回码测试失败: 期望 ${EXPECTED_RETURN_CODE}, 实际 ${ACTUAL_RETURN_CODE}\e[0m"; test_logic_passed=0; fi + if ! diff -q <(tr -d '[:space:]' < "${output_actual_file_from_ir}") <(tr -d '[:space:]' < "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then + echo -e "\e[31m 标准输出测试失败\e[0m"; test_logic_passed=0 + display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_from_ir}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" fi else if [ $ACTUAL_RETURN_CODE -ne 0 ]; then echo -e "\e[33m警告: 程序以非零状态 ${ACTUAL_RETURN_CODE} 退出 (纯输出比较模式)。\e[0m"; fi - if diff -q <(tr -d '[:space:]' < "${output_actual_file_from_ir}") <(tr -d '[:space:]' < "${output_reference_file}") >/dev/null 2>&1; then - echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m" - else - echo -e "\e[31m 失败: 输出不匹配\e[0m" - display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - display_file_content "${output_actual_file_from_ir}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - test_logic_passed=0 + if ! diff -q <(tr -d '[:space:]' < "${output_actual_file_from_ir}") <(tr -d '[:space:]' < "${output_reference_file}") >/dev/null 2>&1; then + echo -e "\e[31m 失败: 输出不匹配\e[0m"; test_logic_passed=0 + display_file_content "${output_reference_file}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_from_ir}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" fi fi - else - echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}" - test_logic_passed=1 fi + if [ "$test_logic_passed" -eq 1 ]; then echo -e "\e[32m 测试逻辑通过\e[0m"; fi fi fi - [ "$step_failed" -eq 0 ] && [ "$test_logic_passed" -eq 1 ] && is_passed=1 + if [ "$step_failed" -eq 0 ] && [ "$test_logic_passed" -eq 1 ]; then is_passed=1; fi + + if ${PERFORMANCE_MODE}; then + avg_time_us=0 + if [ "$is_passed" -eq 1 ]; then + avg_time_us=$(( total_time_us / PERF_RUN_COUNT )) + fi + S_AVG=$(( avg_time_us / 1000000 )) + US_AVG=$(( avg_time_us % 1000000 )) + TIME_STRING_AVG="${S_AVG}s${US_AVG}us" + TOTAL_SECONDS_AVG=$(echo "scale=5; ${avg_time_us} / 1000000" | bc) + echo "$(basename ${sy_file}),${TIME_STRING_AVG},${TOTAL_SECONDS_AVG}" >> "${PERFORMANCE_CSV_FILE}" + fi # --- 模式 2: 直接执行模式 (-e) --- elif ${EXECUTE_MODE}; then step_failed=0 test_logic_passed=0 + total_time_us=0 echo " [1/3] 使用 sysyc 编译为汇编 (超时 ${SYSYC_TIMEOUT}s)..." - timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file_S}" ${OPTIMIZE_FLAG} - SYSYC_STATUS=$? - if [ $SYSYC_STATUS -ne 0 ]; then - [ $SYSYC_STATUS -eq 124 ] && echo -e "\e[31m错误: SysY (汇编) 编译超时\e[0m" || echo -e "\e[31m错误: SysY (汇编) 编译失败,退出码: ${SYSYC_STATUS}\e[0m" - step_failed=1 - fi + timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file_S}" ${OPTIMIZE_FLAG}; if [ $? -ne 0 ]; then echo -e "\e[31m错误: SysY (汇编) 编译失败或超时\e[0m"; step_failed=1; fi if [ "$step_failed" -eq 0 ]; then echo " [2/3] 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..." - timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file_S}" -o "${executable_file_S}" -L"${LIB_DIR}" -lsysy_riscv -static - GCC_STATUS=$? - if [ $GCC_STATUS -ne 0 ]; then - [ $GCC_STATUS -eq 124 ] && echo -e "\e[31m错误: GCC 编译超时\e[0m" || echo -e "\e[31m错误: GCC 编译失败,退出码: ${GCC_STATUS}\e[0m" - step_failed=1 - fi + timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file_S}" -o "${executable_file_S}" -L"${LIB_DIR}" -lsysy_riscv -static; if [ $? -ne 0 ]; then echo -e "\e[31m错误: GCC 编译失败或超时\e[0m"; step_failed=1; fi fi if [ "$step_failed" -eq 0 ]; then echo " [3/3] 正在执行 (超时 ${EXEC_TIMEOUT}s)..." - exec_cmd="${QEMU_RISCV64} \"${executable_file_S}\"" - [ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\"" - exec_cmd+=" > \"${output_actual_file_S}\"" - - eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}" - ACTUAL_RETURN_CODE=$? + current_run_failed=0 + for (( i=1; i<=PERF_RUN_COUNT; i++ )); do + if [ ${PERF_RUN_COUNT} -gt 1 ]; then echo -n " 第 $i/${PERF_RUN_COUNT} 次运行... "; fi + exec_cmd="${QEMU_RISCV64} \"${executable_file_S}\"" + [ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\"" + exec_cmd+=" > \"${output_actual_file_S}\" 2> \"${stderr_file_S}\"" + eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}" + ACTUAL_RETURN_CODE=$? + + if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then echo -e "\e[31m超时\e[0m"; current_run_failed=1; break; fi + if ${PERFORMANCE_MODE}; then + TIME_LINE=$(grep "TOTAL:" "${stderr_file_S}") + if [ -n "$TIME_LINE" ]; then + H=$(echo "$TIME_LINE" | sed -E 's/TOTAL: ([0-9]+)H-.*/\1/') + M=$(echo "$TIME_LINE" | sed -E 's/.*-([0-9]+)M-.*/\1/') + S=$(echo "$TIME_LINE" | sed -E 's/.*-([0-9]+)S-.*/\1/') + US=$(echo "$TIME_LINE" | sed -E 's/.*-([0-9]+)us/\1/') + run_time_us=$(( H * 3600000000 + M * 60000000 + S * 1000000 + US )) + total_time_us=$(( total_time_us + run_time_us )) + if [ ${PERF_RUN_COUNT} -gt 1 ]; then echo "耗时: ${run_time_us}us"; fi + else + echo -e "\e[31m未找到时间信息\e[0m"; current_run_failed=1; break + fi + fi + done - if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then - echo -e "\e[31m 执行超时: 运行超过 ${EXEC_TIMEOUT} 秒\e[0m" - else + if [ "$current_run_failed" -eq 0 ]; then + test_logic_passed=1 if [ -f "${output_reference_file}" ]; then LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]') - test_logic_passed=1 if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED" EXPECTED_STDOUT_FILE="${TMP_DIR}/${output_base_name}_sysyc_S.expected_stdout" head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}" - - if [ "$ACTUAL_RETURN_CODE" -eq "$EXPECTED_RETURN_CODE" ]; then - echo -e "\e[32m 返回码测试成功: (${ACTUAL_RETURN_CODE}) 与期望值 (${EXPECTED_RETURN_CODE}) 匹配\e[0m" - else - echo -e "\e[31m 返回码测试失败: 期望: ${EXPECTED_RETURN_CODE}, 实际: ${ACTUAL_RETURN_CODE}\e[0m" - test_logic_passed=0 - fi - - if diff -q <(tr -d '[:space:]' < "${output_actual_file_S}") <(tr -d '[:space:]' < "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then - [ "$test_logic_passed" -eq 1 ] && echo -e "\e[32m 标准输出测试成功\e[0m" - else - echo -e "\e[31m 标准输出测试失败\e[0m" - display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - display_file_content "${output_actual_file_S}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - test_logic_passed=0 + if [ "$ACTUAL_RETURN_CODE" -ne "$EXPECTED_RETURN_CODE" ]; then echo -e "\e[31m 返回码测试失败: 期望 ${EXPECTED_RETURN_CODE}, 实际 ${ACTUAL_RETURN_CODE}\e[0m"; test_logic_passed=0; fi + if ! diff -q <(tr -d '[:space:]' < "${output_actual_file_S}") <(tr -d '[:space:]' < "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then + echo -e "\e[31m 标准输出测试失败\e[0m"; test_logic_passed=0 + display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_S}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" fi else if [ $ACTUAL_RETURN_CODE -ne 0 ]; then echo -e "\e[33m警告: 程序以非零状态 ${ACTUAL_RETURN_CODE} 退出 (纯输出比较模式)。\e[0m"; fi - if diff -q <(tr -d '[:space:]' < "${output_actual_file_S}") <(tr -d '[:space:]' < "${output_reference_file}") >/dev/null 2>&1; then - echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m" - else - echo -e "\e[31m 失败: 输出不匹配\e[0m" - display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - display_file_content "${output_actual_file_S}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" - test_logic_passed=0 + if ! diff -q <(tr -d '[:space:]' < "${output_actual_file_S}") <(tr -d '[:space:]' < "${output_reference_file}") >/dev/null 2>&1; then + echo -e "\e[31m 失败: 输出不匹配\e[0m"; test_logic_passed=0 + display_file_content "${output_reference_file}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" + display_file_content "${output_actual_file_S}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}" "${MAX_OUTPUT_CHARS}" fi fi - else - echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}" - test_logic_passed=1 fi + if [ "$test_logic_passed" -eq 1 ]; then echo -e "\e[32m 测试逻辑通过\e[0m"; fi fi fi - [ "$step_failed" -eq 0 ] && [ "$test_logic_passed" -eq 1 ] && is_passed=1 + if [ "$step_failed" -eq 0 ] && [ "$test_logic_passed" -eq 1 ]; then is_passed=1; fi + + if ${PERFORMANCE_MODE}; then + avg_time_us=0 + if [ "$is_passed" -eq 1 ]; then + avg_time_us=$(( total_time_us / PERF_RUN_COUNT )) + fi + S_AVG=$(( avg_time_us / 1000000 )) + US_AVG=$(( avg_time_us % 1000000 )) + TIME_STRING_AVG="${S_AVG}s${US_AVG}us" + TOTAL_SECONDS_AVG=$(echo "scale=5; ${avg_time_us} / 1000000" | bc) + echo "$(basename ${sy_file}),${TIME_STRING_AVG},${TOTAL_SECONDS_AVG}" >> "${PERFORMANCE_CSV_FILE}" + fi # --- 模式 3: 默认编译模式 --- else @@ -450,4 +487,4 @@ while IFS= read -r sy_file; do done <<< "$sy_files" # --- 修改:调用总结函数 --- -print_summary \ No newline at end of file +print_summary diff --git a/src/backend/RISCv64/RISCv64Backend.cpp b/src/backend/RISCv64/RISCv64Backend.cpp index 183d1a4..f6e4515 100644 --- a/src/backend/RISCv64/RISCv64Backend.cpp +++ b/src/backend/RISCv64/RISCv64Backend.cpp @@ -227,15 +227,13 @@ std::string RISCv64CodeGen::function_gen(Function* func) { << ss_after_eli.str(); } - if (optLevel > 0) { - // 阶段 2.1: 除法强度削弱优化 (Division Strength Reduction) - DivStrengthReduction div_strength_reduction; - div_strength_reduction.runOnMachineFunction(mfunc.get()); + // 阶段 2.1: 除法强度削弱优化 (Division Strength Reduction) + DivStrengthReduction div_strength_reduction; + div_strength_reduction.runOnMachineFunction(mfunc.get()); - // 阶段 2.2: 指令调度 (Instruction Scheduling) - PreRA_Scheduler scheduler; - scheduler.runOnMachineFunction(mfunc.get()); - } + // // 阶段 2.2: 指令调度 (Instruction Scheduling) + // PreRA_Scheduler scheduler; + // scheduler.runOnMachineFunction(mfunc.get()); // 阶段 3: 物理寄存器分配 (Register Allocation) bool allocation_succeeded = false; @@ -343,15 +341,13 @@ std::string RISCv64CodeGen::function_gen(Function* func) { mfunc->dumpStackFrameInfo(std::cerr); } - if (optLevel > 0) { - // 阶段 4: 窥孔优化 (Peephole Optimization) - PeepholeOptimizer peephole; - peephole.runOnMachineFunction(mfunc.get()); + // 阶段 4: 窥孔优化 (Peephole Optimization) + PeepholeOptimizer peephole; + peephole.runOnMachineFunction(mfunc.get()); - // 阶段 5: 局部指令调度 (Local Scheduling) - PostRA_Scheduler local_scheduler; - local_scheduler.runOnMachineFunction(mfunc.get()); - } + // // 阶段 5: 局部指令调度 (Local Scheduling) + // PostRA_Scheduler local_scheduler; + // local_scheduler.runOnMachineFunction(mfunc.get()); // 阶段 3.2: 插入序言和尾声 PrologueEpilogueInsertionPass pei_pass;