debug(llvmlite): function_set_attrs 添加调用诊断,确认属性是否写入函数

This commit is contained in:
TermiNexus committed 2026-08-26 20:35:14 +08:00
1 parent 4ce6095359
commit 18642a9c23
1 file changed
+22 -1
+22 -1
View File
@@ -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
# ============================================================