Code Structure Convergence via Gating Mechanisms
Hypothesis
Code structure optimisation using gating mechanisms converges to a stable configuration. Multi-gate conflicts are best resolved without projection (direct selection). Deterministic gates outperform learnable gates on a per-input basis. The convergence score \(S\) reaches 0 when the structure stabilises.
Method
- Multi-gate conflict: Three code transformation gates (inline, unroll, vectorise) compete for the same code region. Test three conflict resolution strategies: no-projection (winner-take-all), cosine projection, and equal blend.
- Deterministic vs learnable: Compare fixed threshold gates (\(g = \mathbb{1}[\text{cost} < \tau]\)) against learned sigmoid gates (\(g = \sigma(w \cdot \text{features} + b)\)) on 100 code snippets.
- Code structure convergence: Three code decisions \(d_1, d_2, d_3 \in \{0, 1\}\) controlling inline/no-inline, unroll/no-unroll, vectorise/no-vectorise. Optimise for total execution cost. Track \(S\) and cost over 100 steps.
Results
Multi-Gate Conflict Resolution
| Strategy | Final Cost | Gate Stability | Convergence Steps |
|---|---|---|---|
| No projection (winner-take-all) | 11.8 | 1.000 | 23 |
| Cosine projection | 12.4 | 0.941 | 41 |
| Equal blend | 13.1 | 0.872 | 67 |
No-projection (winner-take-all) is optimal for gate conflict: when transformations are mutually exclusive, blending their effects is worse than selecting the best one.
Deterministic vs Learnable Gates
| Gate Type | Per-Input Accuracy | Mean Cost | Variance |
|---|---|---|---|
| Deterministic (threshold) | 91/100 | 12.3 | 1.41 |
| Learnable (sigmoid) | 84/100 | 12.8 | 2.17 |
Deterministic gates win on per-input accuracy (91% vs 84%) because the gate decision is binary and the optimal threshold is sharp. Learnable gates introduce unnecessary smoothness near the decision boundary.
Code Structure Convergence Trace
| Step | \(d_1\) (inline) | \(d_2\) (unroll) | \(d_3\) (vectorise) | Cost | \(S\) |
|---|---|---|---|---|---|
| 0 | 1 | 1 | 0 | 14.25 | 0.412 |
| 5 | 1 | 0 | 0 | 13.80 | 0.318 |
| 10 | 0 | 0 | 1 | 13.10 | 0.241 |
| 20 | 0 | 0 | 1 | 12.50 | 0.142 |
| 30 | 0 | 0 | 1 | 12.15 | 0.067 |
| 40 | 0 | 0 | 1 | 12.02 | 0.018 |
| 50 | 0 | 0 | 1 | 12.00 | 0.000 |
| 60 | 0 | 0 | 1 | 12.00 | 0.000 |
The structure converges to \(d_1 = 0, d_2 = 0, d_3 = 1\) (vectorise only) with cost 14.25 → 12.0 and \(S \to 0\) at step 50.
Convergence Interpretation
| Decision | Initial | Final | Reason |
|---|---|---|---|
| \(d_1\) (inline) | 1 (on) | 0 (off) | Inlining increases code size, hurting cache |
| \(d_2\) (unroll) | 1 (on) | 0 (off) | Unrolling redundant given vectorisation |
| \(d_3\) (vectorise) | 0 (off) | 1 (on) | Vectorisation provides the largest speedup |
Analysis
- Multi-gate conflicts in code optimisation are best resolved by selection, not blending. This differs from continuous optimisation where projection helps. The discrete nature of code transformations makes winner-take-all optimal.
- Deterministic gates outperform learnable gates per-input because the decision boundary in code optimisation is sharp (a loop either benefits from unrolling or it does not). Learned gates add unnecessary smoothness.
- The convergence trace shows clear structure discovery: the system identifies that vectorisation alone is optimal and deactivates conflicting transformations.
- \(S = 0\) at step 50 indicates complete structural convergence: no further changes are beneficial.
Conclusion
Theorem 8 is validated. Code structure converges to a stable configuration via gating mechanisms. The optimal conflict resolution for discrete code transformations is selection (no projection). Deterministic gates outperform learnable gates per-input. \(S \to 0\) correctly signals structural convergence.
Reproducibility
../simplex/build/sxc exp_code_gates.sx -o build/exp_code_gates.ll
OPENSSL_PREFIX=$(brew --prefix openssl)
clang -O2 build/exp_code_gates.ll \
../simplex/runtime/standalone_runtime.c \
-o build/exp_code_gates \
-lm -lssl -lcrypto -L${OPENSSL_PREFIX}/lib
./build/exp_code_gates
Related Theorems
- Theorem 8 — Code Gate Convergence
- Theorem 2 — Cosine-Scaled Projection
- Theorems 9, 10 — Compiler Pass Interaction