Revert "fix(includes): llvmlite/mlirlite/torch 的分配器形参放宽到基类 MemManager"

This reverts commit 9b1399b627.
This commit is contained in:
TermiNexus committed 2026-09-29 15:48:06 +08:00
1 parent 9b1399b627
commit 31b0f79ed2
15 files changed
+272 -272

No files matched your search

+45 -45
View File
@@ -29,7 +29,7 @@ from .__function import Function, BasicBlock, block_append_text, block_set_termi
# (方法以实例方法实现,无虚表;文件末尾的同名顶层函数为一行委托)。
# ============================================================
novirtual class IRBuilder:
Pool: memhub.MemManager* # 内存池
Pool: memhub.MemBuddy* # 内存池
Func: rawptr[Function] # 所属函数
CurBlock: rawptr[BasicBlock] # 当前插入点
Counter: int # SSA 名计数器
@@ -40,7 +40,7 @@ novirtual class IRBuilder:
# ============================================================
def _alloc_ssa_name(self) -> str:
"""分配一个 SSA 名字符串 "%N" 并递增计数器"""
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
buf: str = pool.alloc(16)
if buf is None: return None
viperlib.snprintf(buf, 16, "%%%d", self.Counter)
@@ -54,7 +54,7 @@ novirtual class IRBuilder:
注意:复杂结构体类型(如 13 方法的虚表 {i8*,i8*,...})可能超过 64 字节,
必须使用足够大的缓冲区,否则 '}' 会被截断导致 IR 语法错误。
"""
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
buf: str = pool.alloc(512)
if buf is None: return None
buf[0] = '\0'
@@ -113,7 +113,7 @@ novirtual class IRBuilder:
lhs: rawptr[Value], rhs: rawptr[Value]) -> rawptr[Value]:
"""二元运算通用实现"""
if self is None or lhs is None or rhs is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(lhs.Ty)
line: str = pool.alloc(256)
@@ -161,7 +161,7 @@ novirtual class IRBuilder:
因此本函数不设默认参数;需要显式对齐请用 build_alloca_aligned。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
line: str = pool.alloc(128)
@@ -186,7 +186,7 @@ novirtual class IRBuilder:
align 不设默认值(TPC 并行编译时 include 默认参数不预注册),调用方必须显式传入。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
line: str = pool.alloc(128)
@@ -210,7 +210,7 @@ novirtual class IRBuilder:
导致的支配性违规(Instruction does not dominate all uses)。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
func: rawptr[Function] = self.Func
if func is None or func.Blocks is None:
return self.build_alloca(ty)
@@ -248,7 +248,7 @@ novirtual class IRBuilder:
优化器提升/合并重复访问的场景使用。
"""
if self is None or ptr is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -270,7 +270,7 @@ novirtual class IRBuilder:
限定符(`store volatile i32 %v, i32* %p`)。
"""
if self is None or val is None or ptr is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
val_ty_s: str = self._type_str(val.Ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
line: str = pool.alloc(256)
@@ -399,7 +399,7 @@ novirtual class IRBuilder:
def build_fneg(self, val: rawptr[Value]) -> rawptr[Value]:
"""%N = fneg <ty> <val>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(val.Ty)
line: str = pool.alloc(128)
@@ -419,7 +419,7 @@ novirtual class IRBuilder:
返回类型为 i1 的 SSA 值
"""
if self is None or lhs is None or rhs is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(lhs.Ty)
pred_s: str = _icmp_pred_str(pred)
@@ -443,7 +443,7 @@ novirtual class IRBuilder:
返回类型为 i1 的 SSA 值
"""
if self is None or lhs is None or rhs is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(lhs.Ty)
pred_s: str = _fcmp_pred_str(pred)
@@ -466,7 +466,7 @@ novirtual class IRBuilder:
def build_br(self, target: rawptr[BasicBlock]):
"""br label %<target>"""
if self is None or target is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(64)
if line is None: return
viperlib.snprintf(line, 64, "br label %%%s", target.Name)
@@ -478,7 +478,7 @@ novirtual class IRBuilder:
then_blk: rawptr[BasicBlock], else_blk: rawptr[BasicBlock]):
"""br i1 <cond>, label %<then>, label %<else_>"""
if self is None or cond is None or then_blk is None or else_blk is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(128)
if line is None: return
viperlib.snprintf(line, 128, "br i1 %s, label %%%s, label %%%s",
@@ -490,7 +490,7 @@ novirtual class IRBuilder:
def build_ret(self, val: rawptr[Value]):
"""ret <ty> <val>"""
if self is None or val is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
ty_s: str = self._type_str(val.Ty)
line: str = pool.alloc(128)
if line is None: return
@@ -502,7 +502,7 @@ novirtual class IRBuilder:
def build_ret_void(self):
"""ret void"""
if self is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(16)
if line is None: return
string.strcpy(line, "ret void")
@@ -524,7 +524,7 @@ novirtual class IRBuilder:
is_variadic=1 时发射函数类型签名(如 i32 (i8*, ...) @printf)。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
# 判断返回类型是否为 void
is_void: int = 0
@@ -609,7 +609,7 @@ novirtual class IRBuilder:
间接调用:callee_val 是函数指针 SSA 值。
"""
if self is None or callee_val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
is_void: int = 0
match ret_ty:
@@ -662,7 +662,7 @@ novirtual class IRBuilder:
"""%N = bitcast <from_ty> <val> to <to_ty>"""
if self is None or val is None:
return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -680,7 +680,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = sext <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -695,7 +695,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = trunc <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -710,7 +710,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = zext <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -725,7 +725,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = ptrtoint <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -740,7 +740,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = inttoptr <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -758,7 +758,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fpext <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -773,7 +773,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fptrunc <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -788,7 +788,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fptosi <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -803,7 +803,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = sitofp <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -818,7 +818,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fptoui <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -833,7 +833,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = uitofp <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -857,7 +857,7 @@ novirtual class IRBuilder:
# 直接 GEP 会导致 llc 编译失败
if _is_ptr_type(ptr.Ty) == 0:
return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
elem_ty_s: str = self._type_str(elem_ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -887,7 +887,7 @@ novirtual class IRBuilder:
结果类型是 elem_ty*
"""
if self is None or ptr is None or idx is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
array_ty_s: str = self._type_str(array_ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -917,7 +917,7 @@ novirtual class IRBuilder:
结果类型是 field_ty*
"""
if self is None or ptr is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
struct_ty_s: str = self._type_str(struct_ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -952,7 +952,7 @@ novirtual class IRBuilder:
incoming_head 是 PhiIncoming 链表头,count 是入边数量。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
@@ -998,7 +998,7 @@ novirtual class IRBuilder:
cases_head: rawptr[SwitchCase], case_count: int):
"""switch <ty> <val>, label %<default> [<case_val>, label %<blk> ...]"""
if self is None or val is None or default_blk is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
ty_s: str = self._type_str(val.Ty)
# 构建主体: switch <ty> <val>, label %default
@@ -1039,7 +1039,7 @@ novirtual class IRBuilder:
true_val: rawptr[Value], false_val: rawptr[Value]) -> rawptr[Value]:
"""%N = select i1 <cond>, <ty> <true_val>, <ty> <false_val>"""
if self is None or cond is None or true_val is None or false_val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(true_val.Ty)
line: str = pool.alloc(256)
@@ -1056,7 +1056,7 @@ novirtual class IRBuilder:
def build_unreachable(self):
"""unreachable"""
if self is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(16)
if line is None: return
string.strcpy(line, "unreachable")
@@ -1072,7 +1072,7 @@ novirtual class IRBuilder:
order: int) -> rawptr[Value]:
"""%N = atomicrmw <op> <ptr_ty> <ptr>, <val_ty> <val> <ordering>"""
if self is None or ptr is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ptr_ty_s: str = self._type_str(ptr.Ty)
val_ty_s: str = self._type_str(val.Ty)
@@ -1094,7 +1094,7 @@ novirtual class IRBuilder:
返回 {<ty>, i1} 类型的 SSA 值(简化为返回 i64 表示的值对)
"""
if self is None or ptr is None or cmp_val is None or new_val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ptr_ty_s: str = self._type_str(ptr.Ty)
cmp_ty_s: str = self._type_str(cmp_val.Ty)
@@ -1120,7 +1120,7 @@ novirtual class IRBuilder:
"""%N = va_arg <va_list_ty> <va_list_ptr>, <arg_ty>"""
if self is None or va_list_ptr is None or arg_ty is None:
return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
va_list_ty_s: str = self._type_str(va_list_ptr.Ty)
arg_ty_s: str = self._type_str(arg_ty)
@@ -1145,7 +1145,7 @@ novirtual class IRBuilder:
简化的内联汇编调用,无参数版本。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
# 判断返回类型是否为 void
is_void: int = 0
@@ -1190,7 +1190,7 @@ novirtual class IRBuilder:
# IRBuilder 硬编码大小: Pool(8) + Func(8) + CurBlock(8) + Counter(4) + pad(4) + DbgLoc(8) = 40 字节
BUILDER_SIZE: t.CDefine = 40
def new_builder(pool: memhub.MemManager*, func: rawptr[Function]) -> rawptr[IRBuilder]:
def new_builder(pool: memhub.MemBuddy*, func: rawptr[Function]) -> rawptr[IRBuilder]:
"""创建 IRBuilder,关联到函数 func"""
ptr: rawptr[IRBuilder] = pool.alloc(BUILDER_SIZE)
if ptr is None: return None
@@ -1244,7 +1244,7 @@ def _emit(builder: rawptr[IRBuilder], text: str):
# whole text (after the final ']'), never on an inner line; a trailing
# newline has the suffix inserted before it.
# ============================================================
def _append_dbg_suffix(pool: memhub.MemManager*, text: str, loc: str) -> str:
def _append_dbg_suffix(pool: memhub.MemBuddy*, text: str, loc: str) -> str:
"""Return text + " , !dbg !N" (suffix after the last line for multi-line text)"""
if text is None or loc is None:
return text
@@ -1741,7 +1741,7 @@ novirtual class PhiIncoming(GSListNode[PhiIncoming]):
# PhiIncoming 硬编码大小: Next(8) + Val(8) + Block(8) = 24 字节
PHIINC_SIZE: t.CDefine = 24
def new_phi_incoming(pool: memhub.MemManager*,
def new_phi_incoming(pool: memhub.MemBuddy*,
val: rawptr[Value], block: rawptr[BasicBlock]) -> rawptr[PhiIncoming]:
ptr: rawptr[PhiIncoming] = pool.alloc(PHIINC_SIZE)
if ptr is None: return None
@@ -1776,7 +1776,7 @@ novirtual class SwitchCase(GSListNode[SwitchCase]):
# SwitchCase 硬编码大小: Next(8) + CaseVal(8) + Block(8) = 24 字节
SWCASE_SIZE: t.CDefine = 24
def new_switch_case(pool: memhub.MemManager*,
def new_switch_case(pool: memhub.MemBuddy*,
case_val: rawptr[Value], block: rawptr[BasicBlock]) -> rawptr[SwitchCase]:
ptr: rawptr[SwitchCase] = pool.alloc(SWCASE_SIZE)
if ptr is None: return None
+15 -15
View File
@@ -94,7 +94,7 @@ novirtual class Function(GSListNode[Function]):
# Line 硬编码大小: Next(8) + Buf(8) + Len(8) = 24 字节
LINE_SIZE: t.CDefine = 24
def new_line(pool: memhub.MemManager*, text: str) -> rawptr[Line]:
def new_line(pool: memhub.MemBuddy*, text: str) -> rawptr[Line]:
"""创建一个指令行,复制 text 到新分配的缓冲区"""
ptr: rawptr[Line] = pool.alloc(LINE_SIZE)
if ptr is None: return None
@@ -109,7 +109,7 @@ def new_line(pool: memhub.MemManager*, text: str) -> rawptr[Line]:
return ptr
def _new_gslist(pool: memhub.MemManager*, list_size: sizet) -> t.CPtr:
def _new_gslist(pool: memhub.MemBuddy*, list_size: sizet) -> t.CPtr:
"""从 pool 分配并零初始化一个 GSList 容器(Head/Tail/Count 全零)"""
gsl: t.CPtr = pool.alloc(list_size)
if gsl is None: return None
@@ -120,7 +120,7 @@ def _new_gslist(pool: memhub.MemManager*, list_size: sizet) -> t.CPtr:
# BasicBlock 硬编码大小: Next(8) + Name(8) + Lines(8) + IsTerminated(4) = 32 字节
BLOCK_SIZE: t.CDefine = 32
def new_basic_block(pool: memhub.MemManager*, name: str) -> rawptr[BasicBlock]:
def new_basic_block(pool: memhub.MemBuddy*, name: str) -> rawptr[BasicBlock]:
"""创建一个基本块"""
ptr: rawptr[BasicBlock] = pool.alloc(BLOCK_SIZE)
if ptr is None: return None
@@ -134,7 +134,7 @@ def new_basic_block(pool: memhub.MemManager*, name: str) -> rawptr[BasicBlock]:
# Param 硬编码大小: Next(8) + Ty(8) + Name(8) + Attrs(8) = 32 字节
PARAM_SIZE: t.CDefine = 32
def new_param(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def new_param(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
name: str) -> rawptr[Param]:
"""创建一个函数参数"""
ptr: rawptr[Param] = pool.alloc(PARAM_SIZE)
@@ -149,7 +149,7 @@ def new_param(pool: memhub.MemManager*, ty: rawptr[LLVMType],
# Function 硬编码大小: Next(8) + Name(8) + RetTy(8) + Params(8) + Blocks(8) + IsDeclared(4) + IsVarArg(4) + Attrs(8) + AttrGroup(8) + AttrGroupId(8) + NameLen(8) + DbgRef(8) = 88 字节
FUNC_SIZE: t.CDefine = 88
def new_function(pool: memhub.MemManager*, name: str,
def new_function(pool: memhub.MemBuddy*, name: str,
ret_ty: rawptr[LLVMType]) -> rawptr[Function]:
"""创建一个函数"""
ptr: rawptr[Function] = pool.alloc(FUNC_SIZE)
@@ -260,7 +260,7 @@ def block_get_terminated(block: rawptr[BasicBlock]) -> int:
return block.IsTerminated
def block_append_text(pool: memhub.MemManager*, block: rawptr[BasicBlock],
def block_append_text(pool: memhub.MemBuddy*, block: rawptr[BasicBlock],
text: str):
"""创建指令行并追加到基本块"""
if block is None or text is None: return
@@ -269,7 +269,7 @@ def block_append_text(pool: memhub.MemManager*, block: rawptr[BasicBlock],
block_append_line(block, line)
def block_insert_text_before_terminator(pool: memhub.MemManager*,
def block_insert_text_before_terminator(pool: memhub.MemBuddy*,
block: rawptr[BasicBlock],
text: str):
"""创建指令行并插入到基本块终止指令之前
@@ -527,7 +527,7 @@ def _assign_new_numbers(text: str,
def _replace_ssa_numbers(text: str,
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
map_arr: str,
max_n: int) -> str:
"""构建新文本,将 %N 替换为映射后的新编号
@@ -587,7 +587,7 @@ def _replace_ssa_numbers(text: str,
return new_buf
def RenumberSSA(func: rawptr[Function], pool: memhub.MemManager*):
def RenumberSSA(func: rawptr[Function], pool: memhub.MemBuddy*):
"""重编号函数内所有数字 SSA 名,确保按文本输出顺序单调递增
在 FunctionPrint 序列化函数体之前调用。
@@ -699,7 +699,7 @@ def _peep_is_startwith(s: str, pfx: str) -> int:
return 1 if string.strncmp(s, pfx, pl) == 0 else 0
def _peep_replace_token(pool: memhub.MemManager*,
def _peep_replace_token(pool: memhub.MemBuddy*,
line_buf: str, tok: str, repl: str,
tlen: sizet, rlen: sizet) -> str:
"""将 line_buf 中独立记号 %<tok> 替换为 repl,返回新缓冲(无变化返回原指针)
@@ -791,7 +791,7 @@ def _peep_last_ref_eq(sb: str, a_name: str, nl: sizet) -> int:
return 0
def _peep_alloc_name(eb: str, pool: memhub.MemManager*) -> str:
def _peep_alloc_name(eb: str, pool: memhub.MemBuddy*) -> str:
"""从 '%name = alloca ...' 提取 name(不含 %),失败返回 None"""
if eb is None or eb[0] != '%':
return None
@@ -835,7 +835,7 @@ def _peep_is_alloc_of(eb: str, a_name: str, nl: sizet) -> int:
return 0
def _peephole_opt(func: rawptr[Function], pool: memhub.MemManager*):
def _peephole_opt(func: rawptr[Function], pool: memhub.MemBuddy*):
"""文本层本地窥孔:T1 ret zeroinitializer;T2 入口槽 store→load 转发"""
if func is None or func.Blocks is None or pool is None:
return
@@ -1040,7 +1040,7 @@ def _peephole_opt(func: rawptr[Function], pool: memhub.MemManager*):
_peep_noop_bitcast(func, pool)
def _peep_noop_bitcast(func: rawptr[Function], pool: memhub.MemManager*):
def _peep_noop_bitcast(func: rawptr[Function], pool: memhub.MemBuddy*):
"""T3: 消除 no-op bitcast(bitcast <T> %x to <T>,两侧类型相同)。
生成端(虚调用/联合位宽转换)会产生同类型恒等 bitcast(如
@@ -1128,7 +1128,7 @@ def _peep_noop_bitcast(func: rawptr[Function], pool: memhub.MemManager*):
# }
# ============================================================
def FunctionPrint(buf: str, size: sizet,
func: rawptr[Function], pool: memhub.MemManager*):
func: rawptr[Function], pool: memhub.MemBuddy*):
"""将函数 func 序列化为 IR 文本写入 buf"""
if func is None or buf is None or size == 0: return
@@ -1286,7 +1286,7 @@ def FunctionPrint(buf: str, size: sizet,
def _append_attr_group_def(buf: str, size: sizet,
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
func: rawptr[Function]):
"""输出字符串属性组定义: attributes #N = { "section"="..." }"""
if func is None or func.AttrGroup is None or func.AttrGroupId <= 0:
+12 -12
View File
@@ -83,7 +83,7 @@ from .__verify import (
# ============================================================
# 便捷工厂: 字符串复制
# ============================================================
def _str_dup(pool: memhub.MemManager*, src: str) -> str:
def _str_dup(pool: memhub.MemBuddy*, src: str) -> str:
"""从 pool 分配并复制一个 C 字符串"""
if src is None: return None
slen: sizet = string.strlen(src)
@@ -96,7 +96,7 @@ def _str_dup(pool: memhub.MemManager*, src: str) -> str:
# ============================================================
# 便捷工厂: 创建带名称的基本块
# ============================================================
def create_block(pool: memhub.MemManager*, func: rawptr[Function],
def create_block(pool: memhub.MemBuddy*, func: rawptr[Function],
name: str) -> rawptr[BasicBlock]:
"""创建基本块并添加到函数"""
# 块名与函数参数值名冲突防护:LLVM 中块标签 "entry:" 即值 %entry,
@@ -138,7 +138,7 @@ def create_block(pool: memhub.MemManager*, func: rawptr[Function],
# ============================================================
# 便捷工厂: 创建整数常量值
# ============================================================
def const_int(pool: memhub.MemManager*, bits: int,
def const_int(pool: memhub.MemBuddy*, bits: int,
val: int64) -> rawptr[Value]:
"""创建整数常量值(自动创建类型 + 名字文本)
@@ -167,12 +167,12 @@ def const_int(pool: memhub.MemManager*, bits: int,
return ConstInt(pool, ty, val, name)
def const_int32(pool: memhub.MemManager*, val: int64) -> rawptr[Value]:
def const_int32(pool: memhub.MemBuddy*, val: int64) -> rawptr[Value]:
"""便捷: 创建 i32 整数常量"""
return const_int(pool, 32, val)
def const_int64(pool: memhub.MemManager*, val: int64) -> rawptr[Value]:
def const_int64(pool: memhub.MemBuddy*, val: int64) -> rawptr[Value]:
"""便捷: 创建 i64 整数常量"""
return const_int(pool, 64, val)
@@ -180,7 +180,7 @@ def const_int64(pool: memhub.MemManager*, val: int64) -> rawptr[Value]:
# ============================================================
# 便捷工厂: 创建函数并添加到模块
# ============================================================
def create_function(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_function(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, ret_ty: rawptr[LLVMType]) -> rawptr[Function]:
"""创建函数并添加到模块"""
name_dup: str = _str_dup(pool, name)
@@ -190,7 +190,7 @@ def create_function(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
return func
def add_param(pool: memhub.MemManager*, func: rawptr[Function],
def add_param(pool: memhub.MemBuddy*, func: rawptr[Function],
ty: rawptr[LLVMType], name: str) -> rawptr[Param]:
"""创建参数并添加到函数"""
name_dup: str = _str_dup(pool, name)
@@ -203,7 +203,7 @@ def add_param(pool: memhub.MemManager*, func: rawptr[Function],
# ============================================================
# 便捷工厂: 创建函数声明(declare)
# ============================================================
def create_declare(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_declare(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, ret_ty: rawptr[LLVMType]) -> rawptr[Function]:
"""创建函数声明(declare,无函数体)并添加到模块
@@ -224,7 +224,7 @@ def create_declare(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
# 生成 LLVM IR:
# @.str = private unnamed_addr constant [N x i8] c"contents\00"
# ============================================================
def create_global_string(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_global_string(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, text: str) -> rawptr[GlobalVariable]:
"""创建字符串常量全局变量并添加到模块
@@ -312,7 +312,7 @@ def create_global_string(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
# ============================================================
# 便捷工厂: 创建带初始化值的整数全局变量
# ============================================================
def create_global_int(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_global_int(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, bits: int, val: int64) -> rawptr[GlobalVariable]:
"""创建整数全局变量并添加到模块
@@ -351,9 +351,9 @@ def create_global_int(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
@t.NoVTable
class LLVMModuleCore:
"""模块/函数/值创建核心,封装 pool 避免每次创建都传 pool"""
Pool: memhub.MemManager*
Pool: memhub.MemBuddy*
def __init__(self, pool: memhub.MemManager*):
def __init__(self, pool: memhub.MemBuddy*):
self.Pool = pool
# ---- 模块操作 ----
+16 -16
View File
@@ -131,7 +131,7 @@ class LLVMModule:
self.DataLayout = layout
# ---- DI debug metadata ----
def add_di_node(self, pool: memhub.MemManager*, text: str) -> int:
def add_di_node(self, pool: memhub.MemBuddy*, text: str) -> int:
"""Append a DI metadata node; return its !N id (0-based, unique), -1 on failure.
text is the node body incl. !M references built from earlier ids.
@@ -158,7 +158,7 @@ class LLVMModule:
self.DIMetaCount += 1
return node_id
def add_named_metadata(self, pool: memhub.MemManager*, name: str, refs: str):
def add_named_metadata(self, pool: memhub.MemBuddy*, name: str, refs: str):
"""Register named metadata "!<name> = !{ <refs> }"; merge into an existing
entry with the same name (refs is the brace body, e.g. "!3")."""
if self is None or pool is None or name is None:
@@ -209,7 +209,7 @@ class LLVMModule:
self.DINamedCount += 1
# ---- 命名结构体类型管理 ----
def add_named_type(self, pool: memhub.MemManager*, ty: rawptr[LLVMType]):
def add_named_type(self, pool: memhub.MemBuddy*, ty: rawptr[LLVMType]):
"""将命名结构体类型添加到模块(用于输出 %"name" = type { ... } 定义行)
非命名结构体(Name 为 None 或非 Struct 类型)会被忽略。
@@ -293,7 +293,7 @@ class LLVMModule:
cur = cur.Next
return 0
def ensure_opaque_for_type(self, pool: memhub.MemManager*,
def ensure_opaque_for_type(self, pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]):
"""为类型中的跨模块结构体引用添加 opaque 声明
@@ -304,7 +304,7 @@ class LLVMModule:
# ---- 打印 ----
def PrintModule(self, buf: str, size: sizet,
pool: memhub.MemManager*, mode: int):
pool: memhub.MemBuddy*, mode: int):
"""将模块序列化为 .ll 文本写入 buf"""
if self is None or buf is None or size == 0: return
@@ -514,7 +514,7 @@ class LLVMModule:
# ============================================================
# 工厂函数
# ============================================================
def new_module(pool: memhub.MemManager*, name: str) -> rawptr[LLVMModule]:
def new_module(pool: memhub.MemBuddy*, name: str) -> rawptr[LLVMModule]:
"""创建一个模块"""
# 硬编码 144 字节:18 字段 * 8 字节(无 vtable,@t.NoVTable)
ptr: rawptr[LLVMModule] = pool.alloc(144)
@@ -553,7 +553,7 @@ DI_NODE_SIZE: t.CDefine = 16
# DINamedMeta hardcoded size: Next(8) + Name(8) + Refs(8) = 24 bytes
DI_NAMED_SIZE: t.CDefine = 24
def new_global_variable(pool: memhub.MemManager*,
def new_global_variable(pool: memhub.MemBuddy*,
name: str, ty: rawptr[LLVMType]) -> rawptr[GlobalVariable]:
"""创建一个全局变量"""
ptr: rawptr[GlobalVariable] = pool.alloc(GV_SIZE)
@@ -599,13 +599,13 @@ def module_set_datalayout(mod: rawptr[LLVMModule], layout: str):
# ============================================================
# DI debug metadata
# ============================================================
def module_add_di_node(mod: rawptr[LLVMModule], pool: memhub.MemManager*,
def module_add_di_node(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
text: str) -> int:
"""Append a DI metadata node; return its !N id (-1 on failure)"""
return mod.add_di_node(pool, text)
def module_add_named_metadata(mod: rawptr[LLVMModule], pool: memhub.MemManager*,
def module_add_named_metadata(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
name: str, refs: str):
"""Register named metadata "!<name> = !{ <refs> }" """
mod.add_named_metadata(pool, name, refs)
@@ -649,12 +649,12 @@ def _is_opaque_struct(ty: rawptr[LLVMType]) -> int:
return 0
def module_add_named_type(mod: rawptr[LLVMModule], pool: memhub.MemManager*,
def module_add_named_type(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]):
mod.add_named_type(pool, ty)
def module_set_named_type_packed(mod: rawptr[LLVMModule], pool: memhub.MemManager*,
def module_set_named_type_packed(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
ty: rawptr[LLVMType], is_packed: int):
"""按名称设置命名结构体的 packed 标记(输出 %"name" = type <{...}>)。
@@ -692,7 +692,7 @@ def _is_type_defined_in_module(mod: rawptr[LLVMModule], name: str) -> int:
return mod._is_type_defined_in_module(name)
def _scan_type_for_cross_module_refs(pool: memhub.MemManager*,
def _scan_type_for_cross_module_refs(pool: memhub.MemBuddy*,
mod: rawptr[LLVMModule],
ty: rawptr[LLVMType],
depth: int = 0):
@@ -757,7 +757,7 @@ def _scan_type_for_cross_module_refs(pool: memhub.MemManager*,
def module_ensure_opaque_for_type(mod: rawptr[LLVMModule],
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]):
mod.ensure_opaque_for_type(pool, ty)
@@ -810,7 +810,7 @@ IR_DUMP_CHAIN_LIMIT: t.CDefine = 100000
# mode: OUTPUT_FULL=完整, OUTPUT_STUB=仅声明, OUTPUT_TEXT=仅代码
# ============================================================
def LLVMModulePrint(buf: str, size: sizet,
mod: rawptr[LLVMModule], pool: memhub.MemManager*,
mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
mode: int):
mod.PrintModule(buf, size, pool, mode)
@@ -819,7 +819,7 @@ def LLVMModulePrint(buf: str, size: sizet,
# 全局变量打印
# ============================================================
def _print_global(buf: str, size: sizet,
gv: rawptr[GlobalVariable], pool: memhub.MemManager*):
gv: rawptr[GlobalVariable], pool: memhub.MemBuddy*):
"""打印单个全局变量: @<name> = [linkage] [unnamed_addr] [constant] <ty> <init>"""
if gv is None: return
@@ -883,7 +883,7 @@ def _print_global(buf: str, size: sizet,
# _print_global_stub - 打印全局变量的声明形式(external global,无初值)
# ============================================================
def _print_global_stub(buf: str, size: sizet,
gv: rawptr[GlobalVariable], pool: memhub.MemManager*):
gv: rawptr[GlobalVariable], pool: memhub.MemBuddy*):
"""打印全局变量声明: @<name> = external global <ty>"""
if gv is None: return
+27 -27
View File
@@ -87,7 +87,7 @@ def _TypeSizeGuard() -> sizet:
return sz
def _new_type(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def _new_type(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""从 mpool 分配一个零初始化的 LLVMType"""
sz_nt: sizet = _TypeSizeGuard()
ptr: rawptr[LLVMType] = pool.alloc(sz_nt)
@@ -98,42 +98,42 @@ def _new_type(pool: memhub.MemManager*) -> rawptr[LLVMType]:
# ============================================================
# 类型工厂函数
# ============================================================
def Int1(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int1(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(1))
return ptr
def Int8(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int8(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(8))
return ptr
def Int16(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int16(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(16))
return ptr
def Int32(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int32(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(32))
return ptr
def Int64(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int64(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(64))
return ptr
def Ptr(pool: memhub.MemManager*, pointee: rawptr[LLVMType]) -> rawptr[LLVMType]:
def Ptr(pool: memhub.MemBuddy*, pointee: rawptr[LLVMType]) -> rawptr[LLVMType]:
"""创建指针类型 Ptr(pointee)
LLVM IR 中 **不允许 void* 指针**("pointers to void are invalid"),
@@ -158,7 +158,7 @@ def Ptr(pool: memhub.MemManager*, pointee: rawptr[LLVMType]) -> rawptr[LLVMType]
return ptr
def Func(pool: memhub.MemManager*, ret: rawptr[LLVMType],
def Func(pool: memhub.MemBuddy*, ret: rawptr[LLVMType],
params: rawptr[ParamNode], count: int) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -166,14 +166,14 @@ def Func(pool: memhub.MemManager*, ret: rawptr[LLVMType],
return ptr
def Void(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Void(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Void())
return ptr
def Array(pool: memhub.MemManager*, elem_ty: rawptr[LLVMType],
def Array(pool: memhub.MemBuddy*, elem_ty: rawptr[LLVMType],
count: int) -> rawptr[LLVMType]:
"""创建数组类型 [count x elem_ty]"""
ptr: rawptr[LLVMType] = _new_type(pool)
@@ -182,7 +182,7 @@ def Array(pool: memhub.MemManager*, elem_ty: rawptr[LLVMType],
return ptr
def Struct(pool: memhub.MemManager*, fields: rawptr[ParamNode],
def Struct(pool: memhub.MemBuddy*, fields: rawptr[ParamNode],
fcount: int, name: str) -> rawptr[LLVMType]:
"""创建结构体类型 {ty1, ty2, ...}
@@ -196,7 +196,7 @@ def Struct(pool: memhub.MemManager*, fields: rawptr[ParamNode],
return ptr
def Half(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Half(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建半精度浮点类型 half (16-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -204,7 +204,7 @@ def Half(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def Float(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Float(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建单精度浮点类型 float (32-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -212,7 +212,7 @@ def Float(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def Double(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Double(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建双精度浮点类型 double (64-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -220,7 +220,7 @@ def Double(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def FP128(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def FP128(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建四精度浮点类型 fp128 (128-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -228,7 +228,7 @@ def FP128(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def Label(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Label(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建标签类型 label"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -242,7 +242,7 @@ def Label(pool: memhub.MemManager*) -> rawptr[LLVMType]:
# ParamNode 硬编码大小: Next(8) + Ty(8) = 16 字节
PNODE_SIZE: t.CDefine = 16
def new_param_node(pool: memhub.MemManager*, ty: rawptr[LLVMType]) -> rawptr[ParamNode]:
def new_param_node(pool: memhub.MemBuddy*, ty: rawptr[LLVMType]) -> rawptr[ParamNode]:
"""创建一个参数节点,Ty 设为给定类型指针"""
ptr: rawptr[ParamNode] = pool.alloc(PNODE_SIZE)
if ptr is None: return None
@@ -311,9 +311,9 @@ def paramnode_set_next(node: rawptr[ParamNode], nxt: rawptr[ParamNode]):
@t.NoVTable
class LLVMTypeCore:
"""类型创建核心,封装 pool 避免每次创建类型都传 pool"""
Pool: memhub.MemManager*
Pool: memhub.MemBuddy*
def __init__(self, pool: memhub.MemManager*):
def __init__(self, pool: memhub.MemBuddy*):
self.Pool = pool
def Int1(self) -> rawptr[LLVMType]:
@@ -392,7 +392,7 @@ class LLVMTypeCore:
# pool 用于复杂类型(Array/Struct/Func)递归打印子类型时分配临时缓冲区。
# ============================================================
def TypePrint(buf: str, size: sizet,
ty: rawptr[LLVMType], pool: memhub.MemManager*):
ty: rawptr[LLVMType], pool: memhub.MemBuddy*):
"""将类型 ty 序列化为 IR 文本写入 buf(容量 size)
pool 用于递归打印子类型时分配临时缓冲区。
@@ -406,7 +406,7 @@ def TypePrint(buf: str, size: sizet,
TYPEPRINT_MAX_DEPTH: t.CDefine = 100
def _TypePrintDepth(buf: str, size: sizet,
ty: rawptr[LLVMType], pool: memhub.MemManager*,
ty: rawptr[LLVMType], pool: memhub.MemBuddy*,
depth: int):
"""带递归深度上限的 TypePrint 内部实现"""
if ty is None or buf is None or size == 0:
@@ -551,7 +551,7 @@ def _strappend(dst: str, dst_size: sizet, src: str):
# 供 TypePrint(匿名结构体)和 PrintStructDefinition(命名结构体定义行)复用
# ============================================================
def _print_struct_body(buf: str, size: sizet,
fields: rawptr[ParamNode], pool: memhub.MemManager*,
fields: rawptr[ParamNode], pool: memhub.MemBuddy*,
depth: int = 0, packed: int = 0):
"""打印 {ty1, ty2, ...} 到 buf(追加模式,不覆盖已有内容)
@@ -600,7 +600,7 @@ def _print_struct_body(buf: str, size: sizet,
# 供 LLVMModulePrint 在全局变量/函数之前输出类型定义
# ============================================================
def PrintStructDefinition(buf: str, size: sizet,
ty: rawptr[LLVMType], pool: memhub.MemManager*,
ty: rawptr[LLVMType], pool: memhub.MemBuddy*,
packed: int = 0):
"""打印命名结构体类型定义行 %"name" = type { ty1, ty2, ... }
@@ -681,13 +681,13 @@ def get_struct_fcount(ty: rawptr[LLVMType]) -> int:
# ============================================================
DEEPCOPY_MAX_DEPTH: t.CDefine = 64
def DeepCopyType(pool: memhub.MemManager*,
def DeepCopyType(pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]) -> rawptr[LLVMType]:
"""深拷贝 LLVMType 到 stdlib 区(深度限制防自引用递归),失败返回 None"""
return _DeepCopyDepth(pool, ty, 0)
def _DeepCopyDepth(pool: memhub.MemManager*,
def _DeepCopyDepth(pool: memhub.MemBuddy*,
ty: rawptr[LLVMType], depth: int) -> rawptr[LLVMType]:
"""带深度上限的深拷贝实现
@@ -750,7 +750,7 @@ def _DeepCopyDepth(pool: memhub.MemManager*,
return new_ty
def _DeepCopyParamChain(pool: memhub.MemManager*,
def _DeepCopyParamChain(pool: memhub.MemBuddy*,
head: rawptr[ParamNode], depth: int) -> rawptr[ParamNode]:
"""深拷贝 ParamNode 链表到 stdlib"""
if head is None:
@@ -778,7 +778,7 @@ def _DeepCopyParamChain(pool: memhub.MemManager*,
return first
def _DeepCopyStr(pool: memhub.MemManager*, s: str) -> str:
def _DeepCopyStr(pool: memhub.MemBuddy*, s: str) -> str:
"""深拷贝字符串到 stdlib"""
if s is None:
return None
+7 -7
View File
@@ -32,7 +32,7 @@ class Value(GSListNode[Value]):
# Value 硬编码大小: Next(8) + Ty(8) + Name(8) + IsConst(4) + IntVal(8) + FloatVal(8) = 48 字节
VALUE_SIZE: t.CDefine = 48
def new_value(pool: memhub.MemManager*) -> rawptr[Value]:
def new_value(pool: memhub.MemBuddy*) -> rawptr[Value]:
"""从 mpool 分配一个零初始化的 Value"""
ptr: rawptr[Value] = pool.alloc(VALUE_SIZE)
if ptr:
@@ -40,7 +40,7 @@ def new_value(pool: memhub.MemManager*) -> rawptr[Value]:
return ptr
def ConstInt(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def ConstInt(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
val: int64, name: str) -> rawptr[Value]:
"""创建整数常量值
@@ -58,7 +58,7 @@ def ConstInt(pool: memhub.MemManager*, ty: rawptr[LLVMType],
return v
def ConstNull(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def ConstNull(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
name: str) -> rawptr[Value]:
"""创建 null 指针常量"""
v: rawptr[Value] = new_value(pool)
@@ -70,7 +70,7 @@ def ConstNull(pool: memhub.MemManager*, ty: rawptr[LLVMType],
return v
def ConstZero(pool: memhub.MemManager*, ty: rawptr[LLVMType]) -> rawptr[Value]:
def ConstZero(pool: memhub.MemBuddy*, ty: rawptr[LLVMType]) -> rawptr[Value]:
"""创建 zeroinitializer 常量(零初始化聚合类型:结构体/数组)"""
v: rawptr[Value] = new_value(pool)
if v is None: return None
@@ -81,7 +81,7 @@ def ConstZero(pool: memhub.MemManager*, ty: rawptr[LLVMType]) -> rawptr[Value]:
return v
def ConstFloat(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def ConstFloat(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
val: t.CDouble) -> rawptr[Value]:
"""创建浮点常量值
@@ -118,7 +118,7 @@ def ConstFloat(pool: memhub.MemManager*, ty: rawptr[LLVMType],
return v
def SSAValue(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def SSAValue(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
name: str) -> rawptr[Value]:
"""创建 SSA 临时值
@@ -143,7 +143,7 @@ def SSAValue(pool: memhub.MemManager*, ty: rawptr[LLVMType],
# SSAValue(i32, "%0") -> "i32 %0"
# ============================================================
def ValuePrint(buf: str, size: sizet,
val: rawptr[Value], pool: memhub.MemManager*):
val: rawptr[Value], pool: memhub.MemBuddy*):
"""将值 val 序列化为 IR 文本写入 buf
格式: <type> <name>
+6 -6
View File
@@ -50,7 +50,7 @@ class VerifyResult:
# ============================================================
# 辅助函数
# ============================================================
def _new_verify_result(pool: memhub.MemManager*,
def _new_verify_result(pool: memhub.MemBuddy*,
buf_size: sizet) -> rawptr[VerifyResult]:
"""创建验证结果对象"""
ptr: rawptr[VerifyResult] = pool.alloc(VerifyResult.__sizeof__())
@@ -122,7 +122,7 @@ def _append_cstr(dst: str, dst_size: sizet, src: str):
# ============================================================
# SSA 名表操作
# ============================================================
def _new_name_table(pool: memhub.MemManager*) -> rawptr[GSList[NameEntry]]:
def _new_name_table(pool: memhub.MemBuddy*) -> rawptr[GSList[NameEntry]]:
"""创建空名表"""
gsl: rawptr[GSList[NameEntry]] = pool.alloc(24) # GSList: Head+Tail+Count
if gsl is None: return None
@@ -134,7 +134,7 @@ def _new_name_table(pool: memhub.MemManager*) -> rawptr[GSList[NameEntry]]:
NENTRY_SIZE: t.CDefine = 16
def _name_table_add(table: rawptr[GSList[NameEntry]],
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
name: str) -> int:
"""添加名到表。返回 1=重复, 0=成功添加"""
if table is None or name is None: return 0
@@ -177,7 +177,7 @@ def _is_char_name(ch: char) -> int:
return 0
def _extract_def_name(pool: memhub.MemManager*,
def _extract_def_name(pool: memhub.MemBuddy*,
text: str) -> str:
"""从定义行提取被定义的 SSA 名。
@@ -292,7 +292,7 @@ def _str_matches_at(text: str, start: int, length: int,
# ============================================================
# 主验证函数
# ============================================================
def verify_function(pool: memhub.MemManager*,
def verify_function(pool: memhub.MemBuddy*,
func: rawptr[Function],
result: rawptr[VerifyResult]) -> int:
"""验证函数的 SSA 正确性。
@@ -415,7 +415,7 @@ def verify_function(pool: memhub.MemManager*,
return result.ErrorCount
def verify_module(pool: memhub.MemManager*, mod: rawptr[LLVMModule], result: rawptr[VerifyResult]) -> int:
def verify_module(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule], result: rawptr[VerifyResult]) -> int:
"""验证模块中所有函数的 SSA 正确性。
返回: 错误总数
+44 -44
View File
@@ -29,7 +29,7 @@ from .__function import Function, BasicBlock, block_append_text, block_set_termi
# (方法以实例方法实现,无虚表;文件末尾的同名顶层函数为一行委托)。
# ============================================================
novirtual class IRBuilder:
Pool: memhub.MemManager* # 内存池
Pool: memhub.MemBuddy* # 内存池
Func: rawptr[Function] # 所属函数
CurBlock: rawptr[BasicBlock] # 当前插入点
Counter: int # SSA 名计数器
@@ -39,7 +39,7 @@ novirtual class IRBuilder:
# ============================================================
def _alloc_ssa_name(self) -> str:
"""分配一个 SSA 名字符串 "%N" 并递增计数器"""
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
buf: str = pool.alloc(16)
if buf is None: return None
viperlib.snprintf(buf, 16, "%%%d", self.Counter)
@@ -53,7 +53,7 @@ novirtual class IRBuilder:
注意:复杂结构体类型(如 13 方法的虚表 !llvm.struct<(...)>)可能超过
64 字节,必须使用足够大的缓冲区,否则 '>' 会被截断导致语法错误。
"""
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
buf: str = pool.alloc(512)
if buf is None: return None
buf[0] = '\0'
@@ -90,7 +90,7 @@ novirtual class IRBuilder:
lhs: rawptr[Value], rhs: rawptr[Value]) -> rawptr[Value]:
"""二元运算通用实现"""
if self is None or lhs is None or rhs is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(lhs.Ty)
line: str = pool.alloc(256)
@@ -138,7 +138,7 @@ novirtual class IRBuilder:
因此本函数不设默认参数;需要显式对齐请用 build_alloca_aligned。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
line: str = pool.alloc(128)
@@ -163,7 +163,7 @@ novirtual class IRBuilder:
align 不设默认值(TPC 并行编译时 include 默认参数不预注册),调用方必须显式传入。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
line: str = pool.alloc(128)
@@ -187,7 +187,7 @@ novirtual class IRBuilder:
导致的支配性违规(Instruction does not dominate all uses)。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
func: rawptr[Function] = self.Func
if func is None or func.Blocks is None:
return self.build_alloca(ty)
@@ -225,7 +225,7 @@ novirtual class IRBuilder:
优化器提升/合并重复访问的场景使用。
"""
if self is None or ptr is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -247,7 +247,7 @@ novirtual class IRBuilder:
限定符(`store volatile i32 %v, i32* %p`)。
"""
if self is None or val is None or ptr is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
val_ty_s: str = self._type_str(val.Ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
line: str = pool.alloc(256)
@@ -376,7 +376,7 @@ novirtual class IRBuilder:
def build_fneg(self, val: rawptr[Value]) -> rawptr[Value]:
"""%N = fneg <ty> <val>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(val.Ty)
line: str = pool.alloc(128)
@@ -396,7 +396,7 @@ novirtual class IRBuilder:
返回类型为 i1 的 SSA 值
"""
if self is None or lhs is None or rhs is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(lhs.Ty)
pred_s: str = _icmp_pred_str(pred)
@@ -420,7 +420,7 @@ novirtual class IRBuilder:
返回类型为 i1 的 SSA 值
"""
if self is None or lhs is None or rhs is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(lhs.Ty)
pred_s: str = _fcmp_pred_str(pred)
@@ -443,7 +443,7 @@ novirtual class IRBuilder:
def build_br(self, target: rawptr[BasicBlock]):
"""br label %<target>"""
if self is None or target is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(64)
if line is None: return
viperlib.snprintf(line, 64, "br label %%%s", target.Name)
@@ -455,7 +455,7 @@ novirtual class IRBuilder:
then_blk: rawptr[BasicBlock], else_blk: rawptr[BasicBlock]):
"""br i1 <cond>, label %<then>, label %<else_>"""
if self is None or cond is None or then_blk is None or else_blk is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(128)
if line is None: return
viperlib.snprintf(line, 128, "br i1 %s, label %%%s, label %%%s",
@@ -467,7 +467,7 @@ novirtual class IRBuilder:
def build_ret(self, val: rawptr[Value]):
"""ret <ty> <val>"""
if self is None or val is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
ty_s: str = self._type_str(val.Ty)
line: str = pool.alloc(128)
if line is None: return
@@ -479,7 +479,7 @@ novirtual class IRBuilder:
def build_ret_void(self):
"""ret void"""
if self is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(16)
if line is None: return
string.strcpy(line, "ret void")
@@ -501,7 +501,7 @@ novirtual class IRBuilder:
is_variadic=1 时发射函数类型签名(如 i32 (i8*, ...) @printf)。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
# 判断返回类型是否为 void
is_void: int = 0
@@ -586,7 +586,7 @@ novirtual class IRBuilder:
间接调用:callee_val 是函数指针 SSA 值。
"""
if self is None or callee_val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
is_void: int = 0
match ret_ty:
@@ -639,7 +639,7 @@ novirtual class IRBuilder:
"""%N = bitcast <from_ty> <val> to <to_ty>"""
if self is None or val is None:
return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -657,7 +657,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = sext <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -672,7 +672,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = trunc <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -687,7 +687,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = zext <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -702,7 +702,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = ptrtoint <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -717,7 +717,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = inttoptr <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -735,7 +735,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fpext <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -750,7 +750,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fptrunc <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -765,7 +765,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fptosi <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -780,7 +780,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = sitofp <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -795,7 +795,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = fptoui <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -810,7 +810,7 @@ novirtual class IRBuilder:
to_ty: rawptr[LLVMType]) -> rawptr[Value]:
"""%N = uitofp <from_ty> <val> to <to_ty>"""
if self is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
from_ty_s: str = self._type_str(val.Ty)
to_ty_s: str = self._type_str(to_ty)
@@ -834,7 +834,7 @@ novirtual class IRBuilder:
# 直接 GEP 会导致 llc 编译失败
if _is_ptr_type(ptr.Ty) == 0:
return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
elem_ty_s: str = self._type_str(elem_ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -864,7 +864,7 @@ novirtual class IRBuilder:
结果类型是 elem_ty*
"""
if self is None or ptr is None or idx is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
array_ty_s: str = self._type_str(array_ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -894,7 +894,7 @@ novirtual class IRBuilder:
结果类型是 field_ty*
"""
if self is None or ptr is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
struct_ty_s: str = self._type_str(struct_ty)
ptr_ty_s: str = self._type_str(ptr.Ty)
@@ -929,7 +929,7 @@ novirtual class IRBuilder:
incoming_head 是 PhiIncoming 链表头,count 是入边数量。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(ty)
@@ -975,7 +975,7 @@ novirtual class IRBuilder:
cases_head: rawptr[SwitchCase], case_count: int):
"""switch <ty> <val>, label %<default> [<case_val>, label %<blk> ...]"""
if self is None or val is None or default_blk is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
ty_s: str = self._type_str(val.Ty)
# 构建主体: switch <ty> <val>, label %default
@@ -1016,7 +1016,7 @@ novirtual class IRBuilder:
true_val: rawptr[Value], false_val: rawptr[Value]) -> rawptr[Value]:
"""%N = select i1 <cond>, <ty> <true_val>, <ty> <false_val>"""
if self is None or cond is None or true_val is None or false_val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ty_s: str = self._type_str(true_val.Ty)
line: str = pool.alloc(256)
@@ -1033,7 +1033,7 @@ novirtual class IRBuilder:
def build_unreachable(self):
"""unreachable"""
if self is None: return
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
line: str = pool.alloc(16)
if line is None: return
string.strcpy(line, "unreachable")
@@ -1049,7 +1049,7 @@ novirtual class IRBuilder:
order: int) -> rawptr[Value]:
"""%N = atomicrmw <op> <ptr_ty> <ptr>, <val_ty> <val> <ordering>"""
if self is None or ptr is None or val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ptr_ty_s: str = self._type_str(ptr.Ty)
val_ty_s: str = self._type_str(val.Ty)
@@ -1071,7 +1071,7 @@ novirtual class IRBuilder:
返回 {<ty>, i1} 类型的 SSA 值(简化为返回 i64 表示的值对)
"""
if self is None or ptr is None or cmp_val is None or new_val is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
ptr_ty_s: str = self._type_str(ptr.Ty)
cmp_ty_s: str = self._type_str(cmp_val.Ty)
@@ -1097,7 +1097,7 @@ novirtual class IRBuilder:
"""%N = va_arg <va_list_ty> <va_list_ptr>, <arg_ty>"""
if self is None or va_list_ptr is None or arg_ty is None:
return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
name: str = self._alloc_ssa_name()
va_list_ty_s: str = self._type_str(va_list_ptr.Ty)
arg_ty_s: str = self._type_str(arg_ty)
@@ -1122,7 +1122,7 @@ novirtual class IRBuilder:
简化的内联汇编调用,无参数版本。
"""
if self is None: return None
pool: memhub.MemManager* = self.Pool
pool: memhub.MemBuddy* = self.Pool
# 判断返回类型是否为 void
is_void: int = 0
@@ -1167,7 +1167,7 @@ novirtual class IRBuilder:
# IRBuilder 硬编码大小: Pool(8) + Func(8) + CurBlock(8) + Counter(4) = 32 字节
BUILDER_SIZE: t.CDefine = 32
def new_builder(pool: memhub.MemManager*, func: rawptr[Function]) -> rawptr[IRBuilder]:
def new_builder(pool: memhub.MemBuddy*, func: rawptr[Function]) -> rawptr[IRBuilder]:
"""创建 IRBuilder,关联到函数 func"""
ptr: rawptr[IRBuilder] = pool.alloc(BUILDER_SIZE)
if ptr is None: return None
@@ -1677,7 +1677,7 @@ novirtual class PhiIncoming(GSListNode[PhiIncoming]):
# PhiIncoming 硬编码大小: Next(8) + Val(8) + Block(8) = 24 字节
PHIINC_SIZE: t.CDefine = 24
def new_phi_incoming(pool: memhub.MemManager*,
def new_phi_incoming(pool: memhub.MemBuddy*,
val: rawptr[Value], block: rawptr[BasicBlock]) -> rawptr[PhiIncoming]:
ptr: rawptr[PhiIncoming] = pool.alloc(PHIINC_SIZE)
if ptr is None: return None
@@ -1712,7 +1712,7 @@ novirtual class SwitchCase(GSListNode[SwitchCase]):
# SwitchCase 硬编码大小: Next(8) + CaseVal(8) + Block(8) = 24 字节
SWCASE_SIZE: t.CDefine = 24
def new_switch_case(pool: memhub.MemManager*,
def new_switch_case(pool: memhub.MemBuddy*,
case_val: rawptr[Value], block: rawptr[BasicBlock]) -> rawptr[SwitchCase]:
ptr: rawptr[SwitchCase] = pool.alloc(SWCASE_SIZE)
if ptr is None: return None
+17 -17
View File
@@ -96,7 +96,7 @@ novirtual class Function(GSListNode[Function]):
# Line 硬编码大小: Next(8) + Buf(8) + Len(8) = 24 字节
LINE_SIZE: t.CDefine = 24
def new_line(pool: memhub.MemManager*, text: str) -> rawptr[Line]:
def new_line(pool: memhub.MemBuddy*, text: str) -> rawptr[Line]:
"""创建一个指令行,复制 text 到新分配的缓冲区"""
ptr: rawptr[Line] = pool.alloc(LINE_SIZE)
if ptr is None: return None
@@ -111,7 +111,7 @@ def new_line(pool: memhub.MemManager*, text: str) -> rawptr[Line]:
return ptr
def _new_gslist(pool: memhub.MemManager*, list_size: sizet) -> t.CPtr:
def _new_gslist(pool: memhub.MemBuddy*, list_size: sizet) -> t.CPtr:
"""从 pool 分配并零初始化一个 GSList 容器(Head/Tail/Count 全零)"""
gsl: t.CPtr = pool.alloc(list_size)
if gsl is None: return None
@@ -122,7 +122,7 @@ def _new_gslist(pool: memhub.MemManager*, list_size: sizet) -> t.CPtr:
# BasicBlock 硬编码大小: Next(8) + Name(8) + Lines(8) + IsTerminated(4) = 32 字节
BLOCK_SIZE: t.CDefine = 32
def new_basic_block(pool: memhub.MemManager*, name: str) -> rawptr[BasicBlock]:
def new_basic_block(pool: memhub.MemBuddy*, name: str) -> rawptr[BasicBlock]:
"""创建一个基本块"""
ptr: rawptr[BasicBlock] = pool.alloc(BLOCK_SIZE)
if ptr is None: return None
@@ -136,7 +136,7 @@ def new_basic_block(pool: memhub.MemManager*, name: str) -> rawptr[BasicBlock]:
# Param 硬编码大小: Next(8) + Ty(8) + Name(8) + Attrs(8) = 32 字节
PARAM_SIZE: t.CDefine = 32
def new_param(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def new_param(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
name: str) -> rawptr[Param]:
"""创建一个函数参数"""
ptr: rawptr[Param] = pool.alloc(PARAM_SIZE)
@@ -151,7 +151,7 @@ def new_param(pool: memhub.MemManager*, ty: rawptr[LLVMType],
# Function 硬编码大小: Next(8) + Name(8) + RetTy(8) + Params(8) + Blocks(8) + IsDeclared(4) + IsVarArg(4) + Attrs(8) + AttrGroup(8) + AttrGroupId(8) + NameLen(8) = 80 字节
FUNC_SIZE: t.CDefine = 80
def new_function(pool: memhub.MemManager*, name: str,
def new_function(pool: memhub.MemBuddy*, name: str,
ret_ty: rawptr[LLVMType]) -> rawptr[Function]:
"""创建一个函数"""
ptr: rawptr[Function] = pool.alloc(FUNC_SIZE)
@@ -261,7 +261,7 @@ def block_get_terminated(block: rawptr[BasicBlock]) -> int:
return block.IsTerminated
def block_append_text(pool: memhub.MemManager*, block: rawptr[BasicBlock],
def block_append_text(pool: memhub.MemBuddy*, block: rawptr[BasicBlock],
text: str):
"""创建指令行并追加到基本块"""
if block is None or text is None: return
@@ -270,7 +270,7 @@ def block_append_text(pool: memhub.MemManager*, block: rawptr[BasicBlock],
block_append_line(block, line)
def block_insert_text_before_terminator(pool: memhub.MemManager*,
def block_insert_text_before_terminator(pool: memhub.MemBuddy*,
block: rawptr[BasicBlock],
text: str):
"""创建指令行并插入到基本块终止指令之前
@@ -539,7 +539,7 @@ def _assign_new_numbers(text: str,
def _replace_ssa_numbers(text: str,
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
map_arr: str,
max_n: int) -> str:
"""构建新文本,将 %N 替换为映射后的新编号
@@ -599,7 +599,7 @@ def _replace_ssa_numbers(text: str,
return new_buf
def RenumberSSA(func: rawptr[Function], pool: memhub.MemManager*):
def RenumberSSA(func: rawptr[Function], pool: memhub.MemBuddy*):
"""重编号函数内所有数字 SSA 名,确保按文本输出顺序单调递增
在 FunctionPrint 序列化函数体之前调用。
@@ -711,7 +711,7 @@ def _peep_is_startwith(s: str, pfx: str) -> int:
return 1 if string.strncmp(s, pfx, pl) == 0 else 0
def _peep_replace_token(pool: memhub.MemManager*,
def _peep_replace_token(pool: memhub.MemBuddy*,
line_buf: str, tok: str, repl: str,
tlen: sizet, rlen: sizet) -> str:
"""将 line_buf 中独立记号 %<tok> 替换为 repl,返回新缓冲(无变化返回原指针)
@@ -803,7 +803,7 @@ def _peep_last_ref_eq(sb: str, a_name: str, nl: sizet) -> int:
return 0
def _peep_alloc_name(eb: str, pool: memhub.MemManager*) -> str:
def _peep_alloc_name(eb: str, pool: memhub.MemBuddy*) -> str:
"""从 '%name = alloca ...' 提取 name(不含 %),失败返回 None"""
if eb is None or eb[0] != '%':
return None
@@ -847,7 +847,7 @@ def _peep_is_alloc_of(eb: str, a_name: str, nl: sizet) -> int:
return 0
def _peephole_opt(func: rawptr[Function], pool: memhub.MemManager*):
def _peephole_opt(func: rawptr[Function], pool: memhub.MemBuddy*):
"""文本层本地窥孔:T1 ret zeroinitializer;T2 入口槽 store→load 转发"""
if func is None or func.Blocks is None or pool is None:
return
@@ -1052,7 +1052,7 @@ def _peephole_opt(func: rawptr[Function], pool: memhub.MemManager*):
_peep_noop_bitcast(func, pool)
def _peep_noop_bitcast(func: rawptr[Function], pool: memhub.MemManager*):
def _peep_noop_bitcast(func: rawptr[Function], pool: memhub.MemBuddy*):
"""T3: 消除 no-op bitcast(bitcast <T> %x to <T>,两侧类型相同)。
生成端(虚调用/联合位宽转换)会产生同类型恒等 bitcast(如
@@ -1150,7 +1150,7 @@ MLIR_ALIAS_CAP: t.CDefine = 512
# 渲染阶段:1=扫描(登记) 2=发射(写文本)
_MLIR_Pass: int = 0
_MLIR_Pool: memhub.MemManager* = None
_MLIR_Pool: memhub.MemBuddy* = None
_MLIR_Func: rawptr[Function] = None
_MLIR_SrcBlk: rawptr[BasicBlock] = None
@@ -1213,7 +1213,7 @@ def _mlir_dup(s: str) -> str:
return b
def _MlirTablesInit(pool: memhub.MemManager*):
def _MlirTablesInit(pool: memhub.MemBuddy*):
"""初始化两个表和解析用 scratch 缓冲"""
global _MLIR_Pool, _MLIR_Pass, _MLIR_ConstTy, _MLIR_ConstVal, _MLIR_ConstName, _MLIR_ConstCount, _MLIR_AliasFrom, _MLIR_AliasTo, _MLIR_AliasCount, _SC_Op, _SC_Tb, _SC_Ty, _SC_Ty2, _SC_V1, _SC_V2, _SC_V3, _SC_Callee, _SC_Args, _SC_ArgTs, _SC_IdxV, _SC_IdxT, _SC_Mt, _SC_Tmp, _SC_Tmp2, _SC_P1, _SC_P2, _SC_B1, _SC_B2, _SC_Buf
if pool is None:
@@ -3129,7 +3129,7 @@ def _MlirRenderLine(text: str, out: str, outsz: sizet):
# 函数签名与函数体
# ============================================================
def _MlirFuncSig(buf: str, size: sizet,
func: rawptr[Function], pool: memhub.MemManager*):
func: rawptr[Function], pool: memhub.MemBuddy*):
"""llvm.func @name(%a: i32, %b: i32) -> i32(void 返回省略 -> ty)"""
_append_cstr(buf, size, "llvm.func @")
if func.Name is not None:
@@ -3187,7 +3187,7 @@ def _MlirFuncSig(buf: str, size: sizet,
def FunctionPrint(buf: str, size: sizet,
func: rawptr[Function], pool: memhub.MemManager*):
func: rawptr[Function], pool: memhub.MemBuddy*):
"""将函数 func 序列化为 MLIR(llvm 方言) 文本**追加**写入 buf
注意:与 llvmlite 版一致,本函数为追加写(供模块打印连续拼接多个函数),
+12 -12
View File
@@ -91,7 +91,7 @@ from .__verify import (
# ============================================================
# 便捷工厂: 字符串复制
# ============================================================
def _str_dup(pool: memhub.MemManager*, src: str) -> str:
def _str_dup(pool: memhub.MemBuddy*, src: str) -> str:
"""从 pool 分配并复制一个 C 字符串"""
if src is None: return None
slen: sizet = string.strlen(src)
@@ -104,7 +104,7 @@ def _str_dup(pool: memhub.MemManager*, src: str) -> str:
# ============================================================
# 便捷工厂: 创建带名称的基本块
# ============================================================
def create_block(pool: memhub.MemManager*, func: rawptr[Function],
def create_block(pool: memhub.MemBuddy*, func: rawptr[Function],
name: str) -> rawptr[BasicBlock]:
"""创建基本块并添加到函数"""
# 块名与函数参数值名冲突防护:LLVM 中块标签 "entry:" 即值 %entry,
@@ -146,7 +146,7 @@ def create_block(pool: memhub.MemManager*, func: rawptr[Function],
# ============================================================
# 便捷工厂: 创建整数常量值
# ============================================================
def const_int(pool: memhub.MemManager*, bits: int,
def const_int(pool: memhub.MemBuddy*, bits: int,
val: int64) -> rawptr[Value]:
"""创建整数常量值(自动创建类型 + 名字文本)
@@ -175,12 +175,12 @@ def const_int(pool: memhub.MemManager*, bits: int,
return ConstInt(pool, ty, val, name)
def const_int32(pool: memhub.MemManager*, val: int64) -> rawptr[Value]:
def const_int32(pool: memhub.MemBuddy*, val: int64) -> rawptr[Value]:
"""便捷: 创建 i32 整数常量"""
return const_int(pool, 32, val)
def const_int64(pool: memhub.MemManager*, val: int64) -> rawptr[Value]:
def const_int64(pool: memhub.MemBuddy*, val: int64) -> rawptr[Value]:
"""便捷: 创建 i64 整数常量"""
return const_int(pool, 64, val)
@@ -188,7 +188,7 @@ def const_int64(pool: memhub.MemManager*, val: int64) -> rawptr[Value]:
# ============================================================
# 便捷工厂: 创建函数并添加到模块
# ============================================================
def create_function(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_function(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, ret_ty: rawptr[LLVMType]) -> rawptr[Function]:
"""创建函数并添加到模块"""
name_dup: str = _str_dup(pool, name)
@@ -198,7 +198,7 @@ def create_function(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
return func
def add_param(pool: memhub.MemManager*, func: rawptr[Function],
def add_param(pool: memhub.MemBuddy*, func: rawptr[Function],
ty: rawptr[LLVMType], name: str) -> rawptr[Param]:
"""创建参数并添加到函数"""
name_dup: str = _str_dup(pool, name)
@@ -211,7 +211,7 @@ def add_param(pool: memhub.MemManager*, func: rawptr[Function],
# ============================================================
# 便捷工厂: 创建函数声明(declare)
# ============================================================
def create_declare(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_declare(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, ret_ty: rawptr[LLVMType]) -> rawptr[Function]:
"""创建函数声明(declare,无函数体)并添加到模块
@@ -233,7 +233,7 @@ def create_declare(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
# (c"contents\00"),MLIR 渲染阶段(__module.vp/_MlirGlobalInit)
# 再转成 MLIR attribute 形态 "contents"。
# ============================================================
def create_global_string(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_global_string(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, text: str) -> rawptr[GlobalVariable]:
"""创建字符串常量全局变量并添加到模块
@@ -321,7 +321,7 @@ def create_global_string(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
# ============================================================
# 便捷工厂: 创建带初始化值的整数全局变量
# ============================================================
def create_global_int(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
def create_global_int(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule],
name: str, bits: int, val: int64) -> rawptr[GlobalVariable]:
"""创建整数全局变量并添加到模块
@@ -360,9 +360,9 @@ def create_global_int(pool: memhub.MemManager*, mod: rawptr[LLVMModule],
@t.NoVTable
class LLVMModuleCore:
"""模块/函数/值创建核心,封装 pool 避免每次创建都传 pool"""
Pool: memhub.MemManager*
Pool: memhub.MemBuddy*
def __init__(self, pool: memhub.MemManager*):
def __init__(self, pool: memhub.MemBuddy*):
self.Pool = pool
# ---- 模块操作 ----
+18 -18
View File
@@ -112,7 +112,7 @@ class LLVMModule:
self.DataLayout = layout
# ---- 命名结构体类型管理 ----
def add_named_type(self, pool: memhub.MemManager*, ty: rawptr[LLVMType]):
def add_named_type(self, pool: memhub.MemBuddy*, ty: rawptr[LLVMType]):
"""登记命名结构体类型到模块(MLIR 侧仅保留模型,不产生输出)
非命名结构体(Name 为 None 或非 Struct 类型)会被忽略。
@@ -199,7 +199,7 @@ class LLVMModule:
cur = cur.Next
return 0
def ensure_opaque_for_type(self, pool: memhub.MemManager*,
def ensure_opaque_for_type(self, pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]):
"""为类型中的跨模块结构体引用添加 opaque 声明
@@ -210,7 +210,7 @@ class LLVMModule:
# ---- 打印 ----
def PrintModule(self, buf: str, size: sizet,
pool: memhub.MemManager*, mode: int):
pool: memhub.MemBuddy*, mode: int):
"""将模块序列化为 MLIR(llvm 方言) 文本写入 buf
输出骨架: module { <全局> <函数> }
@@ -308,7 +308,7 @@ class LLVMModule:
# 同时把节点的 packed 标记(NamedTypeNode.IsPacked,设置点唯一:
# module_set_named_type_packed)与类型一并登记,供渲染侧按名字回查。
# ============================================================
def _MlirPublishNamedTypes(mod: rawptr[LLVMModule], pool: memhub.MemManager*):
def _MlirPublishNamedTypes(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*):
"""按 NamedTypeHead 重建命名结构体登记表(打印期查表用,含 packed 标记)
先数一遍真实链表长度(不依赖 NamedTypeCount 字段),再按该数量精确分配槽位、
@@ -347,7 +347,7 @@ def _MlirPublishNamedTypes(mod: rawptr[LLVMModule], pool: memhub.MemManager*):
# ============================================================
# 工厂函数
# ============================================================
def new_module(pool: memhub.MemManager*, name: str) -> rawptr[LLVMModule]:
def new_module(pool: memhub.MemBuddy*, name: str) -> rawptr[LLVMModule]:
"""创建一个模块"""
# 硬编码 96 字节:12 字段 * 8 字节(无 vtable,@t.NoVTable)
ptr: rawptr[LLVMModule] = pool.alloc(96)
@@ -374,7 +374,7 @@ GV_SIZE: t.CDefine = 72
# NamedTypeNode 硬编码大小: Next(8) + Ty(8) + IsPacked(8) = 24 字节
NAMED_TYPE_NODE_SIZE: t.CDefine = 24
def new_global_variable(pool: memhub.MemManager*,
def new_global_variable(pool: memhub.MemBuddy*,
name: str, ty: rawptr[LLVMType]) -> rawptr[GlobalVariable]:
"""创建一个全局变量"""
ptr: rawptr[GlobalVariable] = pool.alloc(GV_SIZE)
@@ -455,12 +455,12 @@ def _is_opaque_struct(ty: rawptr[LLVMType]) -> int:
return 0
def module_add_named_type(mod: rawptr[LLVMModule], pool: memhub.MemManager*,
def module_add_named_type(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]):
mod.add_named_type(pool, ty)
def module_set_named_type_packed(mod: rawptr[LLVMModule], pool: memhub.MemManager*,
def module_set_named_type_packed(mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
ty: rawptr[LLVMType], is_packed: int):
"""按名称设置命名结构体的 packed 标记(MLIR 输出 !llvm.struct<"name", packed (…)>)。
@@ -501,7 +501,7 @@ def _is_type_defined_in_module(mod: rawptr[LLVMModule], name: str) -> int:
return mod._is_type_defined_in_module(name)
def _scan_type_for_cross_module_refs(pool: memhub.MemManager*,
def _scan_type_for_cross_module_refs(pool: memhub.MemBuddy*,
mod: rawptr[LLVMModule],
ty: rawptr[LLVMType],
depth: int = 0):
@@ -566,7 +566,7 @@ def _scan_type_for_cross_module_refs(pool: memhub.MemManager*,
def module_ensure_opaque_for_type(mod: rawptr[LLVMModule],
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]):
mod.ensure_opaque_for_type(pool, ty)
@@ -619,7 +619,7 @@ IR_DUMP_CHAIN_LIMIT: t.CDefine = 100000
# mode: OUTPUT_FULL=完整, OUTPUT_STUB=仅声明, OUTPUT_TEXT=仅代码
# ============================================================
def LLVMModulePrint(buf: str, size: sizet,
mod: rawptr[LLVMModule], pool: memhub.MemManager*,
mod: rawptr[LLVMModule], pool: memhub.MemBuddy*,
mode: int):
mod.PrintModule(buf, size, pool, mode)
@@ -641,7 +641,7 @@ def LLVMModulePrint(buf: str, size: sizet,
# 两条路径都覆盖不到的形态一律 MlirFail **响亮报错**,绝不静默输出错误 MLIR。
# ============================================================
def _MlirGlobalLine(buf: str, size: sizet,
pool: memhub.MemManager*, gv: rawptr[GlobalVariable],
pool: memhub.MemBuddy*, gv: rawptr[GlobalVariable],
stub: int):
"""渲染一条 llvm.mlir.global(stub=1 时输出 external 声明形态)"""
if gv is None:
@@ -730,7 +730,7 @@ def _MlirGlobalLine(buf: str, size: sizet,
def _MlirGlobalInit(buf: str, size: sizet,
pool: memhub.MemManager*, gv: rawptr[GlobalVariable]):
pool: memhub.MemBuddy*, gv: rawptr[GlobalVariable]):
"""渲染全局初值(MLIR attribute 文本)"""
x: str = gv.Initializer
if x is None:
@@ -778,7 +778,7 @@ def _MlirGlobalInit(buf: str, size: sizet,
MlirFail("MLIR 渲染: 全局初值形态 v1 不支持(属性路径与 initializer region 均未覆盖)")
def _MlirCStringToMlir(buf: str, size: sizet, src: str, pool: memhub.MemManager*):
def _MlirCStringToMlir(buf: str, size: sizet, src: str, pool: memhub.MemBuddy*):
"""c"hello\\00" → "hello\\00"(仅去掉 'c' 前缀,转义内容原样保留)
⚠ 实测 mlir-opt 22.1.8:`llvm.mlir.global` 的字符串属性**长度必须等于**
@@ -794,7 +794,7 @@ def _MlirCStringToMlir(buf: str, size: sizet, src: str, pool: memhub.MemManager*
def _MlirArrayDenseAt(buf: str, size: sizet, src: str,
pool: memhub.MemManager*) -> str:
pool: memhub.MemBuddy*) -> str:
"""LLVM 数组字面量 → MLIR dense 内容(嵌套递归),返回新游标"""
if src is None or src[0] != '[':
MlirFail("MLIR 渲染: 全局数组初值形态 v1 不支持")
@@ -878,7 +878,7 @@ _MlirGvInsBuf: str = None # insertvalue 链缓冲
_MlirGvTopType: str = None # 顶层聚合 MLIR 类型文本
_MlirGvLast: str = None # 当前节点的 SSA 名
_MlirGvPrev: str = None # insertvalue 链前一结果 SSA 名
_MlirGvPool: memhub.MemManager* = None
_MlirGvPool: memhub.MemBuddy* = None
_MlirGvLeafSeq: int = 0
_MlirGvInsSeq: int = 0
@@ -937,7 +937,7 @@ def _MlirGvHasSymOrStruct(x: str) -> int:
def _MlirGlobalInitRegion(buf: str, size: sizet,
pool: memhub.MemManager*, gv: rawptr[GlobalVariable]):
pool: memhub.MemBuddy*, gv: rawptr[GlobalVariable]):
"""渲染 initializer region 的体(2 空格缩进的行序列,不含最外层大括号)
每行都带 '\\n';调用方在末尾补 '}',模块层再统一缩进 2 空格。
@@ -1366,7 +1366,7 @@ def _MlirGvInsertLeaf(path: str):
def _MlirAppendIndented(dst: str, dst_size: sizet, src: str,
indent: int, pool: memhub.MemManager*):
indent: int, pool: memhub.MemBuddy*):
"""把 src 的每一行前加 indent 个空格后追加到 dst(模块成员缩进)"""
if dst is None or src is None:
return
+37 -37
View File
@@ -88,7 +88,7 @@ def _TypeSizeGuard() -> sizet:
return sz
def _new_type(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def _new_type(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""从 mpool 分配一个零初始化的 LLVMType"""
sz_nt: sizet = _TypeSizeGuard()
ptr: rawptr[LLVMType] = pool.alloc(sz_nt)
@@ -100,42 +100,42 @@ def _new_type(pool: memhub.MemManager*) -> rawptr[LLVMType]:
# ============================================================
# 类型工厂函数
# ============================================================
def Int1(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int1(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(1))
return ptr
def Int8(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int8(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(8))
return ptr
def Int16(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int16(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(16))
return ptr
def Int32(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int32(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(32))
return ptr
def Int64(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Int64(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Int(64))
return ptr
def Ptr(pool: memhub.MemManager*, pointee: rawptr[LLVMType]) -> rawptr[LLVMType]:
def Ptr(pool: memhub.MemBuddy*, pointee: rawptr[LLVMType]) -> rawptr[LLVMType]:
"""创建指针类型 Ptr(pointee)
LLVM IR 中 **不允许 void* 指针**("pointers to void are invalid"),
@@ -160,7 +160,7 @@ def Ptr(pool: memhub.MemManager*, pointee: rawptr[LLVMType]) -> rawptr[LLVMType]
return ptr
def Func(pool: memhub.MemManager*, ret: rawptr[LLVMType],
def Func(pool: memhub.MemBuddy*, ret: rawptr[LLVMType],
params: rawptr[ParamNode], count: int) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -168,14 +168,14 @@ def Func(pool: memhub.MemManager*, ret: rawptr[LLVMType],
return ptr
def Void(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Void(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
c.DerefAs(ptr, LLVMType.Void())
return ptr
def Array(pool: memhub.MemManager*, elem_ty: rawptr[LLVMType],
def Array(pool: memhub.MemBuddy*, elem_ty: rawptr[LLVMType],
count: int) -> rawptr[LLVMType]:
"""创建数组类型 [count x elem_ty]"""
ptr: rawptr[LLVMType] = _new_type(pool)
@@ -184,7 +184,7 @@ def Array(pool: memhub.MemManager*, elem_ty: rawptr[LLVMType],
return ptr
def Struct(pool: memhub.MemManager*, fields: rawptr[ParamNode],
def Struct(pool: memhub.MemBuddy*, fields: rawptr[ParamNode],
fcount: int, name: str) -> rawptr[LLVMType]:
"""创建结构体类型 {ty1, ty2, ...}
@@ -198,7 +198,7 @@ def Struct(pool: memhub.MemManager*, fields: rawptr[ParamNode],
return ptr
def Half(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Half(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建半精度浮点类型 half (16-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -206,7 +206,7 @@ def Half(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def Float(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Float(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建单精度浮点类型 float (32-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -214,7 +214,7 @@ def Float(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def Double(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Double(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建双精度浮点类型 double (64-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -222,7 +222,7 @@ def Double(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def FP128(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def FP128(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建四精度浮点类型 fp128 (128-bit)"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -230,7 +230,7 @@ def FP128(pool: memhub.MemManager*) -> rawptr[LLVMType]:
return ptr
def Label(pool: memhub.MemManager*) -> rawptr[LLVMType]:
def Label(pool: memhub.MemBuddy*) -> rawptr[LLVMType]:
"""创建标签类型 label"""
ptr: rawptr[LLVMType] = _new_type(pool)
if ptr is None: return None
@@ -244,7 +244,7 @@ def Label(pool: memhub.MemManager*) -> rawptr[LLVMType]:
# ParamNode 硬编码大小: Next(8) + Ty(8) = 16 字节
PNODE_SIZE: t.CDefine = 16
def new_param_node(pool: memhub.MemManager*, ty: rawptr[LLVMType]) -> rawptr[ParamNode]:
def new_param_node(pool: memhub.MemBuddy*, ty: rawptr[LLVMType]) -> rawptr[ParamNode]:
"""创建一个参数节点,Ty 设为给定类型指针"""
ptr: rawptr[ParamNode] = pool.alloc(PNODE_SIZE)
if ptr is None: return None
@@ -313,9 +313,9 @@ def paramnode_set_next(node: rawptr[ParamNode], nxt: rawptr[ParamNode]):
@t.NoVTable
class LLVMTypeCore:
"""类型创建核心,封装 pool 避免每次创建类型都传 pool"""
Pool: memhub.MemManager*
Pool: memhub.MemBuddy*
def __init__(self, pool: memhub.MemManager*):
def __init__(self, pool: memhub.MemBuddy*):
self.Pool = pool
def Int1(self) -> rawptr[LLVMType]:
@@ -467,7 +467,7 @@ def _MlirNamedSlot(idx: int) -> sizet*:
return (sizet*)(t.CPtr(uint64(_MlirNamedTys) + uint64(idx * 16)))
def MlirNamedTypesBegin(pool: memhub.MemManager*, capacity: sizet):
def MlirNamedTypesBegin(pool: memhub.MemBuddy*, capacity: sizet):
"""按模块的具名结构体**实际数量**分配登记表并清空计数(模块打印开始时调用)
按需精确分配(TPV 真实模块里具名类型可达数千个,见 README §5.9),
@@ -558,7 +558,7 @@ def _MlirNamedPackedOf(name: str) -> int:
return int(slot[1])
def _MlirFailNamed(pool: memhub.MemManager*, reason: str, name: str):
def _MlirFailNamed(pool: memhub.MemBuddy*, reason: str, name: str):
"""带类型名 + 当前中间文本行的响亮报错
MlirFail 是即时终止、拿不到调用栈(见 README §5.8),故把定位信息备齐:
@@ -583,7 +583,7 @@ def _MlirFailNamed(pool: memhub.MemManager*, reason: str, name: str):
MlirFail(buf)
def _MlirNamedCheckName(pool: memhub.MemManager*, name: str):
def _MlirNamedCheckName(pool: memhub.MemBuddy*, name: str):
"""校验名字可作 MLIR 字符串字面量内容(`"` / `\\` / 控制字符 ⇒ 响亮报错)"""
if name is None or name[0] == '\0':
MlirFail("MLIR 渲染: 命名结构体名字为空")
@@ -599,7 +599,7 @@ def _MlirNamedCheckName(pool: memhub.MemManager*, name: str):
def _MlirNamedStructAppend(dst: str, dstsz: sizet, name: str,
fields: rawptr[ParamNode], fcount: int, packed: int,
pool: memhub.MemManager*, depth: int):
pool: memhub.MemBuddy*, depth: int):
"""追加 MLIR 具名结构体类型文本(追加写 dst)
非 packed: `!llvm.struct<"name", (字段…)>`;
@@ -669,7 +669,7 @@ def _MlirNamedStructAppend(dst: str, dstsz: sizet, name: str,
# pool 用于递归打印子类型时分配临时缓冲区。
# ============================================================
def TypePrint(buf: str, size: sizet,
ty: rawptr[LLVMType], pool: memhub.MemManager*):
ty: rawptr[LLVMType], pool: memhub.MemBuddy*):
"""将类型 ty 序列化为 MLIR 类型文本写入 buf(容量 size)
pool 用于递归打印子类型时分配临时缓冲区。
@@ -687,7 +687,7 @@ def TypePrint(buf: str, size: sizet,
# 需要转换成 MLIR 语法(!llvm.ptr、!llvm.array<4 x i32>、
# !llvm.struct<(i32, !llvm.ptr)>、!llvm.struct<(…)>)后才能写进 op 行。
# ============================================================
def MlirTypeText(dst: str, dstsz: sizet, src: str, pool: memhub.MemManager*):
def MlirTypeText(dst: str, dstsz: sizet, src: str, pool: memhub.MemBuddy*):
"""把 LLVM 语法类型文本转换为 MLIR 类型文本(追加到 dst)"""
if dst is None or dstsz == 0 or src is None:
return
@@ -764,7 +764,7 @@ def _MlirAllDigits(p: str, lt: sizet, start: sizet) -> int:
return 1
def _MlirTypeAppend(dst: str, dstsz: sizet, src: str, pool: memhub.MemManager*,
def _MlirTypeAppend(dst: str, dstsz: sizet, src: str, pool: memhub.MemBuddy*,
depth: int):
"""LLVM 类型文本 → MLIR 类型文本(递归实现,追加写 dst)"""
if dst is None or dstsz == 0 or src is None:
@@ -847,7 +847,7 @@ def _MlirTypeAppend(dst: str, dstsz: sizet, src: str, pool: memhub.MemManager*,
def _MlirArrayText(dst: str, dstsz: sizet, p: str, lt: sizet,
pool: memhub.MemManager*, depth: int):
pool: memhub.MemBuddy*, depth: int):
"""[N x elem] → !llvm.array<N x elem>"""
inner: str = p + 1
innerlen: sizet = lt - 2
@@ -882,7 +882,7 @@ def _MlirArrayText(dst: str, dstsz: sizet, p: str, lt: sizet,
def _MlirStructText(dst: str, dstsz: sizet, p: str, lt: sizet,
pool: memhub.MemManager*, depth: int):
pool: memhub.MemBuddy*, depth: int):
"""{f0, f1, …} → !llvm.struct<(f0, f1, …)>;空 → !llvm.struct<()>"""
inner: str = p + 1
innerlen: sizet = lt - 2
@@ -924,7 +924,7 @@ def _MlirStructText(dst: str, dstsz: sizet, p: str, lt: sizet,
def _MlirNamedStructText(dst: str, dstsz: sizet, p: str, lt: sizet,
pool: memhub.MemManager*, depth: int):
pool: memhub.MemBuddy*, depth: int):
"""命名结构体类型文本(形如 `%"name"` / `%name`)→ 使用处的内联具名形态
MLIR 无独立类型定义语法、不允许无体前向声明(实测 mlir-opt 22.1.8:
@@ -999,7 +999,7 @@ def _MlirFindFuncParen(p: str, lt: sizet) -> sizet:
def _MlirFuncText(dst: str, dstsz: sizet, p: str, lt: sizet, fp: sizet,
pool: memhub.MemManager*, depth: int):
pool: memhub.MemBuddy*, depth: int):
"""ret (p0, p1) → !llvm.func<ret (p0, p1)>"""
ret_len: sizet = fp
rbuf: str = pool.alloc(ret_len + 1)
@@ -1052,7 +1052,7 @@ def _MlirFuncText(dst: str, dstsz: sizet, p: str, lt: sizet, fp: sizet,
TYPEPRINT_MAX_DEPTH: t.CDefine = 100
def _MlirTypePrintDepth(buf: str, size: sizet,
ty: rawptr[LLVMType], pool: memhub.MemManager*,
ty: rawptr[LLVMType], pool: memhub.MemBuddy*,
depth: int):
"""带递归深度上限的 MLIR 类型打印内部实现(覆盖式写入 buf)"""
if ty is None or buf is None or size == 0:
@@ -1143,7 +1143,7 @@ def _MlirTypePrintDepth(buf: str, size: sizet,
def _MlirStructFields(buf: str, size: sizet,
fields: rawptr[ParamNode], pool: memhub.MemManager*,
fields: rawptr[ParamNode], pool: memhub.MemBuddy*,
depth: int):
"""渲染 !llvm.struct<(f0, f1, …)>(覆盖式写入 buf)"""
buf[0] = '\0'
@@ -1199,7 +1199,7 @@ def _strappend(dst: str, dst_size: sizet, src: str):
# 调用方(LLVMModulePrint)无需为此分叉。
# ============================================================
def PrintStructDefinition(buf: str, size: sizet,
ty: rawptr[LLVMType], pool: memhub.MemManager*,
ty: rawptr[LLVMType], pool: memhub.MemBuddy*,
packed: int = 0):
"""MLIR 侧无类型定义行:写入空串(保留 API 同形)"""
if buf is None or size == 0:
@@ -1263,13 +1263,13 @@ def get_struct_fcount(ty: rawptr[LLVMType]) -> int:
# ============================================================
DEEPCOPY_MAX_DEPTH: t.CDefine = 64
def DeepCopyType(pool: memhub.MemManager*,
def DeepCopyType(pool: memhub.MemBuddy*,
ty: rawptr[LLVMType]) -> rawptr[LLVMType]:
"""深拷贝 LLVMType 到 stdlib 区(深度限制防自引用递归),失败返回 None"""
return _DeepCopyDepth(pool, ty, 0)
def _DeepCopyDepth(pool: memhub.MemManager*,
def _DeepCopyDepth(pool: memhub.MemBuddy*,
ty: rawptr[LLVMType], depth: int) -> rawptr[LLVMType]:
"""带深度上限的深拷贝实现
@@ -1332,7 +1332,7 @@ def _DeepCopyDepth(pool: memhub.MemManager*,
return new_ty
def _DeepCopyParamChain(pool: memhub.MemManager*,
def _DeepCopyParamChain(pool: memhub.MemBuddy*,
head: rawptr[ParamNode], depth: int) -> rawptr[ParamNode]:
"""深拷贝 ParamNode 链表到 stdlib"""
if head is None:
@@ -1360,7 +1360,7 @@ def _DeepCopyParamChain(pool: memhub.MemManager*,
return first
def _DeepCopyStr(pool: memhub.MemManager*, s: str) -> str:
def _DeepCopyStr(pool: memhub.MemBuddy*, s: str) -> str:
"""深拷贝字符串到 stdlib"""
if s is None:
return None
+7 -7
View File
@@ -32,7 +32,7 @@ class Value(GSListNode[Value]):
# Value 硬编码大小: Next(8) + Ty(8) + Name(8) + IsConst(4) + IntVal(8) + FloatVal(8) = 48 字节
VALUE_SIZE: t.CDefine = 48
def new_value(pool: memhub.MemManager*) -> rawptr[Value]:
def new_value(pool: memhub.MemBuddy*) -> rawptr[Value]:
"""从 mpool 分配一个零初始化的 Value"""
ptr: rawptr[Value] = pool.alloc(VALUE_SIZE)
if ptr:
@@ -40,7 +40,7 @@ def new_value(pool: memhub.MemManager*) -> rawptr[Value]:
return ptr
def ConstInt(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def ConstInt(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
val: int64, name: str) -> rawptr[Value]:
"""创建整数常量值
@@ -58,7 +58,7 @@ def ConstInt(pool: memhub.MemManager*, ty: rawptr[LLVMType],
return v
def ConstNull(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def ConstNull(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
name: str) -> rawptr[Value]:
"""创建 null 指针常量"""
v: rawptr[Value] = new_value(pool)
@@ -70,7 +70,7 @@ def ConstNull(pool: memhub.MemManager*, ty: rawptr[LLVMType],
return v
def ConstZero(pool: memhub.MemManager*, ty: rawptr[LLVMType]) -> rawptr[Value]:
def ConstZero(pool: memhub.MemBuddy*, ty: rawptr[LLVMType]) -> rawptr[Value]:
"""创建 zeroinitializer 常量(零初始化聚合类型:结构体/数组)"""
v: rawptr[Value] = new_value(pool)
if v is None: return None
@@ -81,7 +81,7 @@ def ConstZero(pool: memhub.MemManager*, ty: rawptr[LLVMType]) -> rawptr[Value]:
return v
def ConstFloat(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def ConstFloat(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
val: t.CDouble) -> rawptr[Value]:
"""创建浮点常量值
@@ -118,7 +118,7 @@ def ConstFloat(pool: memhub.MemManager*, ty: rawptr[LLVMType],
return v
def SSAValue(pool: memhub.MemManager*, ty: rawptr[LLVMType],
def SSAValue(pool: memhub.MemBuddy*, ty: rawptr[LLVMType],
name: str) -> rawptr[Value]:
"""创建 SSA 临时值
@@ -146,7 +146,7 @@ def SSAValue(pool: memhub.MemManager*, ty: rawptr[LLVMType],
# 操作数解析路径(常量必须先物化为 llvm.mlir.constant/zero/addressof op)。
# ============================================================
def ValuePrint(buf: str, size: sizet,
val: rawptr[Value], pool: memhub.MemManager*):
val: rawptr[Value], pool: memhub.MemBuddy*):
"""将值 val 序列化为 MLIR 文本写入 buf
pool 用于复杂类型(Array/Struct/Func)递归打印时分配临时缓冲区。
+6 -6
View File
@@ -54,7 +54,7 @@ class VerifyResult:
# ============================================================
# 辅助函数
# ============================================================
def _new_verify_result(pool: memhub.MemManager*,
def _new_verify_result(pool: memhub.MemBuddy*,
buf_size: sizet) -> rawptr[VerifyResult]:
"""创建验证结果对象"""
ptr: rawptr[VerifyResult] = pool.alloc(VerifyResult.__sizeof__())
@@ -126,7 +126,7 @@ def _append_cstr(dst: str, dst_size: sizet, src: str):
# ============================================================
# SSA 名表操作
# ============================================================
def _new_name_table(pool: memhub.MemManager*) -> rawptr[GSList[NameEntry]]:
def _new_name_table(pool: memhub.MemBuddy*) -> rawptr[GSList[NameEntry]]:
"""创建空名表"""
gsl: rawptr[GSList[NameEntry]] = pool.alloc(24) # GSList: Head+Tail+Count
if gsl is None: return None
@@ -138,7 +138,7 @@ def _new_name_table(pool: memhub.MemManager*) -> rawptr[GSList[NameEntry]]:
NENTRY_SIZE: t.CDefine = 16
def _name_table_add(table: rawptr[GSList[NameEntry]],
pool: memhub.MemManager*,
pool: memhub.MemBuddy*,
name: str) -> int:
"""添加名到表。返回 1=重复, 0=成功添加"""
if table is None or name is None: return 0
@@ -181,7 +181,7 @@ def _is_char_name(ch: char) -> int:
return 0
def _extract_def_name(pool: memhub.MemManager*,
def _extract_def_name(pool: memhub.MemBuddy*,
text: str) -> str:
"""从定义行提取被定义的 SSA 名。
@@ -296,7 +296,7 @@ def _str_matches_at(text: str, start: int, length: int,
# ============================================================
# 主验证函数
# ============================================================
def verify_function(pool: memhub.MemManager*,
def verify_function(pool: memhub.MemBuddy*,
func: rawptr[Function],
result: rawptr[VerifyResult]) -> int:
"""验证函数的 SSA 正确性。
@@ -419,7 +419,7 @@ def verify_function(pool: memhub.MemManager*,
return result.ErrorCount
def verify_module(pool: memhub.MemManager*, mod: rawptr[LLVMModule], result: rawptr[VerifyResult]) -> int:
def verify_module(pool: memhub.MemBuddy*, mod: rawptr[LLVMModule], result: rawptr[VerifyResult]) -> int:
"""验证模块中所有函数的 SSA 正确性。
返回: 错误总数
+3 -3
View File
@@ -39,7 +39,7 @@ novirtual class Tensor:
TENSOR_SIZE: t.CDefine = 32
def _zeros_block(pool: memhub.MemManager*, bytes_n: sizet) -> t.CPtr:
def _zeros_block(pool: memhub.MemBuddy*, bytes_n: sizet) -> t.CPtr:
"""分配并清零一块内存"""
p: t.CPtr = pool.alloc(bytes_n)
if p is not None:
@@ -47,7 +47,7 @@ def _zeros_block(pool: memhub.MemManager*, bytes_n: sizet) -> t.CPtr:
return p
def zeros(pool: memhub.MemManager*, rows: sizet,
def zeros(pool: memhub.MemBuddy*, rows: sizet,
cols: sizet, with_grad: int = 1) -> Tensor | t.CPtr:
"""torch.zeros: 创建 rows x cols 的全零 Tensor, 可选分配 Grad 缓冲"""
if pool is None:
@@ -67,7 +67,7 @@ def zeros(pool: memhub.MemManager*, rows: sizet,
return t0
def from_raw(pool: memhub.MemManager*, data: t.CPtr,
def from_raw(pool: memhub.MemBuddy*, data: t.CPtr,
rows: sizet, cols: sizet,
with_grad: int = 0) -> Tensor | t.CPtr:
"""torch.from_raw: 包装一块已有内存为 Tensor(不复制, 不拥有)"""