Language models detect common roles of variables in code behavior

Do LMs Know What Variables Do? Interpreting Code Representations via Variable Roles

Computation and LanguageMachine Learning

Summary

Variables in programming often serve common purposes like counting, searching, or storing best options. The authors studied if large language models can identify these typical roles in code and how well certain analysis tools keep track of them. They found models can detect these roles better than simple approaches, but controlling code generation by changing variable roles is tricky and can harm code correctness. Their work provides a benchmark to analyze how well models understand variable behavior separately from generating correct code.

What this means in practice

  • For software engineers: Identify and analyze typical roles variables play in large codebases to improve program understanding and maintenance.
  • For software testing teams: Detect changes in variable roles during automated code generation to evaluate impacts on correctness and guide test design.

Authors

Célian Vasson, Daniil Ognev, Kentaro Inui, Benjamin Heinzerling

Abstract

Variable roles describe recurring behaviors such as accumulation, traversal, and retaining the best candidate encountered. We use Sajaniemi's independently defined taxonomy to study which role distinctions can be recovered from large language models, how sparse autoencoders (SAEs) retain those distinctions, and whether associated directions support control of generation. We introduce static annotation rules for all eleven roles and a corpus of 126,926 natural-code files across five languages. Across five model-SAE systems, residual probes outperform individual SAE features and tested lexical baselines, with modest losses after neutral target renaming. Combining SAE features closes much of the single-feature gap, but probes on SAE reconstructions are less sensitive to role-changing edits reviewed by LLMs. High paired ranking accuracy coexists with frequent false positives at fixed thresholds. During generation, interventions often change role annotations at the cost of correctness; changes accompanied by passing tests show no clear aggregate advantage over random controls. The benchmark separates role-related recoverability, SAE fidelity, and correctness-preserving control through matched prediction, source edits, and generation interventions.