Automatic Identification of Parallelizable Loops Using Transformer-Based Source Code Representations
Authors: Izavan dos S. Correia, Henrique C. T. Santos, Tiago A. E. Ferreira
Organizations: Graduate Program in Applied Informatics (PPGIA), Federal Rural University of Pernambuco (UFRPE), Recife, Pernambuco, Brazil. · Undergraduate Program in Analysis and Systems Development (TADS), Federal Institute of Pernambuco (IFPE), Recife, Pernambuco, Brazil.
Automatic parallelization remains a challenging problem in software engineering, particularly in identifying code regions where loops can be safely executed in parallel on modern multi-core architectures. Traditional static analysis techniques, such as dependence analysis and polyhedral models, often struggle with irregular or dynamically structured code. In this work, we propose a Transformer-based approach to classify the parallelization potential of source code, focusing on distinguishing independent (parallelizable) loops from undefined ones. We adopt DistilBERT to process source code sequences using subword tokenization, enabling the model to capture contextual syntactic and semantic patterns without handcrafted features. The approach is evaluated on a balanced dataset combining synthetically generated loops and manually annotated real-world code, using 10-fold cross-validation and multiple performance metrics. Results show consistently high performance, with mean accuracy above 99% and low false positive rates, demonstrating robustness and reliability. Compared to prior token-based methods, the proposed approach simplifies preprocessing while improving generalization and maintaining computational efficiency. These findings highlight the potential of lightweight Transformer models for practical identification of parallelization opportunities at the loop level.
Figures & tables
Figure 1: Workflow of the proposed methodology.
Figure 2: Boxplot of training, validation, and test accuracies for the 10 cross-validation folds.
Figure 3: Histograms of training, validation, and test accuracies for the 10 folds.
Set
Mean
Std. Dev.
Median
95% CI
Training
0.99965
0.00030
0.99983
0.99944–0.99986
Validation
0.99753
0.00109
0.99784
0.99675–0.99831
Test
0.99600
0.00261
0.99650
0.99413–0.99787
Table 1: Statistical metrics for training, validation, and test accuracies across the 10 cross-validation folds.
Figure 4: Boxplots of precision, recall, and F1-Score metrics across the 10 cross-validation folds.
Figure 5: Histograms of precision, recall, and F1-Score metrics across the 10 cross-validation folds.
Metric
Mean
Std. Dev.
Median
95% CI
Precision
0.9971
0.0026
0.9976
0.9953–0.9989
Recall
0.9949
0.0038
0.9952
0.9922–0.9976
F1-Score
0.9960
0.0026
0.9970
0.9941–0.9979
Table 2: Statistical metrics for Precision, Recall, and F1-Score on the test set.
Figure 6: Boxplot of the validation error (Binary Cross-Entropy) for the 10 folds.
Figure 7: Histogram of the validation error (Binary Cross-Entropy) for the 10 folds.
Metric
Mean
Std. Dev.
Median
95% CI
Validation Error
0.01772
0.00965
0.01512
0.01081–0.02462
Table 3: Statistical metrics of the validation error (Binary Cross-Entropy).
Figure 8: Boxplot of the False Positive Rate (FPR) across the 10-fold cross-validation.
Figure 9: Histogram of the False Positive Rate (FPR) across the 10-fold cross-validation.
Metric
Mean
Std. Dev.
Median
95% CI
FPR
0.002872
0.002439
0.002418
0.001197–0.004546
Table 4: Statistical metrics for the False Positive Rate (FPR) across the 10-fold cross-validation.
Figure 10: Training and validation loss and accuracy curves for Fold 7.
Actual / Predicted
Undefined
Independent
Undefined
414
1
Independent
2
417
Table 5: Confusion matrix for Fold 7.
Metric
Precision
Recall
F1-score
Independent Class
0.9976
0.9952
0.9964
Table 6: Classification report for Fold 7.
Figure 11: Training and validation loss and accuracy curves for Fold 8.
Actual / Predicted
Undefined
Independent
Undefined
419
0
Independent
1
414
Table 7: Confusion matrix for Fold 8.
Metric
Precision
Recall
F1-score
Independent Class
1.0000
0.9976
0.9988
Table 8: Classification report for Fold 8.
Figure 12: Training and validation loss and accuracy curves for Fold 2 (worst critical error).
Actual / Predicted
Undefined
Independent
Undefined
432
3
Independent
5
394
Table 9: Confusion matrix for Fold 2 (worst critical error).
Metric
Precision
Recall
F1-score
Independent
0.9924
0.9875
0.9899
Undefined
0.9886
0.9931
0.9908
Table 10: Classification report for Fold 2 (worst critical error).
Looped Transformers scale latent computation by repeatedly applying shared blocks, but sequential looping increases latency and KV-cache memory with the loop count. Parallel loop Transformers (PLT) alleviate this cost through cross-loop position offsets (CLP) and shared-KV gated sliding-window attention, making loop count a practical design choice. We therefore study PLT loop-count selection through a gain--cost view: an extra loop may refine representations, but CLP also introduces a positional mismatch at each loop boundary. We instantiate this study by training LoopCoder-v2, a family of 7B PLT coders with different loop counts, from scratch on 18T tokens, followed by matched instruction tuning and evaluation. Empirically, the two-loop variant delivers broad gains over the non-looped baseline across code generation, code reasoning, agentic software engineering, and tool-use benchmarks, improving SWE-bench Verified from 43.0 to 64.4 points and Multi-SWE from 14.0 to 31.0 points. In contrast, variants with three or more loops regress, revealing a strongly non-monotonic loop-count effect. Our diagnostics show that loop 2 provides the main productive refinement, while later loops yield diminishing, oscillatory updates and reduced representational diversity. Because the CLP-induced mismatch remains roughly fixed as refinement gains shrink, the offset cost increasingly dominates. This gain--cost trade-off explains PLT's saturation at two loops and provides diagnostics for loop-count selection.
Jian Yang, Shawn Guo, Wei Zhang +16
1Beihang University · 2IQuest Research · 4Renming University of China +1
Looped Transformers improve parameter efficiency by repeatedly applying the same Transformer layers. However, generating each token still involves sequential computation and repeated access to model weights, leading to high decoding latency. Recent pipelined self-speculative decoding methods reduce this delay by drafting tokens from early-loop predictions and asynchronously advancing tokens at different recurrent depths in the same batch, but they do not fully exploit early-loop features for longer lookahead and still spend computation verifying incorrect speculative branches. We introduce \method, a speculative decoding framework for looped transformers that enables earlier drafting of future tokens and assigns complementary roles to different recurrent depths. A lightweight lookahead drafter (LAD) predicts multiple future tokens with early-loop states, and deeper-loop feedback helps decide which draft branches to continue and which to pause, while the full-depth model makes the final token decisions. LoopAhead achieves up to 7.82x decoding speedup over autoregressive decoding while preserving the target model's output distribution.
Transformers have become the dominant architecture for sequence modeling by using self-attention to enable expressive and highly parallel processing. However, the resulting quadratic time and memory costs limit efficiency in long-context settings. Recurrent models such as LSTMs provide explicit nonlinear state updates and strong state-tracking capabilities, yet their strictly sequential computation limits parallelism. We introduce the Parallel Recursive LSTM (PR-LSTM), a hierarchical recurrent architecture that replaces left-to-right recurrence with recursive nonlinear state composition over a balanced computation tree. Tokens are first mapped independently to latent states, which are then recursively merged by a learned gated composition block. This structure uses the reduction pattern underlying parallel scans as a fixed execution schedule, rather than assuming an associative recurrence. As a result, PR-LSTM retains nonlinear gated state representations while reducing recurrent parallel depth from linear to logarithmic. Empirically, PR-LSTM achieves strong sequence-length generalization on formal-language benchmarks, solving more tasks than standard RNN, LSTM, and Transformer baselines, while avoiding the quadratic scaling of attention. These results suggest that recurrent computation can be reorganized hierarchically to expose parallelism without restricting the transition dynamics to linear or associative forms.
Tristan Gaudreault, Yongyi Mao
School of Electrical Engineering and Computer Science University of Ottawa Ottawa, ON