From ed7c9ec61929cbb7e4a112176c12a0c3fbeeb782 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 29 Oct 2023 20:13:03 +0000 Subject: [PATCH 1/6] Test: use custom passes for -o:speed --- src/llvm_backend.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 716c50837..d65a28e23 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1494,7 +1494,115 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { array_add(&passes, "default"); break; case 2: - array_add(&passes, "default"); + // array_add(&passes, "default"); + array_add(&passes, u8R"( + +annotation2metadata, +forceattrs, +inferattrs, + + +function( + lower-expect, + simplifycfg, + sroa, + early-cse<> +), + +called-value-propagation, +globalopt, +function( + mem2reg, + simplifycfg +), +require, +function( + invalidate +), +require, +cgscc( + devirt<4>( + inline, + inline, + function-attrs, + function( + sroa, + early-cse, + speculative-execution, + jump-threading, + simplifycfg, + constraint-elimination, + libcalls-shrinkwrap, + tailcallelim, + simplifycfg, + loop-mssa( + loop-instsimplify, + loop-simplifycfg, + licm, + loop-rotate, + licm, + simple-loop-unswitch + ), + simplifycfg, + sroa, + vector-combine, + mldst-motion, + gvn<>, + bdce, + jump-threading, + adce, + memcpyopt, + dse, + move-auto-init, + simplifycfg + ), + function-attrs, + function( + require + ) + ) +), + + +deadargelim, +globalopt, +globaldce, +elim-avail-extern, +rpo-function-attrs, +recompute-globalsaa, +function( + float2int, + lower-constant-intrinsics, + loop( + loop-rotate, + loop-deletion + ), + loop-distribute, + inject-tli-mappings, + loop-load-elim, + slp-vectorizer, + vector-combine, + transform-warning, + sroa, + alignment-from-assumptions, + loop-sink, + instsimplify, + div-rem-pairs, + tailcallelim, + simplifycfg +), +globaldce, +constmerge, +cg-profile, +rel-lookup-table-converter, + +function( + annotation-remarks +), +verify + + +)"); break; case 3: array_add(&passes, "default"); @@ -1528,6 +1636,19 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { } passes_str = gb_string_appendc(passes_str, passes[i]); } + for (isize i = 0; i < gb_string_length(passes_str); /**/) { + switch (passes_str[i]) { + case ' ': + case '\n': + case '\t': + gb_memmove(&passes_str[i], &passes_str[i+1], gb_string_length(passes_str)-i); + GB_STRING_HEADER(passes_str)->length -= 1; + continue; + default: + i += 1; + break; + } + } LLVMErrorRef llvm_err = LLVMRunPasses(wd->m->mod, passes_str, wd->target_machine, pb_options); From fc06d7011dd107b074d05efb202c4fb9a08e5a8a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 29 Oct 2023 20:31:53 +0000 Subject: [PATCH 2/6] Use default passes without coro, openmp, and sroa passes --- src/llvm_backend.cpp | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index d65a28e23..ae6f8ddcd 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1500,19 +1500,17 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { annotation2metadata, forceattrs, inferattrs, - - function( lower-expect, simplifycfg, - sroa, early-cse<> ), - +ipsccp, called-value-propagation, globalopt, function( mem2reg, + instcombine, simplifycfg ), require, @@ -1526,15 +1524,18 @@ cgscc( inline, function-attrs, function( - sroa, early-cse, speculative-execution, jump-threading, + correlated-propagation, simplifycfg, + instcombine, + aggressive-instcombine, constraint-elimination, libcalls-shrinkwrap, tailcallelim, simplifycfg, + reassociate, loop-mssa( loop-instsimplify, loop-simplifycfg, @@ -1544,26 +1545,37 @@ cgscc( simple-loop-unswitch ), simplifycfg, - sroa, + instcombine, + loop( + loop-idiom, + indvars, + loop-deletion, + loop-unroll-full + ), vector-combine, mldst-motion, gvn<>, + sccp, bdce, + instcombine, jump-threading, + correlated-propagation, adce, memcpyopt, dse, move-auto-init, - simplifycfg + loop-mssa( + licm + ), + simplifycfg, + instcombine ), function-attrs, function( require - ) + ), ) ), - - deadargelim, globalopt, globaldce, @@ -1579,11 +1591,19 @@ function( ), loop-distribute, inject-tli-mappings, + loop-vectorize, loop-load-elim, + instcombine, + simplifycfg, slp-vectorizer, vector-combine, + instcombine, + loop-unroll, transform-warning, - sroa, + instcombine, + loop-mssa( + licm + ), alignment-from-assumptions, loop-sink, instsimplify, @@ -1595,7 +1615,6 @@ globaldce, constmerge, cg-profile, rel-lookup-table-converter, - function( annotation-remarks ), From f54a026accb34a22680256cdfdf9e366daff28b2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 29 Oct 2023 20:33:29 +0000 Subject: [PATCH 3/6] Remove trailing comma --- src/llvm_backend.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index ae6f8ddcd..ed16a3aaf 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1573,7 +1573,7 @@ cgscc( function-attrs, function( require - ), + ) ) ), deadargelim, From d589914956fa6afb150e5ddbf52fc697e50df814 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 29 Oct 2023 20:43:02 +0000 Subject: [PATCH 4/6] Ignore `store undef` calls --- src/llvm_backend_general.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 0f0df7ca5..fdcf94f29 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -962,8 +962,12 @@ gb_internal bool lb_is_type_proc_recursive(Type *t) { gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { GB_ASSERT(value.value != nullptr); - Type *a = type_deref(ptr.type); + if (LLVMIsUndef(value.value)) { + return; + } + + Type *a = type_deref(ptr.type); if (LLVMIsNull(value.value)) { LLVMTypeRef src_t = llvm_addr_type(p->module, ptr); if (is_type_proc(a)) { From 61ad2a1345a3c8a9a8f84491043b5509de460976 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 29 Oct 2023 20:45:19 +0000 Subject: [PATCH 5/6] TEST: Add sroa passes back --- src/llvm_backend.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index ed16a3aaf..a81fbdd37 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1503,6 +1503,7 @@ inferattrs, function( lower-expect, simplifycfg, + sroa, early-cse<> ), ipsccp, @@ -1524,6 +1525,7 @@ cgscc( inline, function-attrs, function( + sroa, early-cse, speculative-execution, jump-threading, @@ -1552,6 +1554,7 @@ cgscc( loop-deletion, loop-unroll-full ), + sroa, vector-combine, mldst-motion, gvn<>, @@ -1600,6 +1603,7 @@ function( instcombine, loop-unroll, transform-warning, + sroa, instcombine, loop-mssa( licm @@ -1620,7 +1624,6 @@ function( ), verify - )"); break; case 3: From b7af4e7f6b3f8f96ca6d3efa492098293bfa4109 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 30 Oct 2023 12:31:24 +0000 Subject: [PATCH 6/6] Explicitly write out the passes for each level and remove certain passes --- src/llvm_backend.cpp | 287 ++++++++++++++++++++++++++++++++++++--- src/llvm_backend_opt.cpp | 7 +- src/tilde_stmt.cpp | 20 +-- 3 files changed, 282 insertions(+), 32 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index a81fbdd37..513b40a4d 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1491,19 +1491,15 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { array_add(&passes, "function(annotation-remarks)"); break; case 1: - array_add(&passes, "default"); - break; - case 2: - // array_add(&passes, "default"); +// default +// Passes removed: coro, openmp, sroa array_add(&passes, u8R"( - annotation2metadata, forceattrs, inferattrs, function( lower-expect, simplifycfg, - sroa, early-cse<> ), ipsccp, @@ -1512,20 +1508,18 @@ globalopt, function( mem2reg, instcombine, - simplifycfg -), -require, -function( - invalidate -), -require, -cgscc( + simplifycfg), + require, + function( + invalidate + ), + require, + cgscc( devirt<4>( inline, inline, function-attrs, function( - sroa, early-cse, speculative-execution, jump-threading, @@ -1534,7 +1528,6 @@ cgscc( instcombine, aggressive-instcombine, constraint-elimination, - libcalls-shrinkwrap, tailcallelim, simplifycfg, reassociate, @@ -1554,7 +1547,6 @@ cgscc( loop-deletion, loop-unroll-full ), - sroa, vector-combine, mldst-motion, gvn<>, @@ -1603,7 +1595,6 @@ function( instcombine, loop-unroll, transform-warning, - sroa, instcombine, loop-mssa( licm @@ -1623,11 +1614,267 @@ function( annotation-remarks ), verify - )"); break; +// default +// Passes removed: coro, openmp, sroa + case 2: + array_add(&passes, u8R"( +annotation2metadata, +forceattrs, +inferattrs, +function( + lower-expect, + simplifycfg, + early-cse<> +), +ipsccp, +called-value-propagation, +globalopt, +function( + mem2reg, + instcombine, + simplifycfg +), +require, +function( + invalidate +), +require, +cgscc( + devirt<4>( + inline, + inline, + function-attrs, + function( + early-cse, + speculative-execution, + jump-threading, + correlated-propagation, + simplifycfg, + instcombine, + aggressive-instcombine, + constraint-elimination, + libcalls-shrinkwrap, + tailcallelim, + simplifycfg, + reassociate, + loop-mssa( + loop-instsimplify, + loop-simplifycfg, + licm, + loop-rotate, + licm, + simple-loop-unswitch + ), + simplifycfg, + instcombine, + loop( + loop-idiom, + indvars, + loop-deletion, + loop-unroll-full + ), + vector-combine, + mldst-motion, + gvn<>, + sccp, + bdce, + instcombine, + jump-threading, + correlated-propagation, + adce, + memcpyopt, + dse, + move-auto-init, + loop-mssa( + licm + ), + simplifycfg, + instcombine + ), + function-attrs, + function( + require + ) + ) +), +deadargelim, +globalopt, +globaldce, +elim-avail-extern, +rpo-function-attrs, +recompute-globalsaa, +function( + float2int, + lower-constant-intrinsics, + loop( + loop-rotate, + loop-deletion + ), + loop-distribute, + inject-tli-mappings, + loop-vectorize, + loop-load-elim, + instcombine, + simplifycfg, + slp-vectorizer, + vector-combine, + instcombine, + loop-unroll, + transform-warning, + instcombine, + loop-mssa( + licm + ), + alignment-from-assumptions, + loop-sink, + instsimplify, + div-rem-pairs, + tailcallelim, + simplifycfg +), +globaldce, +constmerge, +cg-profile, +rel-lookup-table-converter, +function( + annotation-remarks +), +verify +)"); + break; + case 3: - array_add(&passes, "default"); +// default +// Passes removed: coro, openmp, sroa + array_add(&passes, u8R"( +annotation2metadata, +forceattrs, +inferattrs, +function( + lower-expect, + simplifycfg, + early-cse<>, + callsite-splitting +), +ipsccp, +called-value-propagation, +globalopt, +function( + mem2reg, + instcombine, + simplifycfg +), +require, +function( + invalidate +), +require, +cgscc( + devirt<4>( + inline, + inline, + function-attrs, + argpromotion, + function( + early-cse, + speculative-execution, + jump-threading, + correlated-propagation, + simplifycfg, + instcombine, + aggressive-instcombine, + constraint-elimination, + libcalls-shrinkwrap, + tailcallelim, + simplifycfg, + reassociate, + loop-mssa( + loop-instsimplify, + loop-simplifycfg, + licm, + loop-rotate, + licm, + simple-loop-unswitch + ), + simplifycfg, + instcombine, + loop( + loop-idiom, + indvars, + loop-deletion, + loop-unroll-full + ), + vector-combine, + mldst-motion, + gvn<>, + sccp, + bdce, + instcombine, + jump-threading, + correlated-propagation, + adce, + memcpyopt, + dse, + move-auto-init, + loop-mssa( + licm + ), + simplifycfg, + instcombine + ), + function-attrs, + function( + require + ), + ) +), +deadargelim, +globalopt, +globaldce, +elim-avail-extern, +rpo-function-attrs, +recompute-globalsaa, +function( + float2int, + lower-constant-intrinsics, + chr, + loop( + loop-rotate, + loop-deletion + ), + loop-distribute, + inject-tli-mappings, + loop-vectorize, + loop-load-elim, + instcombine, + simplifycfg, + slp-vectorizer, + vector-combine, + instcombine, + loop-unroll, + transform-warning, + instcombine, + loop-mssa( + licm + ), + alignment-from-assumptions, + loop-sink, + instsimplify, + div-rem-pairs, + tailcallelim, + simplifycfg +), +globaldce, +constmerge, +cg-profile, +rel-lookup-table-converter, +function( + annotation-remarks +), +verify +)"); break; } diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 4424bde7c..2e03b7974 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -322,7 +322,11 @@ gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) { // NOTE(bill): Explicit instructions are set here because some instructions could have side effects switch (LLVMGetInstructionOpcode(curr_instr)) { // case LLVMAlloca: - + case LLVMLoad: + if (LLVMGetVolatile(curr_instr)) { + break; + } + /*fallthrough*/ case LLVMFNeg: case LLVMAdd: case LLVMFAdd: @@ -342,7 +346,6 @@ gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) { case LLVMAnd: case LLVMOr: case LLVMXor: - case LLVMLoad: case LLVMGetElementPtr: case LLVMTrunc: case LLVMZExt: diff --git a/src/tilde_stmt.cpp b/src/tilde_stmt.cpp index f45dda0bc..1a7dcce4d 100644 --- a/src/tilde_stmt.cpp +++ b/src/tilde_stmt.cpp @@ -1032,22 +1032,22 @@ gb_internal void cg_build_assignment(cgProcedure *p, Array const &lvals, continue; } - Type *type = cg_addr_type(lval); + Type *type = cg_addr_type(lval); if (!cg_addr_is_empty(lval)) { GB_ASSERT_MSG(are_types_identical(init.type, type), "%s = %s", type_to_string(init.type), type_to_string(type)); } if (init.kind == cgValue_Addr && !cg_addr_is_empty(lval)) { - // NOTE(bill): This is needed for certain constructs such as this: - // a, b = b, a - // NOTE(bill): This is a bodge and not necessarily a good way of doing things whatsoever - TB_CharUnits size = cast(TB_CharUnits)type_size_of(type); - TB_CharUnits align = cast(TB_CharUnits)type_align_of(type); - TB_Node *copy = tb_inst_local(p->func, size, align); - tb_inst_memcpy(p->func, copy, init.node, tb_inst_uint(p->func, TB_TYPE_INT, size), align); - // use the copy instead - init.node = copy; + // NOTE(bill): This is needed for certain constructs such as this: + // a, b = b, a + // NOTE(bill): This is a bodge and not necessarily a good way of doing things whatsoever + TB_CharUnits size = cast(TB_CharUnits)type_size_of(type); + TB_CharUnits align = cast(TB_CharUnits)type_align_of(type); + TB_Node *copy = tb_inst_local(p->func, size, align); + tb_inst_memcpy(p->func, copy, init.node, tb_inst_uint(p->func, TB_TYPE_INT, size), align); + // use the copy instead + init.node = copy; } inits[i] = init; }