From 18642a9c2335ff86111054b80cabab3d8a29201a Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Wed, 26 Aug 2026 20:35:14 +0800 Subject: [PATCH] =?UTF-8?q?debug(llvmlite):=20function=5Fset=5Fattrs=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B0=83=E7=94=A8=E8=AF=8A=E6=96=AD=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E8=AE=A4=E5=B1=9E=E6=80=A7=E6=98=AF=E5=90=A6=E5=86=99?= =?UTF-8?q?=E5=85=A5=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- llvmlite/__function.vp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/llvmlite/__function.vp b/llvmlite/__function.vp index 18a963b..048356f 100644 --- a/llvmlite/__function.vp +++ b/llvmlite/__function.vp @@ -301,6 +301,11 @@ def block_insert_text_before_terminator(pool: memhub.MemBuddy | t.CPtr, def function_set_attrs(func: Function | t.CPtr, attrs: t.CChar | t.CPtr): """设置函数级属性文本(如 "nounwind"/"noinline")""" if func is None: return + # [ATTR-DIAG] 确认 function_set_attrs 是否被调用及传入内容 + if attrs is not None: + stdio.printf("[ATTR] function_set_attrs: func='%s' attrs='%s'\n", func.Name, attrs) + else: + stdio.printf("[ATTR] function_set_attrs: func='%s' attrs=None\n", func.Name) func.Attrs = attrs @@ -785,10 +790,25 @@ def FunctionPrint(buf: t.CChar | t.CPtr, size: t.CSizeT, # ============================================================ # 内部辅助 # ============================================================ +# _append_cstr 写入位置缓存(性能优化:同一缓冲连续追加时 O(1) 定位, +# 避免每次 strlen(dst) 从头扫描导致的 O(n²) —— IR 序列化热点)。 +# 缓冲切换(dst 不同)或缓冲被重置(dst[0]=0 且 pos>0)时自动回退 strlen。 +_g_last_dst: t.CUInt64T = 0 +_g_last_pos: t.CSizeT = 0 + def _append_cstr(dst: t.CChar | t.CPtr, dst_size: t.CSizeT, src: t.CChar | t.CPtr): """将 C 字符串 src 追加到 dst 末尾""" + global _g_last_dst, _g_last_pos if dst is None or src is None: return - dlen: t.CSizeT = string.strlen(dst) + dlen: t.CSizeT = 0 + if _g_last_dst == t.CUInt64T(dst): + dlen = _g_last_pos + # 缓冲被重置(调用方 buf[0]=0 后复用同一地址)→ 回到头部 + if dlen > 0 and dst[0] == '\0': + dlen = 0 + else: + dlen = string.strlen(dst) + _g_last_dst = t.CUInt64T(dst) # 无符号下溢防护:dlen 超过缓冲大小时(缓冲已被破坏/悬空)直接返回, # 否则 remain = dst_size - dlen 下溢成巨值会把 dst[dlen+i] 写穿到缓冲外 if dlen >= dst_size: @@ -801,6 +821,7 @@ def _append_cstr(dst: t.CChar | t.CPtr, dst_size: t.CSizeT, src: t.CChar | t.CPt dst[dlen + i] = src[i] i += 1 dst[dlen + i] = '\0' + _g_last_pos = dlen + i # ============================================================