A Lightweight Machine Learning Approach for Loop Unrolling Decisions in GCC
Year: 2026
Type: preprint
Abstract: Modern compilers such as GCC rely on hand-crafted, general-purpose heuristics to decide whether to completely unroll a loop — a decision that trades code size and instruction-cache pressure for reduced loop-control overhead and increased instruction-level parallelism. These heuristics are tuned to perform well on average across arbitrary codebases but are not adapted to the specific characteristics of an individual loop. We investigate whether a lightweight, interpretable machine learning model, trained on features extracted from GCC's GIMPLE intermediate representation, can predict the profitability of complete loop unrolling as accurately as GCC's built-in heuristic. Unlike prior work that targets LLVM or employs increasingly complex models (XGBoost, multi-layer neural networks, graph neural networks), we deliberately start from a minimal, fully interpretable Decision Tree classifier evaluated directly against ground-truth runtime measurements. We construct a dataset of 30 hand-written C functions, of which 21 yield a valid, non-trivially-resolved loop after -O2 (the remaining 9 are excluded because GCC resolves them entirely at compile time), measure the true speedup of -funroll-loops via repeated empirical timing, and extract a small set of static features from the corresponding GIMPLE dump. The resulting dataset is heavily imbalanced (19 UNROLL / 2 NO-UNROLL samples), against which a trivial majority-class baseline already achieves 90.5% accuracy. An unweighted Decision Tree matches this baseline exactly (90.5% under Leave-One-Out Cross-Validation), while the same model trained with scikit-learn's standard class_weight="balanced" setting collapses to 28.6% — a 61.9-percentage-point drop caused entirely by re-weighting two minority-class samples to roughly 9.5× the weight of each majority-class sample, which flips a single decision-tree branch responsible for the large majority of predictions. Direct inspection of GCC's own decision logic in tree-ssa-loop-ivcanon.cc further reveals that none of our five features correspond to num_branches_on_hot_path, the dominant factor in GCC's actual unrolling decision. We report both findings transparently: the class-imbalance sensitivity of a standard re-weighting technique on small datasets, and the missing branch-count feature relative to GCC's real heuristic, using them to motivate feature and evaluation design for subsequent iterations. All experiments, source code, and the generated dataset are made publicly available for reproducibility.
Author: Bakr, Mohammed
Language: English
Subfield: Hardware and Architecture
Field: Computer Science
Domain: Physical Sciences
Open Access status: green
OpenAlex ID: W7203948842
Cites:
Cited by:
Related to:
