Skip to content

Commit 1e8f741

Browse files
authored
[mono][llvm] Run a few llvm passes on the emitted methods to reduce memory usage. (#97744)
Extracted from #97096. Author: Johan Lorensson <lateralusx.github@gmail.com>
1 parent e719462 commit 1e8f741

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/mono/mono/mini/mini-llvm.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
#define __STDC_CONSTANT_MACROS
3030
#endif
3131

32+
#include "llvm-c/Core.h"
3233
#include "llvm-c/BitWriter.h"
3334
#include "llvm-c/Analysis.h"
35+
#include "llvm-c/Transforms/InstCombine.h"
36+
#include "llvm-c/Transforms/Scalar.h"
37+
#include "llvm-c/Transforms/IPO.h"
3438

3539
#include "mini-llvm-cpp.h"
3640
#include "llvm-jit.h"
@@ -138,6 +142,7 @@ typedef struct {
138142
GHashTable *no_method_table_lmethods;
139143
GHashTable *intrins_id_to_intrins;
140144
GHashTable *intrins_id_to_type;
145+
LLVMPassManagerRef func_pass_manager;
141146
} MonoLLVMModule;
142147

143148
/*
@@ -13337,6 +13342,11 @@ emit_method_inner (EmitContext *ctx)
1333713342
#endif
1333813343
} else {
1333913344
//LLVMVerifyFunction (method, 0);
13345+
if (ctx->module->func_pass_manager) {
13346+
LLVMInitializeFunctionPassManager (ctx->module->func_pass_manager);
13347+
LLVMRunFunctionPassManager(ctx->module->func_pass_manager, ctx->lmethod);
13348+
LLVMFinalizeFunctionPassManager (ctx->module->func_pass_manager);
13349+
}
1334013350
llvm_jit_finalize_method (ctx);
1334113351
}
1334213352

@@ -13856,13 +13866,22 @@ mono_llvm_create_aot_module (MonoAssembly *assembly, const char *global_prefix,
1385613866
LLVMSetLinkage (module->sentinel_exception, LLVMExternalLinkage);
1385713867
mono_llvm_set_is_constant (module->sentinel_exception);
1385813868
}
13869+
13870+
module->func_pass_manager = LLVMCreateFunctionPassManagerForModule (module->lmodule);
13871+
if (module->func_pass_manager) {
13872+
LLVMAddCFGSimplificationPass (module->func_pass_manager);
13873+
LLVMAddInstructionCombiningPass (module->func_pass_manager);
13874+
}
1385913875
}
1386013876

1386113877
void
1386213878
mono_llvm_free_aot_module (void)
1386313879
{
1386413880
MonoLLVMModule *module = &aot_module;
1386513881

13882+
if (module->func_pass_manager)
13883+
LLVMDisposePassManager (module->func_pass_manager);
13884+
1386613885
if (module->lmodule)
1386713886
LLVMDisposeModule (module->lmodule);
1386813887

0 commit comments

Comments
 (0)