Skip to content

Commit ad5d31b

Browse files
authored
Expose more functions related to emitting AOT files (#3520)
- Add declarations related to emitting AOT files into a separate header file named `aot_emit_aot_file.h` - Add API `aot_emit_aot_file_buf_ex` and refactor `aot_emit_aot_file_buf` to call it - Expose some APIs in aot_export.h
1 parent 6621793 commit ad5d31b

4 files changed

Lines changed: 103 additions & 32 deletions

File tree

core/iwasm/compilation/aot_compiler.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -782,14 +782,6 @@ aot_compile_wasm(AOTCompContext *comp_ctx);
782782
bool
783783
aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name);
784784

785-
bool
786-
aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
787-
const char *file_name);
788-
789-
uint8 *
790-
aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
791-
uint32 *p_aot_file_size);
792-
793785
bool
794786
aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name);
795787

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
*/
55

6-
#include "aot_compiler.h"
6+
#include "aot_emit_aot_file.h"
77
#include "../aot/aot_runtime.h"
88

99
#define PUT_U64_TO_ADDR(addr, value) \
@@ -1189,9 +1189,9 @@ get_string_literal_section_size(AOTCompContext *comp_ctx,
11891189
static uint32
11901190
get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data);
11911191

1192-
static uint32
1193-
get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
1194-
AOTObjectData *obj_data)
1192+
uint32
1193+
aot_get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
1194+
AOTObjectData *obj_data)
11951195
{
11961196
uint32 size = 0;
11971197
uint32 size_custom_section = 0;
@@ -4228,7 +4228,7 @@ destroy_relocation_symbol_list(AOTSymbolList *symbol_list)
42284228
}
42294229
}
42304230

4231-
static void
4231+
void
42324232
aot_obj_data_destroy(AOTObjectData *obj_data)
42334233
{
42344234
if (obj_data->binary)
@@ -4261,7 +4261,7 @@ aot_obj_data_destroy(AOTObjectData *obj_data)
42614261
wasm_runtime_free(obj_data);
42624262
}
42634263

