I use asserts to a) guarantee compliance (eg preconditions), and b) hint to the compiler about constraints it can take advantage of.
unmasked uniform int Total(uniform int numbers[], uniform int count)
{
assert(count==32);
int total = 0;
foreach(i = 0...count)
{
total += numbers[i];
}
return reduce_add(total);
}
The assert for count==32 allows the compiler to generate much leaner code for the loop. If I compile with --opt=disable-assertions for an optimized release build, the code gen becomes worse.
I think there are two parts to this:
1) It seems counter-intuitive that the constraints introduced by asserts are being removed. The reason I would disable asserts is to remove the runtime checks, failure path and associated string constants. It would be an unusual use-case to want to disable them in-order to allow non-compliant data to be used, thus requiring the less optimal code-gen.
2) Is there a reliable alternative to assert() that could apply hints to the compiler instead? I know that there are tricks where you can add mutating no-op statements like
count=32; (
https://godbolt.org/z/9411vc) but sometimes the constraint is trickier to express than that (eg enforce a pointer is not-null), or the data is const and can't be no-op mutated. For these, assert is quite convenient.