Tissue-resident memory CD8+ T (Trm) cells share core residency gene programs with tumor-infiltrating lymphocytes (TILs). However, the transcriptional, metabolic, and epigenetic regulation of Trm cell and TIL development and function is largely undefined. Here, we found that the transcription factor Bhlhe40 was specifically required for Trm cell and TIL development and polyfunctionality. Local PD-1 signaling inhibited TIL Bhlhe40 expression, and Bhlhe40 was critical for TIL reinvigoration following anti-PD-L1 blockade. Mechanistically, Bhlhe40 sustained Trm cell and TIL mitochondrial fitness and a functional epigenetic state. Building on these findings, we identified an epigenetic and metabolic regimen that promoted Trm cell and TIL gene signatures associated with tissue residency and polyfunctionality. This regimen empowered the anti-tumor activity of CD8+ T cells and possessed therapeutic potential even at an advanced tumor stage in mouse models. Our results provide mechanistic insights into the local regulation of Trm cell and TIL function.