gpt4 book ai didi

optimization - 将 offsetof() 优化为常数值?

转载 作者:行者123 更新时间:2023-12-03 17:12:09 24 4
gpt4 key购买 nike

我编写了一个小函数来获取结构内部字段的字节偏移量,但为了实现这一点,我每次都会迭代这些字段,直到找到符号。然而,在 C 中,offsetof() 仅在编译时计算一次,因为结构内部的偏移量不再改变。我想知道如何尽可能地优化它,因为它是一个非常基本的函数,我需要为每个帧(3D 游戏引擎)经常调用它。

function offsetof(type_, member::Symbol)
for (i, item) in enumerate(fieldnames(type_))
if item == member
return fieldoffset(type_, i)
end
#print(typeof(i))
end
# what to do when symbol not in type_?
throw("$type_ has no member named $member")
end

用法:

type ABC
a::Int64
b::Int64
c::Int64
end
offsetof(ABC, :a) # 0
offsetof(ABC, :b) # 8
offsetof(ABC, :c) # 16

看一下LLVM代码,代码很多:

@code_llvm offsetof(Model_s, :fov)

输出:

; Function Attrs: uwtable
define i64 @julia_offsetof_61750(i8**, i8**) #0 !dbg !5 {
top:
%2 = call i8**** @jl_get_ptls_states() #6
%3 = alloca [13 x i8**], align 8
%.sub = getelementptr inbounds [13 x i8**], [13 x i8**]* %3, i64 0, i64 0
%4 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 8
%5 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 2
%6 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 3
%7 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 4
%8 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 5
%9 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 6
%10 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 7
%11 = bitcast i8*** %4 to i8*
call void @llvm.memset.p0i8.i32(i8* %11, i8 0, i32 40, i32 8, i1 false)
%12 = bitcast [13 x i8**]* %3 to i64*
%13 = bitcast i8*** %5 to i8*
call void @llvm.memset.p0i8.i64(i8* %13, i8 0, i64 40, i32 8, i1 false)
store i64 22, i64* %12, align 8
%14 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 1
%15 = bitcast i8**** %2 to i64*
%16 = load i64, i64* %15, align 8
%17 = bitcast i8*** %14 to i64*
store i64 %16, i64* %17, align 8
store i8*** %.sub, i8**** %2, align 8
store i8** null, i8*** %10, align 8
%18 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 12
%19 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 11
%20 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 10
%21 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 9
%22 = call i8** @jlsys_fieldnames_43495(i8** inttoptr (i64 375726352 to i8**))
store i8** %22, i8*** %5, align 8
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = bitcast i8** %23 to i64*
%25 = getelementptr i8*, i8** %22, i64 3
%26 = bitcast i8** %25 to i64*
%27 = bitcast i8** %22 to i8****
%28 = bitcast i8**** %2 to i8*
br label %L3

L3: ; preds = %cont2, %top
%"#temp#1.sroa.4.0" = phi i64 [ 1, %top ], [ %47, %cont2 ]
%29 = load i64, i64* %24, align 8
%30 = add i64 %29, 1
%31 = icmp eq i64 %"#temp#1.sroa.4.0", %30
br i1 %31, label %L33, label %if

if: ; preds = %L3
%32 = add i64 %"#temp#1.sroa.4.0", -1
%33 = load i64, i64* %26, align 8
%34 = icmp ult i64 %32, %33
br i1 %34, label %idxend, label %oob

L33: ; preds = %L3
store i8** inttoptr (i64 77348880 to i8**), i8*** %4, align 8
store i8** inttoptr (i64 77362488 to i8**), i8*** %21, align 8
store i8** inttoptr (i64 375726352 to i8**), i8*** %20, align 8
store i8** inttoptr (i64 377039808 to i8**), i8*** %19, align 8
store i8** %1, i8*** %18, align 8
%35 = call i8** @"jsys1_#print_to_string#229_39171"(i8** inttoptr (i64 77362448 to i8**), i8*** %4, i32 5)
store i8** %35, i8*** %10, align 8
call void @jl_throw(i8** %35)
unreachable

