Techniqueadvanced
Bitwidth analysis
Also called bit-width analysis, value range analysis for hardware.
Propagates value ranges forward and backward through a program to find the fewest bits each variable needs, so the synthesized datapath is narrower.
Bitwidth analysis finds, for each variable and operation of a program, a range of integers that contains every value it can take, and from that range the fewest bits that hold it. In software a 32-bit add costs the same as an 8-bit one, but in hardware made by high-level synthesis the width sets the size and delay of each adder, multiplier, register and wire. C code declares most variables as int, so a compiler that trusts the declared types builds a datapath far wider than the values need. Stephenson, Babb and Amarasinghe (2000) built the Bitwise compiler for this: it runs an abstract interpretation over intervals, with forward rules that compute the range of a result from the ranges of its operands, and backward rules that narrow the operands from what the uses of a result need. They measured the savings in the area and power of the hardware that their silicon compiler made from the narrowed programs. The analysis is sound: the computed width never cuts off a value that the program can produce. It is not exact: the true range can be smaller.
In plain words
A variable declared as a 32-bit number often holds only small values, for example a pixel from 0 to 255. If the tool can prove that, the hardware for that variable needs only 8 wires instead of 32. The tool walks through the program and keeps, for each value, the smallest and largest number it can be. Adding two pixels gives at most 510, so 9 bits. It also walks backward: if a result is cut to its low 8 bits at the end, the steps that feed it only need their low 8 bits too. Fewer bits give smaller, faster and cooler circuits.
When it applies
It applies when a program written for a processor is turned into hardware: HLS from C or C++, compilation to FPGAs, and custom accelerators. It also helps in software for packing values into narrow registers and SIMD lanes. It needs ranges for the inputs, which come from the declared types of the inputs, from interface specifications such as an 8-bit pixel or a 12-bit sensor, or from assertions in the code. It does most for code with small inputs, masks, shifts and array indices, and least for code whose values really fill their types, such as hash functions.
The template
Each variable gets an interval of integers. The analysis computes intervals by rules that are sound: if every operand lies in its interval, the result lies in the result interval.
- Seed. Inputs get the range of their type or of their specification. A constant gets .
- Forward rules. For and :
- and ;
- with ;
- if , and if also ;
- and for a constant , where is the arithmetic shift;
- at a join of control flow, such as a phi function, the hull of the incoming intervals;
- after a test such as
if (x < k), the interval on the true branch.
- Loops. Iterate the rules until the intervals stop changing. Since an interval can grow one step per pass, apply a widening operator that jumps an unstable bound to the end of the type, or use the trip count when it is known. Bitwise uses known trip counts, array bounds and type casts to get tighter ranges than widening gives.
- Backward rules. A use can narrow its operands. If a value is used only through its low bits, for example in
r & 0xFFor a store to an 8-bit array, then for , , , , , and , only the low bits of the operands matter, and the operation can be built bits wide. A use as an array index also bounds the index by the array size, for a program that does not access out of bounds. - Overflow. The rules compute in the integers. If an interval leaves the range of the declared type, the program wraps around, and the true values are no longer in the interval. Then set the interval to the full range of the type, unless the wrap is what the program means, in which case keep the low bits as in step 4.
- Width. An interval with needs the unsigned width , the bit length of . An interval with needs the signed two's complement width , that is, one sign bit plus the larger of the bit lengths of and .
Soundness of the forward rules. Take and .
- Add and subtract. From and , adding gives , and since , also .
- Multiply. For a fixed , the map is linear, so on it lies between its values at and . So lies between the least and largest of and . Each of these is linear in , so on it lies between its values at and . Together, lies between the least and largest of .
- And. Read integers in two's complement with infinitely many bits, as in the two's complement page, so a nonnegative number has only finitely many 1 bits and a negative one has infinitely many. If , then has a 1 bit only where has one. So it has finitely many 1 bits and is nonnegative, and it is a sum of a subset of the powers of 2 that make up , so it is at most . If also , the same holds with , so .
- Shifts. and are nondecreasing functions of , so they map into the interval between their values at and .
- Joins and tests. A value that reaches a join comes from one of the incoming edges, so it lies in one of the incoming intervals and so in their hull. On the true branch of
x < k, the value also satisfies .
By induction over the operations in the order of the program, every computed interval of straight-line code contains every value that the variable takes, as long as no value wraps, which step 5 guards. For loops, the result of step 3 is an interval for each variable that the rules map into itself and that contains the values on entry. Such a post-fixed point contains the values of every iteration, by induction on the number of iterations; this is the soundness theorem of abstract interpretation (Cousot and Cousot, 1977), and widening only makes the intervals larger.
Soundness of the backward rule for low bits. Taking the low bits is the map , and it respects , and : , and the same for and , since divides the difference of the two sides. Bit of , and depends only on bit of and . And . So if only the low bits of a result are used, operands that agree in their low bits give the same used value, and a -bit datapath computes it. The rule does not hold for right shifts, division, remainder or compares, whose low bits depend on high bits of the operands.
Soundness of the width. For , bits hold every integer in , and when is the bit length of . For , bits in two's complement hold every integer in . With the bit length of , we have , so ; and with the bit length of , we have . So every value in fits. One bit less fails for one of the two ends, so the formula gives the fewest bits for the interval, though not always for the true set of values.
Classic examples
An average of 16 pixels. Take x[16] with 8-bit unsigned pixels, so each , and
acc = 0
for (i = 0; i < 16; i++) acc = acc + x[i]
y = acc >> 4
z = (y * 3) & 0x3FF
d = x[0] - y
where every variable is declared int, 32 bits.
| Variable | Interval | Bits |
|---|---|---|
acc after the loop |
12 | |
y |
8 | |
y * 3 |
10 | |
z |
10 | |
d |
9, signed | |
i |
5 |
The loop runs 16 times, and after passes , so the trip count gives , which is 12 bits. Plain widening would see the upper bound grow at each pass and move it to the top of the type, which keeps acc at 32 bits, so the trip count matters here. The shift gives . The mask gives , and it does not narrow z further. For d, the subtract rule gives , and the width formula gives bits. In place of 32 bits each, the variables acc, y, z, d and i need 12, 8, 10, 9 and 5 bits, and the loop needs a 12-bit adder.
Low bits only. In w = (a * b + c) & 0xFF with 32-bit a, b and c, the forward rules give no gain, since the product of two full 32-bit ranges is wide. The backward rule shows that only the low 8 bits of a, b and c matter, so an 8-bit multiplier that keeps the low 8 bits of the product and an 8-bit adder compute w. A 32 by 32 multiplier is replaced by an 8 by 8 one.
Loop counters. A counter for (i = 0; i < n; i++) with n known to be at most 100 has , 7 bits, and the compare i < n needs only 7 bits.
Variations
- Value range propagation in compilers. The same intervals drive the removal of bound checks and of dead branches. Constant propagation, such as sparse conditional constant propagation, is the case of intervals of one value.
- Bit-level analysis. Instead of intervals, track for each bit whether it is known to be 0, known to be 1, or unknown. This catches masks and alignments that intervals miss, such as a value that is always a multiple of 8, and the two analyses can run together.
- Fixed-point formats. For signal processing code in real numbers, the same propagation finds the integer bits of a fixed-point format, and an error analysis finds the fraction bits.
- Profiling. Run the program on test data and record the ranges. This gives narrower widths, but it is not sound: an input outside the tests can overflow. Use it only with a run-time check or a proof.
- Better domains. Intervals lose the relations between variables. Relational domains, such as octagons or polyhedra, keep relations like and give tighter ranges, at a higher cost.
Pitfalls
- Wrap-around. The interval rules hold in the integers. Code that relies on wrap-around, such as hash functions and checksums, leaves the range of its type, and the analysis must fall back to the full width or to the low-bits rule.
- Too wide after widening. Widening jumps to the end of the type, so loops without known trip counts often keep their full width. Use loop bounds, assertions or the low-bits rule.
- Signed and unsigned. A negative lower bound costs a sign bit. Mixing signed and unsigned operands in C converts the signed one to unsigned, which changes the range of a negative value to a large positive one.
- Undefined behavior. The backward rule from array bounds assumes that the program never indexes out of bounds. If it can, narrowing the index hides the bug in hardware.
- Interfaces. The widths of the ports of a module are part of its interface. Narrow the internal signals freely, but change the ports only together with the code that drives them.
- Not exact. The interval of
x - xis , not , since intervals forget that the two operands are equal. The computed widths are upper bounds.
Exercises
- Compute the intervals and widths of
s = a * b + cfor 10-bit unsignedaandband a 12-bit signedc. - Show that for and , the interval rule for cannot use , and give a sound interval.
- Give a loop for which widening returns the full width but a known trip count gives a small one, and compute both.
- Prove that is not determined by , and explain what this means for the backward rule.
References
- Mark Stephenson, Jonathan Babb and Saman Amarasinghe, Bitwidth analysis with application to silicon compilation, Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2000), ACM SIGPLAN Notices 35(5), 108 to 120, 2000, the Bitwise compiler, the forward and backward propagation of data ranges, the use of array bounds, type casts and loop trip counts, and the results for silicon compilation
- Patrick Cousot and Radhia Cousot, Abstract interpretation, a unified lattice model for static analysis of programs by construction or approximation of fixpoints, Proceedings of the 4th ACM Symposium on Principles of Programming Languages, 238 to 252, 1977, the abstract interpretation framework, the interval example and the widening operator
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 39ZVKP Edit this page History Report an error Markdown