From ee8a29e85c2c49b3802e6bf8a4bd2eabf52c7c09 Mon Sep 17 00:00:00 2001 From: advik Date: Wed, 24 Jul 2024 18:05:40 +0530 Subject: [PATCH 1/2] Use builder0 for variable declaration --- src/libasr/codegen/asr_to_llvm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 407be4083b..5ae8e4b2c1 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -3541,7 +3541,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor template void declare_vars(const T &x, bool create_vtabs=true) { - get_builder0() llvm::Value *target_var; uint32_t debug_arg_count = 0; std::vector var_order = ASRUtils::determine_variable_declaration_order(x.m_symtab); @@ -3666,7 +3665,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Constant *init_value = llvm::Constant::getNullValue(type); gptr->setInitializer(init_value); } else { - ptr = builder->CreateAlloca(type, array_size, v->m_name); + get_builder0() + ptr = builder0.CreateAlloca(type, array_size, v->m_name); } } set_pointer_variable_to_null(llvm::ConstantPointerNull::get( From 89261214940154e21a18e25fc6a93aa61fcc5551 Mon Sep 17 00:00:00 2001 From: advik Date: Thu, 25 Jul 2024 20:30:48 +0530 Subject: [PATCH 2/2] Update tests --- tests/reference/llvm-expr_01-54467c1.json | 2 +- tests/reference/llvm-expr_01-54467c1.stdout | 6 +- .../llvm-func_inline_01-2d4583a.json | 2 +- .../llvm-func_inline_01-2d4583a.stdout | 2 +- .../llvm-test_unary_op_03-046fb86.json | 2 +- .../llvm-test_unary_op_03-046fb86.stdout | 2 +- tests/reference/llvm_dbg-expr_01-9fc5f30.json | 2 +- .../reference/llvm_dbg-expr_01-9fc5f30.stdout | 67 +++++++++---------- 8 files changed, 42 insertions(+), 43 deletions(-) diff --git a/tests/reference/llvm-expr_01-54467c1.json b/tests/reference/llvm-expr_01-54467c1.json index 2c12ec9785..2551b3e498 100644 --- a/tests/reference/llvm-expr_01-54467c1.json +++ b/tests/reference/llvm-expr_01-54467c1.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "llvm-expr_01-54467c1.stdout", - "stdout_hash": "951a792984bf209b37aafc95435eb319906b5c9cb22a62a018556f06", + "stdout_hash": "959f37d41e451c0bbd728144640ad29bad8eb6dfbaf71e1bd2d45b41", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/llvm-expr_01-54467c1.stdout b/tests/reference/llvm-expr_01-54467c1.stdout index e4b4598e72..cdb1c175cd 100644 --- a/tests/reference/llvm-expr_01-54467c1.stdout +++ b/tests/reference/llvm-expr_01-54467c1.stdout @@ -16,10 +16,10 @@ return: ; preds = %.entry define void @__module___main___main0() { .entry: - %x = alloca i32, align 4 - %x2 = alloca i64, align 8 - %y = alloca float, align 4 %y2 = alloca double, align 8 + %y = alloca float, align 4 + %x2 = alloca i64, align 8 + %x = alloca i32, align 4 store i32 25, i32* %x, align 4 %0 = load i32, i32* %x, align 4 call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)) diff --git a/tests/reference/llvm-func_inline_01-2d4583a.json b/tests/reference/llvm-func_inline_01-2d4583a.json index 60381a81be..51220350bd 100644 --- a/tests/reference/llvm-func_inline_01-2d4583a.json +++ b/tests/reference/llvm-func_inline_01-2d4583a.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "llvm-func_inline_01-2d4583a.stdout", - "stdout_hash": "3d0670fb3a5502d046ad0acaa0b5d0f06cd05285df45bef0eca0b626", + "stdout_hash": "1c8552bcbbcba5edca127739cb9b5bb01968019462c909cee8aa26ad", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/llvm-func_inline_01-2d4583a.stdout b/tests/reference/llvm-func_inline_01-2d4583a.stdout index 9c38b8e90a..ee20936121 100644 --- a/tests/reference/llvm-func_inline_01-2d4583a.stdout +++ b/tests/reference/llvm-func_inline_01-2d4583a.stdout @@ -58,8 +58,8 @@ return: ; preds = %unreachable_after_r define void @__module___main____xx_lcompilers_changed_main_xx() { .entry: - %ans = alloca i64, align 8 %x = alloca i64, align 8 + %ans = alloca i64, align 8 store i64 40, i64* %x, align 4 %0 = call i64 @__module___main___fib(i64* %x) store i64 %0, i64* %ans, align 4 diff --git a/tests/reference/llvm-test_unary_op_03-046fb86.json b/tests/reference/llvm-test_unary_op_03-046fb86.json index 800d513680..1fd1e3df9a 100644 --- a/tests/reference/llvm-test_unary_op_03-046fb86.json +++ b/tests/reference/llvm-test_unary_op_03-046fb86.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "llvm-test_unary_op_03-046fb86.stdout", - "stdout_hash": "8ae64ade26bcceb59c961b87142054ab443d47b4467569b01d9d24f9", + "stdout_hash": "64ad587abac8ae2bc570c04dc969a26374e4abcba7a02f051325c816", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/llvm-test_unary_op_03-046fb86.stdout b/tests/reference/llvm-test_unary_op_03-046fb86.stdout index 7774aa0a9a..859d985071 100644 --- a/tests/reference/llvm-test_unary_op_03-046fb86.stdout +++ b/tests/reference/llvm-test_unary_op_03-046fb86.stdout @@ -15,8 +15,8 @@ return: ; preds = %.entry define void @__module___main___f() { .entry: - %i = alloca i32, align 4 %res = alloca i32, align 4 + %i = alloca i32, align 4 store i32 5, i32* %i, align 4 %0 = load i32, i32* %i, align 4 %1 = xor i32 %0, -1 diff --git a/tests/reference/llvm_dbg-expr_01-9fc5f30.json b/tests/reference/llvm_dbg-expr_01-9fc5f30.json index 649301a2b9..c66eee2bb2 100644 --- a/tests/reference/llvm_dbg-expr_01-9fc5f30.json +++ b/tests/reference/llvm_dbg-expr_01-9fc5f30.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "llvm_dbg-expr_01-9fc5f30.stdout", - "stdout_hash": "70643017f0ad204393988f111369cdd921c1c297149078182707cb54", + "stdout_hash": "8270bd08ebf0a1ff9ee16359845337e4d39c2affdf7c5c10eb213c9b", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/llvm_dbg-expr_01-9fc5f30.stdout b/tests/reference/llvm_dbg-expr_01-9fc5f30.stdout index 405da5aad9..f9149d853d 100644 --- a/tests/reference/llvm_dbg-expr_01-9fc5f30.stdout +++ b/tests/reference/llvm_dbg-expr_01-9fc5f30.stdout @@ -16,21 +16,21 @@ return: ; preds = %.entry define void @__module___main___main0() !dbg !7 { .entry: - %x = alloca i32, align 4, !dbg !8 - call void @llvm.dbg.declare(metadata i32* %x, metadata !9, metadata !DIExpression()), !dbg !11 - %x2 = alloca i64, align 8 - call void @llvm.dbg.declare(metadata i64* %x2, metadata !12, metadata !DIExpression()), !dbg !14 - %y = alloca float, align 4 - call void @llvm.dbg.declare(metadata float* %y, metadata !15, metadata !DIExpression()), !dbg !17 %y2 = alloca double, align 8 - call void @llvm.dbg.declare(metadata double* %y2, metadata !18, metadata !DIExpression()), !dbg !20 - store i32 25, i32* %x, align 4, !dbg !21 - %0 = load i32, i32* %x, align 4, !dbg !21 - call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)), !dbg !21 - br label %return, !dbg !21 + %y = alloca float, align 4 + %x2 = alloca i64, align 8 + %x = alloca i32, align 4 + call void @llvm.dbg.declare(metadata i32* %x, metadata !8, metadata !DIExpression()), !dbg !10 + call void @llvm.dbg.declare(metadata i64* %x2, metadata !11, metadata !DIExpression()), !dbg !13 + call void @llvm.dbg.declare(metadata float* %y, metadata !14, metadata !DIExpression()), !dbg !16 + call void @llvm.dbg.declare(metadata double* %y2, metadata !17, metadata !DIExpression()), !dbg !19 + store i32 25, i32* %x, align 4, !dbg !20 + %0 = load i32, i32* %x, align 4, !dbg !20 + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)), !dbg !20 + br label %return, !dbg !20 return: ; preds = %.entry - ret void, !dbg !21 + ret void, !dbg !20 } ; Function Attrs: nounwind readnone speculatable willreturn @@ -38,11 +38,11 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) #0 declare void @_lfortran_printf(i8*, ...) -define i32 @main(i32 %0, i8** %1) !dbg !22 { +define i32 @main(i32 %0, i8** %1) !dbg !21 { .entry: - call void @_lpython_call_initial_functions(i32 %0, i8** %1), !dbg !25 - call void @__module___main_____main__global_stmts(), !dbg !25 - ret i32 0, !dbg !25 + call void @_lpython_call_initial_functions(i32 %0, i8** %1), !dbg !24 + call void @__module___main_____main__global_stmts(), !dbg !24 + ret i32 0, !dbg !24 } declare void @_lpython_call_initial_functions(i32, i8**) @@ -59,21 +59,20 @@ attributes #0 = { nounwind readnone speculatable willreturn } !5 = !{null} !6 = !DILocation(line: 9, column: 1, scope: !3) !7 = distinct !DISubprogram(name: "main0", scope: !1, file: !1, line: 1, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) -!8 = !DILocation(line: 1, column: 1, scope: !7) -!9 = !DILocalVariable(name: "x", arg: 1, scope: !7, file: !1, line: 2, type: !10) -!10 = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed) -!11 = !DILocation(line: 2, scope: !7) -!12 = !DILocalVariable(name: "x2", arg: 2, scope: !7, file: !1, line: 3, type: !13) -!13 = !DIBasicType(name: "integer", size: 64, encoding: DW_ATE_signed) -!14 = !DILocation(line: 3, scope: !7) -!15 = !DILocalVariable(name: "y", arg: 3, scope: !7, file: !1, line: 4, type: !16) -!16 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float) -!17 = !DILocation(line: 4, scope: !7) -!18 = !DILocalVariable(name: "y2", arg: 4, scope: !7, file: !1, line: 5, type: !19) -!19 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float) -!20 = !DILocation(line: 5, scope: !7) -!21 = !DILocation(line: 6, column: 5, scope: !7) -!22 = distinct !DISubprogram(name: "main_program", scope: !1, file: !1, line: 1, type: !23, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) -!23 = !DISubroutineType(types: !24) -!24 = !{!10} -!25 = !DILocation(line: 1, column: 1, scope: !22) +!8 = !DILocalVariable(name: "x", arg: 1, scope: !7, file: !1, line: 2, type: !9) +!9 = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed) +!10 = !DILocation(line: 2, scope: !7) +!11 = !DILocalVariable(name: "x2", arg: 2, scope: !7, file: !1, line: 3, type: !12) +!12 = !DIBasicType(name: "integer", size: 64, encoding: DW_ATE_signed) +!13 = !DILocation(line: 3, scope: !7) +!14 = !DILocalVariable(name: "y", arg: 3, scope: !7, file: !1, line: 4, type: !15) +!15 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float) +!16 = !DILocation(line: 4, scope: !7) +!17 = !DILocalVariable(name: "y2", arg: 4, scope: !7, file: !1, line: 5, type: !18) +!18 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float) +!19 = !DILocation(line: 5, scope: !7) +!20 = !DILocation(line: 6, column: 5, scope: !7) +!21 = distinct !DISubprogram(name: "main_program", scope: !1, file: !1, line: 1, type: !22, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!22 = !DISubroutineType(types: !23) +!23 = !{!9} +!24 = !DILocation(line: 1, column: 1, scope: !21)