243 const std::shared_ptr<ScmProgram>& program,
245 const std::unordered_map<std::string, double>& params = {},
248 if (!program || program->steps.empty()) {
249 throw std::invalid_argument(
"Empty ImpScheme program");
254 CompilerOptions options = raw_options.
effective();
257 validate_ast(program);
260 AstPtr ast = pass_parameter_binding(program, params, options);
264 if (options.enable_kernel_fusion) {
265 ast = pass_kernel_fusion(ast, catalog, options);
270 ast = pass_physical_binding(ast, catalog);
274 if (options.enable_direction_selection) {
275 ast = pass_direction_selection(ast, catalog);
280 ast = pass_register_allocation(ast, options);
284 CompiledImpulseProgram result = emit_bytecode(ast, options);
291 static void validate_ast(
const std::shared_ptr<ScmProgram>& prog) {
293 if (prog->steps.empty()) {
294 throw std::invalid_argument(
"ScmProgram must contain at least one step");
300 static AstPtr pass_parameter_binding_node(
const AstPtr& step,
const std::unordered_map<std::string, double>& params) {
301 if (!step)
return nullptr;
303 auto w = std::static_pointer_cast<ScmWalk>(step);
304 auto new_w = std::make_shared<ScmWalk>(*w);
306 auto vf = std::static_pointer_cast<ScmVectorFilter>(new_w->predicate);
307 auto new_vf = std::make_shared<ScmVectorFilter>(*vf);
309 auto sym = std::static_pointer_cast<ScmSymbol>(new_vf->threshold);
310 auto it = params.find(sym->name);
311 if (it != params.end()) {
315 new_w->predicate = new_vf;
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);
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);
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);
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);
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& )
349 std::vector<AstPtr> new_steps;
350 for (
const auto& step : prog->steps) {
351 new_steps.push_back(pass_parameter_binding_node(step, params));
353 return std::make_shared<ScmProgram>(std::move(new_steps));
357 static AstPtr pass_kernel_fusion(
359 const GraphCatalog* catalog,
360 const CompilerOptions& options)
362 auto prog = std::static_pointer_cast<ScmProgram>(root);
363 std::vector<AstPtr> fused_steps;
366 while (i < prog->steps.size()) {
367 if (i + 1 < prog->steps.size() &&
371 auto w1 = std::static_pointer_cast<ScmWalk>(prog->steps[i]);
372 auto w2 = std::static_pointer_cast<ScmWalk>(prog->steps[i + 1]);
376 !w1->predicate && !w2->predicate)
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);
387 fused_steps.push_back(prog->steps[i]);
390 return std::make_shared<ScmProgram>(std::move(fused_steps));
393 static AstPtr bind_node_physical(
const AstPtr& step,
const GraphCatalog* catalog) {
394 if (!step)
return nullptr;
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;
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;
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;
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;
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;
426 for (
auto& bs : new_fp->body_steps) {
427 bs = bind_node_physical(bs, catalog);
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);
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));
442 return std::make_shared<ScmProgram>(std::move(sub_steps));
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;
451 for (
const auto& step : prog->steps) {
452 bound_steps.push_back(bind_node_physical(step, catalog));
454 return std::make_shared<ScmProgram>(std::move(bound_steps));
457 static AstPtr pass_direction_selection(
const AstPtr& root,
const GraphCatalog* ) {
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) {
465 auto w = std::static_pointer_cast<ScmWalk>(step);
466 auto new_w = std::make_shared<ScmWalk>(*w);
468 new_w->is_adaptive =
true;
470 opt_steps.push_back(new_w);
472 opt_steps.push_back(step);
475 return std::make_shared<ScmProgram>(std::move(opt_steps));
478 static AstPtr alloc_set_branch(
const AstPtr& branch, uint16_t input_reg, uint16_t& next_temp_reg, uint16_t& out_res_reg) {
480 out_res_reg = input_reg;
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;
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));
500 return std::make_shared<ScmProgram>(std::move(sub_steps));
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;
511 out_res_reg = input_reg;
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;
520 for (
size_t idx = 0; idx < prog->steps.size(); ++idx) {
521 const auto& step = prog->steps[idx];
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;
529 new_w->dst_reg =
static_cast<uint16_t
>(idx + 1);
531 current_reg = new_w->dst_reg;
532 alloc_steps.push_back(new_w);
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;
540 new_w2->dst_reg =
static_cast<uint16_t
>(idx + 1);
542 current_reg = new_w2->dst_reg;
543 alloc_steps.push_back(new_w2);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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;
616 alloc_steps.push_back(step);
619 return std::make_shared<ScmProgram>(std::move(alloc_steps));
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;
629 uint16_t get_reg(
const std::string& name) {
630 if (env.find(name) == env.end()) {
631 env[name] = next_reg++;
637 static void emit_node(
const AstPtr& step, CompileContext& ctx,
const CompilerOptions& options) {
640 if (!step)
throw std::runtime_error(
"ImpScheme: unresolvable (null) AST node during emission");
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) {
647 seed_inst.dst_reg = 0;
648 ctx.instructions.push_back(seed_inst);
651 bool is_stream = !w->shader_steps.empty();
655 if (w->is_adaptive && !is_stream) {
657 }
else if (w->predicate && !is_stream) {
660 inst.opcode = opcode;
662 inst.dst_reg = w->dst_reg;
663 if (ctx.env.count(
"frontier")) {
664 inst.dst_reg = ctx.env[
"frontier"];
665 w->src_reg = ctx.env[
"frontier"];
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);
670 if ((is_first_walk || w->inlined_seed) && options.enable_seed_inlining) {
673 if (options.enable_early_exit && !ctx.env.count(
"frontier") && !w->inlined_seed) {
678 size_t walk_idx = ctx.instructions.size();
679 ctx.instructions.push_back(inst);
683 ctx.instructions.push_back(begin);
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)");
689 ctx.instructions[walk_idx].flags =
static_cast<uint8_t
>(shader_pc_start);
692 for (
const auto& sstep : w->shader_steps) {
694 int reg = next_sreg++;
697 s_load.dst_reg =
static_cast<uint16_t
>(reg);
698 ctx.instructions.push_back(s_load);
702 s_filt.dst_reg =
static_cast<uint16_t
>(reg);
703 ctx.instructions.push_back(s_filt);
705 int reg = next_sreg++;
708 s_load.dst_reg =
static_cast<uint16_t
>(reg);
709 ctx.instructions.push_back(s_load);
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);
721 ctx.instructions.push_back(yield_inst);
725 ctx.instructions.push_back(end_inst);
727 ctx.instructions.push_back(inst);
729 ctx.result_reg = inst.dst_reg;
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) {
736 seed_inst.dst_reg = 0;
737 ctx.instructions.push_back(seed_inst);
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);
746 if (options.enable_seed_inlining && w2->src_reg == 0) {
749 if (options.enable_early_exit) {
752 ctx.instructions.push_back(inst);
753 ctx.result_reg = inst.dst_reg;
755 auto c = std::static_pointer_cast<ScmCollect>(step);
771 uint16_t src_reg = ctx.result_reg;
773 ?
static_cast<uint16_t
>((src_reg == 0) ? 1 : 0)
775 inst.dst_reg = dst_reg;
776 inst.payload = src_reg;
778 ctx.instructions.push_back(inst);
779 ctx.result_reg = dst_reg;
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) {
790 mov.payload = ctx.result_reg;
791 ctx.instructions.push_back(mov);
794 for (
const auto& stmt : let->body) {
795 emit_node(stmt, ctx, options);
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) {
805 mov.payload = ctx.result_reg;
806 ctx.instructions.push_back(mov);
809 auto loop = std::static_pointer_cast<ScmLoopWhile>(step);
810 size_t start_pc = ctx.instructions.size();
813 emit_node(loop->condition, ctx, options);
818 jz.dst_reg = ctx.result_reg;
820 size_t jz_idx = ctx.instructions.size();
821 ctx.instructions.push_back(jz);
824 for (
const auto& stmt : loop->body) {
825 emit_node(stmt, ctx, options);
831 jmp.payload =
static_cast<uint32_t
>(start_pc) -
static_cast<uint32_t
>(ctx.instructions.size());
832 ctx.instructions.push_back(jmp);
835 ctx.instructions[jz_idx].payload =
static_cast<uint32_t
>(ctx.instructions.size()) -
static_cast<uint32_t
>(jz_idx);
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;
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;
846 emit_node(rhs_w, ctx, options);
848 emit_node(set_op->rhs, ctx, options);
850 uint16_t rhs_reg = ctx.result_reg;
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);
863 auto sub = std::static_pointer_cast<ScmProgram>(step);
864 for (
const auto& s : sub->steps) {
865 emit_node(s, ctx, options);
868 auto fp = std::static_pointer_cast<ScmFixpoint>(step);
869 if (ctx.instructions.empty() && fp->src_reg == 0) {
872 seed_inst.dst_reg = 0;
873 ctx.instructions.push_back(seed_inst);
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;
884 auto v = std::static_pointer_cast<ScmVarRef>(step);
885 ctx.result_reg = ctx.get_reg(v->var);
887 auto init = std::static_pointer_cast<ScmBitsetInit>(step);
889 ctx.result_reg = ctx.next_reg++;
890 inst.dst_reg = ctx.result_reg;
900 ctx.instructions.push_back(inst);
902 auto card = std::static_pointer_cast<ScmCardinality>(step);
905 inst.dst_reg = ctx.next_reg++;
906 uint16_t src = ctx.get_reg(card->var);
908 ctx.instructions.push_back(inst);
909 ctx.result_reg = inst.dst_reg;
911 auto ret = std::static_pointer_cast<ScmReturn>(step);
912 emit_node(ret->expr, ctx, options);
915 inst.dst_reg = ctx.result_reg;
916 ctx.instructions.push_back(inst);
918 auto n = std::static_pointer_cast<ScmLaneInitSeeds>(step);
919 if (!n->inline_seeds.empty()) {
920 for (uint64_t seed_id : n->inline_seeds) {
923 load_inst.dst_reg = 62;
924 load_inst.payload =
static_cast<uint32_t
>(seed_id);
925 ctx.instructions.push_back(load_inst);
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);
936 if (n->inline_seeds.empty() && n->seed_reg == 0 && n->width_flag == 0) {
938 inst.dst_reg = n->dst_reg;
939 inst.payload =
static_cast<uint32_t
>(n->seed_reg & 0xFFFF) | (3U << 16);
941 inst.flags = n->width_flag;
942 inst.dst_reg = n->dst_reg;
943 inst.payload =
static_cast<uint32_t
>(n->seed_reg & 0xFFFF);
945 ctx.instructions.push_back(inst);
946 ctx.result_reg = inst.dst_reg;
948 auto n = std::static_pointer_cast<ScmLaneInitGroups>(step);
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;
957 auto n = std::static_pointer_cast<ScmLaneSetEdgeMask>(step);
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;
969 auto n = std::static_pointer_cast<ScmLaneWalk>(step);
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;
980 auto red = std::static_pointer_cast<ScmReduce>(step);
986 switch (red->op_val) {
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);
1002 auto n = std::static_pointer_cast<ScmLanePruneMask>(step);
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;
1013 auto n = std::static_pointer_cast<ScmLaneFilterQuorum>(step);
1016 inst.dst_reg = n->dst_reg;
1017 if (n->mask_reg > 0 && n->mask_reg != 0xFF) {
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);
1024 inst.payload =
static_cast<uint32_t
>(n->src_reg & 0xFFFF) |
1025 (
static_cast<uint32_t
>(n->min_quorum & 0xFFFF) << 16);
1027 ctx.instructions.push_back(inst);
1028 ctx.result_reg = inst.dst_reg;
1030 auto n = std::static_pointer_cast<ScmLaneReduceAnd>(step);
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;
1038 auto n = std::static_pointer_cast<ScmLaneReduceAttr>(step);
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;
1049 auto n = std::static_pointer_cast<ScmLaneSetCover>(step);
1050 if (n->mask_reg > 0 && n->mask_reg < 64) {
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);
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;
1066 auto n = std::static_pointer_cast<ScmVectorAnnSearch>(step);
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;
1077 auto n = std::static_pointer_cast<ScmVectorRerankDot>(step);
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;
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;
1096 for (
const auto& step : prog->steps) {
1097 emit_node(step, ctx, options);
1101 if (ctx.instructions.empty() || ctx.instructions.back().opcode !=
OP_HALT) {
1104 halt_inst.dst_reg = ctx.result_reg;
1105 ctx.instructions.push_back(halt_inst);
1108 result.instructions = ctx.instructions;
1109 result.result_register = ctx.result_reg;