Function compilation is refactored (#13253).

Change-Id: Ic3c5d6822d90703d7f02de3d1a3d15fe4366af07