4264-
static AOTObjectData *
4264+
AOTObjectData *
42654265
aot_obj_data_create(AOTCompContext *comp_ctx)
42664266
{
42674267
char *err = NULL;
@@ -4443,25 +4443,48 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
44434443
uint32 *p_aot_file_size)
44444444
{
44454445
AOTObjectData *obj_data = aot_obj_data_create(comp_ctx);
4446-
uint8 *aot_file_buf, *buf, *buf_end;
4447-
uint32 aot_file_size, offset = 0;
4446+
uint8 *aot_file_buf;
4447+
uint32 aot_file_size;
44484448

44494449
if (!obj_data)
44504450
return NULL;
44514451

4452-
aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
4452+
aot_file_size = aot_get_aot_file_size(comp_ctx, comp_data, obj_data);
44534453
if (aot_file_size == 0) {
44544454
aot_set_last_error("get aot file size failed");
44554455
goto fail1;
44564456
}
44574457

4458-
if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
4458+
if (!(aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
44594459
aot_set_last_error("allocate memory failed.");
44604460
goto fail1;
44614461
}
44624462

44634463
memset(aot_file_buf, 0, aot_file_size);
4464-
buf_end = buf + aot_file_size;
4464+
if (!aot_emit_aot_file_buf_ex(comp_ctx, comp_data, obj_data, aot_file_buf,
4465+
aot_file_size))
4466+
goto fail2;
4467+
4468+
*p_aot_file_size = aot_file_size;
4469+
4470+
aot_obj_data_destroy(obj_data);
4471+
return aot_file_buf;
4472+
4473+
fail2:
4474+
wasm_runtime_free(aot_file_buf);
4475+
4476+
fail1:
4477+
aot_obj_data_destroy(obj_data);
4478+
return NULL;
4479+
}
4480+
4481+
bool
4482+
aot_emit_aot_file_buf_ex(AOTCompContext *comp_ctx, AOTCompData *comp_data,
4483+
AOTObjectData *obj_data, uint8 *buf,
4484+
uint32 aot_file_size)
4485+
{
4486+
uint8 *buf_end = buf + aot_file_size;
4487+
uint32 offset = 0;
44654488

44664489
if (!aot_emit_file_header(buf, buf_end, &offset, comp_data, obj_data)
44674490
|| !aot_emit_target_info_section(buf, buf_end, &offset, comp_data,
@@ -4482,28 +4505,18 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
44824505
comp_ctx)
44834506
#endif
44844507
)
4485-
goto fail2;
4508+
return false;
44864509

44874510
#if 0
44884511
dump_buf(buf, offset, "sections");
44894512
#endif
44904513

44914514
if (offset != aot_file_size) {
44924515
aot_set_last_error("emit aot file failed.");
4493-
goto fail2;
4516+
return false;
44944517
}
44954518

4496-
*p_aot_file_size = aot_file_size;
4497-
4498-
aot_obj_data_destroy(obj_data);
4499-
return aot_file_buf;
4500-
4501-
fail2:
4502-
wasm_runtime_free(aot_file_buf);
4503-
4504-
fail1:
4505-
aot_obj_data_destroy(obj_data);
4506-
return NULL;
4519+
return true;
45074520
}
45084521

45094522
bool
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#ifndef _AOT_EMIT_AOT_FILE_H_
7+
#define _AOT_EMIT_AOT_FILE_H_
8+
9+
#include "aot_compiler.h"
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
typedef struct AOTObjectData AOTObjectData;
16+
17+
AOTObjectData *
18+
aot_obj_data_create(AOTCompContext *comp_ctx);
19+
20+
void
21+
aot_obj_data_destroy(AOTObjectData *obj_data);
22+
23+
uint32
24+
aot_get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
25+
AOTObjectData *obj_data);
26+
27+
bool
28+
aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
29+
const char *file_name);
30+
31+
uint8 *
32+
aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
33+
uint32 *p_aot_file_size);
34+
35+
bool
36+
aot_emit_aot_file_buf_ex(AOTCompContext *comp_ctx, AOTCompData *comp_data,
37+
AOTObjectData *obj_data, uint8 *aot_file_buf,
38+
uint32 aot_file_size);
39+
40+
#ifdef __cplusplus
41+
} /* end of extern "C" */
42+
#endif
43+
44+
#endif /* end of _AOT_EMIT_AOT_FILE_H_ */

core/iwasm/include/aot_export.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ typedef struct AOTCompData *aot_comp_data_t;
2727
struct AOTCompContext;
2828
typedef struct AOTCompContext *aot_comp_context_t;
2929

30+
struct AOTObjectData;
31+
typedef struct AOTObjectData *aot_obj_data_t;
32+
3033
aot_comp_data_t
3134
aot_create_comp_data(void *wasm_module, const char *target_arch,
3235
bool gc_enabled);
@@ -62,6 +65,25 @@ aot_destroy_comp_context(aot_comp_context_t comp_ctx);
6265
bool
6366
aot_compile_wasm(aot_comp_context_t comp_ctx);
6467

68+
aot_obj_data_t
69+
aot_obj_data_create(aot_comp_context_t comp_ctx);
70+
71+
void
72+
aot_obj_data_destroy(aot_obj_data_t obj_data);
73+
74+
uint32_t
75+
aot_get_aot_file_size(aot_comp_context_t comp_ctx, aot_comp_data_t comp_data,
76+
aot_obj_data_t obj_data);
77+
78+
uint8_t *
79+
aot_emit_aot_file_buf(aot_comp_context_t comp_ctx, aot_comp_data_t comp_data,
80+
uint32_t *p_aot_file_size);
81+
82+
bool
83+
aot_emit_aot_file_buf_ex(aot_comp_context_t comp_ctx, aot_comp_data_t comp_data,
84+
aot_obj_data_t obj_data, uint8_t *aot_file_buf,
85+
uint32_t aot_file_size);
86+
6587
bool
6688
aot_emit_llvm_file(aot_comp_context_t comp_ctx, const char *file_name);
6789

0 commit comments

Comments
 (0)