Skip to content

Commit 0343aaf

Browse files
Modify AOT static PGO to conform to llvm-18 and add a CI job to test static PGO on the coremark benchmark (#4345)
* static PGO compatible with llvm18 and add CI job to test static PGO on coremark benchmark * update comments and warning info, bitmaps section in llvm profdata shouldn't be used in PGO
1 parent 2fe7105 commit 0343aaf

File tree

7 files changed

+38
-6
lines changed

7 files changed

+38
-6
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3323,7 +3323,7 @@ do_data_relocation(AOTModule *module, AOTRelocationGroup *group,
33233323
uint8 *data_addr;
33243324
uint32 data_size = 0, i;
33253325
AOTRelocation *relocation = group->relocations;
3326-
void *symbol_addr;
3326+
void *symbol_addr = NULL;
33273327
char *symbol, *data_section_name;
33283328

33293329
if (!strncmp(group->section_name, ".rela.", 6)) {

core/iwasm/aot/aot_runtime.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,8 +4877,8 @@ aot_dump_pgo_prof_data_to_buf(AOTModuleInstance *module_inst, char *buf,
48774877
}
48784878

48794879
prof_header.magic = 0xFF6C70726F667281LL;
4880-
/* Version 8 */
4881-
prof_header.version = 0x0000000000000008LL;
4880+
/* Version 9 */
4881+
prof_header.version = 0x0000000000000009LL;
48824882
/* with VARIANT_MASK_IR_PROF (IR Instrumentation) */
48834883
prof_header.version |= 0x1ULL << 56;
48844884
/* with VARIANT_MASK_MEMPROF (Memory Profile) */
@@ -4887,14 +4887,19 @@ aot_dump_pgo_prof_data_to_buf(AOTModuleInstance *module_inst, char *buf,
48874887
prof_header.num_prof_counters = num_prof_counters;
48884888
prof_header.names_size = prof_names_size;
48894889
prof_header.value_kind_last = 1;
4890+
/* __llvm_prf_bits won't be used in PGO, set dummy value here */
4891+
prof_header.num_prof_bitmaps = 0;
4892+
prof_header.bitmap_delta = 0;
48904893

48914894
if (!is_little_endian()) {
48924895
aot_exchange_uint64((uint8 *)&prof_header.magic);
48934896
aot_exchange_uint64((uint8 *)&prof_header.version);
48944897
aot_exchange_uint64((uint8 *)&prof_header.num_prof_data);
48954898
aot_exchange_uint64((uint8 *)&prof_header.num_prof_counters);
4899+
aot_exchange_uint64((uint8 *)&prof_header.num_prof_bitmaps);
48964900
aot_exchange_uint64((uint8 *)&prof_header.names_size);
48974901
aot_exchange_uint64((uint8 *)&prof_header.counters_delta);
4902+
aot_exchange_uint64((uint8 *)&prof_header.bitmap_delta);
48984903
aot_exchange_uint64((uint8 *)&prof_header.value_kind_last);
48994904
}
49004905

@@ -4912,19 +4917,23 @@ aot_dump_pgo_prof_data_to_buf(AOTModuleInstance *module_inst, char *buf,
49124917
prof_data_64->func_md5 = prof_data->func_md5;
49134918
prof_data_64->func_hash = prof_data->func_hash;
49144919
prof_data_64->offset_counters = prof_data->offset_counters;
4920+
prof_data_64->offset_bitmaps = prof_data->offset_bitmaps;
49154921
prof_data_64->func_ptr = prof_data->func_ptr;
49164922
prof_data_64->values = (uint64)(uintptr_t)prof_data->values;
49174923
prof_data_64->num_counters = prof_data->num_counters;
4924+
/* __llvm_prf_bits won't be used in PGO, set dummy value here */
4925+
prof_data_64->num_bitmaps = 0;
49184926
prof_data_64->num_value_sites[0] = prof_data->num_value_sites[0];
49194927
prof_data_64->num_value_sites[1] = prof_data->num_value_sites[1];
49204928

49214929
if (!is_little_endian()) {
49224930
aot_exchange_uint64((uint8 *)&prof_data_64->func_hash);
49234931
aot_exchange_uint64((uint8 *)&prof_data_64->offset_counters);
4924-
aot_exchange_uint64((uint8 *)&prof_data_64->offset_counters);
4932+
aot_exchange_uint64((uint8 *)&prof_data_64->offset_bitmaps);
49254933
aot_exchange_uint64((uint8 *)&prof_data_64->func_ptr);
49264934
aot_exchange_uint64((uint8 *)&prof_data_64->values);
49274935
aot_exchange_uint32((uint8 *)&prof_data_64->num_counters);
4936+
aot_exchange_uint32((uint8 *)&prof_data_64->num_bitmaps);
49284937
aot_exchange_uint16((uint8 *)&prof_data_64->num_value_sites[0]);
49294938
aot_exchange_uint16((uint8 *)&prof_data_64->num_value_sites[1]);
49304939
}

core/iwasm/aot/aot_runtime.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ typedef struct AOTFrame {
437437
} AOTFrame;
438438

439439
#if WASM_ENABLE_STATIC_PGO != 0
440+
/* The bitmaps fields in LLVMProfileRawHeader, LLVMProfileData,
441+
* LLVMProfileData_64 all dummy fields, it's used in MC/DC code coverage
442+
* instead of PGO. See https://llvm.org/docs/InstrProfileFormat.html#bitmap */
440443
typedef struct LLVMProfileRawHeader {
441444
uint64 magic;
442445
uint64 version;
@@ -445,8 +448,11 @@ typedef struct LLVMProfileRawHeader {
445448
uint64 padding_bytes_before_counters;
446449
uint64 num_prof_counters;
447450
uint64 padding_bytes_after_counters;
451+
uint64 num_prof_bitmaps;
452+
uint64 padding_bytes_after_bitmaps;
448453
uint64 names_size;
449454
uint64 counters_delta;
455+
uint64 bitmap_delta;
450456
uint64 names_delta;
451457
uint64 value_kind_last;
452458
} LLVMProfileRawHeader;
@@ -464,10 +470,12 @@ typedef struct LLVMProfileData {
464470
uint64 func_md5;
465471
uint64 func_hash;
466472
uint64 offset_counters;
473+
uint64 offset_bitmaps;
467474
uintptr_t func_ptr;
468475
ValueProfNode **values;
469476
uint32 num_counters;
470477
uint16 num_value_sites[2];
478+
uint32 num_bitmaps;
471479
} LLVMProfileData;
472480

473481
/* The profiling data for writing to the output file, the width of
@@ -477,10 +485,12 @@ typedef struct LLVMProfileData_64 {
477485
uint64 func_md5;
478486
uint64 func_hash;
479487
uint64 offset_counters;
488+
uint64 offset_bitmaps;
480489
uint64 func_ptr;
481490
uint64 values;
482491
uint32 num_counters;
483492
uint16 num_value_sites[2];
493+
uint32 num_bitmaps;
484494
} LLVMProfileData_64;
485495
#endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
486496

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,6 +3378,12 @@ aot_resolve_object_data_sections(AOTObjectData *obj_data)
33783378
bh_memcpy_s(data_section->name, size, buf, size);
33793379
data_section->is_name_allocated = true;
33803380
}
3381+
else if (obj_data->comp_ctx->enable_llvm_pgo
3382+
&& !strcmp(name, "__llvm_prf_bits")) {
3383+
LOG_WARNING("__llvm_prf_bits section is not supported and "
3384+
"shouldn't be used in PGO.");
3385+
return false;
3386+
}
33813387

33823388
if (obj_data->comp_ctx->enable_llvm_pgo
33833389
&& !strcmp(name, "__llvm_prf_names")) {

tests/benchmarks/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Refer to the `README.md` under each folder for how to build and run the benchmar
88

99
## Install `llvm-profdata`
1010

11+
> PS: the `llvm-profdata` vesion needs to be the same major version with llvm libraries used to build wamrc.
12+
1113
The tool `llvm-profdata` is used when running the `test_pgo.sh` script under the benchmark folder. There are two ways to install it:
1214

1315
1. Refer to https://apt.llvm.org/, e.g. in Ubuntu 20.04, add lines below to /etc/apt/source.list
@@ -18,19 +20,22 @@ deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main
1820
# 15
1921
deb http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main
2022
deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main
23+
# 18
24+
deb http://apt.llvm.org/focal/ llvm-toolchain-focal-18 main
25+
deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-18 main
2126
```
2227

2328
Then run `sudo apt update`, `sudo apt install llvm`. And after installing:
2429

2530
```bash
2631
cd /usr/bin
27-
sudo ln -s llvm-profdata-15 llvm-profdata
32+
sudo ln -s llvm-profdata-18 llvm-profdata
2833
```
2934

3035
2. Build manually
3136

3237
```bash
33-
git clone --depth 1 --branch release/15.x https://github.com/llvm/llvm-project.git
38+
git clone --depth 1 --branch release/18.x https://github.com/llvm/llvm-project.git
3439
cd llvm-project
3540
mkdir build && cd build
3641
cmake ../llvm \

tests/benchmarks/coremark/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Copyright (C) 2019 Intel Corporation. All rights reserved.
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
set -e
56

67
PLATFORM=$(uname -s | tr A-Z a-z)
78

tests/benchmarks/coremark/test_pgo.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Copyright (C) 2019 Intel Corporation. All rights reserved.
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
set -e
56

67
PLATFORM=$(uname -s | tr A-Z a-z)
78

0 commit comments

Comments
 (0)