Skip to content

Commit dcc81b9

Browse files
author
pylon
committed
性能优化
1 parent 464daa7 commit dcc81b9

27 files changed

Lines changed: 4087 additions & 11 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 层:内核层 / il2cpp —— AAPCS64 直调帧,与托管侧 PylonCallFrame 保持布局一致。
2+
#pragma once
3+
4+
#include <cstddef>
5+
#include <cstdint>
6+
7+
namespace pylon::il2cpp {
8+
9+
/// 返回值形状;值必须与托管侧 PylonRetKind 一致。
10+
enum class RetKind : int32_t {
11+
Int = 0, // x0(void、指针、bool、枚举都走这里)
12+
F32 = 1, // s0
13+
F64 = 2, // d0
14+
S16 = 3, // x0/x1(<=16 字节、非 HFA 的 struct)
15+
H1F = 4, H2F = 5, H3F = 6, H4F = 7, // float HFA:s0-s3
16+
H1D = 8, H2D = 9, H3D = 10, H4D = 11, // double HFA:d0-d3
17+
Indirect = 12, // >16 字节:x8 指向调用方缓冲
18+
};
19+
20+
constexpr int32_t kMaxIndirectReturn = 512;
21+
22+
/// 直调帧。托管侧 PylonCallFrame 必须逐字段同布局。
23+
struct CallFrame {
24+
intptr_t x[16]; // x0-x7 + 8 个栈槽(AAPCS64 第 9..16 个整数参数)
25+
uint64_t d[8]; // d0-d7 原始位(float 在低 32 位)
26+
void* indirect; // >16 字节返回:调用方缓冲
27+
int32_t ret; // RetKind
28+
int32_t retSize; // RetKind::Indirect 的有效字节数
29+
uint64_t out[4]; // x0/x1 或 s0-s3/d0-d3 的原始位
30+
void* method; // MethodInfo*
31+
void* exc; // 出参:捕获到的 Il2CppException*(无异常为 null)
32+
};
33+
34+
constexpr size_t kCallFrameBytes = sizeof(CallFrame);
35+
static_assert(sizeof(CallFrame) == 256);
36+
static_assert(offsetof(CallFrame, out) == 208);
37+
static_assert(offsetof(CallFrame, exc) == 248);
38+
39+
} // namespace pylon::il2cpp

