[SPGO] The most frequent transition becomes unreachable
According to the current implementation of the SampleProfileLoader, we are unable to correctly handle this case:
Assume that Weight becomes larger or equal to std::numeric_limits<uint32_t>::max(). After the first if statement, we set it equal to std::numeric_limits<uint32_t>::max() regardless of the condition.
https://github.com/llvm/llvm-project/blob/1aceee7bb6c4423da73f71aff2004493bdf620d1/llvm/lib/Transforms/IPO/SampleProfile.cpp#L1710-L1723
Later, we add +1 to the Weight and cast it to uint32_t, which becomes 0. Therefore, the most frequent edge is marked as unreachable.
cc @wlei-llvm
cc @david-xl @WenleiHe @MatzeB
When processing branch weights, it should be converted to uint64_t and rescale to 32bit writing back. Such weights updates should be done with common utility methods. iFDO/iPGO may experience similar issue. @minglotus-6
Later, we add +1 to the Weight and cast it to uint32_t, which becomes 0. Therefore, the most frequent edge is marked as unreachable.
A diagnostic message around this line is more user-visible when overflow happens in a real build.
A diagnostic message around this line is more user-visible when overflow happens in a real build.
I agree that the diagnostic message would be more useful in this case. However, if the profile was collected over a long sampling period, almost all branch transitions would result in spam with such a diagnostic.
When processing branch weights, it should be converted to uint64_t and rescale to 32bit writing back.
Additionally, we can perform some post-processing:
Before passing the vector Weights into metadata, we calculate the greatest common divisor (GCD) of all its elements, and then divide each element by this value.
What is your opinion about this proposal?
Fixed in https://github.com/llvm/llvm-project/pull/90217