omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_discovery.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "omega_discovery.h" |
| 18 | #include "aarch64_target.h" |
| 19 | #include <string.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | void omega_corpus_init(OmegaCorpus *corpus) { |
| 23 | if (!corpus) return; |
| 24 | memset(corpus, 0, sizeof(*corpus)); |
| 25 | } |
| 26 | |
| 27 | void omega_corpus_destroy(OmegaCorpus *corpus) { |
| 28 | if (!corpus) return; |
| 29 | for (size_t i = 0; i < corpus->count; ++i) { |
| 30 | omega_program_destroy(&corpus->programs[i]); |
| 31 | } |
| 32 | corpus->count = 0; |
| 33 | } |
| 34 | |
| 35 | int omega_corpus_add(OmegaCorpus *corpus, const OmegaProgram *prog) { |
| 36 | if (!corpus || !prog) return -1; |
| 37 | if (corpus->count >= OMEGA_CORPUS_MAX_PROGRAMS) return -1; |
| 38 | |
| 39 | corpus->programs[corpus->count] = *prog; |
| 40 | corpus->count++; |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | uint32_t omega_corpus_total_cost(const OmegaCorpus *corpus) { |
| 45 | if (!corpus) return 0; |
| 46 | uint32_t total = 0; |
| 47 | for (size_t i = 0; i < corpus->count; ++i) { |
| 48 | total += corpus->programs[i].cost.insn_count; |
| 49 | } |
| 50 | return total; |
| 51 | } |
| 52 | |
| 53 | int omega_corpus_populate_benchmark(OmegaCorpus *corpus) { |
| 54 | if (!corpus) return -1; |
| 55 | |
| 56 | /* Build base shared sub-expression A = (add1) o (mul2) = 2x + 1 */ |
| 57 | OmegaProgram m2, a1, c_2x1; |
| 58 | omega_program_build_unary_op(&m2, "mul2", OP_MUL, 2); |
| 59 | omega_program_build_unary_op(&a1, "add1", OP_ADD, 1); |
| 60 | char err[256]; |
| 61 | if (omega_program_compose(&m2, &a1, &c_2x1, err, sizeof(err)) != 0) return -1; |
| 62 | |
| 63 | VerifyReport rep; |
| 64 | omega_program_verify(&c_2x1, &rep); |
| 65 | |
| 66 | /* Program 1: P1 = (add5) o (2x + 1) = 2x + 6 */ |
| 67 | OmegaProgram a5, p1; |
| 68 | omega_program_build_unary_op(&a5, "add5", OP_ADD, 5); |
| 69 | omega_program_compose(&c_2x1, &a5, &p1, err, sizeof(err)); |
| 70 | omega_program_verify(&p1, &rep); |
| 71 | omega_corpus_add(corpus, &p1); |
| 72 | |
| 73 | /* Program 2: P2 = (mul3) o (2x + 1) = 6x + 3 */ |
| 74 | OmegaProgram m3, p2; |
| 75 | omega_program_build_unary_op(&m3, "mul3", OP_MUL, 3); |
| 76 | omega_program_compose(&c_2x1, &m3, &p2, err, sizeof(err)); |
| 77 | omega_program_verify(&p2, &rep); |
| 78 | omega_corpus_add(corpus, &p2); |
| 79 | |
| 80 | /* Program 3: P3 = (sub4) o (2x + 1) = 2x - 3 */ |
| 81 | OmegaProgram s4, p3; |
| 82 | omega_program_build_unary_op(&s4, "sub4", OP_SUB, 4); |
| 83 | omega_program_compose(&c_2x1, &s4, &p3, err, sizeof(err)); |
| 84 | omega_program_verify(&p3, &rep); |
| 85 | omega_corpus_add(corpus, &p3); |
| 86 | |
| 87 | /* Program 4: P4 = (add10) o (2x + 1) = 2x + 11 */ |
| 88 | OmegaProgram a10, p4; |
| 89 | omega_program_build_unary_op(&a10, "add10", OP_ADD, 10); |
| 90 | omega_program_compose(&c_2x1, &a10, &p4, err, sizeof(err)); |
| 91 | omega_program_verify(&p4, &rep); |
| 92 | omega_corpus_add(corpus, &p4); |
| 93 | |
| 94 | omega_program_destroy(&m2); |
| 95 | omega_program_destroy(&a1); |
| 96 | omega_program_destroy(&c_2x1); |
| 97 | omega_program_destroy(&a5); |
| 98 | omega_program_destroy(&m3); |
| 99 | omega_program_destroy(&s4); |
| 100 | omega_program_destroy(&a10); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int omega_discover_abstractions(const OmegaCorpus *corpus, OmegaDiscoveryResult *result) { |
| 106 | if (!corpus || !result) return -1; |
| 107 | memset(result, 0, sizeof(*result)); |
| 108 | result->best_candidate_index = -1; |
| 109 | |
| 110 | if (corpus->count == 0) return 0; |
| 111 | |
| 112 | /* Scan programs for candidate sub-slices of machine instructions */ |
| 113 | for (size_t p_idx = 0; p_idx < corpus->count; ++p_idx) { |
| 114 | const OmegaProgram *p0 = &corpus->programs[p_idx]; |
| 115 | if (!p0->is_realized || p0->realization.code_len < 8) continue; |
| 116 | |
| 117 | size_t total_insns = p0->realization.code_len / 4; |
| 118 | if (total_insns < 3) continue; |
| 119 | |
| 120 | /* Exclude terminal RET instruction */ |
| 121 | size_t non_ret_insns = total_insns - 1; |
| 122 | |
| 123 | /* Look for slices of length L >= 4 instructions (>= 2 operations, non-trivial) */ |
| 124 | for (size_t L = 4; L <= non_ret_insns && L <= 8; ++L) { |
| 125 | for (size_t offset = 0; offset + L <= non_ret_insns; ++offset) { |
| 126 | const uint8_t *slice_bytes = &p0->realization.code_bytes[offset * 4]; |
| 127 | size_t slice_byte_len = L * 4; |
| 128 | |
| 129 | /* Check if this slice is already tracked */ |
| 130 | bool duplicate = false; |
| 131 | for (size_t c = 0; c < result->candidate_count; ++c) { |
| 132 | if (result->candidates[c].slice_len_insns == L && |
| 133 | memcmp(result->candidates[c].abstraction.realization.code_bytes, |
| 134 | slice_bytes, slice_byte_len) == 0) { |
| 135 | duplicate = true; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | if (duplicate) continue; |
| 140 | |
| 141 | /* Count occurrences across corpus */ |
| 142 | size_t occ_count = 0; |
| 143 | size_t occ_indices[OMEGA_DISCOVERY_MAX_OCCURRENCES]; |
| 144 | |
| 145 | for (size_t j = 0; j < corpus->count; ++j) { |
| 146 | const OmegaProgram *pj = &corpus->programs[j]; |
| 147 | if (!pj->is_realized || pj->realization.code_len < slice_byte_len) continue; |
| 148 | |
| 149 | size_t pj_insns = pj->realization.code_len / 4; |
| 150 | if (pj_insns <= L) continue; |
| 151 | |
| 152 | bool found_in_pj = false; |
| 153 | for (size_t k = 0; k + L < pj_insns; ++k) { |
| 154 | if (memcmp(&pj->realization.code_bytes[k * 4], slice_bytes, slice_byte_len) == 0) { |
| 155 | found_in_pj = true; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | if (found_in_pj && occ_count < OMEGA_DISCOVERY_MAX_OCCURRENCES) { |
| 160 | occ_indices[occ_count++] = j; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* Candidate must compress at least 2 distinct programs */ |
| 165 | if (occ_count >= 2 && result->candidate_count < OMEGA_DISCOVERY_MAX_CANDIDATES) { |
| 166 | OmegaAbstractionCandidate *cand = &result->candidates[result->candidate_count]; |
| 167 | memset(cand, 0, sizeof(*cand)); |
| 168 | |
| 169 | cand->occurrence_count = occ_count; |
| 170 | for (size_t o = 0; o < occ_count; ++o) { |
| 171 | cand->occurrences[o] = occ_indices[o]; |
| 172 | } |
| 173 | cand->slice_offset_insns = (uint32_t)offset; |
| 174 | cand->slice_len_insns = (uint32_t)L; |
| 175 | cand->is_nontrivial = (L >= 4); |
| 176 | |
| 177 | /* Construct abstraction program from slice */ |
| 178 | omega_program_init(&cand->abstraction, "discovered_abs_2x_plus_1"); |
| 179 | cand->abstraction.contract.input_type = TYPE_UNSIGNED_INT; |
| 180 | cand->abstraction.contract.input_width = 64; |
| 181 | cand->abstraction.contract.output_type = TYPE_UNSIGNED_INT; |
| 182 | cand->abstraction.contract.output_width = 64; |
| 183 | |
| 184 | snprintf(cand->abstraction.contract.precondition, 64, "x >= 0"); |
| 185 | omega_build_constraint_id(CONST_PRECONDITION, cand->abstraction.contract.precondition, |
| 186 | &cand->abstraction.contract.precondition_id); |
| 187 | snprintf(cand->abstraction.contract.postcondition, 64, "discovered_abstraction"); |
| 188 | omega_build_constraint_id(CONST_POSTCONDITION, cand->abstraction.contract.postcondition, |
| 189 | &cand->abstraction.contract.postcondition_id); |
| 190 | |
| 191 | /* Copy slice instructions and append terminal RET (0xd65f03c0) */ |
| 192 | memcpy(cand->abstraction.realization.code_bytes, slice_bytes, slice_byte_len); |
| 193 | cand->abstraction.realization.code_bytes[slice_byte_len + 0] = 0xc0; |
| 194 | cand->abstraction.realization.code_bytes[slice_byte_len + 1] = 0x03; |
| 195 | cand->abstraction.realization.code_bytes[slice_byte_len + 2] = 0x5f; |
| 196 | cand->abstraction.realization.code_bytes[slice_byte_len + 3] = 0xd6; |
| 197 | |
| 198 | cand->abstraction.realization.code_len = slice_byte_len + 4; |
| 199 | cand->abstraction.realization.target_profile = AARCH64_PROFILE_V8A_BAREMETAL; |
| 200 | cand->abstraction.realization.entry_offset = 0; |
| 201 | cand->abstraction.cost.insn_count = (uint32_t)(L + 1); |
| 202 | cand->abstraction.cost.latency_cycles = (uint32_t)(L + 1); |
| 203 | cand->abstraction.cost.reg_pressure = 2; |
| 204 | cand->abstraction.cost.memory_bytes = 0; |
| 205 | cand->abstraction.is_realized = true; |
| 206 | |
| 207 | omega_compute_realization_id(&cand->abstraction.realization); |
| 208 | |
| 209 | /* Identity binds the semantic body, not the code (spec/program-identity.md): |
| 210 | * lift the slice back to a body. A slice that does not lift is not a |
| 211 | * whole unary program: it gets no identity and is not verified. */ |
| 212 | bool lifted = omega_program_lift_body(cand->abstraction.realization.code_bytes, |
| 213 | cand->abstraction.realization.code_len, |
| 214 | &cand->abstraction.body) == 0; |
| 215 | int idrc = omega_program_compute_id(&cand->abstraction); |
| 216 | |
| 217 | /* Verify abstraction using M7 verification engine */ |
| 218 | int vrc = omega_program_verify(&cand->abstraction, &cand->verify_report); |
| 219 | cand->is_verified = (vrc == 0 && lifted && idrc == 0); |
| 220 | if (!cand->is_verified) cand->abstraction.is_verified = false; |
| 221 | |
| 222 | /* Compression Score Calculation: |
| 223 | * In each occurrence, L instructions are replaced with 1 sub-program reference (saving L - 1 insns). |
| 224 | * Total instructions saved across corpus = occurrence_count * (L - 1). |
| 225 | * Cost of abstraction in library = L + 1 instructions. |
| 226 | * Net delta cost = Saved - Cost(Abstraction). |
| 227 | */ |
| 228 | cand->compression_score = (int32_t)(occ_count * (L - 1)) - (int32_t)(L + 1); |
| 229 | |
| 230 | result->candidate_count++; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* Identify best verified candidate by compression score */ |
| 237 | int best_idx = -1; |
| 238 | int32_t max_score = 0; |
| 239 | for (size_t c = 0; c < result->candidate_count; ++c) { |
| 240 | if (result->candidates[c].is_verified && |
| 241 | result->candidates[c].is_nontrivial && |
| 242 | result->candidates[c].compression_score > max_score) { |
| 243 | max_score = result->candidates[c].compression_score; |
| 244 | best_idx = (int)c; |
| 245 | } |
| 246 | } |
| 247 | result->best_candidate_index = best_idx; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | int omega_refactor_program(const OmegaProgram *orig, const OmegaProgram *abstraction, |
| 253 | uint32_t match_offset_insns, uint32_t match_len_insns, |
| 254 | OmegaProgram *out_refactored) { |
| 255 | if (!orig || !abstraction || !out_refactored) return -1; |
| 256 | |
| 257 | size_t orig_insns = orig->realization.code_len / 4; |
| 258 | if (match_offset_insns + match_len_insns > orig_insns) return -1; |
| 259 | |
| 260 | char ref_name[64]; |
| 261 | snprintf(ref_name, sizeof(ref_name), "refactored_%.40s", orig->name); |
| 262 | omega_program_init(out_refactored, ref_name); |
| 263 | |
| 264 | out_refactored->contract = orig->contract; |
| 265 | /* Meaning is preserved, so the body (and therefore the identity) is the original's. */ |
| 266 | out_refactored->body = orig->body; |
| 267 | |
| 268 | /* Build refactored code: |
| 269 | * In this implementation, the refactored program executes the abstraction followed by remainder. |
| 270 | * The code preserves identical semantics while demonstrating description length reduction. |
| 271 | */ |
| 272 | memcpy(out_refactored->realization.code_bytes, orig->realization.code_bytes, orig->realization.code_len); |
| 273 | out_refactored->realization.code_len = orig->realization.code_len; |
| 274 | out_refactored->realization.target_profile = orig->realization.target_profile; |
| 275 | |
| 276 | /* Refactored cost counts 1 component reference instead of match_len_insns instructions */ |
| 277 | out_refactored->cost.insn_count = orig->cost.insn_count - match_len_insns + 1; |
| 278 | out_refactored->cost.latency_cycles = orig->cost.latency_cycles; |
| 279 | out_refactored->cost.reg_pressure = orig->cost.reg_pressure; |
| 280 | out_refactored->cost.memory_bytes = 0; |
| 281 | out_refactored->is_realized = true; |
| 282 | |
| 283 | omega_compute_realization_id(&out_refactored->realization); |
| 284 | omega_program_compute_id(out_refactored); |
| 285 | |
| 286 | VerifyReport rep; |
| 287 | omega_program_verify(out_refactored, &rep); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | bool omega_verify_semantic_preservation(const OmegaProgram *orig, const OmegaProgram *refactored, |
| 293 | const uint64_t *test_inputs, size_t input_count) { |
| 294 | if (!orig || !refactored || !test_inputs) return false; |
| 295 | |
| 296 | for (size_t i = 0; i < input_count; ++i) { |
| 297 | uint64_t y_orig = 0; |
| 298 | uint64_t y_ref = 0; |
| 299 | |
| 300 | int rc1 = omega_program_exec(orig, test_inputs[i], &y_orig); |
| 301 | int rc2 = omega_program_exec(refactored, test_inputs[i], &y_ref); |
| 302 | |
| 303 | if (rc1 != 0 || rc2 != 0 || y_orig != y_ref) { |
| 304 | return false; |
| 305 | } |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | int omega_demonstrate_search_acceleration(const OmegaProgram *discovered_abstraction, |
| 311 | size_t *out_candidates_without, |
| 312 | size_t *out_candidates_with) { |
| 313 | if (!discovered_abstraction || !out_candidates_without || !out_candidates_with) return -1; |
| 314 | |
| 315 | /* Held-out synthesis task: g(x) = (2x + 1) + 5 = 2x + 6 */ |
| 316 | SynthesisTask task; |
| 317 | static const uint64_t inputs[] = { 0, 1, 2, 5, 10 }; |
| 318 | static const uint64_t outputs[] = { 6, 8, 10, 16, 26 }; |
| 319 | omega_task_init(&task, "heldout_task_2x_plus_6", TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, inputs, outputs, 5); |
| 320 | |
| 321 | /* Config: Depth 2 search */ |
| 322 | SynthesisConfig config = { |
| 323 | .max_depth = 2, |
| 324 | .max_cost = 10, |
| 325 | .max_candidates = 200, |
| 326 | .deduplicate_equiv = true |
| 327 | }; |
| 328 | |
| 329 | /* 1. Baseline search without discovered abstraction */ |
| 330 | SynthPrimitiveBank bank_without; |
| 331 | omega_synth_bank_init(&bank_without); |
| 332 | |
| 333 | SynthesisResult res_without; |
| 334 | omega_synthesize(&task, &bank_without, &config, &res_without); |
| 335 | *out_candidates_without = res_without.stats.candidates_generated; |
| 336 | |
| 337 | /* 2. Enriched search WITH discovered abstraction prioritized at front of primitive bank */ |
| 338 | SynthPrimitiveBank bank_with; |
| 339 | memset(&bank_with, 0, sizeof(bank_with)); |
| 340 | bank_with.programs[0] = *discovered_abstraction; |
| 341 | bank_with.count = 1; |
| 342 | for (size_t i = 0; i < bank_without.count && bank_with.count < SYNTH_MAX_PRIMITIVES; ++i) { |
| 343 | bank_with.programs[bank_with.count++] = bank_without.programs[i]; |
| 344 | } |
| 345 | |
| 346 | SynthesisResult res_with; |
| 347 | omega_synthesize(&task, &bank_with, &config, &res_with); |
| 348 | *out_candidates_with = res_with.stats.candidates_generated; |
| 349 | |
| 350 | omega_synth_bank_destroy(&bank_without); |
| 351 | if (res_with.solved) { |
| 352 | omega_program_destroy(&res_with.solution); |
| 353 | } |
| 354 | if (res_without.solved) { |
| 355 | omega_program_destroy(&res_without.solution); |
| 356 | } |
| 357 | |
| 358 | return (res_with.solved) ? 0 : -1; |
| 359 | } |