Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,18 @@ $ node
> eval(result.js_code);
```

### Lambda debug outputs (API v9)

`comp.rescript.compileWithDebug(source)` returns `lambda` before the backend's
Lambda optimization passes and `lambda_optimized` after the whole-term passes,
before export grouping and JavaScript lowering. Both use the same Lambda printer
so their text can be compared directly. JavaScript lowering performs further
transformations; this snapshot is not the final JavaScript IR.

Regular `comp.rescript.compile(source)` does not capture these debug outputs.
Older bundles do not provide `lambda_optimized`; consumers should treat it as
optional when supporting multiple API versions.

### Testing the Playground bundle

Run `yarn workspace playground test` for a quick sanity check to see if all the build artifacts are working together correctly. When releasing the playground bundle, the test will always be executed before publishing to catch regressions.
Expand Down
1 change: 1 addition & 0 deletions changes/developer-playground-lambda-diff.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add before/after optimization views and inline diffs to the developer playground's Lambda tab, with optimized Lambda output in playground API v9. https://github.com/rescript-lang/rescript/pull/8636
6 changes: 5 additions & 1 deletion compiler/core/lam_compile_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ let required_modules (lam : Lambda.t) : unit Lam_module_ident.Hash_set.t =
collect lam;
required

let compile (output_prefix : string) export_idents hoisted (lam : Lambda.t) =
let compile ?on_optimized_lambda (output_prefix : string) export_idents hoisted

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems I cannot extract optimized lambda for the playground without this addition

(lam : Lambda.t) =
let debug_ir = !Js_config.debug_ir in
let diagnostics =
if debug_ir then Some (Ir_diagnostics.create ~output_prefix) else None
Expand Down Expand Up @@ -329,6 +330,9 @@ let compile (output_prefix : string) export_idents hoisted (lam : Lambda.t) =
lam
in

(* Capture the final whole-term optimization result before export grouping
and JavaScript lowering. The callback is absent in normal compilation. *)
Option.iter (fun capture -> capture lam) on_optimized_lambda;
let ({Lam_coercion.groups} as coerced_input), meta =
Lam_coercion.coerce_and_group_big_lambda meta lam
in
Expand Down
4 changes: 4 additions & 0 deletions compiler/core/lam_compile_main.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
*)

val compile :
?on_optimized_lambda:(Lambda.t -> unit) ->
string ->
Ident.t list ->
Lambda.hoisted_function list ->
Lambda.t ->
J.deps_program
(** For toplevel, [filename] is [""] which is the same as
{!Env.get_unit_name ()}

[on_optimized_lambda], when supplied, observes the whole-term Lambda after
optimization and before export grouping and JavaScript lowering.
*)

val lambda_as_module : J.deps_program -> string -> unit
25 changes: 22 additions & 3 deletions compiler/jsoo/jsoo_playground_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
* v6: Added `config.experimental_features` and `config.jsx_preserve_mode` to the BundleConfig.
* v7: Added debug dump output APIs for developer playground tooling.
* v8: Added genType and source map configuration and compilation outputs.
* v9: Added optimized Lambda output to debug compilation.
* *)
let api_version = "8"
let api_version = "9"

module Js = Js_of_ocaml.Js

Expand Down Expand Up @@ -669,8 +670,18 @@ module Compile = struct
let {Translmod.lambda; exports; hoisted_functions} =
Translmod.transl_implementation modulename typed_tree
in
let optimized_lambda = ref None in
let on_optimized_lambda =
if include_debug_outputs then
Some
(fun lambda ->
optimized_lambda :=
Some (Printer.to_string Printlambda.lambda lambda))
else None
in
let lambda_output =
Lam_compile_main.compile "" exports hoisted_functions lambda
Lam_compile_main.compile ?on_optimized_lambda "" exports
hoisted_functions lambda
in
let js_code, source_map =
render_javascript ~module_system ~filename ~source:str ~source_map_mode
Expand Down Expand Up @@ -727,7 +738,15 @@ module Compile = struct
("lam", inject @@ Js.string lam);
|]
in
Js.Unsafe.obj (Array.concat [attrs; debug_attrs; gentype_attrs])
let optimized_lambda_attrs =
match !optimized_lambda with
| None -> [||]
| Some output ->
Js.Unsafe.[|("lambda_optimized", inject @@ Js.string output)|]
in
Js.Unsafe.obj
(Array.concat
[attrs; debug_attrs; gentype_attrs; optimized_lambda_attrs])
else Js.Unsafe.obj attrs
with e -> (
match e with
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prepare-pages-site": "node scripts/prepare-pages-site.mjs",
"res:build": "rescript",
"res:watch": "rescript -w",
"test": "rescript && node --test scripts/source-map-navigation.test.mjs scripts/pane-layout.test.mjs",
"test": "rescript && node --test scripts/source-map-navigation.test.mjs scripts/pane-layout.test.mjs scripts/lambda-diff.test.mjs",
"dev": "vite --host 127.0.0.1",
"build": "rescript && vite build",
"preview": "vite preview --host 127.0.0.1"
Expand Down
Loading
Loading