Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): Don't trigger recompilation for unnecessary compiler flags #2106

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 76 additions & 38 deletions compiler/src/utils/config.re
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
type digestable =
| Digestable
| NotDigestable;
// Digestible configurations are ones that affect Grain object files.
// Any config options that don't change object files should be marked
// NotDigestible to avoid unnecessary recompiles
type digestible =
| Digestible
| NotDigestible;

type config_opt =
| Opt((ref('a), 'a, digestable)): config_opt;
| Opt((ref('a), 'a, digestible)): config_opt;

type saved_config_opt =
| SavedOpt((ref('a), 'a, digestable)): saved_config_opt;
| SavedOpt((ref('a), 'a, digestible)): saved_config_opt;

type digestable_opt =
| DigestableOpt('a): digestable_opt;
type digestible_opt =
| DigestibleOpt('a): digestible_opt;

type config = list(saved_config_opt);

Expand All @@ -36,10 +39,10 @@ type config_spec =
let opts: ref(list(config_opt)) = (ref([]): ref(list(config_opt)));
let specs: ref(list(config_spec)) = (ref([]): ref(list(config_spec)));

let internal_opt: 'a. ('a, digestable) => ref('a) =
(v, digestable) => {
let internal_opt: 'a. ('a, digestible) => ref('a) =
(v, digestible) => {
let cur = ref(v);
opts := [Opt((cur, v, digestable)), ...opts^];
opts := [Opt((cur, v, digestible)), ...opts^];
cur;
};

Expand Down Expand Up @@ -86,6 +89,7 @@ let opt:
~env_docs: string=?,
~env_doc: string=?,
~env: string=?,
~digestible: digestible,
~names: list(string),
~conv: Cmdliner.Arg.conv('a),
'a
Expand All @@ -99,11 +103,12 @@ let opt:
~env_docs=?,
~env_doc=?,
~env=?,
~digestible,
~names,
~conv as c,
v,
) => {
let cur = internal_opt(v, Digestable);
let cur = internal_opt(v, digestible);
specs :=
[
Spec(
Expand Down Expand Up @@ -138,12 +143,23 @@ let toggle_flag:
~env_docs: string=?,
~env_doc: string=?,
~env: string=?,
~digestible: digestible,
~names: list(string),
bool
) =>
ref(bool) = (
(~docs=?, ~docv=?, ~doc=?, ~env_docs=?, ~env_doc=?, ~env=?, ~names, default) => {
let cur = internal_opt(default, Digestable);
ref(bool) =
(
~docs=?,
~docv=?,
~doc=?,
~env_docs=?,
~env_doc=?,
~env=?,
~digestible,
~names,
default,
) => {
let cur = internal_opt(default, digestible);
specs :=
[
Spec(
Expand Down Expand Up @@ -172,24 +188,12 @@ let toggle_flag:
...specs^,
];
cur;
}:
(
~docs: string=?,
~docv: string=?,
~doc: string=?,
~env_docs: string=?,
~env_doc: string=?,
~env: string=?,
~names: list(string),
bool
) =>
ref(bool)
);
};

let save_config = () => {
let single_save =
fun
| Opt((cur, _, digestable)) => SavedOpt((cur, cur^, digestable));
| Opt((cur, _, digestible)) => SavedOpt((cur, cur^, digestible));
List.map(single_save, opts^);
};

Expand Down Expand Up @@ -221,13 +225,13 @@ let get_root_config_digest = () => {
| None =>
let config_opts =
root_config^
|> List.filter((SavedOpt((_, _, digestable))) =>
switch (digestable) {
| Digestable => true
| NotDigestable => false
|> List.filter((SavedOpt((_, _, digestible))) =>
switch (digestible) {
| Digestible => true
| NotDigestible => false
}
)
|> List.map((SavedOpt((_, opt, _))) => DigestableOpt(opt));
|> List.map((SavedOpt((_, opt, _))) => DigestibleOpt(opt));
let config = Marshal.to_bytes(config_opts, []);
let ret = Digest.to_hex(Digest.bytes(config));
root_config_digest := Some(ret);
Expand Down Expand Up @@ -325,6 +329,7 @@ let profile =
~doc="Set a compilation profile.",
~names=["profile"],
~conv=option_conv(Cmdliner.Arg.enum([("release", Release)])),
~digestible=Digestible,
None,
);

Expand All @@ -335,6 +340,7 @@ let memory_base =
~doc="Set the start address for the Grain runtime heap.",
~names=["memory-base"],
~conv=option_conv(Cmdliner.Arg.int),
~digestible=Digestible,
None,
);

Expand All @@ -344,6 +350,7 @@ let include_dirs =
~conv=Cmdliner.Arg.(list(dir)),
~doc="Extra library include directories",
~docv="DIR",
~digestible=NotDigestible,
[],
);

Expand All @@ -353,17 +360,24 @@ let stdlib_dir =
~conv=option_conv(Cmdliner.Arg.string),
~doc="Path to the standard library (stdlib) directory",
~env="GRAIN_STDLIB",
~digestible=NotDigestible,
None,
);

let color_enabled =
toggle_flag(~names=["no-color"], ~doc="Disable colored output", true);
toggle_flag(
~names=["no-color"],
~doc="Disable colored output",
~digestible=NotDigestible,
true,
);

let initial_memory_pages =
opt(
~names=["initial-memory-pages"],
~conv=Cmdliner.Arg.int,
~doc="Initial number of WebAssembly memory pages",
~digestible=NotDigestible,
64,
);

Expand All @@ -372,36 +386,45 @@ let maximum_memory_pages =
~names=["maximum-memory-pages"],
~conv=option_conv(Cmdliner.Arg.int),
~doc="Maximum number of WebAssembly memory pages",
~digestible=NotDigestible,
None,
);

let import_memory =
toggle_flag(
~names=["import-memory"],
~doc="Import the memory from `env.memory`",
~digestible=NotDigestible,
false,
);

type compilation_mode =
| Normal /* Standard compilation with regular bells and whistles */
| Runtime /* GC doesn't exist yet, allocations happen in runtime heap */;

let compilation_mode = internal_opt(Normal, Digestable);
let compilation_mode = internal_opt(Normal, NotDigestible);

let statically_link =
toggle_flag(~names=["no-link"], ~doc="Disable static linking", true);
toggle_flag(
~names=["no-link"],
~doc="Disable static linking",
~digestible=NotDigestible,
true,
);

let no_tail_call =
toggle_flag(
~names=["no-wasm-tail-call"],
~doc="Disables tail-call optimization",
~digestible=Digestible,
false,
);

let strict_sequence =
toggle_flag(
~names=["strict-sequence"],
~doc="Enable strict sequencing",
~digestible=NotDigestible,
false,
);

Expand All @@ -412,20 +435,23 @@ let debug =
toggle_flag(
~names=["debug"],
~doc="Compile with debugging information",
~digestible=NotDigestible,
false,
);

let wat =
toggle_flag(
~names=["wat"],
~doc="Additionally produce a WebAssembly Text (.wat) file",
~digestible=NotDigestible,
false,
);

let verbose =
toggle_flag(
~names=["verbose"],
~doc="Print critical information at various stages of compilation",
~digestible=NotDigestible,
false,
);

Expand All @@ -434,27 +460,31 @@ let sexp_locs_enabled =
~names=["hide-locs"],
~doc=
"Hide locations from intermediate trees. Only has an effect with `--verbose'.",
~digestible=NotDigestible,
true,
);

let no_pervasives =
toggle_flag(
~names=["no-pervasives"],
~doc="Don't automatically import the Grain Pervasives module.",
~digestible=Digestible,
false,
);

let no_gc =
toggle_flag(
~names=["no-gc"],
~doc="Turn off reference counting garbage collection.",
~digestible=Digestible,
false,
);

let bulk_memory =
toggle_flag(
~names=["no-bulk-memory"],
~doc="Turn off Bulk Memory operations",
~digestible=Digestible,
true,
);

Expand All @@ -463,27 +493,35 @@ let wasi_polyfill =
~names=["wasi-polyfill"],
~conv=option_conv(Cmdliner.Arg.string),
~doc="Custom WASI implementation",
~digestible=NotDigestible,
None,
);

let use_start_section =
toggle_flag(
~names=["use-start-section"],
~doc="Replace the _start export with a start section during linking.",
~digestible=NotDigestible,
false,
);

let elide_type_info =
toggle_flag(
~names=["elide-type-info"],
~doc="Don't include runtime type information used by toString/print",
~digestible=Digestible,
false,
);

let source_map =
toggle_flag(~names=["source-map"], ~doc="Generate source maps", false);
toggle_flag(
~names=["source-map"],
~doc="Generate source maps",
~digestible=NotDigestible,
false,
);

let print_warnings = internal_opt(true, NotDigestable);
let print_warnings = internal_opt(true, NotDigestible);

let with_cli_options = (term: 'a): Cmdliner.Term.t('a) => {
open Cmdliner;
Expand Down
Loading