Skip to content

Commit 8beb328

Browse files
authored
Seeded random.random() with system clock if it is left unseeded (#2549)
1 parent 85ced05 commit 8beb328

27 files changed

+48
-40
lines changed

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,19 +3034,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
30343034
}
30353035
builder->SetInsertPoint(BB);
30363036

3037-
// Call the `_lpython_set_argv` function to assign command line argument
3038-
// values to `argc` and `argv`.
3037+
// Call the `_lpython_call_initial_functions` function to assign command line argument
3038+
// values to `argc` and `argv`, and set the random seed to the system clock.
30393039
{
30403040
if (compiler_options.emit_debug_info) debug_emit_loc(x);
3041-
llvm::Function *fn = module->getFunction("_lpython_set_argv");
3041+
llvm::Function *fn = module->getFunction("_lpython_call_initial_functions");
30423042
if(!fn) {
30433043
llvm::FunctionType *function_type = llvm::FunctionType::get(
30443044
llvm::Type::getVoidTy(context), {
30453045
llvm::Type::getInt32Ty(context),
30463046
character_type->getPointerTo()
30473047
}, false);
30483048
fn = llvm::Function::Create(function_type,
3049-
llvm::Function::ExternalLinkage, "_lpython_set_argv", *module);
3049+
llvm::Function::ExternalLinkage, "_lpython_call_initial_functions", *module);
30503050
}
30513051
std::vector<llvm::Value *> args;
30523052
for (llvm::Argument &llvm_arg : F->args()) {

src/libasr/runtime/lfortran_intrinsics.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,13 @@ LFORTRAN_API char *_lpython_get_argv(int32_t index) {
25452545

25462546
// << Command line arguments << ------------------------------------------------
25472547

2548+
// Initial setup
2549+
LFORTRAN_API void _lpython_call_initial_functions(int32_t argc_1, char *argv_1[]) {
2550+
_lpython_set_argv(argc_1, argv_1);
2551+
_lfortran_init_random_clock();
2552+
}
2553+
// << Initial setup << ---------------------------------------------------------
2554+
25482555
// >> Runtime Stacktrace >> ----------------------------------------------------
25492556
#ifdef HAVE_RUNTIME_STACKTRACE
25502557
#ifdef HAVE_LFORTRAN_UNWIND

src/libasr/runtime/lfortran_intrinsics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ LFORTRAN_API int32_t _lfortran_all(bool *mask, int32_t n);
277277
LFORTRAN_API void _lpython_set_argv(int32_t argc_1, char *argv_1[]);
278278
LFORTRAN_API int32_t _lpython_get_argc();
279279
LFORTRAN_API char *_lpython_get_argv(int32_t index);
280+
LFORTRAN_API void _lpython_call_initial_functions(int32_t argc_1, char *argv_1[]);
280281
LFORTRAN_API void print_stacktrace_addresses(char *filename, bool use_colors);
281282
LFORTRAN_API char *_lfortran_get_env_variable(char *name);
282283
LFORTRAN_API int _lfortran_exec_command(char *cmd);

tests/reference/llvm-assert1-8df4f31.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-assert1-8df4f31.stdout",
9-
"stdout_hash": "15764832892f7e0cd2a438b247a148564f5f83b3a00ca929bd9cc625",
9+
"stdout_hash": "de8886bdb5b86970a042b8ee00f309cf6bcf9e3e192cb4875ab3b2b0",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/llvm-assert1-8df4f31.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source_filename = "LFortran"
33

44
define i32 @main(i32 %0, i8** %1) {
55
.entry:
6-
call void @_lpython_set_argv(i32 %0, i8** %1)
6+
call void @_lpython_call_initial_functions(i32 %0, i8** %1)
77
ret i32 0
88
}
99

10-
declare void @_lpython_set_argv(i32, i8**)
10+
declare void @_lpython_call_initial_functions(i32, i8**)

tests/reference/llvm-bindc_01-c984f09.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-bindc_01-c984f09.stdout",
9-
"stdout_hash": "36302aceabcf2682f3e954ab4bc56dd960fe532dc7799cb58baa19a3",
9+
"stdout_hash": "054106835033c19d6d0b10c264d915a4d4b11fee2a70abd6d66bade3",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/llvm-bindc_01-c984f09.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ declare void @exit(i32)
7373

7474
define i32 @main(i32 %0, i8** %1) {
7575
.entry:
76-
call void @_lpython_set_argv(i32 %0, i8** %1)
76+
call void @_lpython_call_initial_functions(i32 %0, i8** %1)
7777
call void @__module___main_____main__global_stmts()
7878
ret i32 0
7979
}
8080

81-
declare void @_lpython_set_argv(i32, i8**)
81+
declare void @_lpython_call_initial_functions(i32, i8**)

tests/reference/llvm-bool1-af4376b.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-bool1-af4376b.stdout",
9-
"stdout_hash": "cb60cdc2beb220bbe693b54079b5ebc7efcdc2cdc830c3cc0e213c39",
9+
"stdout_hash": "7c68133cc3f970d6eddca6cf982fb405b5a1c8014ef5aa916ea38cf6",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/llvm-bool1-af4376b.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ declare void @_lfortran_printf(i8*, ...)
5656

5757
define i32 @main(i32 %0, i8** %1) {
5858
.entry:
59-
call void @_lpython_set_argv(i32 %0, i8** %1)
59+
call void @_lpython_call_initial_functions(i32 %0, i8** %1)
6060
call void @__module___main_____main__global_stmts()
6161
ret i32 0
6262
}
6363

64-
declare void @_lpython_set_argv(i32, i8**)
64+
declare void @_lpython_call_initial_functions(i32, i8**)

tests/reference/llvm-expr14-b96b5b1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-expr14-b96b5b1.stdout",
9-
"stdout_hash": "15764832892f7e0cd2a438b247a148564f5f83b3a00ca929bd9cc625",
9+
"stdout_hash": "de8886bdb5b86970a042b8ee00f309cf6bcf9e3e192cb4875ab3b2b0",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)