chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.77k stars 417 forks source link

are we sure about ref-maybe-const for 2.0? #21398

Closed mppf closed 1 year ago

mppf commented 1 year ago

We are preparing for the Chapel 2.0 release where the language is stable. This issue asks about whether or not we are confident that we want to keep the ref-maybe-const feature as it stands for 2.0.

Background & History

The abstract intents table describes the current situation. The default intent varies between const in / const ref / ref depending on the type and other details. In particular:

This design has evolved over time.

Historically, arrays were in the same category as sync / single / atomic and the default intent was ref. In 2012, we had some discussion on an email thread about potentially making these cases all const ref. Then, in 2017, we ran into some unintended consequences when combining default intent functions (such as writeln) with array element access that uses return intent overload (see issue #5217). In response to those issues, we considered 3 proposals: 1) just const ref #5218 2) ref-if-modified aka ref-maybe-const #5219 3) specialize-blank #5220 and went with ref-maybe-const. One reason for that is that the just-const-ref proposal ran into some challenges with combining with task intents and promotion.

Later, we have heard some feedback from a user that the default intents table was too complicated to remember and we sought to improve the documentation (PR #20700).

One more note -- partly because the default intent for arrays was historically ref, the compiler allows mutation of array call-expr temporaries (such as with modifyArray(returnArray())) even though it does not allow such mutation of literals/rvalues in other contexts (see this comment on issue #17195).

We have also identified these related issues:

New Problems Identified

This issue discusses a few new problems with ref-maybe-const that, for the most part, have not been brought up previously.

Compiler Architecture

Presently, we are both trying to stabilize the language as well as re-implementing the resolution piece as part of the dyno effort. The current ref-maybe-const design is difficult to implement in full generality without creating a sort of full-program resolution phase (which is the normal mode of operation for the production resolver, but not for the prototype dyno resolver). In the context of the dyno resolver, these problems are particularly apparent with recursive functions.

In the past, we have considered whether or not ref-maybe-const is problematic for separate compilation. I think it is manageable for separate compilation, but at the very least it does add some complexity.

Documentation and API Stability

When writing and publishing a library, it's important to be able to clearly document the API and to be able to offer API stability by being aware of cases where the API changes. The ref-maybe-const intent causes problems for both of these, because, for function signatures using default intent, the function signature alone isn't enough to tell you if you can pass a const variable as an argument. In particular, if the function accepts an array argument, then it depends on the body of the function whether the array formal is const ref or ref. But, the body of the function is not available in the documentation, and furthermore, somebody trying to understand how to they can use the function shouldn't have to look at the body of the function.

It's certainly the case that we could try to improve the tooling in this area. The chpldoc output could include results of resolution & indicate the concrete intents. However, that isn't always possible for a generic function, since the intent can vary depending on the type it is instantiated with. Additionally, even if the chpldoc output did clarify the situation, we would still have the problem that somebody creating a library could easily change one of these intents from const ref to ref in the process of updating the implementation. They might update the implementation and produce a new minor version without ever realizing that they are changing the API.

Interfaces

This one isn't really a new problem since it has its own issue #16988, but it deserves a few more words.

When writing an interface, we need to be able to write a function signature without a body. So, the ref-maybe-const rule cannot apply because it's not possible to analyze the function body. See also #16988 which asks what array-typed formals should mean in a routine in an interface.

Universal Method Call Syntax

One issue we face is how to request the version of a method for a particular interface. If resolved, this strategy can enable interfaces as a solution for special method naming. The way Rust handles it is to use universal method call syntax. With universal method call syntax, receiver.method(arg) and method(receiver, arg) are interchangeable. That means that a specific interface can be requested with SomeInterface.method(receiver, arg). See also More https://github.com/chapel-lang/chapel/issues/19050#issuecomment-1375836834 . Note that we have not discussed unified method call syntax much yet (it probably deserves its own issue).

The point here is that if we adopted unified method call syntax, it would seem really weird to have the 1st argument to certain routines have a different default intent from the 2nd argument. It depends on the details of the universal method call syntax how bad this would be. (Can you call a function that isn't declared as a method as a method, like a.f(b) for proc f(a, b) ? If so, does the special receiver intent apply to a?)

Possible Approaches

I'll add links to issues for possible approaches here as I write them.

vasslitvinov commented 1 year ago

I see a good amount of benefits in switching from ref-maybe-const to const-ref. The API benefits stand out for me. I believe we can address the challenges from this switch.

It would be nice to allow maybe-const in prototype modules. However I do not think it is worth the extra complexity in the implementation -- and in the language spec. Also, we can add it later if we want.

vasslitvinov commented 1 year ago

How impactful would this change be? In a back-of-the-envelope experiment, I switched some? most? ref-maybe-const intents to const-ref intents. Running a paratest over these subdirectories in test/: release studies patterns puzzles constrained-generics, I got 748 successes and 368 failures, so about 1/3rd of all test runs failed. Almost all failures were in test/studies.

How does it affect my thinking from the previous comment? This looks like a change on par with nilable+managed classes w.r.t. magnitude. So, still worth it if we believe in the benefits.

Click for details I applied this diff: ```diff --- compiler/passes/resolveIntents.cpp +++ compiler/passes/resolveIntents.cpp @@ -128,7 +128,7 @@ || isTupleContainingSyncType(t) || isTupleContainingSingleType(t) || isTupleContainingAtomicType(t)) { - retval = INTENT_REF_MAYBE_CONST; + retval = INTENT_CONST_REF; } else if (isManagedPtrType(t)) { // TODO: INTENT_REF_MAYBE_CONST could ``` Sanity checking passed: with this diff, the compiler catches both errors in this code: ```chpl var A: [1..4] int; proc asdf1(arr) { arr[2] = 3; // error 1 } proc asdf2(ref arr) { forall i in A.domain { arr[i] = 4; // error 2 } } proc main { asdf2(A); writeln(A); asdf1(A); writeln(A); } ``` The subdirs I tested (listed above and below) contain 1401 .chpl files and 193 .notest files. Cf. the total of successes and failures: 748 + 368 = 1116 and the number of failed tests: 335. Counts of tests failed in each subdir: ``` test/constrained-generics 2 test/patterns 5 test/puzzles 0 test/release 46 test/studies 282 ```
Here are all the tests that failed in my paratest ``` constrained-generics/random/demo-random.chpl constrained-generics/random/test-random.chpl patterns/cltools/grep/grep.chpl patterns/eureka/eureka.chpl patterns/selection/sequential/sequential_quickselect.chpl patterns/sorting/hybrid/mergesort.chpl patterns/sorting/hybrid/quicksort.chpl release/examples/benchmarks/hpcc/fft.chpl release/examples/benchmarks/hpcc/hpl.chpl release/examples/benchmarks/hpcc/ptrans.chpl release/examples/benchmarks/hpcc/ra.chpl release/examples/benchmarks/hpcc/stream-ep.chpl release/examples/benchmarks/hpcc/variants/ra-cleanloop.chpl release/examples/benchmarks/hpcc/variants/stream-promoted.chpl release/examples/benchmarks/isx/isx.chpl release/examples/benchmarks/lcals/LCALSMain.chpl release/examples/benchmarks/lulesh/lulesh.chpl release/examples/benchmarks/lulesh/test3DLulesh.chpl release/examples/benchmarks/miniMD/miniMD.chpl release/examples/benchmarks/shootout/binarytrees.chpl release/examples/benchmarks/shootout/fasta.chpl release/examples/benchmarks/shootout/knucleotide.chpl release/examples/benchmarks/shootout/mandelbrot-fast.chpl release/examples/benchmarks/shootout/mandelbrot.chpl release/examples/benchmarks/shootout/meteor-fast.chpl release/examples/benchmarks/shootout/meteor.chpl release/examples/benchmarks/shootout/revcomp-fast.chpl release/examples/benchmarks/shootout/revcomp.chpl release/examples/benchmarks/shootout/spectralnorm.chpl release/examples/hello4-datapar-dist.chpl release/examples/patterns/readcsv.chpl release/examples/primers/LinearAlgebralib.chpl release/examples/primers/arrays.chpl release/examples/primers/associative.chpl release/examples/primers/cClient.chpl release/examples/primers/chplvis/chplvis2.chpl release/examples/primers/chplvis/chplvis3.chpl release/examples/primers/distributions.chpl release/examples/primers/forallLoops.chpl release/examples/primers/interopWithC.chpl release/examples/primers/learnChapelInYMinutes.chpl release/examples/primers/listOps.chpl release/examples/primers/parIters.chpl release/examples/primers/replicated.chpl release/examples/primers/slices.chpl release/examples/primers/sparse.chpl release/examples/spec/Arrays/decl-with-anon-domain.chpl release/examples/spec/Arrays/index-using-var-arg-tuple.chpl release/examples/spec/Data_Parallelism/forallStmt.chpl release/examples/spec/Data_Parallelism/promotes-default.chpl release/examples/spec/Expressions/query.chpl release/examples/users-guide/datapar/forall.chpl release/examples/users-guide/datapar/promotion.chpl studies/590o/alaska/graph.chpl studies/590o/alexco/AverageFiles.chpl studies/590o/bradc/coforall-private-indices-workaround.chpl studies/590o/bradc/coforall-private-indices.chpl studies/590o/bradc/forall-begin-private-indices.chpl studies/590o/kfm/solver-blc.chpl studies/590o/kfm/solver.chpl studies/IMSuite/leader_elect_lcr/leader_elect_lcr.chpl studies/IMSuite/vertexColor/vertexColoring-serial.chpl studies/adventOfCode/2021/bradc/day10a.chpl studies/adventOfCode/2021/bradc/day11.chpl studies/adventOfCode/2021/bradc/day11a.chpl studies/adventOfCode/2021/bradc/day13.chpl studies/adventOfCode/2021/bradc/day14.chpl studies/adventOfCode/2021/bradc/day15a.chpl studies/adventOfCode/2021/bradc/day16.chpl studies/adventOfCode/2021/bradc/day16a.chpl studies/adventOfCode/2021/bradc/day19.chpl studies/adventOfCode/2021/bradc/day19a.chpl studies/adventOfCode/2021/bradc/day20.chpl studies/adventOfCode/2021/bradc/day23.chpl studies/adventOfCode/2021/bradc/day23a.chpl studies/adventOfCode/2021/bradc/day25.chpl studies/adventOfCode/2021/bradc/day4.chpl studies/adventOfCode/2021/bradc/day4a.chpl studies/adventOfCode/2021/bradc/day7.chpl studies/adventOfCode/2021/bradc/day7a.chpl studies/adventOfCode/2022/day09/jeremiah/day9b.chpl studies/adventOfCode/2022/day12/jeremiah/day12a-par-alt.chpl studies/adventOfCode/2022/day12/jeremiah/day12a-par.chpl studies/adventOfCode/2022/day12/jeremiah/day12a.chpl studies/adventOfCode/2022/day12/jeremiah/day12b-par.chpl studies/adventOfCode/2022/day12/jeremiah/day12b.chpl studies/amr/advection/amr/AMR_AdvectionCTU_driver.chpl studies/amr/advection/level/Level_AdvectionCTU_driver.chpl studies/amr/diffusion/level/Level_DiffusionBE_driver.chpl studies/bale/histogram/histo-atomics-forall-opt.chpl studies/bale/histogram/histo-atomics.chpl studies/bale/indexgather/ig-auto-agg.chpl studies/bale/indexgather/ig-forall-opt.chpl studies/bale/indexgather/ig-variants.chpl studies/bale/indexgather/ig.chpl studies/bale/toposort/toposort.chpl studies/beer/beer.chpl studies/blas/marybeth/saxpy1.chpl studies/cholesky/jglewis/test_cholesky.chpl studies/cholesky/jglewis/test_dataflow_cholesky.chpl studies/cholesky/jglewis/version2/dataflow/test_dataflow_cholesky.chpl studies/cholesky/jglewis/version2/elemental/block_distribution/test_elemental_ch\ olesky_block.chpl studies/cholesky/jglewis/version2/elemental/fully_blocked/test_fully_blocked_ele\ mental_cholesky.chpl studies/cholesky/jglewis/version2/elemental/strided/test_elemental_cholesky_expl\ icitly_symmetric_strided.chpl studies/cholesky/jglewis/version2/elemental/test_elemental_cholesky.chpl studies/cholesky/jglewis/version2/elemental/unsymmetric_indices/test_elemental_c\ holesky_unsymmetric_ranges.chpl studies/cholesky/jglewis/version2/performance/test_cholesky_performance.chpl studies/cholesky/jglewis/version2/standard_cholesky/test_standard_cholesky.chpl studies/cholesky/marybeth/blockCholChoice.chpl studies/cholesky/marybeth/blockChol.chpl studies/cholesky/marybeth/blockCholesky.chpl studies/cholesky/marybeth/chol.chpl studies/colostate/Jacobi1D-DiamondByHand-Chapel_dyn.chpl studies/colostate/Jacobi1D-DiamondByHand-Chapel_static.chpl studies/colostate/Jacobi1D-NaiveParallel-Chapel_static.chpl studies/colostate/Jacobi2D-DiamondByHandParam-Chapel_dyn.chpl studies/colostate/Jacobi2D-DiamondByHandParam-Chapel_static.chpl studies/colostate/Jacobi2D-NaiveParallel-Chapel_dyn.chpl studies/colostate/Jacobi2D-NaiveParallel-Chapel_static.chpl studies/colostate/bradc/cfd-mini-serial-blc.chpl studies/colostate/cfd-mini-serial.chpl studies/comd/elegant/arrayOfStructs/CoMD.chpl studies/comd/llnl/CoMD.chpl studies/dedup/dedup-distributed-ints.chpl studies/dedup/dedup-distributed-strings.chpl studies/dedup/dedup-extern.chpl studies/dedup/dedup-externblock.chpl studies/dedup/dedup-local.chpl studies/dedup/dedup-spawn-sort.chpl studies/elegance/promotedOp.chpl studies/graph500/kristyn/Graph500_1D_onV/main.chpl studies/graph500/v2/main.chpl studies/graph500/v3/main.chpl studies/hpcc/FFT/bradc/bitreverse2.chpl studies/hpcc/FFT/bradc/bitreverse.chpl studies/hpcc/FFT/bradc/butterfly2.chpl studies/hpcc/FFT/bradc/butterfly.chpl studies/hpcc/FFT/bradc/fft-badtuple.chpl studies/hpcc/FFT/bradc/fft.chpl studies/hpcc/FFT/bradc/twiddles-onebased.chpl studies/hpcc/FFT/bradc/twiddles.chpl studies/hpcc/FFT/diten/fft.chpl studies/hpcc/FFT/fft-candidate-2d.chpl studies/hpcc/FFT/fft-hpcc06-mta.chpl studies/hpcc/FFT/fft-hpcc06.chpl studies/hpcc/FFT/fft-testPow4.chpl studies/hpcc/FFT/marybeth/fft-test-even.chpl studies/hpcc/FFT/marybeth/fft2d.chpl studies/hpcc/FFT/marybeth/fft.chpl studies/hpcc/HPL/bradc/hpl-blc-noreindex.chpl studies/hpcc/HPL/bradc/hpl-blc.chpl studies/hpcc/HPL/marybeth/HPL.chpl studies/hpcc/HPL/stonea/errors/writelnCausesError.chpl studies/hpcc/HPL/stonea/serial/hplExample2.chpl studies/hpcc/HPL/stonea/serial/hplExample3.chpl studies/hpcc/HPL/stonea/serial/hplExample4.chpl studies/hpcc/HPL/stonea/serial/hplExample5.chpl studies/hpcc/HPL/vass/bc.dim.chpl studies/hpcc/HPL/vass/bc.md.chpl studies/hpcc/HPL/vass/bl.dim.chpl studies/hpcc/HPL/vass/bug.chpl studies/hpcc/HPL/vass/hpl.chpl studies/hpcc/HPL/vass/schurComplement.chpl studies/hpcc/PTRANS/PTRANS.chpl studies/hpcc/PTRANS/jglewis/ptrans_2011-blkcyc.chpl studies/hpcc/PTRANS/jglewis/ptrans_2011.chpl studies/hpcc/RA/bradc/parallel/ra-dist.chpl studies/hpcc/RA/bradc/ra-bradc-old1.chpl studies/hpcc/RA/bradc/ra-bradc-old2.chpl studies/hpcc/RA/bradc/ra-bradc.chpl studies/hpcc/RA/diten/ra.chpl studies/hpcc/RA/marybeth/ra-uint-test1.chpl studies/hpcc/RA/marybeth/ra2-param-test.chpl studies/hpcc/RA/marybeth/ra2.chpl studies/hpcc/RA/marybeth/ra3.chpl studies/hpcc/RA/marybeth/ra.chpl studies/hpcc/RA/ra-seq.chpl studies/hpcc/RA/testRAStream.chpl studies/hpcc/STREAMS/bradc/stream-block1d-dom.chpl studies/hpcc/STREAMS/bradc/stream-fragmented-timecoforall.chpl studies/hpcc/STREAMS/bradc/stream-fragmented.chpl studies/hpcc/STREAMS/bradc/stream-nopromote.chpl studies/hpcc/STREAMS/diten/stream-fragmented-local.chpl studies/hpcc/STREAMS/elliot/stream-spmd-barrier.chpl studies/hpcc/STREAMS/stream-nopromote.chpl studies/hpcc/STREAMS/waynew/stream.chpl studies/isx/isx-bucket-spmd.chpl studies/isx/isx-hand-optimized.chpl studies/isx/isx-no-return.chpl studies/isx/isx-per-task.chpl studies/jacobi/bocchino/jacobi.workaround.chpl studies/jacobi/bocchino/jacobi.chpl studies/jacobi/bradc/jacobi-simple-brad.chpl studies/jacobi/bradc/jacobi-simple-compiles.chpl studies/jacobi/bradc/jacobi-slice-brad.chpl studies/jacobi/bradc/jacobi-slice-compiles.chpl studies/jacobi/jacobi.chpl studies/jacobi/waynew/example1.chpl studies/jacobi/waynew/example2.chpl studies/labelprop/labelprop-tweets.chpl studies/lammps/shemmy/m-lammps.chpl studies/lsms/shemmy/m-lsms.chpl studies/lsms/shemmy/s-lsms.chpl studies/lu/marybeth/blockLU.chpl studies/lu/marybeth/blocks.chpl studies/lu/marybeth/lu1.chpl studies/lu/marybeth/lu3.chpl studies/lu/marybeth/nopivlu.chpl studies/lu/marybeth/nopivlublock-alias.chpl studies/lu/marybeth/nopivlublock.chpl studies/lu/marybeth/pivlu.chpl studies/lu/marybeth/pivlublock-alias.chpl studies/lu/marybeth/pivlublock.chpl studies/lulesh/bradc/lulesh-dense.chpl studies/lulesh/bradc/tests/lulesh-eof2.chpl studies/lulesh/bradc/tests/lulesh-eof.chpl studies/lulesh/bradc/xyztuple/lulesh-dense-3tuple.chpl studies/madness/aniruddha/madchap/mytests/par-compress/test_compress.chpl studies/madness/aniruddha/madchap/mytests/par-reconstruct/test_reconstruct.chpl studies/madness/aniruddha/madchap/mytests/par-refine/test_refine.chpl studies/madness/aniruddha/madchap/recordBug/test_likepy.chpl studies/madness/aniruddha/madchap/test_diff.chpl studies/madness/aniruddha/madchap/test_gaxpy.chpl studies/madness/aniruddha/madchap/test_likepy.chpl studies/madness/aniruddha/madchap/test_mul.chpl studies/madness/aniruddha/madchap/test_showboxes.chpl studies/madness/common/test_diff.chpl studies/madness/common/test_gaxpy.chpl studies/madness/common/test_likepy.chpl studies/madness/common/test_mul.chpl studies/madness/common/test_showboxes.chpl studies/madness/dinan/mad_chapel/test_diff.chpl studies/madness/dinan/mad_chapel/test_gaxpy.chpl studies/madness/dinan/mad_chapel/test_likepy.chpl studies/madness/dinan/mad_chapel/test_mul.chpl studies/madness/dinan/mad_chapel/test_showboxes.chpl studies/matrixMult/stonea/distributed/luLike2.chpl studies/matrixMult/stonea/distributed/luLike.chpl studies/matrixMult/stonea/distributed/simpleBlockDist.chpl studies/matrixMult/stonea/mm.chpl studies/matrixlib/QR/qr.chpl studies/matrixlib/testBlockChol.chpl studies/matrixlib/testBlockLU.chpl studies/matrixlib/test_overloading.chpl studies/mstrout/wordCount.chpl studies/paracr/asenjo/PARACR-BC.chpl studies/paracr/asenjo/PARACR-B.chpl studies/parboil/BFS/bfs.chpl studies/parboil/SAD/sadSerial.chpl studies/parboil/stencil/stencil3D.chpl studies/parboil/stencil/stencilSerial.chpl studies/parsec/blackscholes.chpl studies/pidigits/bbp/increm.chpl studies/prk/DGEMM/SUMMA/dgemm.chpl studies/prk/DGEMM/dgemm.chpl studies/prk/PIC/pic.chpl studies/prk/Sparse/sparse.chpl studies/prk/Stencil/optimized/stencil-opt.chpl studies/prk/Stencil/stencil.chpl studies/prk/Stencil/stencil-serial.chpl studies/prk/Transpose/transpose.chpl studies/prk/Transpose/transpose-serial.chpl studies/programs/beer.chpl studies/programs/jacobi.chpl studies/programs/quicksort.chpl studies/rbc/tvandoren/RBCElegant.chpl studies/rbc/tvandoren/RBC.chpl studies/rbc/tvandoren/TestMatrixHelpers.chpl studies/shootout/binary-trees/binary-trees.chpl studies/shootout/binary-trees/binary-trees_iter.chpl studies/shootout/binary-trees/binarytrees-blc.chpl studies/shootout/binary-trees/binarytrees-freeWhileAdding-exp.chpl studies/shootout/binary-trees/binarytrees-freeWhileAdding.chpl studies/shootout/fasta/bradc/fasta-blc-numa.chpl studies/shootout/fasta/caseyb/fasta.chpl studies/shootout/fasta/kbrady/fasta-enum.chpl studies/shootout/fasta/kbrady/fasta-lines.chpl studies/shootout/fasta/kbrady/fasta-printf.chpl studies/shootout/fasta/kbrady/fasta-qio.chpl studies/shootout/k-nucleotide/benmcd/knucleotide-user-hash.chpl studies/shootout/k-nucleotide/bharshbarg/knucleotide-associative.chpl studies/shootout/k-nucleotide/bharshbarg/knucleotide-chaining.chpl studies/shootout/k-nucleotide/bharshbarg/knucleotide-strings.chpl studies/shootout/k-nucleotide/bradc/knucleotide-blc.chpl studies/shootout/mandelbrot/bharshbarg/mandelbrot-unrolled.chpl studies/shootout/mandelbrot/bradc/mandelbrot-blc.chpl studies/shootout/mandelbrot/ferguson/mandelbrot-stdout-putchar.chpl studies/shootout/mandelbrot/ferguson/mandelbrot-stdout.chpl studies/shootout/mandelbrot/ferguson/mandelbrot-tricks.chpl studies/shootout/mandelbrot/ferguson/mandelbrot2-opt.chpl studies/shootout/mandelbrot/jacobnelson/mandelbrot-dist.chpl studies/shootout/mandelbrot/jacobnelson/mandelbrot-fancy.chpl studies/shootout/mandelbrot/lydia/mandelbrot-unzipped.chpl studies/shootout/meteor/kbrady/meteor-implicit-domain.chpl studies/shootout/meteor/kbrady/meteor-parallel.chpl studies/shootout/meteor/kbrady/meteor.chpl studies/shootout/nbody/sidelnik/nbody_forloop_3.chpl studies/shootout/nbody/sidelnik/nbody_iterator_7-refInIterator.chpl studies/shootout/nbody/sidelnik/nbody_rangesub_5.chpl studies/shootout/nbody/sidelnik/nbody_record_2.chpl studies/shootout/nbody/sidelnik/nbody_record_domain_2a.chpl studies/shootout/nbody/sidelnik/nbody_reductions_6.chpl studies/shootout/nbody/sidelnik/nbody_vector_4.chpl studies/shootout/reverse-complement/bharshbarg/revcomp-begin.chpl studies/shootout/reverse-complement/bharshbarg/revcomp-buf.chpl studies/shootout/reverse-complement/bharshbarg/revcomp-line.chpl studies/shootout/reverse-complement/bharshbarg/revcomp-mark-skip-read-begin.chpl studies/shootout/reverse-complement/bradc/revcomp-blc-gcc8-algs.chpl studies/shootout/reverse-complement/bradc/revcomp-blc-gcc8.chpl studies/shootout/reverse-complement/bradc/revcomp-blc.chpl studies/shootout/spectral-norm/bradc/spectralnorm-blc.chpl studies/shootout/spectral-norm/lydia/spectral-norm-barrier.chpl studies/shootout/spectral-norm/lydia/spectral-norm-double-time.chpl studies/shootout/spectral-norm/lydia/spectral-norm-no-reduct.chpl studies/shootout/spectral-norm/lydia/spectral-norm-specify-step.chpl studies/shootout/spectral-norm/lydia/spectral-norm.chpl studies/shootout/spectral-norm/sidelnik/spectralnorm.chpl studies/shootout/submitted/binarytrees3.chpl studies/shootout/submitted/fasta5.chpl studies/shootout/submitted/knucleotide3.chpl studies/shootout/submitted/knucleotide4.chpl studies/shootout/submitted/mandelbrot3.chpl studies/shootout/submitted/mandelbrot.chpl studies/shootout/submitted/spectralnorm2.chpl studies/shootout/submitted/spectralnorm.chpl studies/sort/radix.chpl studies/ssca2/graphio/graphio.chpl studies/ssca2/main/SSCA2_main.chpl studies/ssca2/test-rmatalt/nondet.chpl studies/ssca2/test-rmatalt/reproduc.chpl studies/stencil9/stencil9-block.chpl studies/stencil9/stencil9-explicit.chpl studies/stencil9/stencil9-stencildist.chpl studies/stencil9/stencil9.chpl studies/tce/tce-slide8.chpl ```

P.S. Perhaps some of these errors will go away when we adjust promotion to work in this new world.

mppf commented 1 year ago

Are we willing to consider changing the default intent for all types to const ? That would drastically simplify the intents table and help with #16988. The main case other than ref-maybe-const discussed above is atomic / sync / single. I can see two potential directions, if we are willing to consider them:

mppf commented 1 year ago

It might feel a little bit like it is coming out of left field, but this topic caused me to think about our default intents behavior over the last few days & that led me to create issue #21499 to ask if we need a new intent. It is relevant for this discussion because we could consider having the new intent be the default intent. However, when applied to cases where ref-maybe-const intent is used today (arrays, primarily), using the new intent as the default intent would mean the same thing as having the default intent for these types be const ref.

bradcray commented 1 year ago

The main case other than ref-maybe-const discussed above is atomic / sync / single.

Are atomic / sync / single really ref-maybe-const? I thought they were ref only, and the spec says this as well (and it's what I think they ought to be).

Specifically, I wouldn't have expected them to need ref-maybe-const support because I think of that as helping with a case where we historically wanted the default to be ref, but may have a const we want to pass in as an actual. Yet a const atomic, sync, or single is pretty meaningless ("It's a type for coordinating between tasks by changing its value, but its value never changes"). Thus, I don't feel there's a big need to worry about helping out a user who creates a const version of them in by default intent and getting an error. I might even go so far as to warn about, or disallow const atomics/syncs/singles to head off the potential such confusion.

Assuming I'm not mistaken, I don't see a problem with having them continue to just be ref—I think it still follows the principle of least surprise, even if it's not const like the others.

bradcray commented 1 year ago

Returning to an earlier Vass comment I've been meaning to get back to:

P.S. Perhaps some of these errors will go away when we adjust promotion to work in this new world.

Vass, when Michael and I were first talking about opening this issue, I convinced myself that promotion shouldn't be a problem. Specifically, for something like:

A = B + C;
D = sin(A);

I was thinking of these as shorthand for:

forall (a, b, c) in zip(A, B, C) {
  a = b + c;
}
forall (d,a) in zip(D, A) {
  d = a;
}

such that a const ref intent within the loop wouldn't be a problem since we're only referring to the elements there, not the arrays as a whole. Simultaneously, I was realizing that it'd help with the occasional performance-killing typo that's occurred when someone writes something like:

forall (d, a) in zip(D, A) {
  D = A;
}

since the compiler would error due to the assignment to D, forcing the user to rewrite as:

forall (d, a) in zip(D, A) with (ref D) {
  D = A;
}

Could your implementation be casting a tighter net than it should be by inspecting the loop's iterands rather than just its body?

mppf commented 1 year ago

The main case other than ref-maybe-const discussed above is atomic / sync / single.

Are atomic / sync / single really ref-maybe-const? I thought they were ref only, and the spec says this as well (and it's what I think they ought to be).

They are just ref. I was saying that they are the main outlier from "all default intents are const" beyond the ref-maybe-const cases that this issue focuses on. Even after 11 years, it is still appealing to me to have the default intent for everything be const. I am trying to understand if there is room for proposals in this area or if it is something that we don't have the time/ability to consider changing for 2.0.

vasslitvinov commented 1 year ago

I still like the default intent of const for everything... except atomic/sync/single -- const is so largely useless for those. At a glance, I also like Michael's cvref.

Vass, when Michael and I were first talking about opening this issue, I convinced myself that promotion shouldn't be a problem.

My "promotion is a problem" comes from #5218, which aired this concern. Seems like the problematic case is:

A[I] = 3;  // where I is an array of index(A.domain)

We can discuss this further. I do not want this to prevent "default intent is const for everything".

Could your implementation be casting a tighter net than it should be by inspecting the loop's iterands rather than just its body?

Absolutely, given that it was a quick experiment. However it may be the case that it accepted most of the promotion cases, especially because the above A[I] = 3 situations are uncommon.

bradcray commented 1 year ago

I was saying that they are the main outlier from "all default intents are const" beyond the ref-maybe-const cases that this issue focuses on.

Got it. I'd still prefer atomic/sync/single to default to the useful value of ref rather than the not-particularly-useful value of const. And I'd rather have the asymmetry be between types (given good rationale like we have here) than between argument passing and parallel intents.

A[I] = 3; 

Oh, thanks, I didn't think of that case, and can see why it would be problematic:

forall i in I do
  A[i] = 3;

That does feel unfortunate, though I share your sentiment Vass that I wouldn't want this to be the reason we didn't consider const as a default intent for arrays (though I also don't know how I'd deal with it—forcing these cases to be written long-hand feels a little unfortunate, particularly when they're guaranteed to be parallel-safe like in this example. I could imagine an argument that:

A[I] = B;

should be forced to be written with a forall loop since it could result in non-deterministic results if I contained duplicate indices.

mppf commented 1 year ago

I'm not really understanding why these promotion cases are a potential problem. Isn't what the details compiler does with promotion up to us & not directly part of the language design? If it's working today with ref-maybe-const arguments to the generated promotion wrapper, or something, can't it just keep doing that? Or is it some sort of argument that the language itself will be inconsistent, because promotion means forall and ?

mppf commented 1 year ago

Got it. I'd still prefer atomic/sync/single to default to the useful value of ref rather than the not-particularly-useful value of const. And I'd rather have the asymmetry be between types (given good rationale like we have here) than between argument passing and parallel intents.

But what about:

consider these types similar to class fields, so that even when they are const they can be mutated

I consider this a real option, even though it might seem a bit strange... it would allow us to have the various intent tables to be consistent in all of the ways ("Default intent is always const!" "Task intent is same as formal intent!") but would allow the current patterns to continue. We would basically be saying "In practice, const doesn't mean anything for sync/single/atomic".

I can take this to another issue if there is much more discussion about it -- I asked about it here because I was trying to understand if it is worth creating an issue (vs if the proposal is dead-on-arrival).

vasslitvinov commented 1 year ago

consider these types similar to class fields, so that even when they are const they can be mutated

I am not excited about yet another issue. However I did not follow the idea - @mppf could you explain this proposal in more detail?

bradcray commented 1 year ago

Or is it some sort of argument that the language itself will be inconsistent, because promotion means forall and ?

That's what I was imagining.

consider these types similar to class fields

I don't really like exposing them to the user as though they were classes. Specifically, once we (hopefully) have direct read/write on atomics (https://github.com/chapel-lang/chapel/issues/16237), I wouldn't want:

var x, y: atomic int = 1;
x = y;
y = 2;
writeln(x);

to potentially be construed as printing 2 "because atomics are class-like." I also don't want to say "they're like classes, yet not classes because they store their values directly rather than in a distinct (modifiable) object, don't support memory management styles, don't support nilability." Basically, they don't seem sufficiently class-like that the benefit gained by "all intents are const" unity feels worth the confusion around "there is a new flavor of type that's class-like except when it isn't." To me a two-row table for default intents seems much more straightforward.

We would basically be saying "In practice, const doesn't mean anything for sync/single/atomic".

That's not particularly attractive to me since it sounds like it would permit someone were to write:

proc (const x: atomic int) {
  x = 2;  // or x.write(2);
}

which seems "wrong".

vasslitvinov commented 1 year ago

Re promotion: maybe the case A[I] = <whatever> that is a promotion of = over for i in I should, indeed be disallowed, on the grounds that it can be racy? How do we generalize this scenario to see whether it supports our "all default intents are const" approach?

mppf commented 1 year ago

consider these types similar to class fields, so that even when they are const they can be mutated

I wasn't trying to suggest they change in aliasing behavior in any way; just that const means nothing for them (which is how I characterize the situation for e.g. const x: owned MyClass = ...; x.someField = 32;).

Anyway it sounds like this seems too "wrong" to do. (FWIW, it also seems "wrong" to me that the default intent for these is ref, but I think we have bigger fish, so I do not need to pursue it further).

bradcray commented 1 year ago

just that const means nothing for them (which is how I characterize the situation for e.g. const x: owned MyClass = ...; x.someField = 32;).

I don't view const as meaning nothing for classes, just that it says that the variable pointing/referring to the object can't be changed, with no implications for the object itself.

Anyway it sounds like this seems too "wrong" to do. (FWIW, it also seems "wrong" to me that the default intent for these is ref, but I think we have bigger fish, so I do not need to pursue it further).

:+1:

bradcray commented 1 year ago

Something that I've been feeling nervous about recently when considering getting rid of ref-maybe-const, if we did it everywhere that I wanted to capture here. It seems likely we've discussed this before, but I couldn't see it on this issue with a quick look (other than maybe a quick nod to how we got here?):

In the context of task intents, it seems as though we would still want something ref-maybe-const like in order to be able to continue writing loops like:

var A: [1..n] real;
const B: [1..n] real = ...;

forall i in 1..n do
  A[i] = 2* B[i];

without having to write it as:

forall i in 1..n with (ref A) do
  A[i] = 2 * B[i];

Specifically, if we decided that the default intent for arrays should be const ref then every loop that modifies a loop in parallel via indexing (which I think is a lot of them) would have to pass the array in by reference to override the default. Meanwhile, we can't say that the default intent is simply ref since B can't be passed by ref since it's const (at least, without analysis of the loop to see that it's not modifying B... which starts to feel similar to the ref-maybe-const analysis we already have). So it feels as though parallel loops would effectively want/need some sort of ref-maybe-const analysis.

Of course, we could also decide to have default argument intents and default task intents for loops differ from one another.

Am I rehashing old ground here?

bradcray commented 1 year ago

Related thought: Does the array's default iterator yield values by ref-maybe-const? I think of it as yielding by ref, but then... how would that work for a const array? Checking the sources, I'm seeing:

    pragma "reference to const when const this"

which effectively feels a bit like "ref maybe const" in another form (?).

mppf commented 1 year ago

Something that I've been feeling nervous about recently when considering getting rid of ref-maybe-const, if we did it everywhere that I wanted to capture here. It seems likely we've discussed this before, but I couldn't see it on this issue with a quick look

I think we discussed it (or similar issues) on #5218.

To my eyes, it would be pretty good to make the default formal intent for arrays be const ref but the default task/forall intent for arrays ref-maybe const. The reason this is appealing to me is that it would address the concerns in the top post of this issue: Compiler Architecture, Documentation and API Stability, Interfaces. These problems are really about interprocedural analysis. The task/forall intent being ref-maybe-const can be viewed as a relying on a within-function analysis which does not present the same problems.

Does the array's default iterator yield values by ref-maybe-const?

Yes, but this is really just a shorthand for writing a return intent overload. I think it is separable from the main issue here, which is about the default intent for arrays etc. The pragma "reference to const when const this" is activating something other than the default intent; and it is not user-facing and we can replace it with something else (such as a return intent overload) if needed.

bradcray commented 1 year ago

Great, thanks!

it would be pretty good to make the default formal intent for arrays be const ref but the default task/forall intent for arrays ref-maybe const.

I'm definitely open to this as well. Something you said above ("it would allow us to have the various intent tables to be consistent in all of the ways") made me wonder if you wouldn't be. Thanks for clarifying. Two things were making me wonder about these things: (1) mentally ramping up to mind-meld with Jade on it tomorrow; (2) writing documentation for arrays and loops.

mppf commented 1 year ago

To be clear, it would also be OK with me if you needed to write with (ref A) in your example. One nice outcome of requiring that would be that it makes the potential for race conditions clearer even when working with arrays.

bradcray commented 1 year ago

I agree we should consider it. I just worry that the changes required may be too great. But that's why we should consider it now. :)

vasslitvinov commented 1 year ago

One big unknown is how the switch from ref-maybe-const to const-ref will affect promoted expressions, like A[IdxArray] = 3;. (Perhaps Jade can help us here.)

Keeping the default task/forall intent for arrays ref-maybe-const while changing it for the formals is a big inelegance in language design. I would not want to take this route.

jabraham17 commented 1 year ago

In a recent discussion (Cray/chapel-private#5093), we decided to deprecate ref-maybe-const for argument intents, this intents on record recievers, and task/forall intents. This should entirely remove ref-maybe-const from the user interface