flang the compiler proves your program cannot hang 0.9.0 GitHub Try in the browserTry it Русский

Back to README · Documentation index

What a function call costs

This page answers the question: how to find out how many steps, how much memory and how much time a function takes. There are two ways: measure one run, or get an upper bound for any input size.

Measure one run

The key --cost adds a line with the cost to the output of flang run. File docs/examples/proofs/total.flang:

flang run total.flang --function Итог --args '{"цены": [100, 50], "доставка": 30}' --cost
доказано: утверждений 1
180
цена прогона: шагов 52, пик памяти 21446656 байт, секунд 0.066134

The first two lines are the usual output: the proved postconditions and the result. The third goes to the error stream. «Цена прогона» in it is the cost of this run; memory and seconds vary a little from run to run:

A program generated by flang emit --target c --cli answers a request that has the field "cost":"1" with the fields "steps", "handed", "reserved", "depth" and "seconds".

For language developers: a bound before the run

This part works in a clone of the repository. The command prints an upper bound on the number of steps as a polynomial in the input sizes:

bootstrap/flang run-script cost docs/examples/proofs/total.flang \
  --function 'Итог' --sizes '|цены|=100'
цена «Итог»
  …
  шагов толкователя (flang run) ≤ 20·|цены| + 21
  шагов напечатанного C ≤ |цены| + 1 …
  вызовов функции ≤ |цены| + 1
  действий с миром: нет
при размерах |цены|=100:
  flang run: шагов ≤ 2021
  напечатанный C: шагов ≤ 101

The command answers with one JSON line; what is shown above is its result field with the line breaks expanded. «Толкователь» in the output is the flang run interpreter; «напечатанный C» is the generated C code. For a list of a hundred prices the interpreter takes at most 2021 steps. A real run costs no more than the bound and can cost much less.

KeyWhat it does
--function «Имя»the function to bound
--allevery function of the file, one line each
`--sizes 'н=20,\xs\=100'`substitute sizes and print numbers; without the key only polynomials are printed
--profile <file>convert steps into seconds and bytes using a machine profile
--promise 'polynomial'exit code 1 if the declared cost is below the derived one

Sizes are named so: н is the value of the number н; |s| is the bytes of the string s; |xs| is the length of the list xs; |xs[]| is the size of its element.

bootstrap/flang run-script cost:calibrate prints the machine profile: seconds per step of the interpreter and of the generated C. Save the output to scripts/cost/profiles/<machine>.profile. Take the profile on the machine where the program will run.

Two commands check the bound. bootstrap/flang run-script cost:validate compares the bound with measurements on a set of functions and several sizes. bootstrap/flang run-script cost:forgery makes sure an understated cost is rejected.