Impulse Graph C++ Engine 2.4.0
High-Performance Zero-Copy C-ABI Kernel and C++20 Fluent VM Engine
Loading...
Searching...
No Matches
impulse_compiler.hpp
Go to the documentation of this file.
1
9
10#ifndef IMPULSE_COMPILER_HPP
11#define IMPULSE_COMPILER_HPP
12
13
14#include "impulse_scm_ast.hpp"
15
16namespace impulse::compiler {
17
18// ---------------------------------------------------------------------------
19// Compiled Query Result Object
20// ---------------------------------------------------------------------------
22public:
23 std::vector<impulse_instruction_t> instructions;
25 uint16_t result_register{0};
26
27 [[nodiscard]] size_t instruction_count() const { return instructions.size(); }
28 [[nodiscard]] const impulse_instruction_t* data() const { return instructions.data(); }
29
33 [[nodiscard]] std::string to_impas_string(const GraphCatalog* catalog = nullptr) const {
34 std::ostringstream ss;
35 ss << "; =========================================================================\n";
36 ss << "; IMPULSE VM BYTECODE DISASSEMBLY (.impas) \n";
37 ss << "; =========================================================================\n";
38 ss << ".version 0.9.0\n";
39 ss << ".instructions " << instructions.size() << "\n\n";
40
41 for (size_t pc = 0; pc < instructions.size(); ++pc) {
42 const auto& inst = instructions[pc];
43
44 std::string op_name;
45 std::string comment;
46
47 switch (inst.opcode) {
48 case OP_HALT:
49 op_name = "OP_HALT";
50 comment = "Execution complete";
51 break;
52 case OP_NOP:
53 op_name = "OP_NOP";
54 break;
56 op_name = "OP_INIT_INPUT_NODE";
57 comment = "Seed input node -> R" + std::to_string(inst.dst_reg);
58 break;
59 case OP_CSR_WALK_2HOP: {
60 op_name = "OP_CSR_WALK_2HOP";
61 uint16_t r1 = inst.payload & 0xFFFF;
62 uint16_t r2 = (inst.payload >> 16) & 0xFFFF;
63 comment = "Fused 2-Hop CSR Walk via rel[" + std::to_string(r1) + "] -> rel[" + std::to_string(r2) + "]";
64 break;
65 }
66 case OP_ADAPTIVE_WALK: {
67 op_name = "OP_ADAPTIVE_WALK";
68 uint16_t src = inst.payload & 0xFFFF;
69 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
70 comment = "Adaptive Walk src=R" + std::to_string(src) + " -> dst=R" + std::to_string(inst.dst_reg) + " via rel[" + std::to_string(rel) + "]";
71 if (catalog) {
72 std::string name = catalog->get_relation_name(rel);
73 if (!name.empty()) comment += " (\"" + name + "\")";
74 }
75 break;
76 }
77 case OP_CSR_WALK: {
78 op_name = "OP_CSR_WALK";
79 uint16_t src = inst.payload & 0xFFFF;
80 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
81 comment = "Walk src=R" + std::to_string(src) + " -> dst=R" + std::to_string(inst.dst_reg) + " via rel[" + std::to_string(rel) + "]";
82 if (catalog) {
83 std::string name = catalog->get_relation_name(rel);
84 if (!name.empty()) comment += " (\"" + name + "\")";
85 }
86 break;
87 }
88 case OP_CSC_WALK: {
89 op_name = "OP_CSC_WALK";
90 uint16_t src = inst.payload & 0xFFFF;
91 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
92 comment = "Walk-CSC src=R" + std::to_string(src) + " -> dst=R" + std::to_string(inst.dst_reg) + " via rel[" + std::to_string(rel) + "]";
93 if (catalog) {
94 std::string name = catalog->get_relation_name(rel);
95 if (!name.empty()) comment += " (\"" + name + "\")";
96 }
97 break;
98 }
100 op_name = "OP_CSR_WALK_FILTERED";
101 uint16_t src = inst.payload & 0xFFFF;
102 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
103 comment = "Filtered CSR Walk src=R" + std::to_string(src) + " via rel[" + std::to_string(rel) + "]";
104 break;
105 }
107 op_name = "OP_CREATE_SCRATCH_INDEX";
108 break;
110 op_name = "OP_DROP_SCRATCH_INDEX";
111 break;
113 op_name = "OP_VECTOR_TIME_VALID_AT";
114 break;
116 op_name = "OP_COLLECT_BITSET";
117 comment = "Collect active result bitset from R" + std::to_string(inst.dst_reg);
118 break;
120 op_name = "OP_LANE_INIT_SEEDS";
121 comment = "Init multi-lane seeds R" + std::to_string(inst.payload & 0xFFFF) + " -> R" + std::to_string(inst.dst_reg);
122 break;
124 op_name = "OP_LANE_INIT_GROUPS";
125 comment = "Init group steiner lanes R" + std::to_string(inst.payload & 0xFFFF) + " -> R" + std::to_string(inst.dst_reg);
126 break;
128 op_name = "OP_LANE_SET_EDGE_MASK";
129 comment = "Set counterfactual edge mask on R" + std::to_string(inst.dst_reg);
130 break;
131 case OP_LANE_CSR_WALK: {
132 op_name = "OP_LANE_CSR_WALK";
133 uint16_t src = inst.payload & 0xFFFF;
134 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
135 comment = "Multi-Lane CSR Walk src=R" + std::to_string(src) + " -> dst=R" + std::to_string(inst.dst_reg) + " via rel[" + std::to_string(rel) + "]";
136 if (catalog) {
137 std::string name = catalog->get_relation_name(rel);
138 if (!name.empty()) comment += " (\"" + name + "\")";
139 }
140 break;
141 }
142 case OP_LANE_CSC_WALK: {
143 op_name = "OP_LANE_CSC_WALK";
144 uint16_t src = inst.payload & 0xFFFF;
145 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
146 comment = "Multi-Lane CSC Walk src=R" + std::to_string(src) + " -> dst=R" + std::to_string(inst.dst_reg) + " via rel[" + std::to_string(rel) + "]";
147 if (catalog) {
148 std::string name = catalog->get_relation_name(rel);
149 if (!name.empty()) comment += " (\"" + name + "\")";
150 }
151 break;
152 }
154 op_name = "OP_LANE_PRUNE_MASK";
155 comment = "Prune multi-lane frontier R" + std::to_string(inst.payload & 0xFF) + " -> R" + std::to_string(inst.dst_reg);
156 break;
158 op_name = "OP_LANE_FILTER_QUORUM";
159 comment = "Filter lane quorum >= " + std::to_string((inst.payload >> 8) & 0xFF) + " -> R" + std::to_string(inst.dst_reg);
160 break;
162 op_name = "OP_LANE_REDUCE_AND";
163 comment = "Reduce bitwise AND across all nodes -> R" + std::to_string(inst.dst_reg);
164 break;
166 op_name = "OP_LANE_REDUCE_ATTR";
167 comment = "Reduce per-lane attribute -> R" + std::to_string(inst.dst_reg);
168 break;
170 op_name = "OP_LANE_SET_COVER";
171 comment = "Compute lane set cover -> R" + std::to_string(inst.dst_reg);
172 break;
174 op_name = "OP_VECTOR_ANN_SEARCH";
175 comment = "Vamana ANN search q=R" + std::to_string(inst.payload & 0xFF) + " top_k=" + std::to_string((inst.payload >> 8) & 0xFF) + " -> R" + std::to_string(inst.dst_reg);
176 break;
178 op_name = "OP_VECTOR_RERANK_DOT";
179 comment = "SIMD vector rerank cand=R" + std::to_string(inst.payload & 0xFF) + " q=R" + std::to_string((inst.payload >> 8) & 0xFF) + " -> R" + std::to_string(inst.dst_reg);
180 break;
182 op_name = "OP_FIXPOINT_KLEENE_STAR";
183 uint16_t src = inst.payload & 0xFF;
184 uint16_t rel = (inst.payload >> 16) & 0xFFFF;
185 comment = "Fixpoint Kleene* src=R" + std::to_string(src) + " -> dst=R" + std::to_string(inst.dst_reg) + " via rel[" + std::to_string(rel) + "]";
186 if (catalog) {
187 std::string name = catalog->get_relation_name(rel);
188 if (!name.empty()) comment += " (\"" + name + "\")";
189 }
190 break;
191 }
193 op_name = "OP_SET_DIFFERENCE";
194 comment = "BitSet difference R" + std::to_string(inst.dst_reg) + " &= ~R" + std::to_string(inst.payload & 0xFFFF);
195 break;
196 case OP_SET_UNION:
197 op_name = "OP_SET_UNION";
198 comment = "BitSet union R" + std::to_string(inst.dst_reg) + " |= R" + std::to_string(inst.payload & 0xFFFF);
199 break;
200 case OP_SET_INTERSECT:
201 op_name = "OP_SET_INTERSECT";
202 comment = "BitSet intersect R" + std::to_string(inst.dst_reg) + " &= R" + std::to_string(inst.payload & 0xFFFF);
203 break;
204 case OP_MOV:
205 op_name = "OP_MOV";
206 comment = "Move R" + std::to_string(inst.payload & 0xFFFF) + " -> R" + std::to_string(inst.dst_reg);
207 break;
208 default:
209 op_name = "OP_0x" + std::to_string(inst.opcode);
210 break;
211 }
212
213 char line_buf[256];
214 std::snprintf(line_buf, sizeof(line_buf), " 0x%04x: %-24s flags=0x%02x, dst=R%-2d, payload=0x%08x",
215 static_cast<unsigned int>(pc),
216 op_name.c_str(),
217 static_cast<unsigned int>(inst.flags),
218 static_cast<int>(inst.dst_reg),
219 static_cast<unsigned int>(inst.payload));
220 ss << line_buf;
221
222 if (!comment.empty()) {
223 ss << " ; " << comment;
224 if (inst.flags & IMPULSE_VM_OP_FLAG_INPUT_SEED) ss << " [seed-inlined]";
225 if (inst.flags & IMPULSE_VM_OP_FLAG_HALT_ON_EMPTY) ss << " [early-exit]";
226 }
227 ss << "\n";
228 }
229 ss << "; =========================================================================\n";
230 return ss.str();
231 }
232};
233
234// ---------------------------------------------------------------------------
235// 7-Stage Compiler Pipeline
236// ---------------------------------------------------------------------------
238public:
243 const std::shared_ptr<ScmProgram>& program,
244 const GraphCatalog* catalog = nullptr,
245 const std::unordered_map<std::string, double>& params = {},
247 {
248 if (!program || program->steps.empty()) {
249 throw std::invalid_argument("Empty ImpScheme program");
250 }
251 IMPULSE_ASSERT(program != nullptr);
252 IMPULSE_ASSERT(!program->steps.empty());
253
254 CompilerOptions options = raw_options.effective();
255
256 // Pass 1: Pre-bind validation
257 validate_ast(program);
258
259 // Pass 2: Parameter binding & Constant folding
260 AstPtr ast = pass_parameter_binding(program, params, options);
261 IMPULSE_ASSERT(ast != nullptr);
262
263 // Pass 3: Multi-hop Kernel Fusion (2-Hop Walk)
264 if (options.enable_kernel_fusion) {
265 ast = pass_kernel_fusion(ast, catalog, options);
266 IMPULSE_ASSERT(ast != nullptr);
267 }
268
269 // Pass 4: Physical Binding (Map relation names to catalog IDs)
270 ast = pass_physical_binding(ast, catalog);
271 IMPULSE_ASSERT(ast != nullptr);
272
273 // Pass 5: Direction Selection & CSC Inversion Check
274 if (options.enable_direction_selection) {
275 ast = pass_direction_selection(ast, catalog);
276 IMPULSE_ASSERT(ast != nullptr);
277 }
278
279 // Pass 6: Register Allocation & L1 Cache Ping-Ponging (R0 <-> R1)
280 ast = pass_register_allocation(ast, options);
281 IMPULSE_ASSERT(ast != nullptr);
282
283 // Pass 7: Bytecode Emission with Seed Inlining & Early Exit Flags
284 CompiledImpulseProgram result = emit_bytecode(ast, options);
285 IMPULSE_AUDIT_ASSERT(result.instruction_count() > 0);
286 IMPULSE_AUDIT_ASSERT(result.instructions.back().opcode == OP_HALT);
287 return result;
288 }
289
290private:
291 static void validate_ast(const std::shared_ptr<ScmProgram>& prog) {
292 IMPULSE_ASSERT(prog != nullptr);
293 if (prog->steps.empty()) {
294 throw std::invalid_argument("ScmProgram must contain at least one step");
295 }
296 IMPULSE_AUDIT_ASSERT(!prog->steps.empty());
297 }
298
299
300 static AstPtr pass_parameter_binding_node(const AstPtr& step, const std::unordered_map<std::string, double>& params) {
301 if (!step) return nullptr;
302 if (step->kind() == NodeKind::WALK) {
303 auto w = std::static_pointer_cast<ScmWalk>(step);
304 auto new_w = std::make_shared<ScmWalk>(*w);
305 if (new_w->predicate && new_w->predicate->kind() == NodeKind::VECTOR_FILTER) {
306 auto vf = std::static_pointer_cast<ScmVectorFilter>(new_w->predicate);
307 auto new_vf = std::make_shared<ScmVectorFilter>(*vf);
308 if (new_vf->threshold && new_vf->threshold->kind() == NodeKind::SYMBOL) {
309 auto sym = std::static_pointer_cast<ScmSymbol>(new_vf->threshold);
310 auto it = params.find(sym->name);
311 if (it != params.end()) {
312 new_vf->threshold = ScmLiteral::of_float(it->second);
313 }
314 }
315 new_w->predicate = new_vf;
316 }
317 return new_w;
318 } else if (step->kind() == NodeKind::LET) {
319 auto let = std::static_pointer_cast<ScmLet>(step);
320 auto new_let = std::make_shared<ScmLet>(*let);
321 for (auto& init : new_let->inits) init = pass_parameter_binding_node(init, params);
322 for (auto& st : new_let->body) st = pass_parameter_binding_node(st, params);
323 return new_let;
324 } else if (step->kind() == NodeKind::SET) {
325 auto set_n = std::static_pointer_cast<ScmSet>(step);
326 auto new_set = std::make_shared<ScmSet>(*set_n);
327 new_set->expr = pass_parameter_binding_node(set_n->expr, params);
328 return new_set;
329 } else if (step->kind() == NodeKind::LOOP_WHILE) {
330 auto loop = std::static_pointer_cast<ScmLoopWhile>(step);
331 auto new_loop = std::make_shared<ScmLoopWhile>(*loop);
332 new_loop->condition = pass_parameter_binding_node(loop->condition, params);
333 for (auto& st : new_loop->body) st = pass_parameter_binding_node(st, params);
334 return new_loop;
335 } else if (step->kind() == NodeKind::RETURN) {
336 auto ret = std::static_pointer_cast<ScmReturn>(step);
337 auto new_ret = std::make_shared<ScmReturn>(*ret);
338 new_ret->expr = pass_parameter_binding_node(ret->expr, params);
339 return new_ret;
340 }
341 return step;
342 }
343
344 static AstPtr pass_parameter_binding(
345 const std::shared_ptr<ScmProgram>& prog,
346 const std::unordered_map<std::string, double>& params,
347 const CompilerOptions& /*options*/)
348 {
349 std::vector<AstPtr> new_steps;
350 for (const auto& step : prog->steps) {
351 new_steps.push_back(pass_parameter_binding_node(step, params));
352 }
353 return std::make_shared<ScmProgram>(std::move(new_steps));
354 }
355
356
357 static AstPtr pass_kernel_fusion(
358 const AstPtr& root,
359 const GraphCatalog* catalog,
360 const CompilerOptions& options)
361 {
362 auto prog = std::static_pointer_cast<ScmProgram>(root);
363 std::vector<AstPtr> fused_steps;
364 size_t i = 0;
365
366 while (i < prog->steps.size()) {
367 if (i + 1 < prog->steps.size() &&
368 prog->steps[i]->kind() == NodeKind::WALK &&
369 prog->steps[i + 1]->kind() == NodeKind::WALK)
370 {
371 auto w1 = std::static_pointer_cast<ScmWalk>(prog->steps[i]);
372 auto w2 = std::static_pointer_cast<ScmWalk>(prog->steps[i + 1]);
373
374 if (w1->direction == WalkDirection::FORWARD_CSR &&
375 w2->direction == WalkDirection::FORWARD_CSR &&
376 !w1->predicate && !w2->predicate)
377 {
378 double mult1 = catalog ? catalog->get_multiplicity(w1->relation_name) : 1.0;
379 if (mult1 <= options.fused_2hop_max_multiplicity_threshold) {
380 auto fused = std::make_shared<ScmWalk2Hop>(w1->relation_name, w2->relation_name);
381 fused_steps.push_back(fused);
382 i += 2;
383 continue;
384 }
385 }
386 }
387 fused_steps.push_back(prog->steps[i]);
388 i++;
389 }
390 return std::make_shared<ScmProgram>(std::move(fused_steps));
391 }
392
393 static AstPtr bind_node_physical(const AstPtr& step, const GraphCatalog* catalog) {
394 if (!step) return nullptr;
395 if (step->kind() == NodeKind::WALK) {
396 auto w = std::static_pointer_cast<ScmWalk>(step);
397 auto new_w = std::make_shared<ScmWalk>(*w);
398 new_w->physical_rel_id = catalog ? catalog->get_relation_id(w->relation_name) : 0;
399 return new_w;
400 } else if (step->kind() == NodeKind::WALK_2HOP) {
401 auto w2 = std::static_pointer_cast<ScmWalk2Hop>(step);
402 auto new_w2 = std::make_shared<ScmWalk2Hop>(*w2);
403 new_w2->rel1_id = catalog ? catalog->get_relation_id(w2->rel1_name) : 0;
404 new_w2->rel2_id = catalog ? catalog->get_relation_id(w2->rel2_name) : 1;
405 return new_w2;
406 } else if (step->kind() == NodeKind::LANE_WALK) {
407 auto lw = std::static_pointer_cast<ScmLaneWalk>(step);
408 auto new_lw = std::make_shared<ScmLaneWalk>(*lw);
409 if (new_lw->physical_rel_id < 0) {
410 new_lw->physical_rel_id = catalog ? std::max(0, catalog->get_relation_id(lw->relation_name)) : 0;
411 }
412 return new_lw;
413 } else if (step->kind() == NodeKind::LANE_SET_EDGE_MASK) {
414 auto em = std::static_pointer_cast<ScmLaneSetEdgeMask>(step);
415 auto new_em = std::make_shared<ScmLaneSetEdgeMask>(*em);
416 if (new_em->physical_rel_id < 0) {
417 new_em->physical_rel_id = catalog ? std::max(0, catalog->get_relation_id(em->relation_name)) : 0;
418 }
419 return new_em;
420 } else if (step->kind() == NodeKind::FIXPOINT) {
421 auto fp = std::static_pointer_cast<ScmFixpoint>(step);
422 auto new_fp = std::make_shared<ScmFixpoint>(*fp);
423 if (new_fp->physical_rel_id < 0) {
424 new_fp->physical_rel_id = catalog ? std::max(0, catalog->get_relation_id(fp->relation_name)) : 0;
425 }
426 for (auto& bs : new_fp->body_steps) {
427 bs = bind_node_physical(bs, catalog);
428 }
429 return new_fp;
430 } else if (step->kind() == NodeKind::SET_OP) {
431 auto so = std::static_pointer_cast<ScmSetOp>(step);
432 auto new_so = std::make_shared<ScmSetOp>(*so);
433 new_so->lhs = bind_node_physical(so->lhs, catalog);
434 new_so->rhs = bind_node_physical(so->rhs, catalog);
435 return new_so;
436 } else if (step->kind() == NodeKind::PROGRAM) {
437 auto sub = std::static_pointer_cast<ScmProgram>(step);
438 std::vector<AstPtr> sub_steps;
439 for (const auto& s : sub->steps) {
440 sub_steps.push_back(bind_node_physical(s, catalog));
441 }
442 return std::make_shared<ScmProgram>(std::move(sub_steps));
443 }
444 return step;
445 }
446
447 static AstPtr pass_physical_binding(const AstPtr& root, const GraphCatalog* catalog) {
448 auto prog = std::static_pointer_cast<ScmProgram>(root);
449 std::vector<AstPtr> bound_steps;
450
451 for (const auto& step : prog->steps) {
452 bound_steps.push_back(bind_node_physical(step, catalog));
453 }
454 return std::make_shared<ScmProgram>(std::move(bound_steps));
455 }
456
457 static AstPtr pass_direction_selection(const AstPtr& root, const GraphCatalog* /*catalog*/) {
458 if (!root) return nullptr;
459 auto prog = std::static_pointer_cast<ScmProgram>(root);
460 if (!prog) return nullptr;
461 std::vector<AstPtr> opt_steps;
462 for (const auto& step : prog->steps) {
463 if (!step) continue;
464 if (step->kind() == NodeKind::WALK) {
465 auto w = std::static_pointer_cast<ScmWalk>(step);
466 auto new_w = std::make_shared<ScmWalk>(*w);
467 if (new_w->direction == WalkDirection::FORWARD_CSR && !new_w->predicate) {
468 new_w->is_adaptive = true;
469 }
470 opt_steps.push_back(new_w);
471 } else {
472 opt_steps.push_back(step);
473 }
474 }
475 return std::make_shared<ScmProgram>(std::move(opt_steps));
476 }
477
478 static AstPtr alloc_set_branch(const AstPtr& branch, uint16_t input_reg, uint16_t& next_temp_reg, uint16_t& out_res_reg) {
479 if (!branch) {
480 out_res_reg = input_reg;
481 return nullptr;
482 }
483 if (branch->kind() == NodeKind::WALK) {
484 auto w = std::make_shared<ScmWalk>(*std::static_pointer_cast<ScmWalk>(branch));
485 w->src_reg = input_reg;
486 w->dst_reg = next_temp_reg++;
487 out_res_reg = w->dst_reg;
488 return w;
489 }
490 if (branch->kind() == NodeKind::PROGRAM) {
491 auto sub = std::static_pointer_cast<ScmProgram>(branch);
492 std::vector<AstPtr> sub_steps;
493 uint16_t cur = input_reg;
494 for (const auto& s : sub->steps) {
495 uint16_t step_res = cur;
496 sub_steps.push_back(alloc_set_branch(s, cur, next_temp_reg, step_res));
497 cur = step_res;
498 }
499 out_res_reg = cur;
500 return std::make_shared<ScmProgram>(std::move(sub_steps));
501 }
502 if (branch->kind() == NodeKind::SET_OP) {
503 auto so = std::make_shared<ScmSetOp>(*std::static_pointer_cast<ScmSetOp>(branch));
504 uint16_t lhs_res = input_reg;
505 uint16_t rhs_res = input_reg;
506 so->lhs = alloc_set_branch(so->lhs, input_reg, next_temp_reg, lhs_res);
507 so->rhs = alloc_set_branch(so->rhs, input_reg, next_temp_reg, rhs_res);
508 out_res_reg = lhs_res;
509 return so;
510 }
511 out_res_reg = input_reg;
512 return branch;
513 }
514
515 static AstPtr pass_register_allocation(const AstPtr& root, const CompilerOptions& options) {
516 auto prog = std::static_pointer_cast<ScmProgram>(root);
517 std::vector<AstPtr> alloc_steps;
518 uint16_t current_reg = 0;
519
520 for (size_t idx = 0; idx < prog->steps.size(); ++idx) {
521 const auto& step = prog->steps[idx];
522 if (step->kind() == NodeKind::WALK) {
523 auto w = std::static_pointer_cast<ScmWalk>(step);
524 auto new_w = std::make_shared<ScmWalk>(*w);
525 new_w->src_reg = current_reg;
526 if (options.enable_register_ping_pong) {
527 new_w->dst_reg = (current_reg == 0) ? 1 : 0;
528 } else {
529 new_w->dst_reg = static_cast<uint16_t>(idx + 1);
530 }
531 current_reg = new_w->dst_reg;
532 alloc_steps.push_back(new_w);
533 } else if (step->kind() == NodeKind::WALK_2HOP) {
534 auto w2 = std::static_pointer_cast<ScmWalk2Hop>(step);
535 auto new_w2 = std::make_shared<ScmWalk2Hop>(*w2);
536 new_w2->src_reg = current_reg;
537 if (options.enable_register_ping_pong) {
538 new_w2->dst_reg = (current_reg == 0) ? 1 : 0;
539 } else {
540 new_w2->dst_reg = static_cast<uint16_t>(idx + 1);
541 }
542 current_reg = new_w2->dst_reg;
543 alloc_steps.push_back(new_w2);
544 } else if (step->kind() == NodeKind::COLLECT) {
545 auto c = std::static_pointer_cast<ScmCollect>(step);
546 auto new_c = std::make_shared<ScmCollect>(*c);
547 new_c->target_reg = current_reg;
548 alloc_steps.push_back(new_c);
549 } else if (step->kind() == NodeKind::LANE_INIT_SEEDS) {
550 auto n = std::make_shared<ScmLaneInitSeeds>(*std::static_pointer_cast<ScmLaneInitSeeds>(step));
551 n->dst_reg = current_reg;
552 alloc_steps.push_back(n);
553 } else if (step->kind() == NodeKind::LANE_INIT_GROUPS) {
554 auto n = std::make_shared<ScmLaneInitGroups>(*std::static_pointer_cast<ScmLaneInitGroups>(step));
555 n->dst_reg = current_reg;
556 alloc_steps.push_back(n);
557 } else if (step->kind() == NodeKind::LANE_SET_EDGE_MASK) {
558 auto n = std::make_shared<ScmLaneSetEdgeMask>(*std::static_pointer_cast<ScmLaneSetEdgeMask>(step));
559 n->src_reg = current_reg;
560 n->dst_reg = current_reg;
561 alloc_steps.push_back(n);
562 } else if (step->kind() == NodeKind::LANE_WALK) {
563 auto n = std::make_shared<ScmLaneWalk>(*std::static_pointer_cast<ScmLaneWalk>(step));
564 n->src_reg = current_reg;
565 n->dst_reg = options.enable_register_ping_pong ? ((current_reg == 0) ? 1 : 0) : static_cast<uint16_t>(idx + 1);
566 current_reg = n->dst_reg;
567 alloc_steps.push_back(n);
568 } else if (step->kind() == NodeKind::LANE_PRUNE_MASK) {
569 auto n = std::make_shared<ScmLanePruneMask>(*std::static_pointer_cast<ScmLanePruneMask>(step));
570 n->src_reg = current_reg;
571 n->dst_reg = current_reg;
572 alloc_steps.push_back(n);
573 } else if (step->kind() == NodeKind::LANE_FILTER_QUORUM) {
574 auto n = std::make_shared<ScmLaneFilterQuorum>(*std::static_pointer_cast<ScmLaneFilterQuorum>(step));
575 n->src_reg = current_reg;
576 n->dst_reg = options.enable_register_ping_pong ? ((current_reg == 0) ? 1 : 0) : static_cast<uint16_t>(idx + 1);
577 current_reg = n->dst_reg;
578 alloc_steps.push_back(n);
579 } else if (step->kind() == NodeKind::LANE_REDUCE_AND) {
580 auto n = std::make_shared<ScmLaneReduceAnd>(*std::static_pointer_cast<ScmLaneReduceAnd>(step));
581 n->src_reg = current_reg;
582 n->dst_reg = (current_reg == 0) ? 1 : 0;
583 alloc_steps.push_back(n);
584 } else if (step->kind() == NodeKind::LANE_REDUCE_ATTR) {
585 auto n = std::make_shared<ScmLaneReduceAttr>(*std::static_pointer_cast<ScmLaneReduceAttr>(step));
586 n->src_reg = current_reg;
587 n->dst_reg = (current_reg == 0) ? 1 : 0;
588 alloc_steps.push_back(n);
589 } else if (step->kind() == NodeKind::LANE_SET_COVER) {
590 auto n = std::make_shared<ScmLaneSetCover>(*std::static_pointer_cast<ScmLaneSetCover>(step));
591 n->src_reg = current_reg;
592 n->dst_reg = (current_reg == 0) ? 1 : 0;
593 alloc_steps.push_back(n);
594 } else if (step->kind() == NodeKind::VECTOR_ANN_SEARCH) {
595 auto n = std::make_shared<ScmVectorAnnSearch>(*std::static_pointer_cast<ScmVectorAnnSearch>(step));
596 n->dst_reg = current_reg;
597 alloc_steps.push_back(n);
598 } else if (step->kind() == NodeKind::VECTOR_RERANK_DOT) {
599 auto n = std::make_shared<ScmVectorRerankDot>(*std::static_pointer_cast<ScmVectorRerankDot>(step));
600 n->src_reg = current_reg;
601 n->dst_reg = (current_reg == 0) ? 1 : 0;
602 current_reg = n->dst_reg;
603 alloc_steps.push_back(n);
604 } else if (step->kind() == NodeKind::FIXPOINT) {
605 auto fp = std::make_shared<ScmFixpoint>(*std::static_pointer_cast<ScmFixpoint>(step));
606 fp->src_reg = current_reg;
607 fp->dst_reg = options.enable_register_ping_pong ? ((current_reg == 0) ? 1 : 0) : static_cast<uint16_t>(idx + 1);
608 current_reg = fp->dst_reg;
609 alloc_steps.push_back(fp);
610 } else if (step->kind() == NodeKind::SET_OP) {
611 uint16_t next_temp_reg = std::max<uint16_t>(1, static_cast<uint16_t>(current_reg + 1));
612 uint16_t set_res = current_reg;
613 alloc_steps.push_back(alloc_set_branch(step, current_reg, next_temp_reg, set_res));
614 current_reg = set_res;
615 } else {
616 alloc_steps.push_back(step);
617 }
618 }
619 return std::make_shared<ScmProgram>(std::move(alloc_steps));
620 }
621
622
623 struct CompileContext {
624 std::vector<impulse_instruction_t> instructions;
625 std::unordered_map<std::string, uint16_t> env;
626 uint16_t next_reg = 0;
627 uint16_t result_reg = 0;
628
629 uint16_t get_reg(const std::string& name) {
630 if (env.find(name) == env.end()) {
631 env[name] = next_reg++;
632 }
633 return env[name];
634 }
635 };
636
637 static void emit_node(const AstPtr& step, CompileContext& ctx, const CompilerOptions& options) {
638 // Malformed ImpScheme (e.g. unresolvable loop condition / let init / set! expression) yields
639 // null AST children; reject with an exception (mapped to an error status by the C API).
640 if (!step) throw std::runtime_error("ImpScheme: unresolvable (null) AST node during emission");
641 if (step->kind() == NodeKind::WALK) {
642 auto w = std::static_pointer_cast<ScmWalk>(step);
643 bool is_first_walk = (ctx.instructions.empty() && !ctx.env.count("frontier") && w->src_reg == 0);
644 if (is_first_walk && !options.enable_seed_inlining) {
645 impulse_instruction_t seed_inst{};
646 seed_inst.opcode = OP_INIT_INPUT_NODE;
647 seed_inst.dst_reg = 0;
648 ctx.instructions.push_back(seed_inst);
649 }
651 bool is_stream = !w->shader_steps.empty();
652 uint8_t opcode = (w->direction == WalkDirection::FORWARD_CSR)
653 ? (is_stream ? OP_CSR_WALK_STREAM : OP_CSR_WALK)
654 : (is_stream ? OP_CSC_WALK_STREAM : OP_CSC_WALK);
655 if (w->is_adaptive && !is_stream) {
656 opcode = OP_ADAPTIVE_WALK;
657 } else if (w->predicate && !is_stream) {
658 opcode = OP_CSR_WALK;
659 }
660 inst.opcode = opcode;
661 // For now, assume it modifies R2 (frontier) and reads from R2, or reads from env
662 inst.dst_reg = w->dst_reg; // Default
663 if (ctx.env.count("frontier")) {
664 inst.dst_reg = ctx.env["frontier"];
665 w->src_reg = ctx.env["frontier"];
666 }
667 uint16_t rel_id = static_cast<uint16_t>(std::max(0, w->physical_rel_id));
668 inst.payload = (static_cast<uint32_t>(rel_id) << 16) | static_cast<uint32_t>(w->src_reg);
669 inst.flags = 0;
670 if ((is_first_walk || w->inlined_seed) && options.enable_seed_inlining) {
671 inst.flags |= IMPULSE_VM_OP_FLAG_INPUT_SEED;
672 }
673 if (options.enable_early_exit && !ctx.env.count("frontier") && !w->inlined_seed) {
675 }
676
677 if (is_stream) {
678 size_t walk_idx = ctx.instructions.size();
679 ctx.instructions.push_back(inst);
680
681 impulse_instruction_t begin{};
682 begin.opcode = OP_STREAM_FUNC_BEGIN;
683 ctx.instructions.push_back(begin);
684
685 int shader_pc_start = static_cast<int>(ctx.instructions.size());
686 if (shader_pc_start > 255) {
687 throw std::runtime_error("Edge stream shader PC start exceeds 8-bit flags limit (255)");
688 }
689 ctx.instructions[walk_idx].flags = static_cast<uint8_t>(shader_pc_start);
690
691 int next_sreg = 0;
692 for (const auto& sstep : w->shader_steps) {
693 if (sstep->kind() == NodeKind::STREAM_FILTER) {
694 int reg = next_sreg++;
695 impulse_instruction_t s_load{};
696 s_load.opcode = OP_STREAM_LOAD_SRC_ID;
697 s_load.dst_reg = static_cast<uint16_t>(reg);
698 ctx.instructions.push_back(s_load);
699
700 impulse_instruction_t s_filt{};
701 s_filt.opcode = OP_STREAM_FILTER;
702 s_filt.dst_reg = static_cast<uint16_t>(reg);
703 ctx.instructions.push_back(s_filt);
704 } else if (sstep->kind() == NodeKind::STREAM_PROJECT) {
705 int reg = next_sreg++;
706 impulse_instruction_t s_load{};
707 s_load.opcode = OP_STREAM_LOAD_SRC;
708 s_load.dst_reg = static_cast<uint16_t>(reg);
709 ctx.instructions.push_back(s_load);
710
711 impulse_instruction_t s_proj{};
712 s_proj.opcode = OP_STREAM_SCATTER_REDUCE;
713 s_proj.dst_reg = static_cast<uint16_t>(reg);
714 s_proj.payload = (5 & 0xFFFF) | ((0 & 0xFF) << 16);
715 ctx.instructions.push_back(s_proj);
716 }
717 }
718
719 impulse_instruction_t yield_inst{};
720 yield_inst.opcode = OP_STREAM_YIELD;
721 ctx.instructions.push_back(yield_inst);
722
723 impulse_instruction_t end_inst{};
724 end_inst.opcode = OP_STREAM_FUNC_END;
725 ctx.instructions.push_back(end_inst);
726 } else {
727 ctx.instructions.push_back(inst);
728 }
729 ctx.result_reg = inst.dst_reg;
730 } else if (step->kind() == NodeKind::WALK_2HOP) {
731 auto w2 = std::static_pointer_cast<ScmWalk2Hop>(step);
732 bool is_first_walk = (ctx.instructions.empty() && w2->src_reg == 0);
733 if (is_first_walk && !options.enable_seed_inlining) {
734 impulse_instruction_t seed_inst{};
735 seed_inst.opcode = OP_INIT_INPUT_NODE;
736 seed_inst.dst_reg = 0;
737 ctx.instructions.push_back(seed_inst);
738 }
740 inst.opcode = OP_CSR_WALK_2HOP;
741 inst.dst_reg = w2->dst_reg;
742 uint16_t r1 = static_cast<uint16_t>(std::max(0, w2->rel1_id));
743 uint16_t r2 = static_cast<uint16_t>(std::max(0, w2->rel2_id));
744 inst.payload = static_cast<uint32_t>(r1) | (static_cast<uint32_t>(r2) << 16);
745 inst.flags = 0;
746 if (options.enable_seed_inlining && w2->src_reg == 0) {
747 inst.flags |= IMPULSE_VM_OP_FLAG_INPUT_SEED;
748 }
749 if (options.enable_early_exit) {
751 }
752 ctx.instructions.push_back(inst);
753 ctx.result_reg = inst.dst_reg;
754 } else if (step->kind() == NodeKind::COLLECT) {
755 auto c = std::static_pointer_cast<ScmCollect>(step);
756 if (c->mode == ScmCollect::Mode::NONE) {
757 // Preserve register, do not emit collect opcode
758 } else {
760 if (c->mode == ScmCollect::Mode::BITSET) {
761 inst.opcode = OP_COLLECT_BITSET; // 0x90
762 } else if (c->mode == ScmCollect::Mode::ARRAY || c->mode == ScmCollect::Mode::VECTOR || c->mode == ScmCollect::Mode::LIST) {
763 inst.opcode = OP_COLLECT_ARRAY; // 0x91
764 } else if (c->mode == ScmCollect::Mode::COUNT) {
765 inst.opcode = OP_SET_CARDINALITY; // 0x33
766 } else if (c->mode == ScmCollect::Mode::VALUE_MAP) {
767 inst.opcode = OP_COLLECT_VALUE_MAP; // 0x93
768 } else {
769 inst.opcode = OP_COLLECT_BITSET;
770 }
771 uint16_t src_reg = ctx.result_reg;
772 uint16_t dst_reg = (c->mode == ScmCollect::Mode::COUNT)
773 ? static_cast<uint16_t>((src_reg == 0) ? 1 : 0)
774 : src_reg;
775 inst.dst_reg = dst_reg;
776 inst.payload = src_reg;
777 inst.flags = 0;
778 ctx.instructions.push_back(inst);
779 ctx.result_reg = dst_reg;
780 }
781 } else if (step->kind() == NodeKind::LET) {
782 auto let = std::static_pointer_cast<ScmLet>(step);
783 for (size_t i = 0; i < let->vars.size(); ++i) {
784 uint16_t r = ctx.get_reg(let->vars[i]);
785 emit_node(let->inits[i], ctx, options);
786 if (ctx.result_reg != r) {
788 mov.opcode = OP_MOV;
789 mov.dst_reg = r;
790 mov.payload = ctx.result_reg;
791 ctx.instructions.push_back(mov);
792 }
793 }
794 for (const auto& stmt : let->body) {
795 emit_node(stmt, ctx, options);
796 }
797 } else if (step->kind() == NodeKind::SET) {
798 auto set_node = std::static_pointer_cast<ScmSet>(step);
799 emit_node(set_node->expr, ctx, options);
800 uint16_t r = ctx.get_reg(set_node->var);
801 if (ctx.result_reg != r) {
803 mov.opcode = OP_MOV;
804 mov.dst_reg = r;
805 mov.payload = ctx.result_reg;
806 ctx.instructions.push_back(mov);
807 }
808 } else if (step->kind() == NodeKind::LOOP_WHILE) {
809 auto loop = std::static_pointer_cast<ScmLoopWhile>(step);
810 size_t start_pc = ctx.instructions.size();
811
812 // Emit condition
813 emit_node(loop->condition, ctx, options);
814
815 // JZ instruction (placeholder offset)
817 jz.opcode = OP_JZ;
818 jz.dst_reg = ctx.result_reg; // condition result
819 jz.payload = 0; // Patch later
820 size_t jz_idx = ctx.instructions.size();
821 ctx.instructions.push_back(jz);
822
823 // Body
824 for (const auto& stmt : loop->body) {
825 emit_node(stmt, ctx, options);
826 }
827
828 // JMP back to start
830 jmp.opcode = OP_JMP;
831 jmp.payload = static_cast<uint32_t>(start_pc) - static_cast<uint32_t>(ctx.instructions.size()); // Relative jump backwards
832 ctx.instructions.push_back(jmp);
833
834 // Patch JZ
835 ctx.instructions[jz_idx].payload = static_cast<uint32_t>(ctx.instructions.size()) - static_cast<uint32_t>(jz_idx);
836 } else if (step->kind() == NodeKind::SET_OP) {
837 auto set_op = std::static_pointer_cast<ScmSetOp>(step);
838 emit_node(set_op->lhs, ctx, options);
839 uint16_t lhs_reg = ctx.result_reg;
840
841 if (set_op->rhs && set_op->rhs->kind() == NodeKind::WALK) {
842 auto rhs_w = std::make_shared<ScmWalk>(*std::static_pointer_cast<ScmWalk>(set_op->rhs));
843 if (rhs_w->src_reg == 0 && options.enable_seed_inlining) {
844 rhs_w->inlined_seed = true;
845 }
846 emit_node(rhs_w, ctx, options);
847 } else {
848 emit_node(set_op->rhs, ctx, options);
849 }
850 uint16_t rhs_reg = ctx.result_reg;
851
853 if (set_op->op == ScmSetOp::UNION) inst.opcode = OP_SET_UNION;
854 else if (set_op->op == ScmSetOp::DIFFERENCE) inst.opcode = OP_SET_DIFFERENCE;
855 else inst.opcode = OP_SET_INTERSECT;
856
857 ctx.result_reg = lhs_reg;
858 inst.dst_reg = ctx.result_reg;
859 inst.payload = static_cast<uint32_t>(lhs_reg & 0xFFFF) |
860 (static_cast<uint32_t>(rhs_reg & 0xFFFF) << 16);
861 ctx.instructions.push_back(inst);
862 } else if (step->kind() == NodeKind::PROGRAM) {
863 auto sub = std::static_pointer_cast<ScmProgram>(step);
864 for (const auto& s : sub->steps) {
865 emit_node(s, ctx, options);
866 }
867 } else if (step->kind() == NodeKind::FIXPOINT) {
868 auto fp = std::static_pointer_cast<ScmFixpoint>(step);
869 if (ctx.instructions.empty() && fp->src_reg == 0) {
870 impulse_instruction_t seed_inst{};
871 seed_inst.opcode = OP_INIT_INPUT_NODE;
872 seed_inst.dst_reg = 0;
873 ctx.instructions.push_back(seed_inst);
874 }
876 inst.opcode = OP_FIXPOINT_KLEENE_STAR;
877 inst.flags = (fp->direction == WalkDirection::REVERSE_CSC) ? 1 : 0;
878 inst.dst_reg = fp->dst_reg;
879 uint16_t rel_id = static_cast<uint16_t>(std::max(0, fp->physical_rel_id));
880 inst.payload = (static_cast<uint32_t>(rel_id) << 16) | static_cast<uint32_t>(fp->src_reg & 0xFF);
881 ctx.instructions.push_back(inst);
882 ctx.result_reg = inst.dst_reg;
883 } else if (step->kind() == NodeKind::VAR_REF) {
884 auto v = std::static_pointer_cast<ScmVarRef>(step);
885 ctx.result_reg = ctx.get_reg(v->var);
886 } else if (step->kind() == NodeKind::BITSET_INIT) {
887 auto init = std::static_pointer_cast<ScmBitsetInit>(step);
889 ctx.result_reg = ctx.next_reg++; // Allocate a temp register
890 inst.dst_reg = ctx.result_reg;
891 if (init->type == ScmBitsetInit::InitType::EMPTY) {
892 inst.opcode = OP_CLEAR_REG;
893 } else if (init->type == ScmBitsetInit::InitType::ALL) {
894 inst.opcode = OP_INIT_INPUT_SET;
895 } else if (init->type == ScmBitsetInit::InitType::FROM_NODE) {
896 inst.opcode = OP_INIT_INPUT_NODE;
897 // Typically node 0 or a passed param
898 inst.payload = 0;
899 }
900 ctx.instructions.push_back(inst);
901 } else if (step->kind() == NodeKind::CARDINALITY) {
902 auto card = std::static_pointer_cast<ScmCardinality>(step);
904 inst.opcode = OP_SET_CARDINALITY;
905 inst.dst_reg = ctx.next_reg++; // Result
906 uint16_t src = ctx.get_reg(card->var);
907 inst.payload = src;
908 ctx.instructions.push_back(inst);
909 ctx.result_reg = inst.dst_reg;
910 } else if (step->kind() == NodeKind::RETURN) {
911 auto ret = std::static_pointer_cast<ScmReturn>(step);
912 emit_node(ret->expr, ctx, options);
914 inst.opcode = OP_HALT;
915 inst.dst_reg = ctx.result_reg;
916 ctx.instructions.push_back(inst);
917 } else if (step->kind() == NodeKind::LANE_INIT_SEEDS) {
918 auto n = std::static_pointer_cast<ScmLaneInitSeeds>(step);
919 if (!n->inline_seeds.empty()) {
920 for (uint64_t seed_id : n->inline_seeds) {
921 impulse_instruction_t load_inst{};
922 load_inst.opcode = OP_LOAD_CONST_INT;
923 load_inst.dst_reg = 62;
924 load_inst.payload = static_cast<uint32_t>(seed_id);
925 ctx.instructions.push_back(load_inst);
926
927 impulse_instruction_t union_inst{};
928 union_inst.opcode = OP_SET_UNION;
929 union_inst.dst_reg = n->seed_reg;
930 union_inst.payload = static_cast<uint32_t>(n->seed_reg & 0xFFFF) | (62U << 16);
931 ctx.instructions.push_back(union_inst);
932 }
933 }
935 inst.opcode = OP_LANE_INIT_SEEDS;
936 if (n->inline_seeds.empty() && n->seed_reg == 0 && n->width_flag == 0) {
937 inst.flags = 1;
938 inst.dst_reg = n->dst_reg;
939 inst.payload = static_cast<uint32_t>(n->seed_reg & 0xFFFF) | (3U << 16);
940 } else {
941 inst.flags = n->width_flag;
942 inst.dst_reg = n->dst_reg;
943 inst.payload = static_cast<uint32_t>(n->seed_reg & 0xFFFF);
944 }
945 ctx.instructions.push_back(inst);
946 ctx.result_reg = inst.dst_reg;
947 } else if (step->kind() == NodeKind::LANE_INIT_GROUPS) {
948 auto n = std::static_pointer_cast<ScmLaneInitGroups>(step);
950 inst.opcode = OP_LANE_INIT_GROUPS;
951 inst.dst_reg = n->dst_reg;
952 inst.payload = static_cast<uint32_t>(n->pairs_reg & 0xFFFF) |
953 (static_cast<uint32_t>(n->num_groups & 0xFFFF) << 16);
954 ctx.instructions.push_back(inst);
955 ctx.result_reg = inst.dst_reg;
956 } else if (step->kind() == NodeKind::LANE_SET_EDGE_MASK) {
957 auto n = std::static_pointer_cast<ScmLaneSetEdgeMask>(step);
959 inst.opcode = OP_LANE_SET_EDGE_MASK;
960 inst.flags = 1; // OP_C2_FLAG_SET_FROM_REG
961 inst.dst_reg = n->dst_reg;
962 uint16_t rel_id = static_cast<uint16_t>(std::max(0, n->physical_rel_id));
963 inst.payload = static_cast<uint32_t>(ctx.result_reg & 0xFF) |
964 (static_cast<uint32_t>(n->pairs_reg & 0xFF) << 8) |
965 (static_cast<uint32_t>(rel_id) << 16);
966 ctx.instructions.push_back(inst);
967 ctx.result_reg = inst.dst_reg;
968 } else if (step->kind() == NodeKind::LANE_WALK) {
969 auto n = std::static_pointer_cast<ScmLaneWalk>(step);
971 inst.opcode = (n->direction == WalkDirection::FORWARD_CSR) ? OP_LANE_CSR_WALK : OP_LANE_CSC_WALK;
972 inst.flags = n->fixed_point ? (n->flags != 0 ? n->flags : 0x04) : n->flags;
973 inst.dst_reg = n->dst_reg;
974 uint16_t rel_id = static_cast<uint16_t>(std::max(0, n->physical_rel_id));
975 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFFFF) |
976 (static_cast<uint32_t>(rel_id) << 16);
977 ctx.instructions.push_back(inst);
978 ctx.result_reg = inst.dst_reg;
979 } else if (step->kind() == NodeKind::REDUCE) {
980 auto red = std::static_pointer_cast<ScmReduce>(step);
981 uint8_t opcode = OP_REDUCE;
982 if (red->op_val == ScmReduce::Op::SUM || red->op_val == ScmReduce::Op::COUNT) {
983 opcode = OP_VECTOR_REDUCE_SUM;
984 }
985 int op_id = 0;
986 switch (red->op_val) {
988 case ScmReduce::Op::COUNT: op_id = BINARY_OP_ADD; break;
989 case ScmReduce::Op::MIN: op_id = BINARY_OP_MIN; break;
990 case ScmReduce::Op::MAX: op_id = BINARY_OP_MAX; break;
991 case ScmReduce::Op::ARGMIN: op_id = BINARY_OP_ARGMIN; break;
992 case ScmReduce::Op::ARGMAX: op_id = BINARY_OP_ARGMAX; break;
993 case ScmReduce::Op::FIRST: op_id = 5; break;
994 }
995 uint32_t payload = (static_cast<uint32_t>(op_id) << 16) | (ctx.result_reg & 0xFFFF);
997 inst.opcode = opcode;
998 inst.dst_reg = ctx.result_reg;
999 inst.payload = payload;
1000 ctx.instructions.push_back(inst);
1001 } else if (step->kind() == NodeKind::LANE_PRUNE_MASK) {
1002 auto n = std::static_pointer_cast<ScmLanePruneMask>(step);
1003 impulse_instruction_t inst{};
1004 inst.opcode = OP_LANE_PRUNE_MASK;
1005 inst.flags = n->mode;
1006 inst.dst_reg = n->dst_reg;
1007 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFF) |
1008 (static_cast<uint32_t>(n->arg0 & 0xFFFF) << 8) |
1009 (static_cast<uint32_t>(n->arg1 & 0xFF) << 24);
1010 ctx.instructions.push_back(inst);
1011 ctx.result_reg = inst.dst_reg;
1012 } else if (step->kind() == NodeKind::LANE_FILTER_QUORUM) {
1013 auto n = std::static_pointer_cast<ScmLaneFilterQuorum>(step);
1014 impulse_instruction_t inst{};
1015 inst.opcode = OP_LANE_FILTER_QUORUM;
1016 inst.dst_reg = n->dst_reg;
1017 if (n->mask_reg > 0 && n->mask_reg != 0xFF) {
1018 inst.flags = 1;
1019 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFF) |
1020 (static_cast<uint32_t>(n->min_quorum & 0xFF) << 8) |
1021 (static_cast<uint32_t>(n->mask_reg & 0xFF) << 16);
1022 } else {
1023 inst.flags = 0;
1024 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFFFF) |
1025 (static_cast<uint32_t>(n->min_quorum & 0xFFFF) << 16);
1026 }
1027 ctx.instructions.push_back(inst);
1028 ctx.result_reg = inst.dst_reg;
1029 } else if (step->kind() == NodeKind::LANE_REDUCE_AND) {
1030 auto n = std::static_pointer_cast<ScmLaneReduceAnd>(step);
1031 impulse_instruction_t inst{};
1032 inst.opcode = OP_LANE_REDUCE_AND;
1033 inst.dst_reg = n->dst_reg;
1034 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFFFF);
1035 ctx.instructions.push_back(inst);
1036 ctx.result_reg = inst.dst_reg;
1037 } else if (step->kind() == NodeKind::LANE_REDUCE_ATTR) {
1038 auto n = std::static_pointer_cast<ScmLaneReduceAttr>(step);
1039 impulse_instruction_t inst{};
1040 inst.opcode = OP_LANE_REDUCE_ATTR;
1041 inst.flags = 0;
1042 inst.dst_reg = n->dst_reg;
1043 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFF) |
1044 (static_cast<uint32_t>(n->attr_id & 0xFF) << 8) |
1045 (static_cast<uint32_t>(n->reduce_op & 0xFFFF) << 16);
1046 ctx.instructions.push_back(inst);
1047 ctx.result_reg = inst.dst_reg;
1048 } else if (step->kind() == NodeKind::LANE_SET_COVER) {
1049 auto n = std::static_pointer_cast<ScmLaneSetCover>(step);
1050 if (n->mask_reg > 0 && n->mask_reg < 64) {
1051 impulse_instruction_t load_mask{};
1052 load_mask.opcode = OP_LOAD_CONST_INT;
1053 load_mask.dst_reg = n->mask_reg;
1054 load_mask.payload = static_cast<uint32_t>(n->mask_reg);
1055 ctx.instructions.push_back(load_mask);
1056 }
1057 impulse_instruction_t inst{};
1058 inst.opcode = OP_LANE_SET_COVER;
1059 inst.flags = n->mode;
1060 inst.dst_reg = n->dst_reg;
1061 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFF) |
1062 (static_cast<uint32_t>(n->mask_reg & 0xFF) << 8);
1063 ctx.instructions.push_back(inst);
1064 ctx.result_reg = inst.dst_reg;
1065 } else if (step->kind() == NodeKind::VECTOR_ANN_SEARCH) {
1066 auto n = std::static_pointer_cast<ScmVectorAnnSearch>(step);
1067 impulse_instruction_t inst{};
1068 inst.opcode = OP_VECTOR_ANN_SEARCH;
1069 inst.flags = n->fused_lane_frontier ? 1 : 0;
1070 inst.dst_reg = n->dst_reg;
1071 inst.payload = static_cast<uint32_t>(n->query_reg & 0xFF) |
1072 (static_cast<uint32_t>(n->top_k & 0xFF) << 8) |
1073 (static_cast<uint32_t>(n->index_or_attr_id & 0xFFFF) << 16);
1074 ctx.instructions.push_back(inst);
1075 ctx.result_reg = inst.dst_reg;
1076 } else if (step->kind() == NodeKind::VECTOR_RERANK_DOT) {
1077 auto n = std::static_pointer_cast<ScmVectorRerankDot>(step);
1078 impulse_instruction_t inst{};
1079 inst.opcode = OP_VECTOR_RERANK_DOT;
1080 inst.flags = n->metric;
1081 inst.dst_reg = n->dst_reg;
1082 inst.payload = static_cast<uint32_t>(n->src_reg & 0xFF) |
1083 (static_cast<uint32_t>(n->query_reg & 0xFF) << 8) |
1084 (static_cast<uint32_t>(n->attr_id & 0xFFFF) << 16);
1085 ctx.instructions.push_back(inst);
1086 ctx.result_reg = inst.dst_reg;
1087 }
1088 }
1089
1090 static CompiledImpulseProgram emit_bytecode(const AstPtr& root, const CompilerOptions& options) {
1091 auto prog = std::static_pointer_cast<ScmProgram>(root);
1092 CompiledImpulseProgram result;
1093 result.optimized_ast = root;
1094
1095 CompileContext ctx;
1096 for (const auto& step : prog->steps) {
1097 emit_node(step, ctx, options);
1098 }
1099
1100 // Add HALT if not present
1101 if (ctx.instructions.empty() || ctx.instructions.back().opcode != OP_HALT) {
1102 impulse_instruction_t halt_inst{};
1103 halt_inst.opcode = OP_HALT;
1104 halt_inst.dst_reg = ctx.result_reg;
1105 ctx.instructions.push_back(halt_inst);
1106 }
1107
1108 result.instructions = ctx.instructions;
1109 result.result_register = ctx.result_reg;
1110 return result;
1111 }
1112
1113};
1114
1115} // namespace impulse::compiler
1116
1117#endif // IMPULSE_COMPILER_HPP
Definition impulse_compiler.hpp:21
std::string to_impas_string(const GraphCatalog *catalog=nullptr) const
Formats human-readable ImpAsm disassembly.
Definition impulse_compiler.hpp:33
size_t instruction_count() const
Definition impulse_compiler.hpp:27
uint16_t result_register
Definition impulse_compiler.hpp:25
const impulse_instruction_t * data() const
Definition impulse_compiler.hpp:28
AstPtr optimized_ast
Definition impulse_compiler.hpp:24
std::vector< impulse_instruction_t > instructions
Definition impulse_compiler.hpp:23
Definition impulse_compiler.hpp:237
static CompiledImpulseProgram compile(const std::shared_ptr< ScmProgram > &program, const GraphCatalog *catalog=nullptr, const std::unordered_map< std::string, double > &params={}, const CompilerOptions &raw_options=CompilerOptions::default_options())
Compiles an ImpScheme program AST into executable ImpOps bytecode.
Definition impulse_compiler.hpp:242
@ ALL
Definition impulse_scm_ast.hpp:747
@ EMPTY
Definition impulse_scm_ast.hpp:747
@ FROM_NODE
Definition impulse_scm_ast.hpp:747
@ LIST
Definition impulse_scm_ast.hpp:648
@ COUNT
Definition impulse_scm_ast.hpp:648
@ VALUE_MAP
Definition impulse_scm_ast.hpp:648
@ VECTOR
Definition impulse_scm_ast.hpp:648
@ NONE
Definition impulse_scm_ast.hpp:648
@ BITSET
Definition impulse_scm_ast.hpp:648
@ ARRAY
Definition impulse_scm_ast.hpp:648
static std::shared_ptr< ScmLiteral > of_float(double v)
Definition impulse_scm_ast.hpp:181
@ MAX
Definition impulse_scm_ast.hpp:242
@ FIRST
Definition impulse_scm_ast.hpp:242
@ COUNT
Definition impulse_scm_ast.hpp:242
@ SUM
Definition impulse_scm_ast.hpp:242
@ ARGMIN
Definition impulse_scm_ast.hpp:242
@ MIN
Definition impulse_scm_ast.hpp:242
@ ARGMAX
Definition impulse_scm_ast.hpp:242
@ DIFFERENCE
Definition impulse_scm_ast.hpp:767
@ UNION
Definition impulse_scm_ast.hpp:767
#define IMPULSE_ASSERT(cond)
Definition impulse_assert.h:168
#define IMPULSE_AUDIT_ASSERT(cond)
Definition impulse_assert.h:223
Homoiconic ImpScheme (.impscm) S-Expression AST Node Definitions & Graph Catalog.
#define OP_STREAM_FILTER
Definition impulse_vm.h:226
#define OP_VECTOR_RERANK_DOT
Definition impulse_vm.h:264
#define OP_LANE_CSR_WALK
Definition impulse_vm.h:256
#define OP_FIXPOINT_KLEENE_STAR
Definition impulse_vm.h:202
#define OP_SET_UNION
Definition impulse_vm.h:98
#define OP_LANE_REDUCE_ATTR
Definition impulse_vm.h:261
#define OP_LANE_PRUNE_MASK
Definition impulse_vm.h:258
#define OP_CREATE_SCRATCH_INDEX
Definition impulse_vm.h:77
#define OP_VECTOR_ANN_SEARCH
Definition impulse_vm.h:263
#define OP_CSR_WALK_2HOP
Definition impulse_vm.h:60
#define OP_DROP_SCRATCH_INDEX
Definition impulse_vm.h:78
#define OP_STREAM_FUNC_BEGIN
Definition impulse_vm.h:220
#define OP_CSC_WALK
Definition impulse_vm.h:71
#define OP_HALT
Opcodes Definitions.
Definition impulse_vm.h:45
#define BINARY_OP_ARGMIN
Definition impulse_vm.h:279
#define OP_STREAM_LOAD_SRC_ID
Definition impulse_vm.h:245
#define OP_COLLECT_VALUE_MAP
Definition impulse_vm.h:214
#define OP_INIT_INPUT_NODE
Definition impulse_vm.h:47
#define OP_STREAM_SCATTER_REDUCE
Definition impulse_vm.h:250
#define OP_NOP
Definition impulse_vm.h:46
#define OP_LANE_INIT_SEEDS
Definition impulse_vm.h:253
#define OP_JZ
Definition impulse_vm.h:135
#define OP_REDUCE
Definition impulse_vm.h:121
#define OP_CSC_WALK_STREAM
Definition impulse_vm.h:229
#define OP_CSR_WALK_FILTERED
Definition impulse_vm.h:64
#define BINARY_OP_ARGMAX
Definition impulse_vm.h:280
#define OP_INIT_INPUT_SET
Definition impulse_vm.h:48
impulse_instruction_t
Definition impulse_vm.h:354
#define OP_LOAD_CONST_INT
Definition impulse_vm.h:49
#define OP_JMP
Definition impulse_vm.h:134
#define OP_LANE_CSC_WALK
Definition impulse_vm.h:257
#define OP_STREAM_FUNC_END
Definition impulse_vm.h:221
#define OP_CSR_WALK_STREAM
Definition impulse_vm.h:228
#define OP_ADAPTIVE_WALK
Definition impulse_vm.h:76
#define BINARY_OP_MAX
Definition impulse_vm.h:276
#define OP_COLLECT_ARRAY
Definition impulse_vm.h:212
#define OP_CLEAR_REG
Definition impulse_vm.h:174
#define OP_CSR_WALK
Definition impulse_vm.h:63
#define OP_LANE_FILTER_QUORUM
Definition impulse_vm.h:259
#define OP_LANE_INIT_GROUPS
Definition impulse_vm.h:254
#define OP_SET_INTERSECT
Definition impulse_vm.h:99
#define OP_STREAM_LOAD_SRC
Definition impulse_vm.h:222
#define OP_LANE_REDUCE_AND
Definition impulse_vm.h:260
#define OP_MOV
Definition impulse_vm.h:173
#define IMPULSE_VM_OP_FLAG_HALT_ON_EMPTY
Definition impulse_vm.h:38
#define OP_SET_DIFFERENCE
Definition impulse_vm.h:100
#define OP_VECTOR_TIME_VALID_AT
Definition impulse_vm.h:112
#define OP_VECTOR_REDUCE_SUM
Definition impulse_vm.h:103
#define OP_SET_CARDINALITY
Definition impulse_vm.h:101
#define IMPULSE_VM_OP_FLAG_INPUT_SEED
Definition impulse_vm.h:39
#define BINARY_OP_MIN
Definition impulse_vm.h:275
#define OP_LANE_SET_EDGE_MASK
Definition impulse_vm.h:255
#define OP_COLLECT_BITSET
Definition impulse_vm.h:211
#define OP_STREAM_YIELD
Definition impulse_vm.h:249
#define OP_LANE_SET_COVER
Definition impulse_vm.h:262
#define BINARY_OP_ADD
Definition impulse_vm.h:273
Definition impulse_bytecode_emitter.hpp:16
std::shared_ptr< ImpScmNode > AstPtr
Definition impulse_scm_ast.hpp:159
@ BITSET_INIT
Definition impulse_scm_ast.hpp:101
@ LOOP_WHILE
Definition impulse_scm_ast.hpp:98
@ WALK_2HOP
Definition impulse_scm_ast.hpp:91
@ COLLECT
Definition impulse_scm_ast.hpp:95
@ LANE_FILTER_QUORUM
Definition impulse_scm_ast.hpp:114
@ FIXPOINT
Definition impulse_scm_ast.hpp:120
@ SET_OP
Definition impulse_scm_ast.hpp:103
@ LANE_PRUNE_MASK
Definition impulse_scm_ast.hpp:113
@ LANE_WALK
Definition impulse_scm_ast.hpp:112
@ VECTOR_ANN_SEARCH
Definition impulse_scm_ast.hpp:118
@ SET
Definition impulse_scm_ast.hpp:97
@ SYMBOL
Definition impulse_scm_ast.hpp:105
@ VECTOR_RERANK_DOT
Definition impulse_scm_ast.hpp:119
@ LANE_INIT_SEEDS
Definition impulse_scm_ast.hpp:109
@ LANE_SET_COVER
Definition impulse_scm_ast.hpp:117
@ RETURN
Definition impulse_scm_ast.hpp:99
@ CARDINALITY
Definition impulse_scm_ast.hpp:100
@ LANE_SET_EDGE_MASK
Definition impulse_scm_ast.hpp:111
@ LANE_REDUCE_AND
Definition impulse_scm_ast.hpp:115
@ WALK
Definition impulse_scm_ast.hpp:90
@ REDUCE
Definition impulse_scm_ast.hpp:94
@ PROGRAM
Definition impulse_scm_ast.hpp:89
@ LANE_REDUCE_ATTR
Definition impulse_scm_ast.hpp:116
@ STREAM_PROJECT
Definition impulse_scm_ast.hpp:108
@ LET
Definition impulse_scm_ast.hpp:96
@ LANE_INIT_GROUPS
Definition impulse_scm_ast.hpp:110
@ STREAM_FILTER
Definition impulse_scm_ast.hpp:107
@ VECTOR_FILTER
Definition impulse_scm_ast.hpp:92
@ VAR_REF
Definition impulse_scm_ast.hpp:102
@ FORWARD_CSR
Definition impulse_scm_ast.hpp:124
@ REVERSE_CSC
Definition impulse_scm_ast.hpp:125
Definition impulse_scm_ast.hpp:30
CompilerOptions effective() const
Definition impulse_scm_ast.hpp:71
static CompilerOptions default_options()
Definition impulse_scm_ast.hpp:43
Definition impulse_scm_ast.hpp:879