Skip to content

Fix fatal error when compiling objects with Flambda#965

Merged
mshinwell merged 6 commits intoocaml:trunkfrom
mshinwell:pr7426
Feb 15, 2017
Merged

Fix fatal error when compiling objects with Flambda#965
mshinwell merged 6 commits intoocaml:trunkfrom
mshinwell:pr7426

Conversation

@mshinwell
Copy link
Contributor

(Was PR#7426.)

Translclass uses array operations on object blocks because there is no primitive to set the field of a block using a computed index. At present these "arrays" are said to have kind Paddrarray which causes an error in the Flambda simplifier in the event that a float is assigned to them. I think this error is correct. I believe the reason is as follows:

  • There is an invariant that if an array contains a float then it must have tag Double_array_tag. (Since we're dealing with arrays not blocks, all elements have the same type.)
  • It follows that there is no concept in the middle- and back end of an array that contains floats but where such floats are boxed. As such, it is illegal to use Paddrarray operations with float operands.

One solution might be to use Pgenarray instead, but this might penalise code using objects; also, it's still a lie to the optimizers (which we should particularly avoid now that Flambda exists). Instead I've done what @garrigue suggested and introduced two new primitives for getting and setting fields of blocks where the offset is not necessarily known at compile time. I have not yet added code to the Flambda simplifier to handle the new Pfield_computed like the existing Pfield when the offset turns out to be known; this can be done later.

@mshinwell mshinwell added the bug label Dec 12, 2016
@mshinwell mshinwell added this to the 4.05.0 milestone Dec 12, 2016
@garrigue
Copy link
Contributor

I was a bit afraid that adding 2 instructions to lambda would be heavy, but actually it's really light weight.
I suppose this is the way to go.

@mshinwell
Copy link
Contributor Author

@chambart Please review if you could, thanks.

@chambart
Copy link
Contributor

Ok, this looks good is cleaner than using Popaque and might be useful elsewhere.

@mshinwell
Copy link
Contributor Author

@chambart Please re-review the case in cmmgen.ml for Psetfield_computed, I had to rebase this across the change to the initialization_or_assignment type. You can add the "approved" label again once that's done.

| (Assignment | Heap_initialization), Pointer ->
return_unit (
addr_array_set (transl env arg1) (transl env arg2) (transl env arg3)
dbg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason not to use caml_initialize instead of caml_modify in case of initialization with a pointer value ?
If this is an error, to avoid potential future problems, I also suggest sharing the logic by something like:

type assignment_kind = Caml_modify | Caml_initialize | Simple

let assignment_kind ptr init =
  match init, ptr with
  | Assignment, Pointer -> Caml_modify
  | Heap_initialization, Pointer -> Caml_initialize
  | Assignment, Immediate
  | Heap_initialization, Immediate
  | Root_initialization, (Immediate | Pointer) -> Simple

@chambart
Copy link
Contributor

Rereviewed. This is good to go.

@mshinwell mshinwell merged commit 2711420 into ocaml:trunk Feb 15, 2017
@drvink
Copy link

drvink commented Oct 7, 2017

I don't think this issue is fully fixed, as I am getting this error when trying to build core_kernel.v0.9.115.18+07 on the 4.05.0+flambda switch (Linux x64).

This should repro:

opam init --compiler=4.05.0+flambda
opam repo -k git add janestreet https://github.com/janestreet/opam-repository.git
opam install core_kernel
[ERROR] The compilation of core_kernel failed at "jbuilder build -p core_kernel -j 4".

#=== ERROR while installing core_kernel.v0.9.115.18+07 ========================#
# opam-version 1.2.2
# os           linux
# command      jbuilder build -p core_kernel -j 4
# path         /home/mdl/.opam/4.05.0+flambda/build/core_kernel.v0.9.115.18+07
# compiler     4.05.0+flambda
# exit-code    1
# env-file     /home/mdl/.opam/4.05.0+flambda/build/core_kernel.v0.9.115.18+07/core_kernel-93610-e4db2f.env
# stdout-file  /home/mdl/.opam/4.05.0+flambda/build/core_kernel.v0.9.115.18+07/core_kernel-93610-e4db2f.out
# stderr-file  /home/mdl/.opam/4.05.0+flambda/build/core_kernel.v0.9.115.18+07/core_kernel-93610-e4db2f.err
### stderr ###
# [...]
#     ocamlopt src/core_kernel__Float.{cmx,o}
#     ocamlopt src/core_kernel__Force_once.{cmx,o}
#     ocamlopt src/core_kernel__Obj_array.{cmx,o} (exit 2)
# (cd _build/default && /home/mdl/.opam/4.05.0+flambda/bin/ocamlopt.opt -w -40 -g -I /home/mdl/.opam/4.05.0+flambda/lib/base -I /home/mdl/.opam/4.05.0+flambda/lib/base/caml -I /home/mdl/.opam/4.05.0+flambda/lib/base/md5 -I /home/mdl/.opam/4.05.0+flambda/lib/base/shadow_stdlib -I /home/mdl/.opam/4.05.0+flambda/lib/bin_prot -I /home/mdl/.opam/4.05.0+flambda/lib/bin_prot/shape -I /home/mdl/.opam/4.05.0+flambda/lib/fieldslib -I /home/mdl/.opam/4.05.0+flambda/lib/jane-street-headers -I /home/mdl/.opam/4.05.0+flambda/lib/num -I /home/mdl/.opam/4.05.0+flambda/lib/ocaml -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_assert/runtime-lib -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_bench/runtime-lib -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_compare/runtime-lib -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_expect/collector -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_expect/common -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_expect/config -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_hash/runtime-lib -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_inline_test/config -I /home/mdl/.opam/4.05.0+flambda/lib/ppx_inline_test/runtime-lib -I /home/mdl/.opam/4.05.0+flambda/lib/sexplib -I /home/mdl/.opam/4.05.0+flambda/lib/stdio -I /home/mdl/.opam/4.05.0+flambda/lib/typerep -I /home/mdl/.opam/4.05.0+flambda/lib/variantslib -I base_for_tests/src -no-alias-deps -I src -open Core_kernel__ -o src/core_kernel__Obj_array.cmx -c -impl src/obj_array.pp.ml)
# >> Fatal error: Assignment of a float to a specialised non-float array: (array.unsafe_set[int]<{src/obj_array.ml:185,10-32;src/obj_array.ml:93,2-20;src/obj_array.ml:79,7-68;src/obj_array.ml:56,2-56}>
#                                                           t/1960 i/1963
#                                                           obj/1954)
# Fatal error: exception Misc.Fatal_error
#
# Waiting for 1 job to finish.

@damiendoligez
Copy link
Member

@drvink could you please enter a report at https://caml.inria.fr/mantis, lest we forget about this problem?

@lpw25
Copy link
Contributor

lpw25 commented Oct 10, 2017

The above is not the same bug. It is the issue discussed at #1169. In the end we decided that this was a bug in core_kernel rather than in the compiler -- or rather that the internal invariants of the compiler changed and core_kernel would have to change with them. That change has not yet been made to core_kernel, but it will be made before there are any official releases that support 4.05.0.

@drvink
Copy link

drvink commented Oct 10, 2017

@lpw25 That contradicts what was written in https://caml.inria.fr/mantis/view.php?id=7499

If this error is ever seen on user code when using a compiler with the fix for 0007426, even if using unsafe features, another Mantis issue should be opened.

...which referred to this issue on GitHub--my knowledge of OCaml compiler internals is scant, so apologies if this is the ultimately the wrong issue.

That said, I'm happy to write a note on Mantis if you guys simply want a note that references receiving this error message in the context of #965, #1169, and the aforementioned Mantis issue.

@lpw25
Copy link
Contributor

lpw25 commented Oct 11, 2017

I don't really agree with that note: the way it is currently written this fatal error is intended to happen when users are using unsafe features. Really this fatal error should be treated the same as the case of mutating an immutable block -- which is currently handled by a warning. I'm going to talk with Mark and Pierre about these cases because personally I don't even think they should warn since the warnings are fragile and can usually be made to appear in safe code using GADTs.

It might be worth adding a note on Mantis about this case, although the issue was already discussed in the GPR I linked to.

It is worth noting that in this case the compiler has a point, it isn't 100% safe to use that version of Base with 4.05 because it is possible for code to segfault due to the new float array optimisation.

sadiqj pushed a commit to sadiqj/ocaml that referenced this pull request Feb 21, 2023
stedolan added a commit to stedolan/ocaml that referenced this pull request Mar 21, 2023
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Mar 21, 2023
b11eea1 flambda-backend: Introduce Import_info (ocaml#1036)
bc5b135 flambda-backend: Fix `ocamlobjinfo` on flambda2 .cmx files (ocaml#1029)
c8babbd flambda-backend: Compilation_unit optimisations (ocaml#1035)
e8d3e22 flambda-backend: Use 4.14.0 opam switch for building (includes upgrading ocamlformat to 0.24.1) (ocaml#1030)
eb14a86 flambda-backend: Port PR81 from ocaml-jst (ocaml#1024)
131bc12 flambda-backend: Merge ocaml-jst 2022-12-13 (ocaml#1022)
06c189a flambda-backend: Make stack allocation the default (ocaml#1013)
98debd5 flambda-backend: Initial support for value slots not of value kind (ocaml#946)
deb1714 flambda-backend: Add is_last flag to closinfo words (ocaml#938)
d07fce1 flambda-backend: Disable poll insertion in Configure (ocaml#967)
0f1ce0e flambda-backend: Regenerate ocaml/configure autoconf 2.69 (instead of 2.71) (ocaml#1012)
27132d8 flambda-backend: Fix for spurious typing error related to expanding through functor arguments (ocaml#997)
724fb68 flambda-backend: Use `Compilation_unit.t` instead of `Ident.t` for globals (ocaml#871)
396d5b8 flambda-backend: Add a test for frametable setup in natdynlinked libraries (ocaml#983)
b73ab12 flambda-backend: Fix invocation of `caml_shared_startup` in native dynlink (ocaml#980)
7c7d75a flambda-backend: Fix split_default_wrapper which did not trigger anymore with flambda2 (ocaml#970)
8fb75bd flambda-backend: Port ocaml#11727 and ocaml#11732 (ocaml#965)
fdb7987 flambda-backend: Fix include functor issue after 4.14 merge. (ocaml#948)
9745cdb flambda-backend: Print -dprofile/-dtimings output to stdout like 4.12 (ocaml#943)
5f51f21 flambda-backend: Merge pull request ocaml#932 from mshinwell/4.14-upgrade
841687d flambda-backend: Run make alldepend in ocaml/ (ocaml#936)
72a7658 flambda-backend: Remove reformatting changes only in dynlink/dune (preserving PR889 and adjusting to minimise diff)
6d758cd flambda-backend: Revert whitespace changes in dune files, to match upstream
c86bf6e flambda-backend: Remove duplicate tests for polling
971dbeb flambda-backend: Testsuite fixes
32f8356 flambda-backend: Topeval fix for symbols patch
befea01 flambda-backend: Compilation fixes / rectify merge faults
a84543f flambda-backend: Merge ocaml-jst
8e65056 flambda-backend: Merge ocaml-jst
4d70045 flambda-backend: Remove filename from system frametable (amd64) (ocaml#920)
5e57b7d flambda-backend: Bugfix for runtime frame_descr logic for C frames (ocaml#918)
6423d5e flambda-backend: Merge pull request ocaml#914 from mshinwell/merge-ocaml-jst-2022-10-24
ead605c flambda-backend: Add a missing Extract_exception (ocaml#916)
c8f1481 flambda-backend: Resolve conflicts and add specialise/specialised attributes to Builtin_attributes
cf4d0d3 flambda-backend: Merge fixes (ocaml#21)
c2f742f flambda-backend: Re-enable some tests for Flambda2 (ocaml#881)
3d38d13 flambda-backend: Long frames in frametable (ocaml#797)
85aec7b flambda-backend: Add loop attribute to Builtin_attributes
c0f16e3 flambda-backend: Compilation fixes
90dea23 flambda-backend: Merge flambda-backend/main
5acc6ea flambda-backend: Fixes after merge
e501946 flambda-backend: Merge ocaml-jst
115083b flambda-backend: Merge ocaml-jst
9943b2e flambda-backend: Revert "Revert "Transform tail-recursive functions into recursive continuations (ocaml#893)"" (ocaml#909)
ce339f1 flambda-backend: Fix alloc modes and call kinds for overapplications (ocaml#902)
e6a317c flambda-backend: Revert "Transform tail-recursive functions into recursive continuations (ocaml#893)"
853c488 flambda-backend: Transform tail-recursive functions into recursive continuations (ocaml#893)
5a977e4 flambda-backend: Fix missing End_region primitives on switch arms (ocaml#898)
7fa7f9d flambda-backend: Add missing dependencies to Dune files (ocaml#889)
3cd36f0 flambda-backend: Have Lambda `Pgetglobal` and `Psetglobal` take `Compilation_unit.t` (ocaml#896)
7565915 flambda-backend: [@poll error] attribute (ocaml#745)
9eb9448 flambda-backend: Backport the main safepoints PRs (ocaml#740)
689bdda flambda-backend: Add strict mode for ocamldep (ocaml#892)

git-subtree-dir: ocaml
git-subtree-split: b11eea1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants