The contemporary Mandarin particle le is rather polysemous and has a complicated history (Sun & Bisang 2020, § 3.1.3 and Wiktionary) which will here be simplified. The Middle Chinese verb 了 liǎo ‘finish’ takes a clausal complement P, which it follows. Thus, its basic meaning is ‘P is finished’. Phonological erosion produces the particle le. In the first step of grammaticalization, le ends a first clause and indicates change of state; in other words, it implies that the next predication will be about a different situation ().

.chīwán-le,chī.
MandIeatfinish-PFVthoueat
 When I'am done eating, you will eat.(Li & Thompson 1981:198)

In the next step, le directly follows a verb. There it signals completion, thus, perfective aspect ().

.chī-lesān-wǎnfàn.
MandIeat-PFVthree-bowlrice
 I ate three bowls of rice.(Li & Thompson 1981:198)