Add 'loop-vectorize', 'slp-vectorize', 'unroll-loops' and 'merge-functions' optimization flags #2491.

This commit is contained in:
Christoffer Lerno
2025-09-19 13:34:06 +02:00
parent 3345e70c63
commit d8286fa2a5
4 changed files with 45 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
- Unify generic and regular module namespace.
- `env::PROJECT_VERSION` now returns the version in project.json.
- Comparing slices and arrays of user-defined types that implement == operator now works #2486.
- Add 'loop-vectorize', 'slp-vectorize', 'unroll-loops' and 'merge-functions' optimization flags #2491.
### Fixes
- Compiler assert with var x @noinit = 0 #2452

View File

@@ -128,6 +128,10 @@ static void usage(bool full)
print_opt("--panic-msg=<yes|no>", "Turn panic message output on or off.");
print_opt("--optlevel=<option>", "Code optimization level: none, less, more, max.");
print_opt("--optsize=<option>", "Code size optimization: none, small, tiny.");
print_opt("--unroll-loops=<yes|no>", "Enable loop unrolling.");
print_opt("--loop-vectorize=<yes|no>", "Enable loop auto-vectorization.");
print_opt("--slp-vectorize=<yes|no>", "Enable SLP (superword-level parallelism) auto-vectorization.");
print_opt("--merge-functions=<yes|no>", "Enable function merging.");
print_opt("--single-module=<yes|no>", "Compile all modules together, enables more inlining.");
print_opt("--show-backtrace=<yes|no>", "Show detailed backtrace on segfaults.");
print_opt("--lsp", "Emit data about errors suitable for a LSP.");
@@ -862,6 +866,26 @@ static void parse_option(BuildOptions *options)
options->optlevel = parse_opt_select(OptimizationLevel, argopt, optlevels);
return;
}
if ((argopt = match_argopt("merge-functions")))
{
options->merge_functions = parse_opt_select(MergeFunctions, argopt, on_off);
return;
}
if ((argopt = match_argopt("loop-vectorize")))
{
options->loop_vectorization = parse_opt_select(AutoVectorization, argopt, on_off);
return;
}
if ((argopt = match_argopt("unroll-loops")))
{
options->unroll_loops = parse_opt_select(UnrollLoops, argopt, on_off);
return;
}
if ((argopt = match_argopt("slp-vectorize")))
{
options->slp_vectorization = parse_opt_select(AutoVectorization, argopt, on_off);
return;
}
if ((argopt = match_argopt("safe")))
{
options->safety_level = parse_opt_select(SafetyLevel, argopt, on_off);

View File

@@ -27,10 +27,13 @@ const char *project_default_keys[][2] = {
{"linker", "'builtin' for the builtin linker, 'cc' for the system linker or <path> to a custom compiler."},
{"linker-search-paths", "Linker search paths."},
{"linux-crt", "Set the directory to use for finding crt1.o and related files."},
{"linux-crtbegin", "Set the directory to use for finding crtbegin.o and related files."},
{"loop-vectorize", "Force enable/disable loop auto-vectorization."},
{"macos-min-version", "Set the minimum MacOS version to compile for."},
{"macos-sdk-version", "Set the MacOS SDK compiled for." },
{"macossdk", "Set the directory for the MacOS SDK for cross compilation."},
{"memory-env", "Set the memory environment: normal, small, tiny, none."},
{"merge-functions", "Force enable/disable function merging."},
{"no-entry", "Do not generate (or require) a main function."},
{"opt", "Optimization setting: O0, O1, O2, O3, O4, O5, Os, Oz."},
{"optlevel", "Code optimization level: none, less, more, max."},
@@ -46,6 +49,7 @@ const char *project_default_keys[][2] = {
{"script-dir", "The directory where 'exec' is run."},
{"show-backtrace", "Print backtrace on signals."},
{"single-module", "Compile all modules together, enables more inlining."},
{"slp-vectorize", "Force enable/disable SLP auto-vectorization."},
{"soft-float", "Output soft-float functions."},
{"sources", "Paths to project sources for all targets."},
{"strip-unused", "Strip unused code and globals from the output. (default: true)"},
@@ -55,6 +59,7 @@ const char *project_default_keys[][2] = {
{"test-sources", "Paths to project test sources for all targets."},
{"testfn", "Override the test function."},
{"trap-on-wrap", "Make signed and unsigned integer overflow generate a panic rather than wrapping."},
{"unroll-loops", "Force enable/disable loop unrolling optimization."},
{"use-stdlib", "Include the standard library (default: true)."},
{"vendor", "Vendor specific extensions, ignored by c3c."},
{"version", "Version using semantic versioning."},
@@ -65,7 +70,6 @@ const char *project_default_keys[][2] = {
{"x86-stack-struct-return", "Return structs on the stack for x86."},
{"x86cpu", "Set general level of x64 cpu: baseline, ssse3, sse4, avx1, avx2-v1, avx2-v2 (Skylake/Zen1+), avx512 (Icelake/Zen4+), native."},
{"x86vec", "Set max type of vector use: none, mmx, sse, avx, avx512, native."},
{"linux-crtbegin", "Set the directory to use for finding crtbegin.o and related files."},
};
const int project_default_keys_count = ELEMENTLEN(project_default_keys);
@@ -101,10 +105,13 @@ const char* project_target_keys[][2] = {
{"linker-search-paths", "Additional linker search paths for the target."},
{"linker-search-paths-override", "Linker search paths for this target, overriding global settings."},
{"linux-crt", "Set the directory to use for finding crt1.o and related files."},
{"linux-crtbegin", "Set the directory to use for finding crtbegin.o and related files."},
{"loop-vectorize", "Force enable/disable loop auto-vectorization."},
{"macos-min-version", "Set the minimum MacOS version to compile for."},
{"macos-sdk-version", "Set the MacOS SDK compiled for." },
{"macossdk", "Set the directory for the MacOS SDK for cross compilation."},
{"memory-env", "Set the memory environment: normal, small, tiny, none."},
{"merge-functions", "Force enable/disable function merging."},
{"name", "Set the name to be different from the target name."},
{"no-entry", "Do not generate (or require) a main function."},
{"opt", "Optimization setting: O0, O1, O2, O3, O4, O5, Os, Oz."},
@@ -121,6 +128,7 @@ const char* project_target_keys[][2] = {
{"script-dir", "The directory where 'exec' is run."},
{"show-backtrace", "Print backtrace on signals."},
{"single-module", "Compile all modules together, enables more inlining."},
{"slp-vectorize", "Force enable/disable SLP auto-vectorization."},
{"soft-float", "Output soft-float functions."},
{"sources", "Additional paths to project sources for the target."},
{"sources-override", "Paths to project sources for this target, overriding global settings."},
@@ -132,6 +140,7 @@ const char* project_target_keys[][2] = {
{"testfn", "Override the test function."},
{"trap-on-wrap", "Make signed and unsigned integer overflow generate a panic rather than wrapping."},
{"type", "Type of output, one of 'executable', 'static-lib', 'dynamic-lib', 'benchmark', 'test', 'object-files' and 'prepare'." },
{"unroll-loops", "Force enable/disable loop unrolling optimization."},
{"use-stdlib", "Include the standard library (default: true)."},
{"vendor", "Vendor specific extensions, ignored by c3c."},
{"version", "Version using semantic versioning."},
@@ -142,7 +151,7 @@ const char* project_target_keys[][2] = {
{"x86-stack-struct-return", "Return structs on the stack for x86."},
{"x86cpu", "Set general level of x64 cpu: baseline, ssse3, sse4, avx1, avx2-v1, avx2-v2 (Skylake/Zen1+), avx512 (Icelake/Zen4+), native."},
{"x86vec", "Set max type of vector use: none, mmx, sse, avx, avx512, native."},
{"linux-crtbegin", "Set the directory to use for finding crtbegin.o and related files."},
};
const int project_target_keys_count = ELEMENTLEN(project_target_keys);
@@ -267,6 +276,11 @@ static void load_into_build_target(BuildParseContext context, JSONObject *json,
// Size optimization
target->optsize = GET_SETTING(SizeOptimizationLevel, "optsize", optsizes, "`none`, `small`, `tiny`.");
target->loop_vectorization = (AutoVectorization)get_valid_bool(context, json, "loop-vectorize", target->loop_vectorization);
target->slp_vectorization = (AutoVectorization)get_valid_bool(context, json, "slp-vectorize", target->slp_vectorization);
target->unroll_loops = (UnrollLoops)get_valid_bool(context, json, "unroll-loops", target->unroll_loops);
target->merge_functions = (MergeFunctions)get_valid_bool(context, json, "merge-functions", target->merge_functions);
static const char *opt_settings[8] = {
[OPT_SETTING_O0] = "O0",
[OPT_SETTING_O1] = "O1",

View File

@@ -259,6 +259,10 @@ static void view_target(BuildParseContext context, JSONObject *target, bool verb
TARGET_VIEW_SETTING("x64 CPU level", "x86cpu", x86_cpu_set);
TARGET_VIEW_SETTING("Max vector use type", "x86vec", x86_vector_capability);
TARGET_VIEW_BOOL("Return structs on the stack", "x86-stack-struct-return");
TARGET_VIEW_BOOL("Unroll loops", "unroll-loops");
TARGET_VIEW_BOOL("SLP auto-vectorization", "slp-vectorize");
TARGET_VIEW_BOOL("Loop auto-vectorization", "loop-vectorize");
TARGET_VIEW_BOOL("Merge functions", "merge-functions");
}