Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 8ff3714

Browse files
authored
refactor: Fix leak memory issues (#91)
* refactor: Fix leak memory issues * wip refactor some code * move smart pointer from the codegen visitors to new class * Fix production files * fix remaining errors
1 parent 97de2d5 commit 8ff3714

19 files changed

+424
-349
lines changed

.makim.yaml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ groups:
2525
help: Build Arx for release
2626
env:
2727
ASAN_OPTIONS: '{{ args.asan_options }}'
28+
LSAN_OPTIONS: '{{ args.lsan_options }}'
2829
args:
2930
asan-options:
3031
help: |
3132
Define a custom value for the environment variable
3233
`ASAN_OPTIONS`
3334
type: string
3435
default: ''
36+
lsan-options:
37+
help: |
38+
Define a custom value for the environment variable
39+
`LSAN_OPTIONS`
40+
type: string
41+
default: ''
3542
build-type:
3643
help: |
3744
Define the build type, options are:
@@ -49,6 +56,7 @@ groups:
4956
dependencies:
5057
- target: build.clean
5158
if: {{ args.clean }}
59+
shell: bash
5260
run: |
5361
meson setup \
5462
--prefix $CONDA_PREFIX \
@@ -59,6 +67,22 @@ groups:
5967
build .
6068
meson compile -C build
6169
70+
debug:
71+
help: Build for debugging (+debug)
72+
args:
73+
clean:
74+
help: Clean temporary files before the building step
75+
type: bool
76+
action: store_true
77+
dependencies:
78+
- target: build.release
79+
args:
80+
build-type: "debug"
81+
extras: "-Db_coverage=true -Doptimization=0" # -Db_sanitize=address
82+
clean: {{ args.clean }}
83+
asan-options: "fast_unwind_on_malloc=0:verbosity=2:log_threads=1"
84+
lsan-options: "fast_unwind_on_malloc=0:verbosity=2:log_threads=1"
85+
6286
dev:
6387
help: Build for development (+tests +debug)
6488
args:
@@ -70,9 +94,10 @@ groups:
7094
- target: build.release
7195
args:
7296
build-type: "debug"
73-
extras: "-Ddev=enabled -Db_coverage=true -Doptimization=0"
97+
extras: "-Ddev=enabled -Db_coverage=true -Doptimization=0" # -Db_sanitize=address
7498
clean: {{ args.clean }}
75-
asan-options: "fast_unwind_on_malloc=0"
99+
asan-options: "fast_unwind_on_malloc=0:verbosity=2:log_threads=1"
100+
lsan-options: "fast_unwind_on_malloc=0:verbosity=2:log_threads=1"
76101

77102
env:
78103
targets:
@@ -82,7 +107,6 @@ groups:
82107
touch .env
83108
echo -n @("HOST_UID=" + $(id -u) + "HOST_GID=" + $(id -g)) > .env
84109
85-
86110
conda:
87111
targets:
88112
build:
@@ -92,7 +116,6 @@ groups:
92116
conda build purge
93117
conda mambabuild .
94118
95-
96119
release:
97120
vars:
98121
app: |
@@ -178,6 +201,10 @@ groups:
178201
179202
GDB=""
180203
DEBUG_FLAGS=""
204+
# another options: halt_on_error=true
205+
# it could be a useful flag: -fsanitize-recover
206+
export LSAN_OPTIONS=verbosity=2:log_threads=1
207+
export ASAN_OPTIONS=verbosity=2:check_initialization_order=1:detect_leaks=1
181208
182209
if [[ "{{ args.debug }}" == "True" ]]; then
183210
GDB="gdb --args"

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ inc = include_directories('./src')
4747
SRC_PATH = PROJECT_PATH + '/src'
4848

4949
project_src_files = files(
50+
SRC_PATH + '/codegen/arx-llvm.cpp',
5051
SRC_PATH + '/codegen/ast-to-llvm-ir.cpp',
5152
SRC_PATH + '/codegen/ast-to-object.cpp',
5253
SRC_PATH + '/codegen/ast-to-stdout.cpp',

src/codegen/arx-llvm.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include <llvm/IR/DIBuilder.h> // for DIBuilder
3+
#include <llvm/IR/IRBuilder.h> // for IRBuilder
4+
#include <llvm/IR/Module.h> // for Module
5+
#include <llvm/Support/Error.h> // for ExitOnError
6+
7+
#include "codegen/arx-llvm.h" // for ArxLLVM
8+
#include "codegen/jit.h" // for ArxJIT
9+
#include "parser.h" // for ArxJIT
10+
11+
std::unique_ptr<llvm::LLVMContext> ArxLLVM::context;
12+
std::unique_ptr<llvm::Module> ArxLLVM::module;
13+
std::unique_ptr<llvm::IRBuilder<>> ArxLLVM::ir_builder;
14+
std::unique_ptr<llvm::DIBuilder> ArxLLVM::di_builder;
15+
std::unique_ptr<llvm::orc::ArxJIT> ArxLLVM::jit;
16+
17+
std::map<std::string, llvm::AllocaInst*> ArxLLVM::named_values;
18+
std::map<std::string, std::unique_ptr<PrototypeAST>> ArxLLVM::function_protos;
19+
20+
/* Data types */
21+
llvm::Type* ArxLLVM::DOUBLE_TYPE;
22+
llvm::Type* ArxLLVM::FLOAT_TYPE;
23+
llvm::Type* ArxLLVM::INT8_TYPE;
24+
llvm::Type* ArxLLVM::INT32_TYPE;

src/codegen/arx-llvm.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <llvm/IR/DIBuilder.h> // for DIBuilder
4+
#include <llvm/IR/IRBuilder.h> // for IRBuilder
5+
#include <llvm/IR/Module.h> // for Module
6+
#include <llvm/Support/Error.h> // for ExitOnError
7+
8+
#include "codegen/jit.h" // for ArxJIT
9+
#include "parser.h" // for ArxJIT
10+
11+
class ArxLLVM {
12+
public:
13+
static std::unique_ptr<llvm::LLVMContext> context;
14+
static std::unique_ptr<llvm::Module> module;
15+
static std::unique_ptr<llvm::IRBuilder<>> ir_builder;
16+
static std::unique_ptr<llvm::DIBuilder> di_builder;
17+
static std::unique_ptr<llvm::orc::ArxJIT> jit;
18+
19+
static std::map<std::string, llvm::AllocaInst*> named_values;
20+
static std::map<std::string, std::unique_ptr<PrototypeAST>> function_protos;
21+
22+
/* Data types */
23+
static llvm::Type* DOUBLE_TYPE;
24+
static llvm::Type* FLOAT_TYPE;
25+
static llvm::Type* INT8_TYPE;
26+
static llvm::Type* INT32_TYPE;
27+
};

0 commit comments

Comments
 (0)