[llvm] [polly] [ConstraintElimination] Use SCEV ranges information for Loop counter (PR #91457)

via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 09:24:38 PDT 2024


v01dXYZ wrote:

Little update to show some progress even if the work is not completed yet.

To have a verbose output from gcc/Ranger, use the following command line

```
gcc faulty.c -O2 -o - -S -Q --fdump-tree-all-details=- --param=ranger-debug=all
```

The `evrp` pass outputs a lot of interesting information.

The function `number_of_iterations_exit` is similar to `computeExitLimit`. `tree_niter_desc` is almost the same as `ExitLimit`.
The function `estimate_number_of_iterations` is similar to `computeBackedgeTakenCount`.

It seems to me at the moment `gcc` tries to compute the loop BTC for the loop (or at least the smallest upper bound it can be sure of).

Below to remind me the calling functions a backtrace of `number_of_iterations_exit`:

<details> 
<summary> Backtrace </summary>

``` console
#0  number_of_iterations_exit (loop=loop at entry=0x7ffff7b2c4b0, exit=exit at entry=0x7ffff7d08a80, niter=niter at entry=0x7fffffffbe20, warn=warn at entry=false, every_iteration=every_iteration at entry=false, body=body at entry=0x3160520) at ../.././gcc/tree-ssa-loop-niter.cc:3285
#1  0x0000000001195796 in estimate_numbers_of_iterations (loop=loop at entry=0x7ffff7b2c4b0) at ../.././gcc/tree-ssa-loop-niter.cc:4882
#2  0x0000000001199156 in loop_exits_before_overflow (base=0x7ffff7d01de0, step=0x7ffff7d01de0, at_stmt=0x7ffff7cdaa00, loop=0x7ffff7b2c4b0) at ../.././gcc/tree-ssa-loop-niter.cc:5286
#3  scev_probably_wraps_p (var=var at entry=0x0, base=base at entry=0x7ffff7d01de0, step=step at entry=0x7ffff7d01de0, at_stmt=at_stmt at entry=0x7ffff7cdaa00, loop=0x7ffff7b2c4b0, use_overflow_semantics=use_overflow_semantics at entry=true) at ../.././gcc/tree-ssa-loop-niter.cc:5538
#4  0x00000000013a2622 in get_scev_info (r=..., name=0x7ffff7b2c4b0, stmt=<optimized out>, l=0x7ffff7b2c4b0, init=<synthetic pointer>: <optimized out>, step=<synthetic pointer>: <optimized out>, dir=<synthetic pointer>: <optimized out>) at ../.././gcc/vr-values.cc:204
#5  range_of_var_in_loop (v=..., name=name at entry=0x7ffff7d130d8, l=0x7ffff7b2c4b0, stmt=0x7ffff7cdaa00, query=0x313c140) at ../.././gcc/vr-values.cc:271
#6  0x0000000001f0992c in fold_using_range::range_of_ssa_name_with_loop_info (this=<optimized out>, r=..., name=0x7ffff7d130d8, l=<optimized out>, phi=<optimized out>, src=...) at ../.././gcc/gimple-range-fold.cc:1114
#7  0x0000000001f09fdb in fold_using_range::range_of_phi (this=this at entry=0x7fffffffd64f, r=..., phi=phi at entry=0x7ffff7cdaa00, src=...) at ../.././gcc/gimple-range-fold.cc:981
#8  0x0000000001f0d3d1 in fold_using_range::fold_stmt (this=this at entry=0x7fffffffd64f, r=..., s=s at entry=0x7ffff7cdaa00, src=..., name=name at entry=0x7ffff7d130d8) at ../.././gcc/gimple-range-fold.cc:606
#9  0x0000000001ef896e in gimple_ranger::fold_range_internal (this=0x313c140, r=..., s=0x7ffff7cdaa00, name=0x7ffff7d130d8) at ../.././gcc/gimple-range.cc:277
#10 gimple_ranger::range_of_stmt (this=0x313c140, r=..., s=0x7ffff7cdaa00, name=<optimized out>) at ../.././gcc/gimple-range.cc:338
#11 0x0000000001ef8bb2 in gimple_ranger::register_inferred_ranges (this=0x313c140, s=s at entry=0x7ffff7cdaa00) at ../.././gcc/gimple-range.cc:498
#12 0x0000000001304d41 in rvrp_folder::pre_fold_bb (this=0x7fffffffde20, bb=0x7ffff7cdca80) at ../.././gcc/tree-vrp.cc:1017
#13 0x00000000011e377e in substitute_and_fold_dom_walker::before_dom_children (this=0x7fffffffdd50, bb=0x7ffff7cdca80) at ../.././gcc/tree-ssa-propagate.cc:762
#14 0x0000000001e9db9e in dom_walker::walk (this=0x7fffffffdd50, bb=0x7ffff7cdca80) at ../.././gcc/domwalk.cc:311
#15 0x00000000011e2526 in substitute_and_fold_engine::substitute_and_fold (this=this at entry=0x7fffffffde20, block=block at entry=0x0) at ../.././gcc/tree-ssa-propagate.cc:999
#16 0x000000000130201d in execute_ranger_vrp (fun=0x7ffff7d0d0c0, warn_array_bounds_p=false, final_p=<optimized out>) at ../.././gcc/tree-vrp.cc:1080
#17 0x0000000000ec6419 in execute_one_pass (pass=pass at entry=0x3099300) at ../.././gcc/passes.cc:2649
#18 0x0000000000ec6d20 in execute_pass_list_1 (pass=0x3099300) at ../.././gcc/passes.cc:2758
#19 0x0000000000ec6d32 in execute_pass_list_1 (pass=0x3098e00) at ../.././gcc/passes.cc:2759
#20 0x0000000000ec6d59 in execute_pass_list (fn=0x7ffff7d0d0c0, pass=<optimized out>) at ../.././gcc/passes.cc:2769
#21 0x0000000000ec768d in do_per_function_toporder (callback=callback at entry=0xec6d40 <execute_pass_list(function*, opt_pass*)>, data=0x3098b60) at ../.././gcc/passes.cc:1774
#22 0x0000000000ec78cf in do_per_function_toporder (data=<optimized out>, callback=0xec6d40 <execute_pass_list(function*, opt_pass*)>) at ../.././gcc/passes.cc:1741
#23 execute_ipa_pass_list (pass=0x3098ae0) at ../.././gcc/passes.cc:3103
#24 0x0000000000aed8d7 in ipa_passes () at ../.././gcc/cgraphunit.cc:2214
#25 symbol_table::compile (this=0x7ffff7b2d000) at ../.././gcc/cgraphunit.cc:2337
#26 0x0000000000af06ad in symbol_table::compile (this=0x7ffff7b2d000) at ../.././gcc/cgraphunit.cc:2315
#27 symbol_table::finalize_compilation_unit (this=0x7ffff7b2d000) at ../.././gcc/cgraphunit.cc:2589
#28 0x0000000000ff46b2 in compile_file () at ../.././gcc/toplev.cc:476
#29 0x00000000009076dc in do_compile () at ../.././gcc/toplev.cc:2158
#30 toplev::main (this=this at entry=0x7fffffffe6de, argc=<optimized out>, argc at entry=15, argv=<optimized out>, argv at entry=0x7fffffffe738) at ../.././gcc/toplev.cc:2314
#31 0x0000000000908ebe in main (argc=15, argv=0x7fffffffe738) at ../.././gcc/main.cc:39
```
</details>

Some interesting classes I am still trying to understand:

* `rvrp_folder` inherits `substitute_and_fold`. The latter uses a dom walker which will do a DFS while locally sorting the children according to the CFG reverse post-order, see `before_dom_children` for that.
* `range_query` has a member `relation_oracle` which is an equivalent to ConstraintSystem, keeping track of the relations between different expressions.
* `gimple_ranger`, `ranger_cache`

Some abbreviations:

* GORI: Generates Output Range Info
* chrec: chain of recurrence
* fur: fold under range

https://github.com/llvm/llvm-project/pull/91457


More information about the llvm-commits mailing list