From 5844e6e725acfbe64a9dae8038e32a316267d9bb Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 15 Aug 2026 07:36:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(TPC):=20range=20for=20=E5=BE=AA=E7=8E=AF=20?= =?UTF-8?q?body=20=E4=BB=A5=20break=20=E7=BB=88=E7=BB=93=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E5=8F=91=E5=B0=84=E6=AD=BB=E4=BB=A3=E7=A0=81=20load(?= =?UTF-8?q?=E6=B8=B8=E7=A6=BB=E6=8C=87=E4=BB=A4=E8=87=B4=20opt=20expected?= =?UTF-8?q?=20instruction=20opcode)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: HandlesFor.py L229-233 在 body 使 _reg_values[循环变量] 失效后, 于当前 builder 位置重新 load 循环变量(该值实际未被使用, 死代码)。 当 body 最后一条语句是 break 时当前块已带 terminator, load 落在 br for.end 之后 → 非法 IR → opt/llc 报 'expected instruction opcode'。 触发场景: 装饰器扫描循环 body 内 list[ASTptr].get 调用触发泛型特化 重入编译(_EmitFunctionLlvm L1647 整体清空 _reg_values 且不恢复)。 修复: else 分支加 is_terminated 守卫, 终结时跳过死 load(语义不变)。 --- lib/core/Handles/HandlesFor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/core/Handles/HandlesFor.py b/lib/core/Handles/HandlesFor.py index 9bbcc72..1b9aae4 100644 --- a/lib/core/Handles/HandlesFor.py +++ b/lib/core/Handles/HandlesFor.py @@ -229,7 +229,12 @@ class ForHandle(BaseHandle): StepLoopVal: ir.Value = CachedLoopVal if TargetName in Gen._reg_values and Gen._reg_values[TargetName] is CachedLoopVal: del Gen._reg_values[TargetName] - else: + elif not Gen.builder.block.is_terminated: + # 死代码防御(2026-08-15):body 修改过循环变量时此处重新 load, + # 但该值实际未被使用(step 块 L237 会重新 load)。 + # 若 body 以 break/continue/return 终结,当前块已带 terminator, + # 再发射 load 会落在 terminator 之后 → opt 报 + # "expected instruction opcode" 非法 IR。 StepLoopVal = Gen._load(LoopVar, name=TargetName) if not Gen.builder.block.is_terminated: Gen.builder.branch(StepBB)