From 44f4b4f06289f9c6e4ea80749fa2e9db41839215 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Tue, 4 Jul 2023 12:20:52 +0800 Subject: [PATCH] Add "--enable-llvm-passes=" option to wamrc (#2335) Add "--enable-llvm-passes=" option to wamrc for customizing LLVM passes --- core/iwasm/compilation/aot_llvm.c | 3 +++ core/iwasm/compilation/aot_llvm.h | 2 ++ core/iwasm/compilation/aot_llvm_extra.cpp | 4 ++++ core/iwasm/include/aot_export.h | 1 + wamr-compiler/main.c | 7 +++++++ 5 files changed, 17 insertions(+) diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 89b11d77..5c58eb6b 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -2296,6 +2296,9 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) if (option->enable_stack_estimation) comp_ctx->enable_stack_estimation = true; + if (option->llvm_passes) + comp_ctx->llvm_passes = option->llvm_passes; + comp_ctx->opt_level = option->opt_level; comp_ctx->size_level = option->size_level; diff --git a/core/iwasm/compilation/aot_llvm.h b/core/iwasm/compilation/aot_llvm.h index 2f187b69..d8808a20 100644 --- a/core/iwasm/compilation/aot_llvm.h +++ b/core/iwasm/compilation/aot_llvm.h @@ -417,6 +417,7 @@ typedef struct AOTCompContext { const char *stack_usage_file; char stack_usage_temp_file[64]; + const char *llvm_passes; } AOTCompContext; enum { @@ -455,6 +456,7 @@ typedef struct AOTCompOption { char **custom_sections; uint32 custom_sections_count; const char *stack_usage_file; + const char *llvm_passes; } AOTCompOption, *aot_comp_option_t; bool diff --git a/core/iwasm/compilation/aot_llvm_extra.cpp b/core/iwasm/compilation/aot_llvm_extra.cpp index 10903940..47568d54 100644 --- a/core/iwasm/compilation/aot_llvm_extra.cpp +++ b/core/iwasm/compilation/aot_llvm_extra.cpp @@ -373,6 +373,10 @@ aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module) MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + if (comp_ctx->llvm_passes) { + ExitOnErr(PB.parsePassPipeline(MPM, comp_ctx->llvm_passes)); + } + if (!disable_llvm_lto) { /* Apply LTO for AOT mode */ if (comp_ctx->comp_data->func_count >= 10 diff --git a/core/iwasm/include/aot_export.h b/core/iwasm/include/aot_export.h index dca26aa6..99b27577 100644 --- a/core/iwasm/include/aot_export.h +++ b/core/iwasm/include/aot_export.h @@ -67,6 +67,7 @@ typedef struct AOTCompOption { char **custom_sections; uint32_t custom_sections_count; const char *stack_usage_file; + const char *llvm_passes; } AOTCompOption, *aot_comp_option_t; bool diff --git a/wamr-compiler/main.c b/wamr-compiler/main.c index 37d65952..999c3549 100644 --- a/wamr-compiler/main.c +++ b/wamr-compiler/main.c @@ -70,6 +70,8 @@ print_help() printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n"); printf(" --disable-llvm-lto Disable the LLVM link time optimization\n"); printf(" --enable-llvm-pgo Enable LLVM PGO (Profile-Guided Optimization)\n"); + printf(" --enable-llvm-passes=\n"); + printf(" Enable the specified LLVM passes, using comma to separate\n"); printf(" --use-prof-file= Use profile file collected by LLVM PGO (Profile-Guided Optimization)\n"); printf(" --enable-segue[=] Enable using segment register GS as the base address of linear memory,\n"); printf(" only available on linux/linux-sgx x86-64, which may improve performance,\n"); @@ -342,6 +344,11 @@ main(int argc, char *argv[]) else if (!strcmp(argv[0], "--enable-llvm-pgo")) { option.enable_llvm_pgo = true; } + else if (!strncmp(argv[0], "--enable-llvm-passes=", 21)) { + if (argv[0][21] == '\0') + PRINT_HELP_AND_EXIT(); + option.llvm_passes = argv[0] + 21; + } else if (!strncmp(argv[0], "--use-prof-file=", 16)) { if (argv[0][16] == '\0') PRINT_HELP_AND_EXIT();