Skip to content

Commit 82e9118

Browse files
committed
Mark variables as initialized
The compiler often cannot see through the tangle of code to make 100% correct deduction about whether a variable is initialized or not. It usually errs on the side of caution and emits a warning. One example is in codegen.cpp:emit_function where the variable SP is only ever used when at the same time ctx.debug_enabled is set to true. The compiler (at least gcc 5.x) doesn't keep track of this relationship. From my experience this kind of problem often appears in large code bases which is why I'm kind of surprised I couldn't find a framework to handle them. This is why I'm proposing the following change. Instead of adding one kludgy workaround here a macro JL_MARK_INITIALIZED is introduced. When correctly defined (as I do it for gcc) this macro creates no additional cost in the generated code while preventing the warning. Definitions for other compilers should be added appropriately.
1 parent 62b7d31 commit 82e9118

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

src/codegen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4703,6 +4703,7 @@ static Function *emit_function(jl_lambda_info_t *lam)
47034703
ctx.debug_enabled = false;
47044704
do_coverage = false;
47054705
do_malloc_log = false;
4706+
JL_MARK_INITIALIZED(SP);
47064707
}
47074708
else {
47084709
// TODO: Fix when moving to new LLVM version

src/julia.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ extern "C" {
4343
#if defined(__GNUC__)
4444
# define JL_NORETURN __attribute__ ((noreturn))
4545
# define JL_CONST_FUNC __attribute__((const))
46+
# define JL_MARK_INITIALIZED(var) asm("" : "=rm" (var))
4647
#elif defined(_COMPILER_MICROSOFT_)
4748
# define JL_NORETURN __declspec(noreturn)
4849
// This is the closest I can find for __attribute__((const))
4950
# define JL_CONST_FUNC __declspec(noalias)
51+
# define JL_MARK_INITIALIZED(var) do {} while (0)
5052
#else
5153
# define JL_NORETURN
5254
# define JL_CONST_FUNC
55+
# define JL_MARK_INITIALIZED(var) do {} while (0)
5356
#endif
5457

5558
#define container_of(ptr, type, member) \

0 commit comments

Comments
 (0)