extract_code tool: added patching of function call relocations

This commit is contained in:
Nicolas Kruse 2025-10-26 14:09:45 +01:00
parent 362e5d19c9
commit fb4df412ce
4 changed files with 22 additions and 6 deletions

View File

@ -318,7 +318,7 @@ def compile_to_instruction_list(node_list: Iterable[Node], sdb: stencil_database
else:
raise ValueError(f"Unsupported: {name} {patch.target_symbol_info} {patch.target_symbol_name}")
assert False, aux_function_mem_layout
#assert False, aux_function_mem_layout
# write entry function code
dw.write_com(binw.Command.COPY_CODE)

View File

@ -12,7 +12,7 @@ __attribute__((noinline)) int floor_div(float arg1, float arg2) {
return i;
}
__attribute__((noinline)) float fast_sqrt(float n) {
__attribute__((noinline)) float fast_sqrt2(float n) {
if (n < 0) return -1;
float x = n; // initial guess
@ -25,6 +25,10 @@ __attribute__((noinline)) float fast_sqrt(float n) {
return x;
}
__attribute__((noinline)) float fast_sqrt(float n) {
return n * 3.5 + 4.5;
}
float fast_pow_float(float base, float exponent) {
union {
float f;
@ -37,3 +41,12 @@ float fast_pow_float(float base, float exponent) {
u.i = (uint32_t)y;
return u.f;
}
int main() {
// Test aux functions
float a = 16.0f;
float sqrt_a = fast_sqrt(a);
float pow_a = fast_pow_float(a, 0.5f);
float sqrt2_a = fast_sqrt2(a);
return 0;
}

View File

@ -15,16 +15,17 @@ def test_compiled_vectors():
t2 = t1.sum()
t3 = cp.vector(cp.variable(1 / (v + 1)) for v in range(3))
#t4 = ((t3 * t1) * 2).magnitude()
t4 = ((t3 * t1) * 2).sum()
t4 = ((t3 * t1) * 2).magnitude()
#t4 = ((t3 * t1) * 2).sum()
t5 = cp.sqrt(cp.variable(8.0))
tg = cp.Target()
tg.compile(t2, t4)
tg.compile(t2, t4, t5)
tg.run()
assert isinstance(t2, cp.variable) and tg.read_value(t2) == 10 + 11 + 12 + 0 + 1 + 2
#assert isinstance(t4, cp.variable) and tg.read_value(t4) == ((1/1*10 + 1/2*11 + 1/3*12) * 2)**0.5
assert isinstance(t5, cp.variable) and tg.read_value(t5) == 8.0 * 3.5 + 4.5
if __name__ == "__main__":
test_compiled_vectors()

View File

@ -47,6 +47,8 @@ if __name__ == "__main__":
offs = dr.read_int()
reloc_type = dr.read_int()
value = dr.read_int(signed=True)
assert reloc_type == RelocationType.RELOC_RELATIVE_32.value
program_data[offs:offs + 4] = value.to_bytes(4, byteorder, signed=True)
print(f"PATCH_FUNC patch_offs={offs} reloc_type={reloc_type} value={value}")
elif com == Command.PATCH_OBJECT:
offs = dr.read_int()