python / python/cpython

pypy-style constant promotion to pool

Offen
#140,928 1 Kommentar 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core topic-JIT type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Proposal:

We should have a JIT constant pool and a JIT directive in the optimizer_bytecodes.c to promote certain values to constants. This would unlock significantly more future optimizations.

Steps needed:

  1. Add a constant pool to the executor object, so we can keep promoted constant alive. We already need this to unlock more constant propagation opportunities. The pool would be a PyListObject living in the vmdata field here https://github.com/python/cpython/blob/main/Include/internal/pycore_optimizer.h#L43 .
  2. Introduce a new optimizer macro: PROMOTE_TO_CONST_POOL and associated function promote_to_const_pool(ctx, obj) in optimizer_symbols.c. This macro will store any value it receives as a constant in the constant pool. This allows us to promote contants we see to the constant pool. This takes inspiration from RPython's hint(obj, promote=True)

What optimizations does this unlock?

  1. More constant propagation: we can now constant propagate not just immortal stuff but non-immortal things as well.
  2. Refcount removal. Anything in the the pool can use the cheaper _LOAD_CONST_INLINE_BORROW . Which will automatically benefit this work https://github.com/python/cpython/issues/134584 and vice versa. Ie, we automatically make it cheaper to load and store these values!
  3. On GIL/FT, we can promote method objects to the pool as well, which will allow us to perform more optimizations on them, such as LOAD_ATTR_METHOD folding.

We should also perhaps explore promoting globals to pool constants. Though that's a little risky, since we don't want to hold large objects around longer than needed.

The first PR should implement the first two steps and optimization 1. Optimization 2 should be a separate PR.

Linked PRs
  • gh-140968

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit Include/internal/pycore_optimizer.h und dem vmdata-Feld des Executors und untersuche anschließend optimizer_bytecodes.c und optimizer_symbols.c. Die erste PR ist abgeschlossen, wenn der Executor einen PyListObject-Konstantenpool hat, PROMOTE_TO_CONST_POOL und promote_to_const_pool vorhanden sind und nicht-immortale Werte Konstantenpropagation erhalten; die Entfernung von Refcounts ist ausdrücklich getrennt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.