‎src/core/include/il2cpp/Domain.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Domain {
4444
static bool Ready();
4545

4646
static Outcome<Session> Attach();
47+
static bool Attached();
4748

4849
static void MarkGameThread();
4950
static bool OnGameThread();

‎src/core/src/il2cpp/CallFrame.S‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// AAPCS64 register executor. Exceptions unwind through this frame to Guard.S.
2+
#if defined(__aarch64__)
3+
.text
4+
.p2align 2
5+
.globl pylon_call_frame
6+
.hidden pylon_call_frame
7+
.type pylon_call_frame, %function
8+
pylon_call_frame:
9+
.cfi_startproc
10+
sub sp, sp, #96
11+
.cfi_def_cfa_offset 96
12+
stp x19, x20, [sp, #64]
13+
.cfi_offset w19, -32
14+
.cfi_offset w20, -24
15+
stp x29, x30, [sp, #80]
16+
.cfi_offset w29, -16
17+
.cfi_offset w30, -8
18+
add x29, sp, #80
19+
mov x19, x1
20+
mov x16, x0
21+
22+
ldp x9, x10, [x19, #64]
23+
stp x9, x10, [sp, #0]
24+
ldp x9, x10, [x19, #80]
25+
stp x9, x10, [sp, #16]
26+
ldp x9, x10, [x19, #96]
27+
stp x9, x10, [sp, #32]
28+
ldp x9, x10, [x19, #112]
29+
stp x9, x10, [sp, #48]
30+
ldp d0, d1, [x19, #128]
31+
ldp d2, d3, [x19, #144]
32+
ldp d4, d5, [x19, #160]
33+
ldp d6, d7, [x19, #176]
34+
ldr x8, [x19, #192]
35+
ldp x0, x1, [x19, #0]
36+
ldp x2, x3, [x19, #16]
37+
ldp x4, x5, [x19, #32]
38+
ldp x6, x7, [x19, #48]
39+
blr x16
40+
41+
ldr w9, [x19, #200]
42+
cbz w9, .Linteger
43+
cmp w9, #3
44+
b.eq .Linteger
45+
cmp w9, #12
46+
b.eq .Lrestore
47+
cmp w9, #1
48+
b.eq .Lfloats
49+
cmp w9, #2
50+
b.eq .Ldoubles
51+
cmp w9, #7
52+
b.gt .Ldoubles
53+
.Lfloats:
54+
stp s0, s1, [x19, #208]
55+
stp s2, s3, [x19, #216]
56+
b .Lrestore
57+
.Ldoubles:
58+
stp d0, d1, [x19, #208]
59+
stp d2, d3, [x19, #224]
60+
b .Lrestore
61+
.Linteger:
62+
stp x0, x1, [x19, #208]
63+
.Lrestore:
64+
ldp x19, x20, [sp, #64]
65+
ldp x29, x30, [sp, #80]
66+
add sp, sp, #96
67+
.cfi_def_cfa_offset 0
68+
.cfi_restore w19
69+
.cfi_restore w20
70+
.cfi_restore w29
71+
.cfi_restore w30
72+
ret
73+
.cfi_endproc
74+
.size pylon_call_frame, .-pylon_call_frame
75+
#endif
76+
.section .note.GNU-stack, "", %progbits

‎src/core/src/il2cpp/CallFrame.cpp‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// 层:内核层 / il2cpp —— 直调实现(保护边界复用 il2cpp/Guard)
2+
#include "il2cpp/CallFrame.h"
3+
4+
#include <cstdio>
5+
#include <cstring>
6+
#include <string>
7+
8+
#include "il2cpp/Domain.h"
9+
#include "il2cpp/Guard.h"
10+
11+
namespace pylon::il2cpp {
12+
13+
namespace {
14+
15+
thread_local std::string t_error;
16+
thread_local char t_error_c[512] = {0};
17+
18+
void SetError(const std::string& text) {
19+
t_error = text;
20+
std::snprintf(t_error_c, sizeof(t_error_c), "%s", t_error.c_str());
21+
}
22+
23+
#if defined(__aarch64__)
24+
extern "C" void pylon_call_frame(void* fn, CallFrame* frame);
25+
#endif
26+
27+
struct Pending {
28+
void* fn;
29+
CallFrame* frame;
30+
};
31+
32+
// AAPCS64: x0-x7、d0-d7、8 个栈槽。
33+
#define PYLON_CS_PARAMS \
34+
intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, \
35+
intptr_t, double, double, double, double, double, double, double, double, \
36+
intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, \
37+
intptr_t
38+
39+
struct S16 { uint64_t a, b; };
40+
struct H1F { float a; };
41+
struct H2F { float a, b; };
42+
struct H3F { float a, b, c; };
43+
struct H4F { float a, b, c, d; };
44+
struct H1D { double a; };
45+
struct H2D { double a, b; };
46+
struct H3D { double a, b, c; };
47+
struct H4D { double a, b, c, d; };
48+
struct Big { uint8_t b[kMaxIndirectReturn]; };
49+
50+
/// 在保护边界内跑:只装寄存器、跳转、拷返回物,不能有析构。
51+
void Run(void* p) {
52+
const Pending& c = *static_cast<const Pending*>(p);
53+
#if defined(__aarch64__)
54+
pylon_call_frame(c.fn, c.frame);
55+
#else
56+
CallFrame& f = *c.frame;
57+
const intptr_t* x = f.x;
58+
double d[8];
59+
std::memcpy(d, f.d, sizeof(d));
60+
61+
#define PYLON_CS_ARGS x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], \
62+
d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], \
63+
x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15]
64+
#define PYLON_CS_CALL(T) \
65+
do { \
66+
T r = reinterpret_cast<T (*)(PYLON_CS_PARAMS)>(c.fn)(PYLON_CS_ARGS); \
67+
std::memcpy(f.out, &r, sizeof(r)); \
68+
} while (0)
69+
70+
switch (static_cast<RetKind>(f.ret)) {
71+
case RetKind::Int: PYLON_CS_CALL(intptr_t); break;
72+
case RetKind::F32: PYLON_CS_CALL(float); break;
73+
case RetKind::F64: PYLON_CS_CALL(double); break;
74+
case RetKind::S16: PYLON_CS_CALL(S16); break;
75+
case RetKind::H1F: PYLON_CS_CALL(H1F); break;
76+
case RetKind::H2F: PYLON_CS_CALL(H2F); break;
77+
case RetKind::H3F: PYLON_CS_CALL(H3F); break;
78+
case RetKind::H4F: PYLON_CS_CALL(H4F); break;
79+
case RetKind::H1D: PYLON_CS_CALL(H1D); break;
80+
case RetKind::H2D: PYLON_CS_CALL(H2D); break;
81+
case RetKind::H3D: PYLON_CS_CALL(H3D); break;
82+
case RetKind::H4D: PYLON_CS_CALL(H4D); break;
83+
case RetKind::Indirect: {
84+
Big r = reinterpret_cast<Big (*)(PYLON_CS_PARAMS)>(c.fn)(PYLON_CS_ARGS);
85+
std::memcpy(f.indirect, &r, static_cast<size_t>(f.retSize));
86+
break;
87+
}
88+
}
89+
#undef PYLON_CS_CALL
90+
#undef PYLON_CS_ARGS
91+
#endif
92+
}
93+
94+
} // namespace
95+
96+
} // namespace pylon::il2cpp
97+
98+
extern "C" __attribute__((visibility("default"))) int32_t PylonMod_CallFrame(void* fn, void* framePtr) {
99+
using namespace pylon::il2cpp;
100+
t_error_c[0] = '\0';
101+
if (fn == nullptr) {
102+
SetError("target 为空");
103+
return 0;
104+
}
105+
if (framePtr == nullptr) {
106+
SetError("frame 为空");
107+
return 0;
108+
}
109+
CallFrame& frame = *static_cast<CallFrame*>(framePtr);
110+
frame.exc = nullptr;
111+
const auto kind = static_cast<RetKind>(frame.ret);
112+
if (frame.ret < 0 || frame.ret > static_cast<int32_t>(RetKind::Indirect)) {
113+
SetError("RetKind 非法");
114+
return 0;
115+
}
116+
if (kind == RetKind::Indirect &&
117+
(frame.retSize <= 0 || frame.retSize > kMaxIndirectReturn || frame.indirect == nullptr)) {
118+
SetError("间接返回参数非法");
119+
return 0;
120+
}
121+
122+
if (!Domain::Attached()) {
123+
const auto attached = Domain::Attach();
124+
if (!attached) {
125+
SetError("未 attach 到 IL2CPP 域:" + attached.reason);
126+
return 0;
127+
}
128+
}
129+
if (!Domain::OnGameThread()) {
130+
SetError(Domain::RequireGameThread("直调游戏方法").reason);
131+
return 0;
132+
}
133+
// methodPointer 包装可能转换参数/返回 ABI,不能跳过包装直调内部 icall。
134+
Pending pending{fn, &frame};
135+
Caught caught;
136+
if (Guard::Run(Run, &pending, &caught)) {
137+
t_error_c[0] = '\0';
138+
return 1;
139+
}
140+
141+
frame.exc = caught.exception;
142+
SetError(Describe(caught));
143+
return 0;
144+
}
145+
146+
extern "C" __attribute__((visibility("default"))) const char* PylonMod_CallFrameError() {
147+
return pylon::il2cpp::t_error_c;
148+
}

‎src/core/src/il2cpp/Domain.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ Outcome<Session> Domain::Attach() {
8585
return Outcome<Session>::Good(Session(&g_symbols, domain));
8686
}
8787

88+
bool Domain::Attached() {
89+
return g_attached;
90+
}
91+
8892
void Domain::MarkGameThread() {
8993
const unsigned long tid = static_cast<unsigned long>(gettid());
9094
if (g_gameThread.exchange(tid) == tid) return;
@@ -106,7 +110,8 @@ void Domain::MarkGameThread() {
106110

107111
bool Domain::OnGameThread() {
108112
const unsigned long registered = g_gameThread.load();
109-
return registered != 0 && registered == static_cast<unsigned long>(gettid());
113+
thread_local const unsigned long tid = static_cast<unsigned long>(gettid());
114+
return registered != 0 && registered == tid;
110115
}
111116

112117
unsigned long Domain::GameThreadId() {

‎src/csharp/il2cppinterop/Il2CppInterop.Generator/GeneratorOptions.cs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public class GeneratorOptions
2222
public bool PassthroughNames { get; set; }
2323
public bool Parallel { get; set; } = true;
2424

25-
/// <summary>
26-
/// 为生成的方法体启用"受保护直调"(Pylon GuardedCall + methodPointer):
27-
/// 参数打包进 DirectCallFrame 后由内核 PylonMod_CallGame 直调,替代 il2cpp_runtime_invoke。
28-
/// 只对符合 ABI 直调条件的签名生效;不支持的签名自动回退 runtime_invoke。
29-
/// </summary>
25+
/// <summary>Generate guarded direct-call entries with runtime ABI fallback.</summary>
3026
public bool DirectInvoke { get; set; }
3127

3228

0 commit comments

Comments
 (0)