oob: ; preds = %if
%36 = alloca i64, align 8
store i64 %"#temp#1.sroa.4.0", i64* %36, align 8
call void @jl_bounds_error_ints(i8** %22, i64* nonnull %36, i64 1)
unreachable

idxend: ; preds = %if
%37 = load i8***, i8**** %27, align 8
%38 = getelementptr i8**, i8*** %37, i64 %32
%39 = load i8**, i8*** %38, align 8
%40 = icmp eq i8** %39, null
br i1 %40, label %fail, label %cont2

fail: ; preds = %idxend
call void @jl_throw(i8** inttoptr (i64 84505768 to i8**))
unreachable

cont2: ; preds = %idxend
store i8** %39, i8*** %6, align 8
%41 = call i8** @jl_gc_pool_alloc(i8* %28, i32 1512, i32 32)
%42 = getelementptr i8*, i8** %41, i64 -1
%43 = bitcast i8** %42 to i8***
store i8** inttoptr (i64 109041296 to i8**), i8*** %43, align 8
store i8** %41, i8*** %7, align 8
%44 = getelementptr i8*, i8** %41, i64 1
%45 = bitcast i8** %44 to i8***
%46 = bitcast i8** %41 to i64*
store i64 %"#temp#1.sroa.4.0", i64* %46, align 16
store i8** %39, i8*** %45, align 8
%47 = add i64 %"#temp#1.sroa.4.0", 1
store i8** %39, i8*** %8, align 8
store i8** %39, i8*** %9, align 8
%48 = icmp eq i8** %39, %1
br i1 %48, label %if3, label %L3

if3: ; preds = %cont2
%sext = shl i64 %"#temp#1.sroa.4.0", 32
%49 = ashr exact i64 %sext, 32
%50 = icmp eq i64 %49, %"#temp#1.sroa.4.0"
br i1 %50, label %pass5, label %fail4

fail4: ; preds = %if3
call void @jl_throw(i8** inttoptr (i64 77358144 to i8**))
unreachable

pass5: ; preds = %if3
%51 = trunc i64 %"#temp#1.sroa.4.0" to i32
%52 = call i64 inttoptr (i64 1693737504 to i64 (i8**, i32)*)(i8** inttoptr (i64 375726352 to i8**), i32 %51)
%53 = load i64, i64* %17, align 8
store i64 %53, i64* %15, align 8
ret i64 %52
}

最佳答案

这可能是Base.@pure 表示法的一个用例。对于问题中的示例,它将是:

# note the Base.@pure notation at beginning of definition:

Base.@pure function offsetof(type_, member::Symbol)
for (i, item) in enumerate(fieldnames(type_))
if item == member
return fieldoffset(type_, i)
end
#print(typeof(i))
end
# what to do when symbol not in type_?
throw("$type_ has no member named $member")
end

现在,我们可以:

type ABC
a::Int64
b::Int64
c::Int64
end
offsetof(ABC, :a) # 0

offsetof@code_llvm仍然很长。但如果我们在函数中使用它,Julia 将在编译时运行 offsetof (这是允许的,因为纯函数应该依赖于它们的参数,除了返回根据它们计算的值之外什么也不做) 。例如:

julia> f() = offsetof(ABC,:b)
f (generic function with 1 method)

julia> f()
0x0000000000000008

julia> @code_llvm f()

define i64 @julia_f_63287() #0 !dbg !5 {
top:
%ptls_i8 = call i8* asm "movq %fs:0, $0;\0Aaddq $$-10928, $0", "=r,~{dirflag},~{fpsr},~{flags}"() #1
ret i64 8
}

请注意,f() 仅由 ret i64 8 组成,它返回 0x8,即 offsetof(ABC,: b) .

这是一个描述@pure符号的链接:https://github.com/JuliaLang/julia/issues/414
(另请参阅:https://github.com/JuliaLang/julia/issues/14324)

需要注意:纯粹的表示法可能会不断变化,并且可能会在版本之间发生变化。它在 0.6 中有效。将来,我们可以使用更智能的编译器来更轻松地实现这种优化,该编译器可以进行持续传播。

关于optimization - 将 offsetof() 优化为常数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44668696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com