Skip to content

Commit

Permalink
[mono][llvm] Run a few llvm passes on the emitted methods to reduce m…
Browse files Browse the repository at this point in the history
…emory usage. (#97744)

Extracted from #97096.

Author: Johan Lorensson <lateralusx.github@gmail.com>
  • Loading branch information
vargaz authored Feb 1, 2024
1 parent e719462 commit 1e8f741
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
#define __STDC_CONSTANT_MACROS
#endif

#include "llvm-c/Core.h"
#include "llvm-c/BitWriter.h"
#include "llvm-c/Analysis.h"
#include "llvm-c/Transforms/InstCombine.h"
#include "llvm-c/Transforms/Scalar.h"
#include "llvm-c/Transforms/IPO.h"

#include "mini-llvm-cpp.h"
#include "llvm-jit.h"
Expand Down Expand Up @@ -138,6 +142,7 @@ typedef struct {
GHashTable *no_method_table_lmethods;
GHashTable *intrins_id_to_intrins;
GHashTable *intrins_id_to_type;
LLVMPassManagerRef func_pass_manager;
} MonoLLVMModule;

/*
Expand Down Expand Up @@ -13337,6 +13342,11 @@ emit_method_inner (EmitContext *ctx)
#endif
} else {
//LLVMVerifyFunction (method, 0);
if (ctx->module->func_pass_manager) {
LLVMInitializeFunctionPassManager (ctx->module->func_pass_manager);
LLVMRunFunctionPassManager(ctx->module->func_pass_manager, ctx->lmethod);
LLVMFinalizeFunctionPassManager (ctx->module->func_pass_manager);
}
llvm_jit_finalize_method (ctx);
}

Expand Down Expand Up @@ -13856,13 +13866,22 @@ mono_llvm_create_aot_module (MonoAssembly *assembly, const char *global_prefix,
LLVMSetLinkage (module->sentinel_exception, LLVMExternalLinkage);
mono_llvm_set_is_constant (module->sentinel_exception);
}

module->func_pass_manager = LLVMCreateFunctionPassManagerForModule (module->lmodule);
if (module->func_pass_manager) {
LLVMAddCFGSimplificationPass (module->func_pass_manager);
LLVMAddInstructionCombiningPass (module->func_pass_manager);
}
}

void
mono_llvm_free_aot_module (void)
{
MonoLLVMModule *module = &aot_module;

if (module->func_pass_manager)
LLVMDisposePassManager (module->func_pass_manager);

if (module->lmodule)
LLVMDisposeModule (module->lmodule);

Expand Down

0 comments on commit 1e8f741

Please # to comment.