llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[SPGO] The most frequent transition becomes unreachable

Open Nechda opened this issue 2 years ago • 6 comments

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.

Nechda avatar Apr 11 '24 07:04 Nechda

cc @wlei-llvm

dtcxzyw avatar Apr 11 '24 11:04 dtcxzyw

cc @david-xl @WenleiHe @MatzeB

wlei-llvm avatar Apr 11 '24 17:04 wlei-llvm

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

david-xl avatar Apr 11 '24 17:04 david-xl

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.

mingmingl-llvm avatar Apr 11 '24 19:04 mingmingl-llvm

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?

Nechda avatar Apr 12 '24 08:04 Nechda

Fixed in https://github.com/llvm/llvm-project/pull/90217

Nechda avatar Jun 25 '24 16:06 Nechda