sawansri's picture
r8: fix concurrent mixed-prefill/decode hang in DSpark attention, exact chunk-pruned sampler (0.2 ms/token), checkpoint sampling defaults, per-request draft_depth, DSpark retune
4e018e8 verified
Raw History Blame Contribute Delete
233 kB
func.decl import("input") @require(%condition: i32, %diagnostic: buffer) func.decl import("input") @encode(%text: buffer, %flags: i32, %capacity: i64) -> (buffer, i64) func.decl import("json") @members(%json: buffer) -> (buffer, i32, i64) func.decl import("json") @members_range(%json: buffer, %offset: i64, %length: i64) -> (buffer, i32, i64) func.decl import("json") @compact(%text: buffer) -> (buffer) func.decl import("json") @unescape(%source: buffer, %offset: i64, %length: i64, %target: buffer, %target_offset: i64) -> (i64) func.decl import("tools") @parse_xml(%schemas: buffer, %response: buffer, %truncated: i64) -> (buffer) func.decl import("runner") @execute_5(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer) -> (i64) func.decl import("runner") @execute_7(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer, %b5: hal.buffer, %b6: hal.buffer) -> (i64) func.decl import("runner") @execute_4(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer) -> (i64) func.decl import("runner") @execute_6(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer, %b5: hal.buffer) -> (i64) func.decl import("runner") @feedback(%slot: i32, %source: hal.buffer, %offset: i64, %length: i64) -> (i64) global.rodata.def @name = bytes("496e6b6c696e672d322d4d696e69") global.rodata.def @empty = bytes("") global.rodata.def @no_tools = bytes("5b5d") global.rodata.def @assistant = bytes("617373697374616e74") global.rodata.def @user = bytes("75736572") global.rodata.def @system = bytes("73797374656d") global.rodata.def @tool_role = bytes("746f6f6c") // "role", "content", "messages", "enable_thinking", "chat_template_kwargs", "reasoning_effort", "none" global.rodata.def @role_key = bytes("726f6c65") global.rodata.def @content_key = bytes("636f6e74656e74") global.rodata.def @messages_key = bytes("6d65737361676573") global.rodata.def @thinking_key = bytes("656e61626c655f7468696e6b696e67") global.rodata.def @kwargs_key = bytes("636861745f74656d706c6174655f6b7761726773") global.rodata.def @effort_key = bytes("726561736f6e696e675f6566666f7274") global.rodata.def @effort_none = bytes("6e6f6e65") global.rodata.def @newline = bytes("0a") // "unsupported message: roles are system/user/assistant/tool, content is text (no images)" global.rodata.def @invalid = bytes("756e737570706f72746564206d6573736167653a20726f6c6573206172652073797374656d2f757365722f617373697374616e742f746f6f6c2c20636f6e74656e74206973207465787420286e6f20696d6167657329") // "llm requires pooled epochs without MTP" global.rodata.def @unsupported = bytes("6c6c6d20726571756972657320706f6f6c65642065706f63687320776974686f7574204d5450") // "input exceeds the available context size" global.rodata.def @overflow = bytes("696e70757420657863656564732074686520617661696c61626c6520636f6e746578742073697a65") func.def @check(%condition: i1) { %value = scalar.extui %condition : i1 to i32 %error = global.load @invalid : buffer func.call @require(%value, %error) : (i32, buffer) func.return } func.def @equal(%a: buffer, %b: buffer) -> (i1) { %a_length = buffer.length %a %b_length = buffer.length %b %same_length = index.cmp eq, %a_length, %b_length : offset %same = scf.if %same_length -> (i1) { %c0 = index.constant 0 : offset %compared = buffer.compare %a[%c0], %b[%c0], %a_length %c0_i32 = scalar.constant 0 : i32 %matches = scalar.cmpi eq, %compared, %c0_i32 : i32 scf.yield %matches : i1 } else { %false = scalar.constant false : i1 scf.yield %false : i1 } func.return %same : i1 } func.def @join(%a: buffer, %b: buffer) -> (buffer) { %c0 = index.constant 0 : offset %a_length = buffer.length %a %b_length = buffer.length %b %length = index.add %a_length, %b_length : offset %output = buffer.alloca<private> align(1) %length : buffer buffer.copy %a[%c0], %output[%c0], %a_length buffer.copy %b[%c0], %output[%a_length], %b_length func.return %output : buffer } func.def @join4(%a: buffer, %b: buffer, %c: buffer, %d: buffer) -> (buffer) { %ab = func.call @join(%a, %b) : (buffer, buffer) -> (buffer) %cd = func.call @join(%c, %d) : (buffer, buffer) -> (buffer) %all = func.call @join(%ab, %cd) : (buffer, buffer) -> (buffer) func.return %all : buffer } func.def inline @tool_append(%output: buffer, %position: offset, %text: buffer) -> (offset) { %c0 = index.constant 0 : offset %length = buffer.length %text buffer.copy %text[%c0], %output[%position], %length %end = index.add %position, %length : offset func.return %end : offset } // Copy of text[begin, end). func.def inline @slice(%text: buffer, %begin: offset, %end: offset) -> (buffer) { %c0 = index.constant 0 : offset %size = index.sub %end, %begin : offset %result = buffer.alloca<private> align(1) %size : buffer buffer.copy %text[%begin], %result[%c0], %size func.return %result : buffer } func.def @json_range(%text: buffer, %begin: i64, %length: i64) -> (buffer) { %bounded_begin = scalar.assume %begin [range(%begin, 0, 9223372036854775807)] : i64 %bounded_length = scalar.assume %length [range(%length, 0, 9223372036854775807)] : i64 %start = index.cast %bounded_begin : i64 to offset %size = index.cast %bounded_length : i64 to offset %c0 = index.constant 0 : offset %result = buffer.alloca<private> align(1) %size : buffer buffer.copy %text[%start], %result[%c0], %size func.return %result : buffer } // Decodes a JSON string range (without quotes). func.def @json_key(%source: buffer, %begin: i64, %length: i64) -> (buffer) { %c0_i64 = scalar.constant 0 : i64 %bounded_length = scalar.assume %length [range(%length, 0, 9223372036854775807)] : i64 %size = index.cast %bounded_length : i64 to offset %storage = buffer.alloca<private> align(1) %size : buffer %decoded_length = func.call @unescape(%source, %begin, %length, %storage, %c0_i64) : (buffer, i64, i64, buffer, i64) -> (i64) %result = func.call @json_range(%storage, %c0_i64, %decoded_length) : (buffer, i64, i64) -> (buffer) func.return %result : buffer } func.def inline @text_is_space(%text: buffer, %position: offset) -> (i1) { %byte = buffer.load.i8.u %text[%position] %space = scalar.constant 32 : i32 %tab = scalar.constant 9 : i32 %carriage_return = scalar.constant 13 : i32 %is_space = scalar.cmpi eq, %byte, %space : i32 %after_tab = scalar.cmpi uge, %byte, %tab : i32 %before_return = scalar.cmpi ule, %byte, %carriage_return : i32 %control = scalar.andi %after_tab, %before_return : i1 %result = scalar.ori %is_space, %control : i1 func.return %result : i1 } // First position >= %from where %marker starts and fits before %end; %end if none. func.def inline @find(%text: buffer, %from: offset, %end: offset, %marker: buffer) -> (offset) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %zero = scalar.constant 0 : i32 %false = scalar.constant false : i1 %marker_length = buffer.length %marker %found = scf.while(%position = %from : offset) -> (offset) { %stop = index.add %position, %marker_length : offset %fits = index.cmp ule, %stop, %end : offset %continue = scf.if %fits -> (i1) { %compared = buffer.compare %text[%position], %marker[%c0], %marker_length %differs = scalar.cmpi ne, %compared, %zero : i32 scf.yield %differs : i1 } else { scf.yield %false : i1 } scf.condition %continue, %position : i1, offset } do(%position: offset) { %next = index.add %position, %c1 : offset scf.yield %next : offset } %stop = index.add %found, %marker_length : offset %hit = index.cmp ule, %stop, %end : offset %result = scf.select %hit, %found, %end : offset func.return %result : offset } func.def inline @skip_space(%text: buffer, %from: offset, %end: offset) -> (offset) { %c1 = index.constant 1 : offset %false = scalar.constant false : i1 %result = scf.while(%position = %from : offset) -> (offset) { %inside = index.cmp ult, %position, %end : offset %space = scf.if %inside -> (i1) { %value = func.call @text_is_space(%text, %position) : (buffer, offset) -> (i1) scf.yield %value : i1 } else { scf.yield %false : i1 } scf.condition %space, %position : i1, offset } do(%position: offset) { %next = index.add %position, %c1 : offset scf.yield %next : offset } func.return %result : offset } // End of trailing whitespace trimming: largest e in [begin, end] with text[e-1] not space. func.def inline @trim_end(%text: buffer, %begin: offset, %end: offset) -> (offset) { %c1 = index.constant 1 : offset %false = scalar.constant false : i1 %result = scf.while(%position = %end : offset) -> (offset) { %more = index.cmp ugt, %position, %begin : offset %space = scf.if %more -> (i1) { %last = index.sub %position, %c1 : offset %value = func.call @text_is_space(%text, %last) : (buffer, offset) -> (i1) scf.yield %value : i1 } else { scf.yield %false : i1 } scf.condition %space, %position : i1, offset } do(%position: offset) { %next = index.sub %position, %c1 : offset scf.yield %next : offset } func.return %result : offset } func.def inline @trim(%text: buffer) -> (buffer) { %c0 = index.constant 0 : offset %length = buffer.length %text %begin = func.call @skip_space(%text, %c0, %length) : (buffer, offset, offset) -> (offset) %end = func.call @trim_end(%text, %begin, %length) : (buffer, offset, offset) -> (offset) %result = func.call @slice(%text, %begin, %end) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } func.def inline @byte_is(%text: buffer, %position: offset, %end: offset, %value: i32) -> (i1) { %inside = index.cmp ult, %position, %end : offset %result = scf.if %inside -> (i1) { %byte = buffer.load.i8.u %text[%position] %same = scalar.cmpi eq, %byte, %value : i32 scf.yield %same : i1 } else { %false = scalar.constant false : i1 scf.yield %false : i1 } func.return %result : i1 } func.def public @model_name() -> (buffer) { %name = global.load @name : buffer func.return %name : buffer } func.decl import("json") @parse_i64(%source: buffer, %offset: i64, %length: i64) -> (i64) func.decl import("json") @parse_f64(%source: buffer, %offset: i64, %length: i64) -> (f64) // Sampling record (16 bytes): temperature f32, top_p f32, top_k i32, seed i32. // temperature 0 is greedy (the default); top_k 0 means "up to 64 candidates". global.rodata.def @temperature_key = bytes("74656d7065726174757265") global.rodata.def @top_p_key = bytes("746f705f70") global.rodata.def @top_k_key = bytes("746f705f6b") global.rodata.def @seed_key = bytes("73656564") // "draft_depth": optional per-request DSpark depth override (-1/omitted = auto adaptive, 1 = fixed d1, >=2 = fixed full depth; use deployment JSON draft_depth: 0 to disable DSpark completely). global.rodata.def @draft_depth_key = bytes("64726166745f6465707468") // logits:begin // "logits": true asks for this request's logits rows (loom-skills#736): bit 30 of the record's top_k (kernel g_logits_export). global.rodata.def @logits_key = bytes("6c6f67697473") // logits:end func.def public @request_defaults(%options: buffer) -> (buffer) { %origin = index.constant 0 : offset %bytes = index.constant 20 : offset %record = buffer.alloca<private> align(4) %bytes : buffer %view = buffer.view %record[%origin] : buffer -> view<5xi32> %c0_i32 = scalar.constant 0 : i32 %c_auto_i32 = scalar.constant -1 : i32 %temperature = scalar.constant 1.0 : f32 %top_p = scalar.constant 1.0 : f32 %top_k = scalar.constant 0 : i32 %temperature_bits = scalar.bitcast %temperature : f32 to i32 %top_p_bits = scalar.bitcast %top_p : f32 to i32 view.store %temperature_bits, %view[0] : i32, view<5xi32> view.store %top_p_bits, %view[1] : i32, view<5xi32> view.store %top_k, %view[2] : i32, view<5xi32> view.store %c0_i32, %view[3] : i32, view<5xi32> view.store %c_auto_i32, %view[4] : i32, view<5xi32> func.return %record : buffer } // OpenAI-style temperature / top_p / top_k / seed / draft_depth (numbers; null keeps the default). // Without a seed each request draws from its own nonce. func.def public @request_options(%defaults: buffer, %request: buffer, %nonce: i64) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %bytes = index.constant 20 : offset %record = buffer.alloca<private> align(4) %bytes : buffer buffer.copy %defaults[%origin], %record[%origin], %bytes %view = buffer.view %record[%origin] : buffer -> view<5xi32> %nonce32 = scalar.trunci %nonce : i64 to i32 view.store %nonce32, %view[3] : i32, view<5xi32> %fields, %type, %count = func.call @members(%request) : (buffer) -> (buffer, i32, i64) %bounded_count = scalar.assume %count [range(%count, 0, 2147483647)] : i64 %extent = index.cast %bounded_count : i64 to index %records = buffer.view %fields[%origin] : buffer -> view<[%extent]x5xi64> %number = scalar.constant 1 : i64 %temperature_key = global.load @temperature_key : buffer %top_p_key = global.load @top_p_key : buffer %top_k_key = global.load @top_k_key : buffer %seed_key = global.load @seed_key : buffer %draft_depth_key = global.load @draft_depth_key : buffer // logits:begin %logits_key = global.load @logits_key : buffer %logits_flag = scalar.constant 1073741824 : i32 %logits_mask = scalar.constant 1073741823 : i32 %true_value = scalar.constant 4 : i64 // logits:end scf.for %i = [%c0 to %extent step %c1] { %key_begin = view.load %records[%i, 0] : view<[%extent]x5xi64> -> i64 %key_length = view.load %records[%i, 1] : view<[%extent]x5xi64> -> i64 %value_begin = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %value_length = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %value_type = view.load %records[%i, 4] : view<[%extent]x5xi64> -> i64 %is_number = scalar.cmpi eq, %value_type, %number : i64 scf.if %is_number { %key = func.call @json_key(%request, %key_begin, %key_length) : (buffer, i64, i64) -> (buffer) %is_temperature = func.call @equal(%key, %temperature_key) : (buffer, buffer) -> (i1) %is_top_p = func.call @equal(%key, %top_p_key) : (buffer, buffer) -> (i1) %is_top_k = func.call @equal(%key, %top_k_key) : (buffer, buffer) -> (i1) %is_seed = func.call @equal(%key, %seed_key) : (buffer, buffer) -> (i1) %is_draft_depth = func.call @equal(%key, %draft_depth_key) : (buffer, buffer) -> (i1) %is_float = scalar.ori %is_temperature, %is_top_p : i1 scf.if %is_float { %value64 = func.call @parse_f64(%request, %value_begin, %value_length) : (buffer, i64, i64) -> (f64) %value = scalar.fptrunc %value64 : f64 to f32 %bits = scalar.bitcast %value : f32 to i32 scf.if %is_temperature { view.store %bits, %view[0] : i32, view<5xi32> } else { view.store %bits, %view[1] : i32, view<5xi32> } } %is_int0 = scalar.ori %is_top_k, %is_seed : i1 %is_integer = scalar.ori %is_int0, %is_draft_depth : i1 scf.if %is_integer { %value64 = func.call @parse_i64(%request, %value_begin, %value_length) : (buffer, i64, i64) -> (i64) %value = scalar.trunci %value64 : i64 to i32 scf.if %is_top_k { // logits:begin %old_top_k = view.load %view[2] : view<5xi32> -> i32 %kept_flag = scalar.andi %old_top_k, %logits_flag : i32 %k_bits = scalar.andi %value, %logits_mask : i32 %merged_k = scalar.ori %k_bits, %kept_flag : i32 view.store %merged_k, %view[2] : i32, view<5xi32> // logits:end } else { scf.if %is_seed { view.store %value, %view[3] : i32, view<5xi32> } else { view.store %value, %view[4] : i32, view<5xi32> } } } } // logits:begin %is_true = scalar.cmpi eq, %value_type, %true_value : i64 scf.if %is_true { %flag_key = func.call @json_key(%request, %key_begin, %key_length) : (buffer, i64, i64) -> (buffer) %is_logits = func.call @equal(%flag_key, %logits_key) : (buffer, buffer) -> (i1) scf.if %is_logits { %current_top_k = view.load %view[2] : view<5xi32> -> i32 %flagged_top_k = scalar.ori %current_top_k, %logits_flag : i32 view.store %flagged_top_k, %view[2] : i32, view<5xi32> } } // logits:end } func.return %record : buffer } // Sampling + draft_depth records follow the 640-byte control header, one per resident row. func.def public @bind_request(%state: buffer, %row: i32, %options: buffer) { %origin = index.constant 0 : offset %header = index.constant 2048 : offset %record_bytes = index.constant 20 : offset %bounded_row = scalar.assume %row [range(%row, 0, 1048575)] : i32 %row_index = index.cast %bounded_row : i32 to index %relative = index.scale %row_index, %record_bytes : index, offset -> offset %at = index.add %header, %relative : offset %end = index.add %at, %record_bytes : offset %length = buffer.length %state %fits = index.cmp ule, %end, %length : offset %options_length = buffer.length %options %complete = index.cmp uge, %options_length, %record_bytes : offset %store = scalar.andi %fits, %complete : i1 scf.if %store { buffer.copy %options[%origin], %state[%at], %record_bytes } func.return } // (reasoning supported, selectable efforts bitmap, default effort): // efforts none=0 ... high=4; a toggleable thinking model offers none|high. func.def public @chat_reasoning() -> (i32, i32, i32) { %supported = scalar.constant 1 : i32 %efforts = scalar.constant 127 : i32 %default = scalar.constant 4 : i32 func.return %supported, %efforts, %default : i32, i32, i32 } func.def public @chat_image_limits() -> (i64, i64, i64, i64, i64) { %maximum_count = scalar.constant 4 : i64 %compressed_bytes = scalar.constant 1048576 : i64 %width = scalar.constant 4096 : i64 %height = scalar.constant 4096 : i64 %pixels = scalar.constant 16777216 : i64 func.return %maximum_count, %compressed_bytes, %width, %height, %pixels : i64, i64, i64, i64, i64 } // Chat template, from the config "chat" block (build.py fills these): // {"bos": "", "system_prefix": "<|message_system|><|content_text|>", "suffix": "<|end_message|>", "user_prefix": "<|message_user|><|content_text|>", "assistant_prefix": "<|message_model|><|content_text|>", "default_system": "", "trim_content": false, "think_open": "<|message_model|><|content_thinking|>", "think_close": "<|end_message|>", "history_think": "nonempty", "history_think_scope": "all", "ge global.rodata.def @system_prefix = bytes("3c7c6d6573736167655f73797374656d7c3e3c7c636f6e74656e745f746578747c3e") global.rodata.def @user_prefix = bytes("3c7c6d6573736167655f757365727c3e3c7c636f6e74656e745f746578747c3e") global.rodata.def @assistant_prefix = bytes("3c7c6d6573736167655f6d6f64656c7c3e3c7c636f6e74656e745f746578747c3e") global.rodata.def @message_suffix = bytes("3c7c656e645f6d6573736167657c3e") global.rodata.def @think_open = bytes("3c7c6d6573736167655f6d6f64656c7c3e3c7c636f6e74656e745f7468696e6b696e677c3e") global.rodata.def @think_close = bytes("3c7c656e645f6d6573736167657c3e") global.rodata.def @generation_think = bytes("") global.rodata.def @generation_plain = bytes("") global.rodata.def @default_system = bytes("") global.rodata.def @bos = bytes("") global.rodata.def @tools_prefix = bytes("3c7c6d6573736167655f73797374656d7c3e746f6f6c5f6465636c6172653c7c636f6e74656e745f786d6c7c3e") global.rodata.def @tools_suffix = bytes("3c7c656e645f6d6573736167657c3e") global.rodata.def @tool_line_prefix = bytes("") // Printed between consecutive tools only (LFM2: ", "). global.rodata.def @tool_separator = bytes("") // Pythonic calls (LFM2): the whole call list is wrapped in CALLS_OPEN ... CALLS_CLOSE // (empty for the other formats); CALL_END ends the list in generated text. global.rodata.def @calls_open = bytes("") global.rodata.def @calls_close = bytes("") // Stop token the model emits to end a turn when the template renders that same token next // (GLM-style: <|observation|> after calls, <|user|> after an answer). It stays in the KV row, so the // published history must contain it too, or the next turn duplicates it. global.rodata.def @stop_after_calls = bytes("") global.rodata.def @stop_after_answer = bytes("") global.rodata.def @call_end = bytes("") global.rodata.def @tools_system_separator = bytes("") global.rodata.def @tool_turn_prefix = bytes("") global.rodata.def @tool_item_prefix = bytes("3c7c6d6573736167655f746f6f6c7c3e") global.rodata.def @tool_item_suffix = bytes("3c7c656e645f6d6573736167657c3e") global.rodata.def @tool_turn_suffix = bytes("") global.rodata.def @content_call_separator = bytes("") global.rodata.def @call_separator = bytes("") // The tools JSON array as the template prints it: each tool object (or its // "function" member when TOOLS_FUNCTION_ONLY), one per line. global.rodata.def @function_key = bytes("66756e6374696f6e") func.def @tool_printed(%tool: buffer) -> (buffer) { %only = scalar.constant false : i1 %result = scf.if %only -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %fields, %type, %count = func.call @members(%tool) : (buffer) -> (buffer, i32, i64) %bounded_count = scalar.assume %count [range(%count, 0, 2147483647)] : i64 %extent = index.cast %bounded_count : i64 to index %records = buffer.view %fields[%origin] : buffer -> view<[%extent]x5xi64> %function_key = global.load @function_key : buffer %selected = scf.for %i = [%c0 to %extent step %c1](%current = %tool : buffer) -> (buffer) { %key_origin = view.load %records[%i, 0] : view<[%extent]x5xi64> -> i64 %key_length = view.load %records[%i, 1] : view<[%extent]x5xi64> -> i64 %key = func.call @json_key(%tool, %key_origin, %key_length) : (buffer, i64, i64) -> (buffer) %is_function = func.call @equal(%key, %function_key) : (buffer, buffer) -> (i1) %next = scf.if %is_function -> (buffer) { %value_origin = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %value_length = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %value = func.call @json_range(%tool, %value_origin, %value_length) : (buffer, i64, i64) -> (buffer) scf.yield %value : buffer } else { scf.yield %current : buffer } scf.yield %next : buffer } scf.yield %selected : buffer } else { scf.yield %tool : buffer } func.return %result : buffer } func.def @tools_body(%tools: buffer, %tool_fields: buffer, %tool_count: i64) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %count = scalar.assume %tool_count [range(%tool_count, 1, 4096)] : i64 %extent = index.cast %count : i64 to index %records = buffer.view %tool_fields[%origin] : buffer -> view<[%extent]x5xi64> %prefix = global.load @tools_prefix : buffer %suffix = global.load @tools_suffix : buffer %newline = global.load @tool_line_prefix : buffer %between = global.load @tool_separator : buffer %none = global.load @empty : buffer %body = scf.for %i = [%c0 to %extent step %c1](%text = %prefix : buffer) -> (buffer) { %value_origin = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %value_length = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %value = func.call @json_range(%tools, %value_origin, %value_length) : (buffer, i64, i64) -> (buffer) %printed = func.call @tool_printed(%value) : (buffer) -> (buffer) %later = index.cmp ne, %i, %c0 : index %gap = scf.select %later, %between, %none : buffer %line = func.call @join4(%gap, %newline, %printed, %none) : (buffer, buffer, buffer, buffer) -> (buffer) %next = func.call @join(%text, %line) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } %result = func.call @join(%body, %suffix) : (buffer, buffer) -> (buffer) func.return %result : buffer } // Returns (found, value range begin, length, type) of %key in the object at // %text[begin, begin + length). func.def @json_lookup(%text: buffer, %begin: i64, %length: i64, %key: buffer) -> (i1, i64, i64, i64) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %false = scalar.constant false : i1 %fields, %type, %count = func.call @members_range(%text, %begin, %length) : (buffer, i64, i64) -> (buffer, i32, i64) %object = scalar.constant 2 : i32 %is_object = scalar.cmpi eq, %type, %object : i32 %usable_count = scf.select %is_object, %count, %zero64 : i64 %bounded_count = scalar.assume %usable_count [range(%usable_count, 0, 2147483647)] : i64 %extent = index.cast %bounded_count : i64 to index %records = buffer.view %fields[%origin] : buffer -> view<[%extent]x5xi64> %found, %value_begin, %value_length, %value_type = scf.for %i = [%c0 to %extent step %c1](%f = %false : i1, %b = %zero64 : i64, %l = %zero64 : i64, %t = %zero64 : i64) -> (i1, i64, i64, i64) { %key_origin = view.load %records[%i, 0] : view<[%extent]x5xi64> -> i64 %key_length = view.load %records[%i, 1] : view<[%extent]x5xi64> -> i64 %name = func.call @json_key(%text, %key_origin, %key_length) : (buffer, i64, i64) -> (buffer) %match = func.call @equal(%name, %key) : (buffer, buffer) -> (i1) %vb = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %vl = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %vt = view.load %records[%i, 4] : view<[%extent]x5xi64> -> i64 %nf = scalar.ori %f, %match : i1 %nb = scf.select %match, %vb, %b : i64 %nl = scf.select %match, %vl, %l : i64 %nt = scf.select %match, %vt, %t : i64 scf.yield %nf, %nb, %nl, %nt : i1, i64, i64, i64 } func.return %found, %value_begin, %value_length, %value_type : i1, i64, i64, i64 } // Thinking selection from a value: true/false, or a reasoning_effort string. func.def @thinking_value(%request: buffer, %found: i1, %begin: i64, %length: i64, %type: i64, %effort: i1, %current: i1) -> (i1) { %result = scf.if %found -> (i1) { %selected = scf.if %effort -> (i1) { %value = func.call @json_key(%request, %begin, %length) : (buffer, i64, i64) -> (buffer) %none = global.load @effort_none : buffer %is_none = func.call @equal(%value, %none) : (buffer, buffer) -> (i1) %false = scalar.constant false : i1 %on = scalar.cmpi eq, %is_none, %false : i1 scf.yield %on : i1 } else { %true_type = scalar.constant 4 : i64 %on = scalar.cmpi eq, %type, %true_type : i64 scf.yield %on : i1 } scf.yield %selected : i1 } else { scf.yield %current : i1 } func.return %result : i1 } // 1 + index of the last "user" message in the request's messages array (0: none). func.def @last_user(%request: buffer, %begin: i64, %length: i64) -> (i64) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %one64 = scalar.constant 1 : i64 %elements, %type, %count = func.call @members_range(%request, %begin, %length) : (buffer, i64, i64) -> (buffer, i32, i64) %bounded_count = scalar.assume %count [range(%count, 0, 2147483647)] : i64 %extent = index.cast %bounded_count : i64 to index %records = buffer.view %elements[%origin] : buffer -> view<[%extent]x5xi64> %role_key = global.load @role_key : buffer %user_role = global.load @user : buffer %object_type = scalar.constant 2 : i64 %result = scf.for %i = [%c0 to %extent step %c1](%last = %zero64 : i64) -> (i64) { %mb = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %ml = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %mt = view.load %records[%i, 4] : view<[%extent]x5xi64> -> i64 %is_object = scalar.cmpi eq, %mt, %object_type : i64 %next = scf.if %is_object -> (i64) { %fr, %br, %lr, %tr = func.call @json_lookup(%request, %mb, %ml, %role_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %role = func.call @json_key(%request, %br, %lr) : (buffer, i64, i64) -> (buffer) %is_user = func.call @equal(%role, %user_role) : (buffer, buffer) -> (i1) %index = index.cast %i : index to i64 %ordinal = scalar.addi %index, %one64 : i64 %value = scf.select %is_user, %ordinal, %last : i64 scf.yield %value : i64 } else { scf.yield %last : i64 } scf.yield %next : i64 } func.return %result : i64 } // Reasoning instructions (the template's text per reasoning_effort): selected by the request's // reasoning_effort, else chat_template_kwargs.reasoning_effort, else the template's default effort. global.rodata.def @reasoning_separator = bytes("") global.rodata.def @reasoning_default = bytes("302e39") global.rodata.def @reasoning_name_0 = bytes("6e6f6e65") global.rodata.def @reasoning_text_0 = bytes("30") global.rodata.def @reasoning_name_1 = bytes("6d696e696d616c") global.rodata.def @reasoning_text_1 = bytes("302e31") global.rodata.def @reasoning_name_2 = bytes("6c6f77") global.rodata.def @reasoning_text_2 = bytes("302e32") global.rodata.def @reasoning_name_3 = bytes("6d656469756d") global.rodata.def @reasoning_text_3 = bytes("302e37") global.rodata.def @reasoning_name_4 = bytes("68696768") global.rodata.def @reasoning_text_4 = bytes("302e39") global.rodata.def @reasoning_name_5 = bytes("7868696768") global.rodata.def @reasoning_text_5 = bytes("302e3939") global.rodata.def @reasoning_name_6 = bytes("6d6178") global.rodata.def @reasoning_text_6 = bytes("302e3939") func.def @reasoning_instructions(%request: buffer, %thinking: i1) -> (buffer) { %empty = global.load @empty : buffer %result = scf.if %thinking -> (buffer) { %zero64 = scalar.constant 0 : i64 %false = scalar.constant false : i1 %string_type = scalar.constant 0 : i64 %request_bytes = buffer.length %request %request_length = index.cast %request_bytes : offset to i64 %effort_key = global.load @effort_key : buffer %kwargs_key = global.load @kwargs_key : buffer %fk, %bk, %lk, %tk = func.call @json_lookup(%request, %zero64, %request_length, %kwargs_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %f1, %b1, %l1, %t1 = scf.if %fk -> (i1, i64, i64, i64) { %f, %b, %l, %t = func.call @json_lookup(%request, %bk, %lk, %effort_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) scf.yield %f, %b, %l, %t : i1, i64, i64, i64 } else { scf.yield %false, %zero64, %zero64, %zero64 : i1, i64, i64, i64 } %kwargs_string = scalar.cmpi eq, %t1, %string_type : i64 %use_kwargs = scalar.andi %f1, %kwargs_string : i1 %f2, %b2, %l2, %t2 = func.call @json_lookup(%request, %zero64, %request_length, %effort_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %top_string = scalar.cmpi eq, %t2, %string_type : i64 %use_top = scalar.andi %f2, %top_string : i1 %found = scalar.ori %use_top, %use_kwargs : i1 %begin = scf.select %use_top, %b2, %b1 : i64 %length = scf.select %use_top, %l2, %l1 : i64 %name = scf.if %found -> (buffer) { %value = func.call @json_key(%request, %begin, %length) : (buffer, i64, i64) -> (buffer) scf.yield %value : buffer } else { scf.yield %empty : buffer } %selected_default = global.load @reasoning_default : buffer %name_0 = global.load @reasoning_name_0 : buffer %match_0 = func.call @equal(%name, %name_0) : (buffer, buffer) -> (i1) %text_0 = global.load @reasoning_text_0 : buffer %selected_0 = scf.select %match_0, %text_0, %selected_default : buffer %name_1 = global.load @reasoning_name_1 : buffer %match_1 = func.call @equal(%name, %name_1) : (buffer, buffer) -> (i1) %text_1 = global.load @reasoning_text_1 : buffer %selected_1 = scf.select %match_1, %text_1, %selected_0 : buffer %name_2 = global.load @reasoning_name_2 : buffer %match_2 = func.call @equal(%name, %name_2) : (buffer, buffer) -> (i1) %text_2 = global.load @reasoning_text_2 : buffer %selected_2 = scf.select %match_2, %text_2, %selected_1 : buffer %name_3 = global.load @reasoning_name_3 : buffer %match_3 = func.call @equal(%name, %name_3) : (buffer, buffer) -> (i1) %text_3 = global.load @reasoning_text_3 : buffer %selected_3 = scf.select %match_3, %text_3, %selected_2 : buffer %name_4 = global.load @reasoning_name_4 : buffer %match_4 = func.call @equal(%name, %name_4) : (buffer, buffer) -> (i1) %text_4 = global.load @reasoning_text_4 : buffer %selected_4 = scf.select %match_4, %text_4, %selected_3 : buffer %name_5 = global.load @reasoning_name_5 : buffer %match_5 = func.call @equal(%name, %name_5) : (buffer, buffer) -> (i1) %text_5 = global.load @reasoning_text_5 : buffer %selected_5 = scf.select %match_5, %text_5, %selected_4 : buffer %name_6 = global.load @reasoning_name_6 : buffer %match_6 = func.call @equal(%name, %name_6) : (buffer, buffer) -> (i1) %text_6 = global.load @reasoning_text_6 : buffer %selected_6 = scf.select %match_6, %text_6, %selected_5 : buffer scf.yield %selected_6 : buffer } else { scf.yield %empty : buffer } func.return %result : buffer } // INSTRUCTIONS + separator + BODY, or whichever of the two is not empty. func.def @with_instructions(%instructions: buffer, %body: buffer) -> (buffer) { %origin = index.constant 0 : offset %empty = global.load @empty : buffer %instructions_length = buffer.length %instructions %body_length = buffer.length %body %has_instructions = index.cmp ne, %instructions_length, %origin : offset %has_body = index.cmp ne, %body_length, %origin : offset %both = scalar.andi %has_instructions, %has_body : i1 %separator_value = global.load @reasoning_separator : buffer %separator = scf.select %both, %separator_value, %empty : buffer %value = func.call @join4(%instructions, %separator, %body, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) func.return %value : buffer } // State bits carried through chat_message (opaque to the host): // 0-1 previous role (0 none, 1 user, 2 assistant, 3 tool) // 2 the first system message was already rendered by chat_begin // 4 thinking enabled for the generation prompt // 8+ 1 + index of the last user message (0: none); reasoning is rendered // only after it when HISTORY_THINK_AFTER_QUERY (Qwen-style templates) // chat_begin renders the system block whenever tools are present or the // template has a default system prompt, merging the first system message's // text in the template's order. func.def @generic_chat_begin(%request: buffer, %tools: buffer) -> (buffer, i64) { %origin = index.constant 0 : offset %zero64 = scalar.constant 0 : i64 %four64 = scalar.constant 4 : i64 %sixteen64 = scalar.constant 16 : i64 %false = scalar.constant false : i1 %empty = global.load @empty : buffer %request_bytes = buffer.length %request %request_length = index.cast %request_bytes : offset to i64 // Thinking: default, then enable_thinking, chat_template_kwargs.enable_thinking, reasoning_effort. %toggle = scalar.constant true : i1 %think_default = scalar.constant true : i1 %thinking_key = global.load @thinking_key : buffer %kwargs_key = global.load @kwargs_key : buffer %effort_key = global.load @effort_key : buffer %f1, %b1, %l1, %t1 = func.call @json_lookup(%request, %zero64, %request_length, %thinking_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %think1 = func.call @thinking_value(%request, %f1, %b1, %l1, %t1, %false, %think_default) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) %fk, %bk, %lk, %tk = func.call @json_lookup(%request, %zero64, %request_length, %kwargs_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %think2 = scf.if %fk -> (i1) { %f2, %b2, %l2, %t2 = func.call @json_lookup(%request, %bk, %lk, %thinking_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %value = func.call @thinking_value(%request, %f2, %b2, %l2, %t2, %false, %think1) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) scf.yield %value : i1 } else { scf.yield %think1 : i1 } %fe, %be, %le, %te = func.call @json_lookup(%request, %zero64, %request_length, %effort_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %string_type = scalar.constant 0 : i64 %effort_string = scalar.cmpi eq, %te, %string_type : i64 %use_effort = scalar.andi %fe, %effort_string : i1 %true = scalar.constant true : i1 %think3 = func.call @thinking_value(%request, %use_effort, %be, %le, %te, %true, %think2) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) %thinking = scf.select %toggle, %think3, %think_default : i1 %instructions = func.call @reasoning_instructions(%request, %thinking) : (buffer, i1) -> (buffer) %instructions_length = buffer.length %instructions %has_instructions = index.cmp ne, %instructions_length, %origin : offset // First message: is it a system message with plain text content? %messages_key = global.load @messages_key : buffer %fm, %bm, %lm, %tm = func.call @json_lookup(%request, %zero64, %request_length, %messages_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %array_type = scalar.constant 3 : i64 %is_array = scalar.cmpi eq, %tm, %array_type : i64 %has_messages = scalar.andi %fm, %is_array : i1 %first_system, %system_text = scf.if %has_messages -> (i1, buffer) { %elements, %etype, %ecount = func.call @members_range(%request, %bm, %lm) : (buffer, i64, i64) -> (buffer, i32, i64) %nonempty = scalar.cmpi ne, %ecount, %zero64 : i64 %fs, %text = scf.if %nonempty -> (i1, buffer) { %records = buffer.view %elements[%origin] : buffer -> view<1x5xi64> %mb = view.load %records[0, 2] : view<1x5xi64> -> i64 %ml = view.load %records[0, 3] : view<1x5xi64> -> i64 %role_key = global.load @role_key : buffer %content_key = global.load @content_key : buffer %fr, %br, %lr, %tr = func.call @json_lookup(%request, %mb, %ml, %role_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %fc, %bc, %lc, %tc = func.call @json_lookup(%request, %mb, %ml, %content_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %role = func.call @json_key(%request, %br, %lr) : (buffer, i64, i64) -> (buffer) %system_role = global.load @system : buffer %is_system = func.call @equal(%role, %system_role) : (buffer, buffer) -> (i1) %content_string = scalar.cmpi eq, %tc, %string_type : i64 %plain = scalar.andi %fc, %content_string : i1 %system_plain = scalar.andi %is_system, %plain : i1 %value = scf.if %system_plain -> (buffer) { %decoded = func.call @json_key(%request, %bc, %lc) : (buffer, i64, i64) -> (buffer) scf.yield %decoded : buffer } else { scf.yield %empty : buffer } scf.yield %system_plain, %value : i1, buffer } else { scf.yield %false, %empty : i1, buffer } scf.yield %fs, %text : i1, buffer } else { scf.yield %false, %empty : i1, buffer } %last_query = scf.if %has_messages -> (i64) { %value = func.call @last_user(%request, %bm, %lm) : (buffer, i64, i64) -> (i64) scf.yield %value : i64 } else { scf.yield %zero64 : i64 } %trim_content = scalar.constant false : i1 %trimmed_system = scf.if %trim_content -> (buffer) { %value = func.call @trim(%system_text) : (buffer) -> (buffer) scf.yield %value : buffer } else { scf.yield %system_text : buffer } %default_system = global.load @default_system : buffer %default_length = buffer.length %default_system %has_default = index.cmp ne, %default_length, %origin : offset %not_first_system = scalar.cmpi eq, %first_system, %false : i1 %use_default = scalar.andi %not_first_system, %has_default : i1 %system_body = scf.select %use_default, %default_system, %trimmed_system : buffer %tool_fields, %tool_type, %tool_count = func.call @members(%tools) : (buffer) -> (buffer, i32, i64) %has_tools = scalar.cmpi ne, %tool_count, %zero64 : i64 %system_prefix = global.load @system_prefix : buffer %suffix = global.load @message_suffix : buffer %rendered = scf.if %has_tools -> (buffer) { %tools_text = func.call @tools_body(%tools, %tool_fields, %tool_count) : (buffer, buffer, i64) -> (buffer) %body_length = buffer.length %system_body %has_body = index.cmp ne, %body_length, %origin : offset %separator_value = global.load @tools_system_separator : buffer %separator = scf.select %has_body, %separator_value, %empty : buffer %after_system = scalar.constant false : i1 %merged = scf.if %after_system -> (buffer) { %value = func.call @join4(%system_body, %separator, %tools_text, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %value : buffer } else { %value = func.call @join4(%tools_text, %separator, %system_body, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %value : buffer } %instructed = func.call @with_instructions(%instructions, %merged) : (buffer, buffer) -> (buffer) %block = func.call @join4(%system_prefix, %instructed, %suffix, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %block : buffer } else { // With instructions the block also takes the first system message (chat_message then skips it). %plain_body = scf.select %use_default, %default_system, %empty : buffer %rest = scf.select %has_instructions, %system_body, %plain_body : buffer %instructed = func.call @with_instructions(%instructions, %rest) : (buffer, buffer) -> (buffer) %instructed_length = buffer.length %instructed %has_block = index.cmp ne, %instructed_length, %origin : offset %block = scf.if %has_block -> (buffer) { %value = func.call @join4(%system_prefix, %instructed, %suffix, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %value : buffer } else { scf.yield %empty : buffer } scf.yield %block : buffer } %bos = global.load @bos : buffer %prefixed = func.call @join(%bos, %rendered) : (buffer, buffer) -> (buffer) %merges_system = scalar.ori %has_tools, %has_instructions : i1 %consumed = scalar.andi %merges_system, %first_system : i1 %consumed_bit = scf.select %consumed, %four64, %zero64 : i64 %thinking_bit = scf.select %thinking, %sixteen64, %zero64 : i64 %flags = scalar.ori %consumed_bit, %thinking_bit : i64 %shift = scalar.constant 8 : i64 %query_bits = scalar.shli %last_query, %shift : i64 %state = scalar.ori %flags, %query_bits : i64 func.return %prefixed, %state : buffer, i64 } // Text-only content parts (kind 0) are concatenated; image parts are rejected. func.def @text_parts(%content: buffer, %parts: buffer, %part_count: i64) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %text_kind = scalar.constant 0 : i32 %bounded_count = scalar.assume %part_count [range(%part_count, 1, 4096)] : i64 %count = index.cast %bounded_count : i64 to index %records = buffer.view %parts[%origin] : buffer -> view<[%count]x6xi32> %empty = global.load @empty : buffer %result = scf.for %i = [%c0 to %count step %c1](%text = %empty : buffer) -> (buffer) { %kind = view.load %records[%i, 0] : view<[%count]x6xi32> -> i32 %is_text = scalar.cmpi eq, %kind, %text_kind : i32 func.call @check(%is_text) : (i1) %source32 = view.load %records[%i, 1] : view<[%count]x6xi32> -> i32 %length32 = view.load %records[%i, 2] : view<[%count]x6xi32> -> i32 %source64 = scalar.extui %source32 : i32 to i64 %length64 = scalar.extui %length32 : i32 to i64 %piece = func.call @json_range(%content, %source64, %length64) : (buffer, i64, i64) -> (buffer) %next = func.call @join(%text, %piece) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } func.return %result : buffer } // Renders one message. Host field bits: 1 reasoning_content, 2 tool_calls // (assistant only), 4 tool_call_id (tool only). %calls holds the // concatenated render_tool output of the assistant's tool calls. func.def @generic_chat_message(%raw: buffer, %role: buffer, %content: buffer, %parts: buffer, %frames: buffer, %reasoning: buffer, %calls: buffer, %state: i64, %ordinal: i64, %fields: i64, %part_count: i64) -> (buffer, i64, buffer, i64) { %c0 = index.constant 0 : offset %zero64 = scalar.constant 0 : i64 %one64 = scalar.constant 1 : i64 %two64 = scalar.constant 2 : i64 %three64 = scalar.constant 3 : i64 %four64 = scalar.constant 4 : i64 %keep_mask = scalar.constant -4 : i64 %false = scalar.constant false : i1 %assistant_role = global.load @assistant : buffer %user_role = global.load @user : buffer %system_role = global.load @system : buffer %tool_role = global.load @tool_role : buffer %assistant = func.call @equal(%role, %assistant_role) : (buffer, buffer) -> (i1) %user = func.call @equal(%role, %user_role) : (buffer, buffer) -> (i1) %system = func.call @equal(%role, %system_role) : (buffer, buffer) -> (i1) %tool = func.call @equal(%role, %tool_role) : (buffer, buffer) -> (i1) %ordinary = scalar.ori %user, %system : i1 %speaker = scalar.ori %assistant, %tool : i1 %supported = scalar.ori %ordinary, %speaker : i1 %assistant_fields = scalar.andi %fields, %three64 : i64 %has_assistant_fields = scalar.cmpi ne, %assistant_fields, %zero64 : i64 %not_assistant_fields = scalar.cmpi eq, %has_assistant_fields, %false : i1 %valid_assistant_fields = scalar.ori %assistant, %not_assistant_fields : i1 %valid = scalar.andi %supported, %valid_assistant_fields : i1 func.call @check(%valid) : (i1) %has_parts = scalar.cmpi ne, %part_count, %zero64 : i64 %raw_text = scf.if %has_parts -> (buffer) { %joined = func.call @text_parts(%content, %parts, %part_count) : (buffer, buffer, i64) -> (buffer) scf.yield %joined : buffer } else { scf.yield %content : buffer } %trim_content = scalar.constant false : i1 %text = scf.if %trim_content -> (buffer) { %value = func.call @trim(%raw_text) : (buffer) -> (buffer) scf.yield %value : buffer } else { scf.yield %raw_text : buffer } %empty = global.load @empty : buffer %prior_role = scalar.andi %state, %three64 : i64 %prior_tool = scalar.cmpi eq, %prior_role, %three64 : i64 %not_tool = scalar.cmpi eq, %tool, %false : i1 %close_tools = scalar.andi %prior_tool, %not_tool : i1 %tool_turn_suffix = global.load @tool_turn_suffix : buffer %before = scf.select %close_tools, %tool_turn_suffix, %empty : buffer %first = scalar.cmpi eq, %ordinal, %zero64 : i64 %consumed_bit = scalar.andi %state, %four64 : i64 %consumed = scalar.cmpi ne, %consumed_bit, %zero64 : i64 %first_system = scalar.andi %first, %system : i1 %skip = scalar.andi %first_system, %consumed : i1 %suffix = global.load @message_suffix : buffer %body = scf.if %skip -> (buffer) { scf.yield %empty : buffer } else { %rendered = scf.if %tool -> (buffer) { %turn = global.load @tool_turn_prefix : buffer %open = scf.select %prior_tool, %empty, %turn : buffer %item_prefix = global.load @tool_item_prefix : buffer %item_suffix = global.load @tool_item_suffix : buffer %value = func.call @join4(%open, %item_prefix, %text, %item_suffix) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %value : buffer } else { %value = scf.if %assistant -> (buffer) { %prefix = global.load @assistant_prefix : buffer %thought = func.call @trim(%reasoning) : (buffer) -> (buffer) %thought_length = buffer.length %thought %has_thought = index.cmp ne, %thought_length, %c0 : offset %always = scalar.constant false : i1 %wanted = scalar.ori %always, %has_thought : i1 %shift = scalar.constant 8 : i64 %last_query = scalar.shrui %state, %shift : i64 %after_query = scalar.cmpi sge, %ordinal, %last_query : i64 %scope_all = scalar.constant true : i1 %in_scope = scalar.ori %scope_all, %after_query : i1 %show = scalar.andi %wanted, %in_scope : i1 %think = scf.if %show -> (buffer) { %open = global.load @think_open : buffer %close = global.load @think_close : buffer %block = func.call @join4(%open, %thought, %close, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %block : buffer } else { scf.yield %empty : buffer } %text_length = buffer.length %text %has_text = index.cmp ne, %text_length, %c0 : offset %calls_length = buffer.length %calls %has_calls = index.cmp ne, %calls_length, %c0 : offset %separate = scalar.andi %has_text, %has_calls : i1 %separator_value = global.load @content_call_separator : buffer %separator = scf.select %separate, %separator_value, %empty : buffer %close_value = global.load @calls_close : buffer %close = scf.select %has_calls, %close_value, %empty : buffer %head = func.call @join4(%prefix, %think, %text, %separator) : (buffer, buffer, buffer, buffer) -> (buffer) %message = func.call @join4(%head, %calls, %close, %suffix) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %message : buffer } else { %system_prefix = global.load @system_prefix : buffer %user_prefix = global.load @user_prefix : buffer %prefix = scf.select %user, %user_prefix, %system_prefix : buffer %message = func.call @join4(%prefix, %text, %suffix, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %message : buffer } scf.yield %value : buffer } scf.yield %rendered : buffer } %fragment = func.call @join(%before, %body) : (buffer, buffer) -> (buffer) %role_tool = scf.select %tool, %three64, %zero64 : i64 %role_assistant = scf.select %assistant, %two64, %role_tool : i64 %new_role = scf.select %user, %one64, %role_assistant : i64 %kept = scalar.andi %state, %keep_mask : i64 %next = scalar.ori %kept, %new_role : i64 func.return %fragment, %next, %empty, %zero64 : buffer, i64, buffer, i64 } // Output state 1 = inside the reasoning block, 0 = answer text. func.def @generic_chat_end(%state: i64) -> (buffer, i64) { %zero64 = scalar.constant 0 : i64 %three64 = scalar.constant 3 : i64 %sixteen64 = scalar.constant 16 : i64 %empty = global.load @empty : buffer %prior_role = scalar.andi %state, %three64 : i64 %prior_tool = scalar.cmpi eq, %prior_role, %three64 : i64 %tool_turn_suffix = global.load @tool_turn_suffix : buffer %before = scf.select %prior_tool, %tool_turn_suffix, %empty : buffer %thinking_bit = scalar.andi %state, %sixteen64 : i64 %thinking = scalar.cmpi ne, %thinking_bit, %zero64 : i64 %think_prompt = global.load @generation_think : buffer %plain_prompt = global.load @generation_plain : buffer %prompt = scf.select %thinking, %think_prompt, %plain_prompt : buffer %starts = scalar.constant 0 : i64 %output_state = scf.select %thinking, %starts, %zero64 : i64 %result = func.call @join(%before, %prompt) : (buffer, buffer) -> (buffer) func.return %result, %output_state : buffer, i64 } // ---- Tool calls -------------------------------------------------------------- // CALL_FORMAT: 4 = pythonic (LFM2: <|tool_call_start|>[NAME(k='v', n=3), ...]<|tool_call_end|>) // CALL_FORMAT: 1 = Hermes JSON (Qwen2.5/Qwen3: <tool_call>\n{"name": ..., "arguments": {...}}\n</tool_call>) // 2 = Qwen XML (Qwen3-Coder/Qwen3.5: <tool_call>\n<function=NAME>\n<parameter=K>\nV\n</parameter>\n</function>\n</tool_call>) // 3 = GLM (GLM-4.5+/Flint: <tool_call>NAME<arg_key>K</arg_key><arg_value>V</arg_value></tool_call>) global.rodata.def @call_open = bytes("3c746f6f6c5f63616c6c3e") global.rodata.def @call_close = bytes("3c2f746f6f6c5f63616c6c3e") // '<tool_call>\n{"name": "', '", "arguments": ', '}\n</tool_call>' global.rodata.def @hermes_open = bytes("3c746f6f6c5f63616c6c3e0a7b226e616d65223a2022") global.rodata.def @hermes_middle = bytes("222c2022617267756d656e7473223a20") global.rodata.def @hermes_close = bytes("7d0a3c2f746f6f6c5f63616c6c3e") // '"name"', '"arguments"' global.rodata.def @name_field = bytes("226e616d6522") global.rodata.def @arguments_field = bytes("22617267756d656e747322") // '<tool_call>\n<function=', '>\n', '<parameter=', '\n</parameter>\n', '</function>\n</tool_call>' global.rodata.def @xml_call_open = bytes("3c746f6f6c5f63616c6c3e0a3c66756e6374696f6e3d") global.rodata.def @xml_tag_end = bytes("3e0a") global.rodata.def @xml_parameter_open = bytes("3c706172616d657465723d") global.rodata.def @xml_parameter_close = bytes("0a3c2f706172616d657465723e0a") global.rodata.def @xml_call_close = bytes("3c2f66756e6374696f6e3e0a3c2f746f6f6c5f63616c6c3e") // '<arg_key>', '</arg_key>', '<arg_value>', '</arg_value>' global.rodata.def @key_open = bytes("3c6172675f6b65793e") global.rodata.def @key_close = bytes("3c2f6172675f6b65793e") global.rodata.def @value_open = bytes("3c6172675f76616c75653e") global.rodata.def @value_close = bytes("3c2f6172675f76616c75653e") // '{"type":"function","function":{"name":"', '","arguments":"', '"}}' global.rodata.def @record_open = bytes("7b2274797065223a2266756e6374696f6e222c2266756e6374696f6e223a7b226e616d65223a22") global.rodata.def @record_middle = bytes("222c22617267756d656e7473223a22") global.rodata.def @record_close = bytes("227d7d") // Renders one call as the template prints it; %ordinal > 0 adds the separator // between consecutive calls. Returns the buffer and its used length. func.def @generic_render_tool(%name: buffer, %arguments: buffer, %ordinal: i64) -> (buffer, i64) { %zero64 = scalar.constant 0 : i64 %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %format = scalar.constant 1 : i32 %hermes = scalar.constant 1 : i32 %glm = scalar.constant 3 : i32 %is_hermes = scalar.cmpi eq, %format, %hermes : i32 %is_glm = scalar.cmpi eq, %format, %glm : i32 %pythonic = scalar.constant 4 : i32 %is_pythonic = scalar.cmpi eq, %format, %pythonic : i32 %has_prior = scalar.cmpi ne, %ordinal, %zero64 : i64 %separator_value = global.load @call_separator : buffer %empty = global.load @empty : buffer %separator = scf.select %has_prior, %separator_value, %empty : buffer %result = scf.if %is_hermes -> (buffer) { // Arguments are printed as the client sent them (the template prints a // string argument verbatim), so generated calls round-trip byte-exactly. %open = global.load @hermes_open : buffer %middle = global.load @hermes_middle : buffer %close = global.load @hermes_close : buffer %head = func.call @join4(%separator, %open, %name, %middle) : (buffer, buffer, buffer, buffer) -> (buffer) %value = func.call @join4(%head, %arguments, %close, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %value : buffer } else { %other = scf.if %is_pythonic -> (buffer) { // NAME(k=v, ...); the first call opens the list (CALLS_OPEN), chat_message and // complete_text close it (CALLS_CLOSE). %list_open = global.load @calls_open : buffer %opener = scf.select %has_prior, %separator_value, %list_open : buffer %py_compacted = func.call @compact(%arguments) : (buffer) -> (buffer) %py_members, %py_type, %py_count = func.call @members(%py_compacted) : (buffer) -> (buffer, i32, i64) %py_object = scalar.constant 2 : i32 %py_is_object = scalar.cmpi eq, %py_type, %py_object : i32 func.call @check(%py_is_object) : (i1) %py_bounded = scalar.assume %py_count [range(%py_count, 0, 33554431)] : i64 %py_extent = index.cast %py_bounded : i64 to index %py_fields = buffer.view %py_members[%origin] : buffer -> view<[%py_extent]x5xi64> %paren_open = global.load @py_open_paren : buffer %paren_close = global.load @py_close_paren : buffer %equals = global.load @py_equals : buffer %comma_space = global.load @py_comma_space : buffer %object_type = scalar.constant 2 : i64 %array_type = scalar.constant 3 : i64 %py_head = func.call @join4(%opener, %name, %paren_open, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) %py_body = scf.for %i = [%c0 to %py_extent step %c1](%text = %py_head : buffer) -> (buffer) { %key_begin = view.load %py_fields[%i, 0] : view<[%py_extent]x5xi64> -> i64 %key_length = view.load %py_fields[%i, 1] : view<[%py_extent]x5xi64> -> i64 %value_begin = view.load %py_fields[%i, 2] : view<[%py_extent]x5xi64> -> i64 %value_length = view.load %py_fields[%i, 3] : view<[%py_extent]x5xi64> -> i64 %value_type = view.load %py_fields[%i, 4] : view<[%py_extent]x5xi64> -> i64 %key = func.call @json_key(%py_compacted, %key_begin, %key_length) : (buffer, i64, i64) -> (buffer) %is_string = scalar.cmpi eq, %value_type, %zero64 : i64 %value = scf.if %is_string -> (buffer) { %decoded = func.call @json_key(%py_compacted, %value_begin, %value_length) : (buffer, i64, i64) -> (buffer) %quoted = func.call @py_quote(%decoded) : (buffer) -> (buffer) scf.yield %quoted : buffer } else { %raw = func.call @json_range(%py_compacted, %value_begin, %value_length) : (buffer, i64, i64) -> (buffer) %is_object = scalar.cmpi eq, %value_type, %object_type : i64 %is_array = scalar.cmpi eq, %value_type, %array_type : i64 %container = scalar.ori %is_object, %is_array : i1 %printed = scf.if %container -> (buffer) { %spaced = func.call @json_spaced(%raw) : (buffer) -> (buffer) scf.yield %spaced : buffer } else { %literal = func.call @py_literal(%raw) : (buffer) -> (buffer) scf.yield %literal : buffer } scf.yield %printed : buffer } %later = index.cmp ne, %i, %c0 : index %lead = scf.select %later, %comma_space, %empty : buffer %opened = func.call @join4(%text, %lead, %key, %equals) : (buffer, buffer, buffer, buffer) -> (buffer) %next = func.call @join(%opened, %value) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } %py_call_text = func.call @join(%py_body, %paren_close) : (buffer, buffer) -> (buffer) scf.yield %py_call_text : buffer } else { // Clients may reserialize arguments; compact keeps the history key stable. %compacted = func.call @compact(%arguments) : (buffer) -> (buffer) %members, %type, %count = func.call @members(%compacted) : (buffer) -> (buffer, i32, i64) %object = scalar.constant 2 : i32 %is_object = scalar.cmpi eq, %type, %object : i32 func.call @check(%is_object) : (i1) %bounded_count = scalar.assume %count [range(%count, 0, 33554431)] : i64 %extent = index.cast %bounded_count : i64 to index %fields = buffer.view %members[%origin] : buffer -> view<[%extent]x5xi64> %xml_open = global.load @xml_call_open : buffer %tag_end = global.load @xml_tag_end : buffer %xml_key = global.load @xml_parameter_open : buffer %xml_value_close = global.load @xml_parameter_close : buffer %xml_close = global.load @xml_call_close : buffer %glm_open = global.load @call_open : buffer %glm_close = global.load @call_close : buffer %glm_key = global.load @key_open : buffer %glm_key_close = global.load @key_close : buffer %glm_value = global.load @value_open : buffer %glm_value_close = global.load @value_close : buffer %call_open = scf.select %is_glm, %glm_open, %xml_open : buffer %name_end = scf.select %is_glm, %empty, %tag_end : buffer %key_open = scf.select %is_glm, %glm_key, %xml_key : buffer %glm_key_end = func.call @join(%glm_key_close, %glm_value) : (buffer, buffer) -> (buffer) %key_end = scf.select %is_glm, %glm_key_end, %tag_end : buffer %value_end = scf.select %is_glm, %glm_value_close, %xml_value_close : buffer %call_close = scf.select %is_glm, %glm_close, %xml_close : buffer %head = func.call @join4(%separator, %call_open, %name, %name_end) : (buffer, buffer, buffer, buffer) -> (buffer) %body = scf.for %i = [%c0 to %extent step %c1](%text = %head : buffer) -> (buffer) { %key_begin = view.load %fields[%i, 0] : view<[%extent]x5xi64> -> i64 %key_length = view.load %fields[%i, 1] : view<[%extent]x5xi64> -> i64 %value_begin = view.load %fields[%i, 2] : view<[%extent]x5xi64> -> i64 %value_length = view.load %fields[%i, 3] : view<[%extent]x5xi64> -> i64 %value_type = view.load %fields[%i, 4] : view<[%extent]x5xi64> -> i64 %key = func.call @json_key(%compacted, %key_begin, %key_length) : (buffer, i64, i64) -> (buffer) %is_string = scalar.cmpi eq, %value_type, %zero64 : i64 %value = scf.if %is_string -> (buffer) { %decoded = func.call @json_key(%compacted, %value_begin, %value_length) : (buffer, i64, i64) -> (buffer) scf.yield %decoded : buffer } else { %raw = func.call @json_range(%compacted, %value_begin, %value_length) : (buffer, i64, i64) -> (buffer) scf.yield %raw : buffer } %opened = func.call @join4(%text, %key_open, %key, %key_end) : (buffer, buffer, buffer, buffer) -> (buffer) %next = func.call @join4(%opened, %value, %value_end, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %next : buffer } %value = func.call @join(%body, %call_close) : (buffer, buffer) -> (buffer) scf.yield %value : buffer } scf.yield %other : buffer } %length_bytes = buffer.length %result %length = index.cast %length_bytes : offset to i64 func.return %result, %length : buffer, i64 } // Text after the last complete </tool_call> belongs to no call; like common // OpenAI-compatible parsers, it is dropped instead of failing the turn. func.def @through_last_call(%response: buffer) -> (buffer) { %origin = index.constant 0 : offset %close = global.load @call_close : buffer %close_length = buffer.length %close %length = buffer.length %response %scanned, %last = scf.while(%state = %origin : offset, %found = %origin : offset) -> (offset, offset) { %position = func.call @find(%response, %state, %length, %close) : (buffer, offset, offset, buffer) -> (offset) %hit = index.cmp ult, %position, %length : offset %after = index.add %position, %close_length : offset %next_found = scf.select %hit, %after, %found : offset scf.condition %hit, %after, %next_found : i1, offset, offset } do(%state: offset, %found: offset) { scf.yield %state, %found : offset, offset } %matched = index.cmp ne, %last, %origin : offset %result = scf.if %matched -> (buffer) { %prefix = func.call @slice(%response, %origin, %last) : (buffer, offset, offset) -> (buffer) scf.yield %prefix : buffer } else { scf.yield %response : buffer } func.return %result : buffer } // Appends text[begin, end) as JSON string content. func.def @json_escape(%text: buffer, %begin: offset, %end: offset, %output: buffer, %position: offset) -> (offset) { %c1 = index.constant 1 : offset %c2 = index.constant 2 : offset %quote = scalar.constant 34 : i32 %backslash = scalar.constant 92 : i32 %newline = scalar.constant 10 : i32 %return = scalar.constant 13 : i32 %tab = scalar.constant 9 : i32 %space = scalar.constant 32 : i32 %letter_n = scalar.constant 110 : i32 %letter_r = scalar.constant 114 : i32 %letter_t = scalar.constant 116 : i32 %final_in, %final_out = scf.while(%in = %begin : offset, %out = %position : offset) -> (offset, offset) { %more = index.cmp ult, %in, %end : offset scf.condition %more, %in, %out : i1, offset, offset } do(%in: offset, %out: offset) { %byte = buffer.load.i8.u %text[%in] %is_quote = scalar.cmpi eq, %byte, %quote : i32 %is_backslash = scalar.cmpi eq, %byte, %backslash : i32 %is_newline = scalar.cmpi eq, %byte, %newline : i32 %is_return = scalar.cmpi eq, %byte, %return : i32 %is_tab = scalar.cmpi eq, %byte, %tab : i32 %is_control = scalar.cmpi ult, %byte, %space : i32 %literal = scalar.ori %is_quote, %is_backslash : i1 %letter_rt = scf.select %is_return, %letter_r, %letter_t : i32 %letter = scf.select %is_newline, %letter_n, %letter_rt : i32 %named_a = scalar.ori %is_newline, %is_return : i1 %named = scalar.ori %named_a, %is_tab : i1 %escaped = scalar.ori %literal, %named : i1 %second = scf.select %literal, %byte, %letter : i32 %written = scf.if %escaped -> (offset) { buffer.store.i8 %backslash, %output[%out] %next_out = index.add %out, %c1 : offset buffer.store.i8 %second, %output[%next_out] %after = index.add %out, %c2 : offset scf.yield %after : offset } else { // Other control bytes are invalid in JSON text; they become spaces. %plain = scf.select %is_control, %space, %byte : i32 buffer.store.i8 %plain, %output[%out] %after = index.add %out, %c1 : offset scf.yield %after : offset } %next_in = index.add %in, %c1 : offset scf.yield %next_in, %written : offset, offset } func.return %final_out : offset } // End (exclusive) of the JSON string whose content starts at %from. func.def inline @string_end(%text: buffer, %from: offset, %end: offset) -> (offset) { %c1 = index.constant 1 : offset %c2 = index.constant 2 : offset %quote = scalar.constant 34 : i32 %backslash = scalar.constant 92 : i32 %result = scf.while(%position = %from : offset) -> (offset) { %inside = index.cmp ult, %position, %end : offset %continue = scf.if %inside -> (i1) { %byte = buffer.load.i8.u %text[%position] %closing = scalar.cmpi eq, %byte, %quote : i32 %false = scalar.constant false : i1 %open = scalar.cmpi eq, %closing, %false : i1 scf.yield %open : i1 } else { %false = scalar.constant false : i1 scf.yield %false : i1 } scf.condition %continue, %position : i1, offset } do(%position: offset) { %byte = buffer.load.i8.u %text[%position] %escape = scalar.cmpi eq, %byte, %backslash : i32 %step = scf.select %escape, %c2, %c1 : offset %next = index.add %position, %step : offset scf.yield %next : offset } %bounded = index.cmp ult, %result, %end : offset %final = scf.select %bounded, %result, %end : offset func.return %final : offset } // Hermes JSON calls, scanned without a JSON parser so a malformed call never // fails the request: arguments are returned as generated (possibly invalid // JSON) for the harness to handle. Unfinished calls are omitted. func.def @hermes_calls(%response: buffer) -> (buffer) { %origin = index.constant 0 : offset %c1 = index.constant 1 : offset %c64 = index.constant 64 : offset %colon = scalar.constant 58 : i32 %quote = scalar.constant 34 : i32 %brace = scalar.constant 125 : i32 %false = scalar.constant false : i1 %true = scalar.constant true : i1 %length = buffer.length %response %twice = index.add %length, %length : offset %scaled = index.add %twice, %twice : offset %capacity = index.add %scaled, %c64 : offset %output = buffer.alloca<private> align(1) %capacity : buffer %open_bracket = scalar.constant 91 : i32 %close_bracket = scalar.constant 93 : i32 %comma = scalar.constant 44 : i32 buffer.store.i8 %open_bracket, %output[%origin] %call_open = global.load @call_open : buffer %call_close = global.load @call_close : buffer %open_length = buffer.length %call_open %close_length = buffer.length %call_close %name_field = global.load @name_field : buffer %arguments_field = global.load @arguments_field : buffer %name_field_length = buffer.length %name_field %arguments_field_length = buffer.length %arguments_field %record_open = global.load @record_open : buffer %record_middle = global.load @record_middle : buffer %record_close = global.load @record_close : buffer %final_position, %final_out, %final_first = scf.while(%position = %origin : offset, %out = %c1 : offset, %first = %true : i1) -> (offset, offset, i1) { %start = func.call @find(%response, %position, %length, %call_open) : (buffer, offset, offset, buffer) -> (offset) %has_start = index.cmp ult, %start, %length : offset %body_begin = index.add %start, %open_length : offset %body_from = scf.select %has_start, %body_begin, %length : offset %stop = func.call @find(%response, %body_from, %length, %call_close) : (buffer, offset, offset, buffer) -> (offset) %has_stop = index.cmp ult, %stop, %length : offset %complete = scalar.andi %has_start, %has_stop : i1 scf.condition %complete, %position, %out, %first : i1, offset, offset, i1 } do(%position: offset, %out: offset, %first: i1) { %start = func.call @find(%response, %position, %length, %call_open) : (buffer, offset, offset, buffer) -> (offset) %body_begin = index.add %start, %open_length : offset %stop = func.call @find(%response, %body_begin, %length, %call_close) : (buffer, offset, offset, buffer) -> (offset) // "name": "<value>" %name_key = func.call @find(%response, %body_begin, %stop, %name_field) : (buffer, offset, offset, buffer) -> (offset) %after_name_key = index.add %name_key, %name_field_length : offset %name_colon = func.call @skip_space(%response, %after_name_key, %stop) : (buffer, offset, offset) -> (offset) %has_name_colon = func.call @byte_is(%response, %name_colon, %stop, %colon) : (buffer, offset, offset, i32) -> (i1) %after_name_colon = index.add %name_colon, %c1 : offset %name_quote = func.call @skip_space(%response, %after_name_colon, %stop) : (buffer, offset, offset) -> (offset) %has_name_quote = func.call @byte_is(%response, %name_quote, %stop, %quote) : (buffer, offset, offset, i32) -> (i1) %name_begin = index.add %name_quote, %c1 : offset %name_end = func.call @string_end(%response, %name_begin, %stop) : (buffer, offset, offset) -> (offset) %name_ok_a = scalar.andi %has_name_colon, %has_name_quote : i1 %name_closed = index.cmp ult, %name_end, %stop : offset %name_ok = scalar.andi %name_ok_a, %name_closed : i1 // "arguments": <value> up to the object's closing brace. %arguments_key = func.call @find(%response, %body_begin, %stop, %arguments_field) : (buffer, offset, offset, buffer) -> (offset) %after_arguments_key = index.add %arguments_key, %arguments_field_length : offset %arguments_colon = func.call @skip_space(%response, %after_arguments_key, %stop) : (buffer, offset, offset) -> (offset) %has_arguments_colon = func.call @byte_is(%response, %arguments_colon, %stop, %colon) : (buffer, offset, offset, i32) -> (i1) %after_arguments_colon = index.add %arguments_colon, %c1 : offset %value_begin = func.call @skip_space(%response, %after_arguments_colon, %stop) : (buffer, offset, offset) -> (offset) %body_end = func.call @trim_end(%response, %value_begin, %stop) : (buffer, offset, offset) -> (offset) %last = index.sub %body_end, %c1 : offset %ends_object = func.call @byte_is(%response, %last, %stop, %brace) : (buffer, offset, offset, i32) -> (i1) %inner_end = scf.select %ends_object, %last, %body_end : offset %value_end = func.call @trim_end(%response, %value_begin, %inner_end) : (buffer, offset, offset) -> (offset) %has_value = index.cmp ult, %value_begin, %value_end : offset %arguments_ok_a = scalar.andi %has_arguments_colon, %has_value : i1 %arguments_ok = scalar.andi %arguments_ok_a, %ends_object : i1 %ok = scalar.andi %name_ok, %arguments_ok : i1 %written, %next_first = scf.if %ok -> (offset, i1) { %separated = scf.if %first -> (offset) { scf.yield %out : offset } else { buffer.store.i8 %comma, %output[%out] %after = index.add %out, %c1 : offset scf.yield %after : offset } %after_open = func.call @tool_append(%output, %separated, %record_open) : (buffer, offset, buffer) -> (offset) // The name is already JSON string content. %name_bytes = index.sub %name_end, %name_begin : offset buffer.copy %response[%name_begin], %output[%after_open], %name_bytes %after_name = index.add %after_open, %name_bytes : offset %after_middle = func.call @tool_append(%output, %after_name, %record_middle) : (buffer, offset, buffer) -> (offset) // A string-valued "arguments" is already escaped JSON text. %string_value = func.call @byte_is(%response, %value_begin, %value_end, %quote) : (buffer, offset, offset, i32) -> (i1) %after_value = scf.if %string_value -> (offset) { %inner_begin = index.add %value_begin, %c1 : offset %inner_stop = index.sub %value_end, %c1 : offset %inner_bytes = index.sub %inner_stop, %inner_begin : offset buffer.copy %response[%inner_begin], %output[%after_middle], %inner_bytes %moved = index.add %after_middle, %inner_bytes : offset scf.yield %moved : offset } else { %moved = func.call @json_escape(%response, %value_begin, %value_end, %output, %after_middle) : (buffer, offset, offset, buffer, offset) -> (offset) scf.yield %moved : offset } %after_record = func.call @tool_append(%output, %after_value, %record_close) : (buffer, offset, buffer) -> (offset) scf.yield %after_record, %false : offset, i1 } else { scf.yield %out, %first : offset, i1 } %next_position = index.add %stop, %close_length : offset scf.yield %next_position, %written, %next_first : offset, offset, i1 } buffer.store.i8 %close_bracket, %output[%final_out] %used = index.add %final_out, %c1 : offset %result = func.call @slice(%output, %origin, %used) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } // GLM spelling -> the host XML grammar: <tool_call>NAME<arg_key>K</arg_key> // <arg_value>V</arg_value></tool_call> becomes <tool_call>\n<function=NAME>\n // <parameter=K>\nV\n</parameter>\n</function>\n</tool_call>. func.def inline @starts_at(%text: buffer, %position: offset, %marker: buffer) -> (i1) { %c0 = index.constant 0 : offset %length = buffer.length %text %marker_length = buffer.length %marker %stop = index.add %position, %marker_length : offset %inside = index.cmp ule, %stop, %length : offset %result = scf.if %inside -> (i1) { %compared = buffer.compare %text[%position], %marker[%c0], %marker_length %zero = scalar.constant 0 : i32 %same = scalar.cmpi eq, %compared, %zero : i32 scf.yield %same : i1 } else { %false = scalar.constant false : i1 scf.yield %false : i1 } func.return %result : i1 } func.def @glm_calls_to_xml(%response: buffer) -> (buffer) { %origin = index.constant 0 : offset %c1 = index.constant 1 : offset %c64 = index.constant 64 : offset %false = scalar.constant false : i1 %true = scalar.constant true : i1 %length = buffer.length %response %twice = index.add %length, %length : offset %scaled = index.add %twice, %length : offset %capacity = index.add %scaled, %c64 : offset %output = buffer.alloca<private> align(1) %capacity : buffer %call_open = global.load @call_open : buffer %call_close = global.load @call_close : buffer %key_open = global.load @key_open : buffer %key_close = global.load @key_close : buffer %value_open = global.load @value_open : buffer %value_close = global.load @value_close : buffer %xml_open = global.load @xml_call_open : buffer %tag_end = global.load @xml_tag_end : buffer %xml_key = global.load @xml_parameter_open : buffer %xml_value_close = global.load @xml_parameter_close : buffer %xml_close = global.load @xml_call_close : buffer %end, %written, %naming = scf.while(%position = %origin : offset, %out = %origin : offset, %open = %false : i1) -> (offset, offset, i1) { %more = index.cmp ult, %position, %length : offset scf.condition %more, %position, %out, %open : i1, offset, offset, i1 } do(%position: offset, %out: offset, %open: i1) { %is_call_open = func.call @starts_at(%response, %position, %call_open) : (buffer, offset, buffer) -> (i1) %is_call_close = func.call @starts_at(%response, %position, %call_close) : (buffer, offset, buffer) -> (i1) %is_key_open = func.call @starts_at(%response, %position, %key_open) : (buffer, offset, buffer) -> (i1) %is_key_close = func.call @starts_at(%response, %position, %key_close) : (buffer, offset, buffer) -> (i1) %is_value_open = func.call @starts_at(%response, %position, %value_open) : (buffer, offset, buffer) -> (i1) %is_value_close = func.call @starts_at(%response, %position, %value_close) : (buffer, offset, buffer) -> (i1) // A function name is closed before its first parameter or the call end. %closes_name_a = scalar.ori %is_call_close, %is_key_open : i1 %closes_name = scalar.andi %closes_name_a, %open : i1 %named = scf.if %closes_name -> (offset) { %after = func.call @tool_append(%output, %out, %tag_end) : (buffer, offset, buffer) -> (offset) scf.yield %after : offset } else { scf.yield %out : offset } %p, %o, %n = scf.if %is_call_open -> (offset, offset, i1) { %after = func.call @tool_append(%output, %named, %xml_open) : (buffer, offset, buffer) -> (offset) %skip = buffer.length %call_open %next = index.add %position, %skip : offset scf.yield %next, %after, %true : offset, offset, i1 } else { %p1, %o1, %n1 = scf.if %is_call_close -> (offset, offset, i1) { %after = func.call @tool_append(%output, %named, %xml_close) : (buffer, offset, buffer) -> (offset) %skip = buffer.length %call_close %next = index.add %position, %skip : offset scf.yield %next, %after, %false : offset, offset, i1 } else { %p2, %o2, %n2 = scf.if %is_key_open -> (offset, offset, i1) { %after = func.call @tool_append(%output, %named, %xml_key) : (buffer, offset, buffer) -> (offset) %skip = buffer.length %key_open %next = index.add %position, %skip : offset scf.yield %next, %after, %false : offset, offset, i1 } else { %p3, %o3, %n3 = scf.if %is_key_close -> (offset, offset, i1) { %after = func.call @tool_append(%output, %out, %tag_end) : (buffer, offset, buffer) -> (offset) %skip = buffer.length %key_close %next = index.add %position, %skip : offset scf.yield %next, %after, %open : offset, offset, i1 } else { %p4, %o4, %n4 = scf.if %is_value_open -> (offset, offset, i1) { %skip = buffer.length %value_open %next = index.add %position, %skip : offset scf.yield %next, %out, %open : offset, offset, i1 } else { %p5, %o5, %n5 = scf.if %is_value_close -> (offset, offset, i1) { %after = func.call @tool_append(%output, %out, %xml_value_close) : (buffer, offset, buffer) -> (offset) %skip = buffer.length %value_close %next = index.add %position, %skip : offset scf.yield %next, %after, %open : offset, offset, i1 } else { // Ordinary byte: names drop whitespace, everything else copies. %space = func.call @text_is_space(%response, %position) : (buffer, offset) -> (i1) %drop = scalar.andi %space, %open : i1 %next = index.add %position, %c1 : offset %after = scf.if %drop -> (offset) { scf.yield %out : offset } else { buffer.copy %response[%position], %output[%out], %c1 %moved = index.add %out, %c1 : offset scf.yield %moved : offset } scf.yield %next, %after, %open : offset, offset, i1 } scf.yield %p5, %o5, %n5 : offset, offset, i1 } scf.yield %p4, %o4, %n4 : offset, offset, i1 } scf.yield %p3, %o3, %n3 : offset, offset, i1 } scf.yield %p2, %o2, %n2 : offset, offset, i1 } scf.yield %p1, %o1, %n1 : offset, offset, i1 } scf.yield %p, %o, %n : offset, offset, i1 } %result = func.call @slice(%output, %origin, %written) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } // ---- Pythonic calls (LFM2) --------------------------------------------------- // History: NAME(k=v, ...) with strings single-quoted (\\ \' \n \r escaped), lists and // objects as tojson (", " / ": " separators), true/false/null as True/False/None, // numbers as sent. Output: the list between CALLS_OPEN and CALL_END becomes // OpenAI records; string literals become JSON strings, True/False/None become // true/false/null, everything else (numbers, JSON lists/objects) is copied. // A call without its closing parenthesis (truncated or malformed) is dropped. global.rodata.def @py_open_paren = bytes("28") global.rodata.def @py_close_paren = bytes("29") global.rodata.def @py_equals = bytes("3d") global.rodata.def @py_comma_space = bytes("2c20") global.rodata.def @json_true = bytes("74727565") global.rodata.def @json_false = bytes("66616c7365") global.rodata.def @json_null = bytes("6e756c6c") global.rodata.def @py_true = bytes("54727565") global.rodata.def @py_false = bytes("46616c7365") global.rodata.def @py_none = bytes("4e6f6e65") // A JSON scalar's text as Python's str() prints it: true/false/null as True/False/None, anything else unchanged. func.def @py_literal(%raw: buffer) -> (buffer) { %json_true = global.load @json_true : buffer %json_false = global.load @json_false : buffer %json_null = global.load @json_null : buffer %py_true = global.load @py_true : buffer %py_false = global.load @py_false : buffer %py_none = global.load @py_none : buffer %is_true = func.call @equal(%raw, %json_true) : (buffer, buffer) -> (i1) %is_false = func.call @equal(%raw, %json_false) : (buffer, buffer) -> (i1) %is_null = func.call @equal(%raw, %json_null) : (buffer, buffer) -> (i1) %v1 = scf.select %is_true, %py_true, %raw : buffer %v2 = scf.select %is_false, %py_false, %v1 : buffer %v3 = scf.select %is_null, %py_none, %v2 : buffer func.return %v3 : buffer } // '": ' global.rodata.def @key_value_gap = bytes("223a20") func.def inline @name_byte(%b: i32) -> (i1) { %c48 = scalar.constant 48 : i32 %c57 = scalar.constant 57 : i32 %c65 = scalar.constant 65 : i32 %c90 = scalar.constant 90 : i32 %c97 = scalar.constant 97 : i32 %c122 = scalar.constant 122 : i32 %c95 = scalar.constant 95 : i32 %c46 = scalar.constant 46 : i32 %c45 = scalar.constant 45 : i32 %ge_digit = scalar.cmpi uge, %b, %c48 : i32 %le_digit = scalar.cmpi ule, %b, %c57 : i32 %digit = scalar.andi %ge_digit, %le_digit : i1 %ge_upper = scalar.cmpi uge, %b, %c65 : i32 %le_upper = scalar.cmpi ule, %b, %c90 : i32 %upper = scalar.andi %ge_upper, %le_upper : i1 %ge_lower = scalar.cmpi uge, %b, %c97 : i32 %le_lower = scalar.cmpi ule, %b, %c122 : i32 %lower = scalar.andi %ge_lower, %le_lower : i1 %under = scalar.cmpi eq, %b, %c95 : i32 %dot = scalar.cmpi eq, %b, %c46 : i32 %dash = scalar.cmpi eq, %b, %c45 : i32 %a = scalar.ori %digit, %upper : i1 %b1 = scalar.ori %a, %lower : i1 %b2 = scalar.ori %b1, %under : i1 %b3 = scalar.ori %b2, %dot : i1 %result = scalar.ori %b3, %dash : i1 func.return %result : i1 } // Python repr-style single-quoted literal, as the template's format_arg_value prints it. func.def @py_quote(%text: buffer) -> (buffer) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %c2 = index.constant 2 : offset %quote = scalar.constant 39 : i32 %backslash = scalar.constant 92 : i32 %newline = scalar.constant 10 : i32 %return = scalar.constant 13 : i32 %letter_n = scalar.constant 110 : i32 %letter_r = scalar.constant 114 : i32 %length = buffer.length %text %twice = index.add %length, %length : offset %capacity = index.add %twice, %c2 : offset %output = buffer.alloca<private> align(1) %capacity : buffer buffer.store.i8 %quote, %output[%c0] %final_in, %final_out = scf.while(%in = %c0 : offset, %out = %c1 : offset) -> (offset, offset) { %more = index.cmp ult, %in, %length : offset scf.condition %more, %in, %out : i1, offset, offset } do(%in: offset, %out: offset) { %byte = buffer.load.i8.u %text[%in] %is_quote = scalar.cmpi eq, %byte, %quote : i32 %is_backslash = scalar.cmpi eq, %byte, %backslash : i32 %is_newline = scalar.cmpi eq, %byte, %newline : i32 %is_return = scalar.cmpi eq, %byte, %return : i32 %literal = scalar.ori %is_quote, %is_backslash : i1 %named = scalar.ori %is_newline, %is_return : i1 %escaped = scalar.ori %literal, %named : i1 %letter = scf.select %is_newline, %letter_n, %letter_r : i32 %second = scf.select %literal, %byte, %letter : i32 %written = scf.if %escaped -> (offset) { buffer.store.i8 %backslash, %output[%out] %next_out = index.add %out, %c1 : offset buffer.store.i8 %second, %output[%next_out] %after = index.add %out, %c2 : offset scf.yield %after : offset } else { buffer.store.i8 %byte, %output[%out] %after = index.add %out, %c1 : offset scf.yield %after : offset } %next_in = index.add %in, %c1 : offset scf.yield %next_in, %written : offset, offset } buffer.store.i8 %quote, %output[%final_out] %used = index.add %final_out, %c1 : offset %result = func.call @slice(%output, %c0, %used) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } // Compact JSON -> json.dumps spacing (", " and ": " outside strings), as tojson prints it. func.def @json_spaced(%text: buffer) -> (buffer) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %c2 = index.constant 2 : offset %quote = scalar.constant 34 : i32 %backslash = scalar.constant 92 : i32 %comma = scalar.constant 44 : i32 %colon = scalar.constant 58 : i32 %space = scalar.constant 32 : i32 %false = scalar.constant false : i1 %length = buffer.length %text %twice = index.add %length, %length : offset %capacity = index.add %twice, %c2 : offset %output = buffer.alloca<private> align(1) %capacity : buffer %final_in, %final_out, %final_inside, %final_escape = scf.while(%in = %c0 : offset, %out = %c0 : offset, %inside = %false : i1, %escape = %false : i1) -> (offset, offset, i1, i1) { %more = index.cmp ult, %in, %length : offset scf.condition %more, %in, %out, %inside, %escape : i1, offset, offset, i1, i1 } do(%in: offset, %out: offset, %inside: i1, %escape: i1) { %byte = buffer.load.i8.u %text[%in] %is_quote = scalar.cmpi eq, %byte, %quote : i32 %is_backslash = scalar.cmpi eq, %byte, %backslash : i32 %is_comma = scalar.cmpi eq, %byte, %comma : i32 %is_colon = scalar.cmpi eq, %byte, %colon : i32 %outside = scalar.cmpi eq, %inside, %false : i1 %plain = scalar.cmpi eq, %escape, %false : i1 %live = scalar.andi %inside, %plain : i1 %starts_escape = scalar.andi %live, %is_backslash : i1 %closes = scalar.andi %live, %is_quote : i1 %opens = scalar.andi %outside, %is_quote : i1 %stays = scalar.cmpi eq, %closes, %false : i1 %kept = scalar.andi %inside, %stays : i1 %next_inside = scalar.ori %kept, %opens : i1 %separator = scalar.ori %is_comma, %is_colon : i1 %spaced = scalar.andi %outside, %separator : i1 buffer.store.i8 %byte, %output[%out] %after = index.add %out, %c1 : offset %written = scf.if %spaced -> (offset) { buffer.store.i8 %space, %output[%after] %moved = index.add %after, %c1 : offset scf.yield %moved : offset } else { scf.yield %after : offset } %next_in = index.add %in, %c1 : offset scf.yield %next_in, %written, %next_inside, %starts_escape : offset, offset, i1, i1 } %result = func.call @slice(%output, %c0, %final_out) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } // Copies the Python string literal whose opening quote is at %begin into // %output at %out as a JSON string. Returns (position after it, output end). func.def @py_string(%text: buffer, %begin: offset, %end: offset, %output: buffer, %out: offset) -> (offset, offset) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %c2 = index.constant 2 : offset %zero = scalar.constant 0 : i32 %dq = scalar.constant 34 : i32 %sq = scalar.constant 39 : i32 %bs = scalar.constant 92 : i32 %nl = scalar.constant 10 : i32 %cr = scalar.constant 13 : i32 %tab = scalar.constant 9 : i32 %space = scalar.constant 32 : i32 %letter_n = scalar.constant 110 : i32 %letter_r = scalar.constant 114 : i32 %letter_t = scalar.constant 116 : i32 %letter_b = scalar.constant 98 : i32 %letter_f = scalar.constant 102 : i32 %letter_u = scalar.constant 117 : i32 %slash = scalar.constant 47 : i32 %false = scalar.constant false : i1 %quote = buffer.load.i8.u %text[%begin] buffer.store.i8 %dq, %output[%out] %start_out = index.add %out, %c1 : offset %start_in = index.add %begin, %c1 : offset %final_in, %final_out = scf.while(%in = %start_in : offset, %o = %start_out : offset) -> (offset, offset) { %more = index.cmp ult, %in, %end : offset %open = scf.if %more -> (i1) { %byte = buffer.load.i8.u %text[%in] %closing = scalar.cmpi eq, %byte, %quote : i32 %not_closing = scalar.cmpi eq, %closing, %false : i1 scf.yield %not_closing : i1 } else { scf.yield %false : i1 } scf.condition %open, %in, %o : i1, offset, offset } do(%in: offset, %o: offset) { %byte = buffer.load.i8.u %text[%in] %after = index.add %in, %c1 : offset %has_next = index.cmp ult, %after, %end : offset %n = scf.if %has_next -> (i32) { %value = buffer.load.i8.u %text[%after] scf.yield %value : i32 } else { scf.yield %zero : i32 } %is_bs = scalar.cmpi eq, %byte, %bs : i32 %bs_next = scalar.andi %is_bs, %has_next : i1 %n_dq = scalar.cmpi eq, %n, %dq : i32 %n_bs = scalar.cmpi eq, %n, %bs : i32 %n_n = scalar.cmpi eq, %n, %letter_n : i32 %n_r = scalar.cmpi eq, %n, %letter_r : i32 %n_t = scalar.cmpi eq, %n, %letter_t : i32 %n_b = scalar.cmpi eq, %n, %letter_b : i32 %n_f = scalar.cmpi eq, %n, %letter_f : i32 %n_u = scalar.cmpi eq, %n, %letter_u : i32 %n_slash = scalar.cmpi eq, %n, %slash : i32 %n_sq = scalar.cmpi eq, %n, %sq : i32 %k1 = scalar.ori %n_dq, %n_bs : i1 %k2 = scalar.ori %k1, %n_n : i1 %k3 = scalar.ori %k2, %n_r : i1 %k4 = scalar.ori %k3, %n_t : i1 %k5 = scalar.ori %k4, %n_b : i1 %k6 = scalar.ori %k5, %n_f : i1 %k7 = scalar.ori %k6, %n_u : i1 %k8 = scalar.ori %k7, %n_slash : i1 // \" \\ \n \r \t \b \f \u \/ are JSON escapes too; \' is a plain quote; // any other backslash is literal (Python keeps unknown escapes). %known = scalar.andi %bs_next, %k8 : i1 %single_quote = scalar.andi %bs_next, %n_sq : i1 %special = scalar.ori %known, %single_quote : i1 %not_special = scalar.cmpi eq, %special, %false : i1 %lone_bs = scalar.andi %is_bs, %not_special : i1 %is_dq = scalar.cmpi eq, %byte, %dq : i32 %is_nl = scalar.cmpi eq, %byte, %nl : i32 %is_cr = scalar.cmpi eq, %byte, %cr : i32 %is_tab = scalar.cmpi eq, %byte, %tab : i32 %is_control = scalar.cmpi ult, %byte, %space : i32 %e1 = scalar.ori %known, %lone_bs : i1 %e2 = scalar.ori %e1, %is_dq : i1 %e3 = scalar.ori %e2, %is_nl : i1 %e4 = scalar.ori %e3, %is_cr : i1 %escaped = scalar.ori %e4, %is_tab : i1 %x_rt = scf.select %is_cr, %letter_r, %letter_t : i32 %x_ws = scf.select %is_nl, %letter_n, %x_rt : i32 %x_dq = scf.select %is_dq, %dq, %x_ws : i32 %x_bs = scf.select %lone_bs, %bs, %x_dq : i32 %x = scf.select %known, %n, %x_bs : i32 %y_plain = scf.select %is_control, %space, %byte : i32 %y = scf.select %single_quote, %sq, %y_plain : i32 %consumed = scf.select %special, %c2, %c1 : offset %written = scf.if %escaped -> (offset) { buffer.store.i8 %bs, %output[%o] %o1 = index.add %o, %c1 : offset buffer.store.i8 %x, %output[%o1] %o2 = index.add %o, %c2 : offset scf.yield %o2 : offset } else { buffer.store.i8 %y, %output[%o] %o1 = index.add %o, %c1 : offset scf.yield %o1 : offset } %next_in = index.add %in, %consumed : offset scf.yield %next_in, %written : offset, offset } %closed = index.cmp ult, %final_in, %end : offset %skip = scf.select %closed, %c1, %c0 : offset %after_quote = index.add %final_in, %skip : offset buffer.store.i8 %dq, %output[%final_out] %used = index.add %final_out, %c1 : offset func.return %after_quote, %used : offset, offset } // Copies one Python argument value at %begin into %output as JSON, up to a // ',' / ')' / ']' outside brackets. Returns (position, output end). func.def @py_value(%text: buffer, %begin: offset, %end: offset, %output: buffer, %out: offset) -> (offset, offset) { %c1 = index.constant 1 : offset %zero64 = scalar.constant 0 : i64 %one64 = scalar.constant 1 : i64 %false = scalar.constant false : i1 %comma = scalar.constant 44 : i32 %rparen = scalar.constant 41 : i32 %lbracket = scalar.constant 91 : i32 %rbracket = scalar.constant 93 : i32 %lbrace = scalar.constant 123 : i32 %rbrace = scalar.constant 125 : i32 %dq = scalar.constant 34 : i32 %sq = scalar.constant 39 : i32 %py_true = global.load @py_true : buffer %py_false = global.load @py_false : buffer %py_none = global.load @py_none : buffer %json_true = global.load @json_true : buffer %json_false = global.load @json_false : buffer %json_null = global.load @json_null : buffer %true_length = buffer.length %py_true %false_length = buffer.length %py_false %final_p, %final_o, %final_d = scf.while(%p = %begin : offset, %o = %out : offset, %d = %zero64 : i64) -> (offset, offset, i64) { %more = index.cmp ult, %p, %end : offset %go = scf.if %more -> (i1) { %byte = buffer.load.i8.u %text[%p] %top = scalar.cmpi eq, %d, %zero64 : i64 %t1 = scalar.cmpi eq, %byte, %comma : i32 %t2 = scalar.cmpi eq, %byte, %rparen : i32 %t3 = scalar.cmpi eq, %byte, %rbracket : i32 %ta = scalar.ori %t1, %t2 : i1 %term = scalar.ori %ta, %t3 : i1 %stop = scalar.andi %top, %term : i1 %continue = scalar.cmpi eq, %stop, %false : i1 scf.yield %continue : i1 } else { scf.yield %false : i1 } scf.condition %go, %p, %o, %d : i1, offset, offset, i64 } do(%p: offset, %o: offset, %d: i64) { %byte = buffer.load.i8.u %text[%p] %is_dq = scalar.cmpi eq, %byte, %dq : i32 %is_sq = scalar.cmpi eq, %byte, %sq : i32 %is_quote = scalar.ori %is_dq, %is_sq : i1 %is_true = func.call @starts_at(%text, %p, %py_true) : (buffer, offset, buffer) -> (i1) %is_false = func.call @starts_at(%text, %p, %py_false) : (buffer, offset, buffer) -> (i1) %is_none = func.call @starts_at(%text, %p, %py_none) : (buffer, offset, buffer) -> (i1) %is_lbracket = scalar.cmpi eq, %byte, %lbracket : i32 %is_lbrace = scalar.cmpi eq, %byte, %lbrace : i32 %is_rbracket = scalar.cmpi eq, %byte, %rbracket : i32 %is_rbrace = scalar.cmpi eq, %byte, %rbrace : i32 %opens = scalar.ori %is_lbracket, %is_lbrace : i1 %closes = scalar.ori %is_rbracket, %is_rbrace : i1 %top = scalar.cmpi eq, %d, %zero64 : i64 %space = func.call @text_is_space(%text, %p) : (buffer, offset) -> (i1) %drop = scalar.andi %top, %space : i1 %np, %no, %nd = scf.if %is_quote -> (offset, offset, i64) { %sp, %so = func.call @py_string(%text, %p, %end, %output, %o) : (buffer, offset, offset, buffer, offset) -> (offset, offset) scf.yield %sp, %so, %d : offset, offset, i64 } else { %p1, %o1, %d1 = scf.if %is_true -> (offset, offset, i64) { %wo = func.call @tool_append(%output, %o, %json_true) : (buffer, offset, buffer) -> (offset) %wp = index.add %p, %true_length : offset scf.yield %wp, %wo, %d : offset, offset, i64 } else { %p2, %o2, %d2 = scf.if %is_false -> (offset, offset, i64) { %wo = func.call @tool_append(%output, %o, %json_false) : (buffer, offset, buffer) -> (offset) %wp = index.add %p, %false_length : offset scf.yield %wp, %wo, %d : offset, offset, i64 } else { %p3, %o3, %d3 = scf.if %is_none -> (offset, offset, i64) { %wo = func.call @tool_append(%output, %o, %json_null) : (buffer, offset, buffer) -> (offset) %wp = index.add %p, %true_length : offset scf.yield %wp, %wo, %d : offset, offset, i64 } else { %wp = index.add %p, %c1 : offset %p4, %o4, %d4 = scf.if %drop -> (offset, offset, i64) { scf.yield %wp, %o, %d : offset, offset, i64 } else { buffer.store.i8 %byte, %output[%o] %wo = index.add %o, %c1 : offset %deeper = scalar.addi %d, %one64 : i64 %shallower = scalar.subi %d, %one64 : i64 %da = scf.select %opens, %deeper, %d : i64 %db = scf.select %closes, %shallower, %da : i64 scf.yield %wp, %wo, %db : offset, offset, i64 } scf.yield %p4, %o4, %d4 : offset, offset, i64 } scf.yield %p3, %o3, %d3 : offset, offset, i64 } scf.yield %p2, %o2, %d2 : offset, offset, i64 } scf.yield %p1, %o1, %d1 : offset, offset, i64 } scf.yield %np, %no, %nd : offset, offset, i64 } func.return %final_p, %final_o : offset, offset } // One call NAME(k=v, ...) at %begin; its arguments object is written to %args. // Returns (end, complete, name end, arguments length). func.def @py_call(%text: buffer, %begin: offset, %end: offset, %args: buffer) -> (offset, i1, offset, offset) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %false = scalar.constant false : i1 %true = scalar.constant true : i1 %lparen = scalar.constant 40 : i32 %rparen = scalar.constant 41 : i32 %comma = scalar.constant 44 : i32 %equals = scalar.constant 61 : i32 %lbrace = scalar.constant 123 : i32 %rbrace = scalar.constant 125 : i32 %dq = scalar.constant 34 : i32 %empty = global.load @empty : buffer %comma_space = global.load @py_comma_space : buffer %gap = global.load @key_value_gap : buffer %name_end = scf.while(%p = %begin : offset) -> (offset) { %more = index.cmp ult, %p, %end : offset %go = scf.if %more -> (i1) { %byte = buffer.load.i8.u %text[%p] %ok = func.call @name_byte(%byte) : (i32) -> (i1) scf.yield %ok : i1 } else { scf.yield %false : i1 } scf.condition %go, %p : i1, offset } do(%p: offset) { %next = index.add %p, %c1 : offset scf.yield %next : offset } %has_paren = func.call @byte_is(%text, %name_end, %end, %lparen) : (buffer, offset, offset, i32) -> (i1) %r_end, %r_ok, %r_length = scf.if %has_paren -> (offset, i1, offset) { buffer.store.i8 %lbrace, %args[%c0] %start = index.add %name_end, %c1 : offset %fp, %fa, %ffirst, %fdone, %fok = scf.while(%p = %start : offset, %a = %c1 : offset, %first = %true : i1, %done = %false : i1, %ok = %false : i1) -> (offset, offset, i1, i1, i1) { %more = index.cmp ult, %p, %end : offset %open = scalar.cmpi eq, %done, %false : i1 %go = scalar.andi %more, %open : i1 scf.condition %go, %p, %a, %first, %done, %ok : i1, offset, offset, i1, i1, i1 } do(%p: offset, %a: offset, %first: i1, %done: i1, %ok: i1) { %q = func.call @skip_space(%text, %p, %end) : (buffer, offset, offset) -> (offset) %at_close = func.call @byte_is(%text, %q, %end, %rparen) : (buffer, offset, offset, i32) -> (i1) %at_comma = func.call @byte_is(%text, %q, %end, %comma) : (buffer, offset, offset, i32) -> (i1) %after_q = index.add %q, %c1 : offset %np, %na, %nfirst, %ndone, %nok = scf.if %at_close -> (offset, offset, i1, i1, i1) { scf.yield %after_q, %a, %first, %true, %true : offset, offset, i1, i1, i1 } else { %p1, %a1, %f1, %d1, %k1 = scf.if %at_comma -> (offset, offset, i1, i1, i1) { scf.yield %after_q, %a, %first, %false, %false : offset, offset, i1, i1, i1 } else { %key_stop = scf.while(%k = %q : offset) -> (offset) { %kmore = index.cmp ult, %k, %end : offset %kgo = scf.if %kmore -> (i1) { %byte = buffer.load.i8.u %text[%k] %s1 = scalar.cmpi eq, %byte, %equals : i32 %s2 = scalar.cmpi eq, %byte, %comma : i32 %s3 = scalar.cmpi eq, %byte, %rparen : i32 %sa = scalar.ori %s1, %s2 : i1 %stop = scalar.ori %sa, %s3 : i1 %continue = scalar.cmpi eq, %stop, %false : i1 scf.yield %continue : i1 } else { scf.yield %false : i1 } scf.condition %kgo, %k : i1, offset } do(%k: offset) { %knext = index.add %k, %c1 : offset scf.yield %knext : offset } %has_equals = func.call @byte_is(%text, %key_stop, %end, %equals) : (buffer, offset, offset, i32) -> (i1) %p2, %a2, %f2, %d2, %k2 = scf.if %has_equals -> (offset, offset, i1, i1, i1) { %key_end = func.call @trim_end(%text, %q, %key_stop) : (buffer, offset, offset) -> (offset) %lead = scf.select %first, %empty, %comma_space : buffer %w1 = func.call @tool_append(%args, %a, %lead) : (buffer, offset, buffer) -> (offset) buffer.store.i8 %dq, %args[%w1] %w2 = index.add %w1, %c1 : offset %w3 = func.call @json_escape(%text, %q, %key_end, %args, %w2) : (buffer, offset, offset, buffer, offset) -> (offset) %w4 = func.call @tool_append(%args, %w3, %gap) : (buffer, offset, buffer) -> (offset) %after_equals = index.add %key_stop, %c1 : offset %value_start = func.call @skip_space(%text, %after_equals, %end) : (buffer, offset, offset) -> (offset) %vp, %va = func.call @py_value(%text, %value_start, %end, %args, %w4) : (buffer, offset, offset, buffer, offset) -> (offset, offset) %no_value = index.cmp eq, %va, %w4 : offset scf.yield %vp, %va, %false, %no_value, %false : offset, offset, i1, i1, i1 } else { scf.yield %key_stop, %a, %first, %true, %false : offset, offset, i1, i1, i1 } scf.yield %p2, %a2, %f2, %d2, %k2 : offset, offset, i1, i1, i1 } scf.yield %p1, %a1, %f1, %d1, %k1 : offset, offset, i1, i1, i1 } scf.yield %np, %na, %nfirst, %ndone, %nok : offset, offset, i1, i1, i1 } buffer.store.i8 %rbrace, %args[%fa] %length = index.add %fa, %c1 : offset scf.yield %fp, %fok, %length : offset, i1, offset } else { scf.yield %name_end, %false, %c0 : offset, i1, offset } func.return %r_end, %r_ok, %name_end, %r_length : offset, i1, offset, offset } func.def @pythonic_calls(%response: buffer) -> (buffer) { %origin = index.constant 0 : offset %c1 = index.constant 1 : offset %c16 = index.constant 16 : offset %c64 = index.constant 64 : offset %false = scalar.constant false : i1 %true = scalar.constant true : i1 %comma = scalar.constant 44 : i32 %open_bracket = scalar.constant 91 : i32 %close_bracket = scalar.constant 93 : i32 %length = buffer.length %response %l2 = index.add %length, %length : offset %l4 = index.add %l2, %l2 : offset %l8 = index.add %l4, %l4 : offset %l16 = index.add %l8, %l8 : offset %l32 = index.add %l16, %l16 : offset %capacity = index.add %l32, %c64 : offset %args_capacity = index.add %l4, %c16 : offset %output = buffer.alloca<private> align(1) %capacity : buffer %args = buffer.alloca<private> align(1) %args_capacity : buffer buffer.store.i8 %open_bracket, %output[%origin] %marker = global.load @text_tool_marker : buffer %list_end = global.load @call_end : buffer %marker_length = buffer.length %marker %list_end_length = buffer.length %list_end %record_open = global.load @record_open : buffer %record_middle = global.load @record_middle : buffer %record_close = global.load @record_close : buffer %final_p, %final_o, %final_first, %final_inside = scf.while(%p = %origin : offset, %o = %c1 : offset, %first = %true : i1, %inside = %false : i1) -> (offset, offset, i1, i1) { %more = index.cmp ult, %p, %length : offset scf.condition %more, %p, %o, %first, %inside : i1, offset, offset, i1, i1 } do(%p: offset, %o: offset, %first: i1, %inside: i1) { %at_open = func.call @starts_at(%response, %p, %marker) : (buffer, offset, buffer) -> (i1) %at_close = func.call @starts_at(%response, %p, %list_end) : (buffer, offset, buffer) -> (i1) %byte = buffer.load.i8.u %response[%p] %name = func.call @name_byte(%byte) : (i32) -> (i1) %begins = scalar.andi %inside, %name : i1 %np, %no, %nfirst, %ninside = scf.if %at_open -> (offset, offset, i1, i1) { %skip = index.add %p, %marker_length : offset scf.yield %skip, %o, %first, %true : offset, offset, i1, i1 } else { %p1, %o1, %f1, %in1 = scf.if %at_close -> (offset, offset, i1, i1) { %skip = index.add %p, %list_end_length : offset scf.yield %skip, %o, %first, %false : offset, offset, i1, i1 } else { %p2, %o2, %f2, %in2 = scf.if %begins -> (offset, offset, i1, i1) { %call_end, %complete, %name_end, %args_length = func.call @py_call(%response, %p, %length, %args) : (buffer, offset, offset, buffer) -> (offset, i1, offset, offset) %written, %next_first = scf.if %complete -> (offset, i1) { %separated = scf.if %first -> (offset) { scf.yield %o : offset } else { buffer.store.i8 %comma, %output[%o] %after = index.add %o, %c1 : offset scf.yield %after : offset } %after_open = func.call @tool_append(%output, %separated, %record_open) : (buffer, offset, buffer) -> (offset) %after_name = func.call @json_escape(%response, %p, %name_end, %output, %after_open) : (buffer, offset, offset, buffer, offset) -> (offset) %after_middle = func.call @tool_append(%output, %after_name, %record_middle) : (buffer, offset, buffer) -> (offset) %after_args = func.call @json_escape(%args, %origin, %args_length, %output, %after_middle) : (buffer, offset, offset, buffer, offset) -> (offset) %after_record = func.call @tool_append(%output, %after_args, %record_close) : (buffer, offset, buffer) -> (offset) scf.yield %after_record, %false : offset, i1 } else { scf.yield %o, %first : offset, i1 } scf.yield %call_end, %written, %next_first, %inside : offset, offset, i1, i1 } else { %step = index.add %p, %c1 : offset scf.yield %step, %o, %first, %inside : offset, offset, i1, i1 } scf.yield %p2, %o2, %f2, %in2 : offset, offset, i1, i1 } scf.yield %p1, %o1, %f1, %in1 : offset, offset, i1, i1 } scf.yield %np, %no, %nfirst, %ninside : offset, offset, i1, i1 } buffer.store.i8 %close_bracket, %output[%final_o] %used = index.add %final_o, %c1 : offset %result = func.call @slice(%output, %origin, %used) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } // The host supplies the reserved suffix that starts at the first call marker. // %truncated is 1 when generation hit its token limit without EOS. func.def @generic_parse_tools(%schemas: buffer, %response: buffer, %truncated: i64) -> (buffer) { %c0 = index.constant 0 : offset %zero64 = scalar.constant 0 : i64 %length = buffer.length %response %has_text = index.cmp ne, %length, %c0 : offset %schema_fields, %schema_type, %schema_count = func.call @members(%schemas) : (buffer) -> (buffer, i32, i64) %has_tools = scalar.cmpi ne, %schema_count, %zero64 : i64 %parse = scalar.andi %has_text, %has_tools : i1 %calls = scf.if %parse -> (buffer) { %format = scalar.constant 1 : i32 %hermes = scalar.constant 1 : i32 %glm = scalar.constant 3 : i32 %is_hermes = scalar.cmpi eq, %format, %hermes : i32 %is_glm = scalar.cmpi eq, %format, %glm : i32 %parsed = scf.if %is_hermes -> (buffer) { %records = func.call @hermes_calls(%response) : (buffer) -> (buffer) scf.yield %records : buffer } else { %pythonic = scalar.constant 4 : i32 %is_pythonic = scalar.cmpi eq, %format, %pythonic : i32 %other = scf.if %is_pythonic -> (buffer) { %py_records = func.call @pythonic_calls(%response) : (buffer) -> (buffer) scf.yield %py_records : buffer } else { %complete = func.call @through_last_call(%response) : (buffer) -> (buffer) %xml = scf.if %is_glm -> (buffer) { %converted = func.call @glm_calls_to_xml(%complete) : (buffer) -> (buffer) scf.yield %converted : buffer } else { scf.yield %complete : buffer } %records = func.call @parse_xml(%schemas, %xml, %truncated) : (buffer, buffer, i64) -> (buffer) scf.yield %records : buffer } scf.yield %other : buffer } scf.yield %parsed : buffer } else { %none = global.load @no_tools : buffer scf.yield %none : buffer } func.return %calls : buffer } // The host supplies the previous raw prediction separately. On EOS it replaces // the first assistant delimiter in this suffix; an open response consumes its // last ordinary token before that delimiter. Full replay encodes both normally. // Encodes the rendered conversation with special-token matching enabled. func.def public @prepare_input(%text: buffer, %markers: buffer, %marker_count: i64, %format: i32, %boundary: i32, %capacity: i64) -> (buffer, i64, buffer) { %c0 = index.constant 0 : offset %c0_i32 = scalar.constant 0 : i32 %c0_i64 = scalar.constant 0 : i64 %c1_i64 = scalar.constant 1 : i64 %no_markers = scalar.cmpi eq, %marker_count, %c0_i64 : i64 func.call @check(%no_markers) : (i1) %probe = scalar.addi %capacity, %c1_i64 : i64 %tokens, %count = func.call @encode(%text, %c0_i32, %probe) : (buffer, i32, i64) -> (buffer, i64) %fits = scalar.cmpi ule, %count, %capacity : i64 %fits32 = scalar.extui %fits : i1 to i32 %error = global.load @overflow : buffer func.call @require(%fits32, %error) : (i32, buffer) %spans = buffer.alloca<private> align(4) %c0 : buffer func.return %tokens, %count, %spans : buffer, i64, buffer } global.rodata.def @text_think_end = bytes("3c7c656e645f6d6573736167657c3e") global.rodata.def @text_tool_marker = bytes("3c7c636f6e74656e745f696e766f6b655f746f6f6c5f6a736f6e7c3e") func.def @text_until(%response: buffer, %marker: buffer, %previous: i64, %phase: i32) -> (i64) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %zero32 = scalar.constant 0 : i32 %false = scalar.constant false : i1 %length = buffer.length %response %marker_length = buffer.length %marker // The owner supplies the last extent returned for this growing response. %previous_value = scalar.assume %previous [range(%previous, 0, 9223372036854775807)] : i64 %start = index.cast %previous_value : i64 to offset %streaming = scalar.cmpi eq, %phase, %zero32 : i32 %end = scf.while(%position = %start : offset) -> (offset) { %has_bytes = index.cmp ult, %position, %length : offset %advance = scf.if %has_bytes -> (i1) { %remaining = index.sub %length, %position : offset %short = index.cmp ult, %remaining, %marker_length : offset %count = scf.select %short, %remaining, %marker_length : offset %comparison = buffer.compare %response[%position], %marker[%c0], %count %mismatch = scalar.cmpi ne, %comparison, %zero32 : i32 %complete_marker = index.cmp uge, %remaining, %marker_length : offset %withhold = scalar.ori %streaming, %complete_marker : i1 %release_fragment = scalar.cmpi eq, %withhold, %false : i1 %advance = scalar.ori %mismatch, %release_fragment : i1 scf.yield %advance : i1 } else { scf.yield %false : i1 } scf.condition %advance, %position : i1, offset } do(%position: offset) { %next = index.add %position, %c1 : offset scf.yield %next : offset } %result = index.cast %end : offset to i64 func.return %result : i64 } // State 1 is an open reasoning block; state 2 consumes its trailing whitespace. // Content (state 0) stops at the call marker (without the separator, @content_until): // the rest goes to parse_tools. // (Adapted from hrx-loom-library program/text/qwen3_8/chat.loom.) func.def @generic_output_next(%response: buffer, %previous: i64, %state: i64, %phase: i32) -> (i64, i64, i64, i64) { %c0_i64 = scalar.constant 0 : i64 %c1_i64 = scalar.constant 1 : i64 %c2_i64 = scalar.constant 2 : i64 %origin = index.constant 0 : offset %step = index.constant 1 : offset %thinking = scalar.cmpi eq, %state, %c1_i64 : i64 %spacing = scalar.cmpi eq, %state, %c2_i64 : i64 %kind, %begin, %end, %next = scf.if %spacing -> (i64, i64, i64, i64) { %start_value = scalar.assume %previous [range(%previous, 0, 9223372036854775807)] : i64 %start = index.cast %start_value : i64 to offset %length = buffer.length %response %false = scalar.constant false : i1 %stop = scf.while(%position = %start : offset) -> (offset) { %available = index.cmp ult, %position, %length : offset %space = scf.if %available -> (i1) { %value = func.call @text_is_space(%response, %position) : (buffer, offset) -> (i1) scf.yield %value : i1 } else { scf.yield %false : i1 } scf.condition %space, %position : i1, offset } do(%position: offset) { %following = index.add %position, %step : offset scf.yield %following : offset } %start_text = index.cast %stop : offset to i64 %end, %skip = func.call @content_until(%response, %start_text, %phase) : (buffer, i64, i32) -> (i64, i64) %has_text = scalar.cmpi ne, %end, %start_text : i64 %next = scf.select %has_text, %c0_i64, %state : i64 scf.yield %c1_i64, %start_text, %end, %next : i64, i64, i64, i64 } else { %kind, %end, %next = scf.if %thinking -> (i64, i64, i64) { %marker = global.load @text_think_end : buffer %stop = func.call @text_until(%response, %marker, %previous, %phase) : (buffer, buffer, i64, i32) -> (i64) %at_marker = scalar.cmpi eq, %stop, %previous : i64 %length_offset = buffer.length %response %length = index.cast %length_offset : offset to i64 %remaining = scalar.subi %length, %stop : i64 %marker_length_offset = buffer.length %marker %marker_length = index.cast %marker_length_offset : offset to i64 %complete = scalar.cmpi uge, %remaining, %marker_length : i64 %consume = scalar.andi %at_marker, %complete : i1 %after = scalar.addi %stop, %marker_length : i64 %reasoning_end = scf.select %consume, %after, %stop : i64 %reasoning_next = scf.select %consume, %c2_i64, %state : i64 %reasoning_kind = scf.select %consume, %c0_i64, %c2_i64 : i64 scf.yield %reasoning_kind, %reasoning_end, %reasoning_next : i64, i64, i64 } else { // The separator before a call is consumed without output (kind 0) once only it is left. %emit, %skip = func.call @content_until(%response, %previous, %phase) : (buffer, i64, i32) -> (i64, i64) %held = scalar.cmpi eq, %emit, %previous : i64 %skips = scalar.cmpi ne, %skip, %emit : i64 %consume = scalar.andi %held, %skips : i1 %content_kind = scf.select %consume, %c0_i64, %c1_i64 : i64 %content_end = scf.select %consume, %skip, %emit : i64 scf.yield %content_kind, %content_end, %state : i64, i64, i64 } scf.yield %kind, %previous, %end, %next : i64, i64, i64, i64 } func.return %kind, %begin, %end, %next : i64, i64, i64, i64 } // Content from %start: (end of the bytes to emit, end of the bytes to consume). Content stops at the // call marker without the content/call separator the template prints between them (it renders // content + separator + calls, so the separator is call framing: keeping it in the content doubled it // in the next turn's rendering, and canonical history never matched the output). While streaming, // trailing whitespace is withheld until it is known not to precede a call. No separator: unchanged. func.def @content_until(%response: buffer, %start: i64, %phase: i32) -> (i64, i64) { %c0 = index.constant 0 : offset %c0_i64 = scalar.constant 0 : i64 %zero32 = scalar.constant 0 : i32 %false = scalar.constant false : i1 %marker = global.load @text_tool_marker : buffer %stop = func.call @text_until(%response, %marker, %start, %phase) : (buffer, buffer, i64, i32) -> (i64) %separator = global.load @content_call_separator : buffer %separator_length_offset = buffer.length %separator %separator_length = index.cast %separator_length_offset : offset to i64 %has_separator = scalar.cmpi ne, %separator_length, %c0_i64 : i64 %length_offset = buffer.length %response %length = index.cast %length_offset : offset to i64 %remaining = scalar.subi %length, %stop : i64 %marker_length_offset = buffer.length %marker %marker_length = index.cast %marker_length_offset : offset to i64 %complete = scalar.cmpi uge, %remaining, %marker_length : i64 %inside = scalar.cmpi ult, %stop, %length : i64 %found = scalar.andi %complete, %inside : i1 %start_value = scalar.assume %start [range(%start, 0, 9223372036854775807)] : i64 %start_offset = index.cast %start_value : i64 to offset %stop_value = scalar.assume %stop [range(%stop, 0, 9223372036854775807)] : i64 %stop_offset = index.cast %stop_value : i64 to offset %run_offset = func.call @trim_end(%response, %start_offset, %stop_offset) : (buffer, offset, offset) -> (offset) %run = index.cast %run_offset : offset to i64 %run_length = scalar.subi %stop, %run : i64 %fits = scalar.cmpi uge, %run_length, %separator_length : i64 %framed = scalar.andi %has_separator, %found : i1 %candidate = scalar.andi %framed, %fits : i1 %separator_begin = scalar.subi %stop, %separator_length : i64 %matches = scf.if %candidate -> (i1) { %begin_value = scalar.assume %separator_begin [range(%separator_begin, 0, 9223372036854775807)] : i64 %begin_offset = index.cast %begin_value : i64 to offset %order = buffer.compare %response[%begin_offset], %separator[%c0], %separator_length_offset %equal = scalar.cmpi eq, %order, %zero32 : i32 scf.yield %equal : i1 } else { scf.yield %false : i1 } %streaming = scalar.cmpi eq, %phase, %zero32 : i32 %unknown = scalar.cmpi eq, %found, %false : i1 %pending = scalar.andi %streaming, %unknown : i1 %hold = scalar.andi %has_separator, %pending : i1 %cut = scf.select %matches, %separator_begin, %stop : i64 %emit = scf.select %hold, %run, %cut : i64 %skip = scf.select %matches, %stop, %emit : i64 func.return %emit, %skip : i64, i64 } // Canonical history of this turn, as chat_message would render it: prompt + // [trimmed reasoning + think close] + content (trimmed when the template trims it) + [separator] + calls + suffix. // Device state is reusable only when the generated part (without the suffix, // which the next input appends) is byte-identical to the actual output and the // response did not end inside reasoning. (Adapted from qwen3_8/chat.loom.) func.def @generic_complete_text(%prompt: buffer, %response: buffer, %raw_content: buffer, %reasoning: buffer, %tools: buffer, %initial_state: i64, %final_state: i64) -> (buffer, i32) { %c0 = index.constant 0 : offset %c1_i64 = scalar.constant 1 : i64 %empty = global.load @empty : buffer %thinking = scalar.cmpi eq, %initial_state, %c1_i64 : i64 %prefix = scf.if %thinking -> (buffer) { %thought = func.call @trim(%reasoning) : (buffer) -> (buffer) %close = global.load @think_close : buffer %value = func.call @join4(%prompt, %thought, %close, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %value : buffer } else { scf.yield %prompt : buffer } %trim_content = scalar.constant false : i1 %content = scf.if %trim_content -> (buffer) { %value = func.call @trim(%raw_content) : (buffer) -> (buffer) scf.yield %value : buffer } else { scf.yield %raw_content : buffer } %content_length = buffer.length %content %has_content = index.cmp ne, %content_length, %c0 : offset %tools_length = buffer.length %tools %has_calls = index.cmp ne, %tools_length, %c0 : offset %separate = scalar.andi %has_content, %has_calls : i1 %separator_value = global.load @content_call_separator : buffer %separator = scf.select %separate, %separator_value, %empty : buffer %suffix = global.load @message_suffix : buffer %close_value = global.load @calls_close : buffer %close = scf.select %has_calls, %close_value, %empty : buffer %head = func.call @join4(%prefix, %content, %separator, %tools) : (buffer, buffer, buffer, buffer) -> (buffer) %answer = func.call @join(%head, %close) : (buffer, buffer) -> (buffer) %suffixed = func.call @join(%answer, %suffix) : (buffer, buffer) -> (buffer) %closed = scalar.cmpi ne, %final_state, %c1_i64 : i64 %stop_calls = global.load @stop_after_calls : buffer %stop_answer = global.load @stop_after_answer : buffer %stop_kind = scf.select %has_calls, %stop_calls, %stop_answer : buffer %stop = scf.select %closed, %stop_kind, %empty : buffer %result = func.call @join(%suffixed, %stop) : (buffer, buffer) -> (buffer) %prompt_length = buffer.length %prompt %answer_length = buffer.length %answer %generated_length = index.sub %answer_length, %prompt_length : offset %response_length = buffer.length %response %same_length = index.cmp eq, %generated_length, %response_length : offset %identical = scf.if %same_length -> (i1) { %zero32 = scalar.constant 0 : i32 %order = buffer.compare %answer[%prompt_length], %response[%c0], %response_length %equal = scalar.cmpi eq, %order, %zero32 : i32 scf.yield %equal : i1 } else { %false = scalar.constant false : i1 scf.yield %false : i1 } %compatible = scalar.andi %closed, %identical : i1 %reusable = scalar.extui %compatible : i1 to i32 func.return %result, %reusable : buffer, i32 } func.def public @encode_state(%bindings: buffer, %count: i32, %origins: buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %bounded_count = scalar.assume %count [range(%count, 1, 2147483647)] : i32 %length = index.cast %bounded_count : i32 to index %origin_length = buffer.length %origins %origin_length64 = index.cast %origin_length : offset to i64 %stride = scalar.constant 40 : i64 %capacity64 = scalar.divui %origin_length64, %stride : i64 %bounded_capacity = scalar.assume %capacity64 [range(%capacity64, 1, 2147483647)] : i64 %capacity = index.cast %bounded_capacity : i64 to index %records = buffer.view %bindings[%origin] : buffer -> view<[%length]x4xi64> %rows = buffer.view %origins[%origin] : buffer -> view<[%capacity]x5xi64> scf.for %i = [%c0 to %length step %c1] { %row64 = view.load %records[%i, 0] : view<[%length]x4xi64> -> i64 %row_bound = scalar.assume %row64 [range(%row64, 0, 2147483646)] : i64 %row_value = index.cast %row_bound : i64 to index %row = index.assume %row_value [lt(%row_value, %capacity)] : index %read = view.load %records[%i, 1] : view<[%length]x4xi64> -> i64 %write = view.load %records[%i, 2] : view<[%length]x4xi64> -> i64 %position = view.load %records[%i, 3] : view<[%length]x4xi64> -> i64 view.store %read, %rows[%row, 0] : i64, view<[%capacity]x5xi64> view.store %write, %rows[%row, 1] : i64, view<[%capacity]x5xi64> view.store %position, %rows[%row, 3] : i64, view<[%capacity]x5xi64> } func.return } // Opaque output deliberately gathers the two halves of each retained hidden // word in reverse order. Native serving preserves bytes, not tensor types. func.def public @capture_epoch(%state: buffer, %shape: i32, %count: i32, %plan: buffer) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %bounded = scalar.assume %count [range(%count, 1, 2147483647)] : i32 %spans = index.cast %bounded : i32 to index %count64 = scalar.extui %bounded : i32 to i64 %record_bytes = scalar.constant 48 : i64 %bytes64 = scalar.muli %count64, %record_bytes : i64 %bounded_bytes = scalar.assume %bytes64 [range(%bytes64, 48, 103079215056)] : i64 %length = index.cast %bounded_bytes : i64 to offset %storage = buffer.alloca<private> align(8) %length : buffer %records = buffer.view %storage[%origin] : buffer -> view<[%spans]x2x3xi64> %half = scalar.constant 2 : i64 %word = scalar.constant 4 : i64 scf.for %i = [%c0 to %spans step %c1] { %span = index.cast %i : index to i64 %low = scalar.muli %span, %word : i64 %high = scalar.addi %low, %half : i64 view.store %span, %records[%i, 0, 0] : i64, view<[%spans]x2x3xi64> view.store %high, %records[%i, 0, 1] : i64, view<[%spans]x2x3xi64> view.store %half, %records[%i, 0, 2] : i64, view<[%spans]x2x3xi64> view.store %span, %records[%i, 1, 0] : i64, view<[%spans]x2x3xi64> view.store %low, %records[%i, 1, 1] : i64, view<[%spans]x2x3xi64> view.store %half, %records[%i, 1, 2] : i64, view<[%spans]x2x3xi64> } func.return %storage : buffer } func.def @plan_capacity(%plan: buffer) -> (index) { %bytes = buffer.length %plan %bytes64 = index.cast %bytes : offset to i64 %stride = scalar.constant 36 : i64 %records = scalar.divui %bytes64, %stride : i64 %bounded = scalar.assume %records [range(%records, 1, 2147483647)] : i64 %capacity = index.cast %bounded : i64 to index func.return %capacity : index } func.def @encode_mixed_plan(%plan: buffer, %inputs: buffer, %record_begin: index, %span_count: i32, %input_count: i32, %packet_capacity: i32, %packet: buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %bounded_span_count = scalar.assume %span_count [range(%span_count, 0, 2147483647)] : i32 %span_extent = index.cast %bounded_span_count : i32 to index %bounded_input_count = scalar.assume %input_count [range(%input_count, 1, 524288)] : i32 %input_extent = index.cast %bounded_input_count : i32 to index %bounded_packet_capacity = scalar.assume %packet_capacity [range(%packet_capacity, 1, 524288)] : i32 %packet_extent = index.cast %bounded_packet_capacity : i32 to index %plan_extent = func.call @plan_capacity(%plan) : (buffer) -> (index) %requests = buffer.view %plan[%origin] : buffer -> view<[%plan_extent]x9xi32> %ids = buffer.view %inputs[%origin] : buffer -> view<[%input_extent]xi32> %records = buffer.view %packet[%origin] : buffer -> view<[%packet_extent]x8xi32> scf.for %i = [%c0 to %span_extent step %c1] { %request_value = index.add %record_begin, %i : index %request = index.assume %request_value [range(%request_value, 0, 2147483646), lt(%request_value, %plan_extent)] : index %input_begin32 = view.load %requests[%request, 4] : view<[%plan_extent]x9xi32> -> i32 %input_length32 = view.load %requests[%request, 5] : view<[%plan_extent]x9xi32> -> i32 %bounded_begin = scalar.assume %input_begin32 [range(%input_begin32, 0, 524287)] : i32 %bounded_length = scalar.assume %input_length32 [range(%input_length32, 0, 524288)] : i32 %input_begin = index.cast %bounded_begin : i32 to index %input_length = index.cast %bounded_length : i32 to index scf.for %j = [%c0 to %input_length step %c1] { %position = index.add %input_begin, %j : index %input_position = index.assume %position [lt(%position, %input_extent)] : index %packet_position = index.assume %position [lt(%position, %packet_extent)] : index %token = view.load %ids[%input_position] : view<[%input_extent]xi32> -> i32 view.store %token, %records[%packet_position, 0] : i32, view<[%packet_extent]x8xi32> } } func.return } func.def @encode_mixed_media(%plan: buffer, %media_plan: buffer, %media_count: i32, %packet_capacity: i32, %packet: buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %bounded_media_count = scalar.assume %media_count [range(%media_count, 1, 524288)] : i32 %media_extent = index.cast %bounded_media_count : i32 to index %bounded_packet_capacity = scalar.assume %packet_capacity [range(%packet_capacity, 1, 524288)] : i32 %packet_extent = index.cast %bounded_packet_capacity : i32 to index %plan_extent = func.call @plan_capacity(%plan) : (buffer) -> (index) %requests = buffer.view %plan[%origin] : buffer -> view<[%plan_extent]x9xi32> %media = buffer.view %media_plan[%origin] : buffer -> view<[%media_extent]x7xi32> %records = buffer.view %packet[%origin] : buffer -> view<[%packet_extent]x8xi32> scf.for %i = [%c0 to %media_extent step %c1] { %plan_index32 = view.load %media[%i, 0] : view<[%media_extent]x7xi32> -> i32 %relative32 = view.load %media[%i, 1] : view<[%media_extent]x7xi32> -> i32 %feature_offset = view.load %media[%i, 2] : view<[%media_extent]x7xi32> -> i32 %feature_length = view.load %media[%i, 3] : view<[%media_extent]x7xi32> -> i32 %feature_index = view.load %media[%i, 6] : view<[%media_extent]x7xi32> -> i32 %plan_index_bound = scalar.assume %plan_index32 [range(%plan_index32, 0, 2147483646)] : i32 %plan_index_value = index.cast %plan_index_bound : i32 to index %bounded_relative = scalar.assume %relative32 [range(%relative32, 0, 524287)] : i32 %plan_index = index.assume %plan_index_value [lt(%plan_index_value, %plan_extent)] : index %relative = index.cast %bounded_relative : i32 to index %input_begin32 = view.load %requests[%plan_index, 4] : view<[%plan_extent]x9xi32> -> i32 %bounded_begin = scalar.assume %input_begin32 [range(%input_begin32, 0, 524287)] : i32 %input_begin = index.cast %bounded_begin : i32 to index %position = index.add %input_begin, %relative : index %packet_position = index.assume %position [lt(%position, %packet_extent)] : index view.store %feature_offset, %records[%packet_position, 1] : i32, view<[%packet_extent]x8xi32> view.store %feature_length, %records[%packet_position, 2] : i32, view<[%packet_extent]x8xi32> view.store %feature_index, %records[%packet_position, 3] : i32, view<[%packet_extent]x8xi32> } func.return } // Device packets retain the first cohort's nine-field plan records. Feedback // slots are span-indexed, including unselected input-only spans. func.def @plain_encode_epoch(%control_state: buffer, %plan: buffer, %inputs: buffer, %media_plan: buffer, %first_count: i32, %next_count: i32, %input_count: i32, %first_media_count: i32, %next_media_count: i32, %first_packet_capacity: i32, %next_packet_capacity: i32, %eos: i32, %metadata: buffer, %tokens: buffer) -> (i32, buffer, i64, i64) { %origin = index.constant 0 : offset %header_bytes = index.constant 16 : offset %count64 = scalar.extui %first_count : i32 to i64 %record_bytes = scalar.constant 36 : i64 %plan_length = scalar.muli %count64, %record_bytes : i64 %plan_bytes = index.cast %plan_length : i64 to offset %header = buffer.view %metadata[%origin] : buffer -> view<4xi32> %c0 = scalar.constant 0 : i32 view.store %first_count, %header[0] : i32, view<4xi32> view.store %eos, %header[1] : i32, view<4xi32> view.store %c0, %header[2] : i32, view<4xi32> view.store %c0, %header[3] : i32, view<4xi32> buffer.copy %plan[%origin], %metadata[%header_bytes], %plan_bytes // Device copy only: span fields 2, 5, 7, 8 carry the row's sampling record // (temperature, top_p, top_k, seed); the kernels never read the originals. %state_length = buffer.length %control_state %sampling_header = index.constant 2048 : offset %sampling_stride = index.constant 20 : offset %span_stride = index.constant 36 : offset %c0_span = index.constant 0 : index %c1_span = index.constant 1 : index %bounded_first = scalar.assume %first_count [range(%first_count, 0, 8192)] : i32 %span_extent = index.cast %bounded_first : i32 to index scf.for %s = [%c0_span to %span_extent step %c1_span] { %span_at = index.scale %s, %span_stride : index, offset -> offset %row_at = index.constant 12 : offset %row_field = index.add %span_at, %row_at : offset %row_view = buffer.view %plan[%row_field] : buffer -> view<1xi32> %row32 = view.load %row_view[0] : view<1xi32> -> i32 %row_bound = scalar.assume %row32 [range(%row32, 0, 1048575)] : i32 %row_index = index.cast %row_bound : i32 to index %record_rel = index.scale %row_index, %sampling_stride : index, offset -> offset %record_at = index.add %sampling_header, %record_rel : offset %record_end = index.add %record_at, %sampling_stride : offset %has_record = index.cmp ule, %record_end, %state_length : offset %packet_at = index.add %header_bytes, %span_at : offset %packet = buffer.view %metadata[%packet_at] : buffer -> view<9xi32> scf.if %has_record { %record = buffer.view %control_state[%record_at] : buffer -> view<4xi32> %temperature = view.load %record[0] : view<4xi32> -> i32 %top_p = view.load %record[1] : view<4xi32> -> i32 %top_k = view.load %record[2] : view<4xi32> -> i32 %seed = view.load %record[3] : view<4xi32> -> i32 view.store %temperature, %packet[2] : i32, view<9xi32> view.store %top_p, %packet[5] : i32, view<9xi32> view.store %top_k, %packet[7] : i32, view<9xi32> view.store %seed, %packet[8] : i32, view<9xi32> } else { view.store %c0, %packet[2] : i32, view<9xi32> } } %media_count = scalar.addi %first_media_count, %next_media_count : i32 %has_media = scalar.cmpi ne, %media_count, %c0 : i32 %input_count64 = scalar.extui %input_count : i32 to i64 %token_stride = scalar.constant 4 : i64 %packet_stride = scalar.constant 32 : i64 %stride = scf.select %has_media, %packet_stride, %token_stride : i64 %input_length = scalar.muli %input_count64, %stride : i64 %input_bytes = index.cast %input_length : i64 to offset scf.if %has_media { // Ordinary records need zero feature fields; inactive capacity is not read. buffer.fill %c0, %tokens[%origin], %input_bytes : i32 %packet_capacity = scalar.addi %first_packet_capacity, %next_packet_capacity : i32 %c0_index = index.constant 0 : index func.call @encode_mixed_plan(%plan, %inputs, %c0_index, %first_count, %input_count, %packet_capacity, %tokens) : (buffer, buffer, index, i32, i32, i32, buffer) %has_next = scalar.cmpi ne, %next_count, %c0 : i32 scf.if %has_next { %bounded_first = scalar.assume %first_count [range(%first_count, 1, 2147483647)] : i32 %next_begin = index.cast %bounded_first : i32 to index func.call @encode_mixed_plan(%plan, %inputs, %next_begin, %next_count, %input_count, %packet_capacity, %tokens) : (buffer, buffer, index, i32, i32, i32, buffer) } func.call @encode_mixed_media(%plan, %media_plan, %media_count, %packet_capacity, %tokens) : (buffer, buffer, i32, i32, buffer) } else { buffer.copy %inputs[%origin], %tokens[%origin], %input_bytes } %transfer_offset = index.constant 8 : offset %transfers = buffer.view %control_state[%transfer_offset] : buffer -> view<2x5xi64> %zero64 = scalar.constant 0 : i64 %one64 = scalar.constant 1 : i64 %header_length = scalar.constant 16 : i64 %metadata_length = scalar.addi %header_length, %plan_length : i64 view.store %zero64, %transfers[0, 0] : i64, view<2x5xi64> view.store %zero64, %transfers[0, 1] : i64, view<2x5xi64> view.store %zero64, %transfers[0, 2] : i64, view<2x5xi64> view.store %zero64, %transfers[0, 3] : i64, view<2x5xi64> view.store %metadata_length, %transfers[0, 4] : i64, view<2x5xi64> view.store %one64, %transfers[1, 0] : i64, view<2x5xi64> view.store %zero64, %transfers[1, 1] : i64, view<2x5xi64> view.store %one64, %transfers[1, 2] : i64, view<2x5xi64> view.store %zero64, %transfers[1, 3] : i64, view<2x5xi64> view.store %input_length, %transfers[1, 4] : i64, view<2x5xi64> %transfer_begin = scalar.constant 8 : i64 %transfer_length = scalar.constant 80 : i64 func.return %first_count, %control_state, %transfer_begin, %transfer_length : i32, buffer, i64, i64 } func.def @plain_publish_epoch(%control_state: buffer, %plan: buffer, %media_plan: buffer, %count: i32, %first_media_count: i32, %next_media_count: i32, %verified: i32, %continues: i32, %next_speculative: buffer, %ordinary: buffer, %first_results: buffer, %next_results: buffer, %progress: buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %c0_i32 = scalar.constant 0 : i32 %c1_i32 = scalar.constant 1 : i32 %c2_i32 = scalar.constant 2 : i32 %progress_bytes = buffer.length %progress buffer.fill %c0_i32, %progress[%origin], %progress_bytes : i32 %bounded_count = scalar.assume %count [range(%count, 1, 2147483647)] : i32 %length = index.cast %bounded_count : i32 to index %plan_extent = func.call @plan_capacity(%plan) : (buffer) -> (index) %requests = buffer.view %plan[%origin] : buffer -> view<[%plan_extent]x9xi32> %outputs = buffer.view %ordinary[%origin] : buffer -> view<[%length]xi32> %published = buffer.view %progress[%origin] : buffer -> view<[%length]x21xi32> scf.for %i = [%c0 to %length step %c1] { %request = index.assume %i [lt(%i, %plan_extent)] : index %consumed = view.load %requests[%request, 0] : view<[%plan_extent]x9xi32> -> i32 %source_position = view.load %requests[%request, 2] : view<[%plan_extent]x9xi32> -> i32 %source_advance = scalar.muli %consumed, %c2_i32 : i32 %next_source_position = scalar.addi %source_position, %source_advance : i32 %flags = view.load %requests[%request, 6] : view<[%plan_extent]x9xi32> -> i32 %bits = scalar.andi %flags, %c1_i32 : i32 %selected = scalar.cmpi ne, %bits, %c0_i32 : i32 %selected_count = scalar.extui %selected : i1 to i32 view.store %consumed, %published[%i, 0] : i32, view<[%length]x21xi32> view.store %consumed, %published[%i, 1] : i32, view<[%length]x21xi32> view.store %selected_count, %published[%i, 2] : i32, view<[%length]x21xi32> view.store %next_source_position, %published[%i, 4] : i32, view<[%length]x21xi32> scf.if %selected { %token = view.load %outputs[%i] : view<[%length]xi32> -> i32 view.store %token, %published[%i, 5] : i32, view<[%length]x21xi32> } } func.return } func.def public @step(%state: buffer, %initialize: i32, %source_position: i32, %input_count: i32, %residual: hal.buffer, %control: hal.buffer, %recurrent: hal.buffer, %attention: hal.buffer, %tokens: hal.buffer, %progress: hal.buffer) -> (i64, i32) { %c0 = scalar.constant 0 : i32 %c2 = scalar.constant 2 : i32 %c0_i64 = scalar.constant 0 : i64 %source_advance = scalar.muli %input_count, %c2 : i32 %next_source_position = scalar.addi %source_position, %source_advance : i32 %error = global.load @unsupported : buffer func.call @require(%c0, %error) : (i32, buffer) func.return %c0_i64, %next_source_position : i64, i32 } // Selects the stage compiled for this packing shape. func.def @plain_epoch(%control_state: buffer, %shape: i32, %proposes: i32, %verifies: i32, %span_count: i32, %output_count: i32, %epoch_count: i32, %continuation_shape: i32, %continuation_proposes: i32, %first_media_count: i32, %next_media_count: i32, %residual: hal.buffer, %metadata: hal.buffer, %origins: hal.buffer, %state: hal.buffer, %tokens: hal.buffer, %outputs: hal.buffer, %carry: hal.buffer, %committed: hal.buffer, %results: hal.buffer, %draft_cache: hal.buffer, %draft_origins: hal.buffer, %next_results: hal.buffer) -> (i64) { %stage_origin = index.constant 128 : offset %bounded_shape = scalar.assume %shape [range(%shape, 0, 127)] : i32 %shape_index = index.cast %bounded_shape : i32 to index %stages = buffer.view %control_state[%stage_origin] : buffer -> view<128xi32> %stage = view.load %stages[%shape_index] : view<128xi32> -> i32 %completion = func.call @execute_6(%stage, %residual, %metadata, %origins, %state, %tokens, %outputs) : (i32, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer) -> (i64) %c0 = scalar.constant 0 : i32 %c0_i64 = scalar.constant 0 : i64 %c4_i64 = scalar.constant 4 : i64 %count64 = scalar.extui %span_count : i32 to i64 %length = scalar.muli %count64, %c4_i64 : i64 %feedback = func.call @feedback(%c0, %outputs, %c0_i64, %length) : (i32, hal.buffer, i64, i64) -> (i64) func.return %completion : i64 } func.decl @llm_storage(%maximum: i64, %span_capacity: i64, %context: i64, %pool: i64, %rows: i64, %pins: i64, %drafting: i64) -> (buffer, buffer, buffer, buffer, buffer) func.def public @resize_state(%old_state: buffer, %rows: i64) -> (buffer, buffer, buffer, buffer, buffer, buffer) { %origin = index.constant 0 : offset %c0_fill = scalar.constant 0 : i32 %header = index.constant 2048 : offset %record_bytes = index.constant 16 : offset %bounded_rows = scalar.assume %rows [range(%rows, 1, 1048576)] : i64 %row_count = index.cast %bounded_rows : i64 to index %table = index.scale %row_count, %record_bytes : index, offset -> offset %wanted = index.add %header, %table : offset %old_length = buffer.length %old_state %grow = index.cmp ugt, %wanted, %old_length : offset %state = scf.if %grow -> (buffer) { %grown = buffer.alloca<private> align(8) %wanted : buffer buffer.fill %c0_fill, %grown[%origin], %wanted : i32 buffer.copy %old_state[%origin], %grown[%origin], %old_length scf.yield %grown : buffer } else { scf.yield %old_state : buffer } %layout_origin = index.constant 88 : offset %layout = buffer.view %state[%layout_origin] : buffer -> view<5xi64> %maximum = view.load %layout[0] : view<5xi64> -> i64 %span_capacity = view.load %layout[1] : view<5xi64> -> i64 %context = view.load %layout[2] : view<5xi64> -> i64 %pool = view.load %layout[3] : view<5xi64> -> i64 %pins = view.load %layout[4] : view<5xi64> -> i64 %kind_at = index.constant 1672 : offset %kind_view = buffer.view %state[%kind_at] : buffer -> view<1xi32> %kind_word = view.load %kind_view[0] : view<1xi32> -> i32 %kind_zero = scalar.constant 0 : i32 %drafting_on = scalar.cmpi ne, %kind_word, %kind_zero : i32 %drafting64 = scalar.extui %drafting_on : i1 to i64 %allocations, %views, %origins, %draft, %geometry = func.call @llm_storage(%maximum, %span_capacity, %context, %pool, %rows, %pins, %drafting64) : (i64, i64, i64, i64, i64, i64, i64) -> (buffer, buffer, buffer, buffer, buffer) func.return %state, %allocations, %views, %origins, %draft, %geometry : buffer, buffer, buffer, buffer, buffer, buffer } global.rodata.def @ink_msg_sys = bytes("3c7c6d6573736167655f73797374656d7c3e") global.rodata.def @ink_msg_user = bytes("3c7c6d6573736167655f757365727c3e") global.rodata.def @ink_msg_model = bytes("3c7c6d6573736167655f6d6f64656c7c3e") global.rodata.def @ink_msg_tool = bytes("3c7c6d6573736167655f746f6f6c7c3e") global.rodata.def @ink_ct_text = bytes("3c7c636f6e74656e745f746578747c3e") global.rodata.def @ink_ct_think = bytes("3c7c636f6e74656e745f7468696e6b696e677c3e") global.rodata.def @ink_ct_xml = bytes("3c7c636f6e74656e745f786d6c7c3e") global.rodata.def @ink_ct_invoke = bytes("3c7c636f6e74656e745f696e766f6b655f746f6f6c5f6a736f6e7c3e") global.rodata.def @ink_end_msg = bytes("3c7c656e645f6d6573736167657c3e") global.rodata.def @ink_end_sampling = bytes("3c7c636f6e74656e745f6d6f64656c5f656e645f73616d706c696e677c3e") global.rodata.def @ink_effort_open = bytes("3c7c6d6573736167655f73797374656d7c3e3c7c636f6e74656e745f746578747c3e5468696e6b696e67206566666f7274206c6576656c3a20") global.rodata.def @ink_effort_zero = bytes("30") global.rodata.def @ink_effort_zero_f = bytes("302e30") global.rodata.def @ink_tool_declare = bytes("3c7c6d6573736167655f73797374656d7c3e746f6f6c5f6465636c6172653c7c636f6e74656e745f786d6c7c3e") global.rodata.def @ink_spec_desc = bytes("7b226465736372697074696f6e223a") global.rodata.def @ink_spec_name = bytes("2c226e616d65223a") global.rodata.def @ink_spec_params = bytes("2c22706172616d6574657273223a") global.rodata.def @ink_spec_type = bytes("2c2274797065223a") global.rodata.def @ink_default_type = bytes("2266756e6374696f6e22") global.rodata.def @ink_empty_str = bytes("2222") global.rodata.def @ink_empty_obj = bytes("7b7d") global.rodata.def @ink_call_name_open = bytes("7b226e616d65223a") global.rodata.def @ink_call_args_mid = bytes("2c2261726773223a") global.rodata.def @ink_lbrace = bytes("7b") global.rodata.def @ink_rbrace = bytes("7d") global.rodata.def @ink_lbracket = bytes("5b") global.rodata.def @ink_rbracket = bytes("5d") global.rodata.def @ink_comma = bytes("2c") global.rodata.def @ink_colon = bytes("3a") global.rodata.def @ink_quote = bytes("22") global.rodata.def @ink_k_name = bytes("6e616d65") global.rodata.def @ink_k_desc = bytes("6465736372697074696f6e") global.rodata.def @ink_k_params = bytes("706172616d6574657273") global.rodata.def @ink_k_type = bytes("74797065") global.rodata.def @ink_k_text = bytes("74657874") global.rodata.def @ink_k_input_text = bytes("696e7075745f74657874") global.rodata.def @ink_k_tool_call_id = bytes("746f6f6c5f63616c6c5f6964") global.rodata.def @ink_k_tool_calls = bytes("746f6f6c5f63616c6c73") global.rodata.def @ink_k_id = bytes("6964") global.rodata.def @ink_k_arguments = bytes("617267756d656e7473") global.rodata.def @ink_k_args = bytes("61726773") global.rodata.def @ink_k_reasoning = bytes("726561736f6e696e675f636f6e74656e74") // ---- Inkling-2-Mini chat format (chat/inkling2.loom) ---------------------------------- // Appended to control.loom by build.py when chat.call_format == "inkling2" (the generic // chat_begin/chat_message/chat_end/render_tool/parse_tools/output_next/complete_text are // renamed to @generic_*). Python model: chat_probe.inkling2_simulate / inkling2_render_calls / // raw_from_chat. // The whole conversation is rendered in chat_begin: tools are declared first as sorted compact // JSON in <|message_system|>tool_declare<|content_xml|>...<|end_message|>, the thinking effort // block is emitted before the first non-system message (or at the end if all messages are // system), tool results look up their function name via tool_call_id, and assistant turns end // with <|content_model_end_sampling|>. Generation prompt is empty; the model generates its own // <|message_model|><|content_thinking|> / <|content_text|> / <name><|content_invoke_tool_json|> // blocks. // Decoded string member of object r[b, b+l) (empty when absent or not a string). func.def @ink_string(%r: buffer, %b: i64, %l: i64, %key: buffer) -> (buffer) { %empty = global.load @empty : buffer %zero64 = scalar.constant 0 : i64 %f, %vb, %vl, %vt = func.call @json_lookup(%r, %b, %l, %key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %is_string = scalar.cmpi eq, %vt, %zero64 : i64 %ok = scalar.andi %f, %is_string : i1 %result = scf.if %ok -> (buffer) { %v = func.call @json_key(%r, %vb, %vl) : (buffer, i64, i64) -> (buffer) scf.yield %v : buffer } else { scf.yield %empty : buffer } func.return %result : buffer } // Re-quotes a raw JSON string body r[b, b+l) as '"..."'. func.def @ink_quote_raw(%r: buffer, %b: i64, %l: i64) -> (buffer) { %quote = global.load @ink_quote : buffer %empty = global.load @empty : buffer %raw = func.call @json_range(%r, %b, %l) : (buffer, i64, i64) -> (buffer) %quoted = func.call @join4(%quote, %raw, %quote, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) func.return %quoted : buffer } // JSON-quotes an arbitrary decoded string %text using @json_escape. func.def @ink_quote_str(%text: buffer) -> (buffer) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %c2 = index.constant 2 : offset %quote = scalar.constant 34 : i32 %length = buffer.length %text %twice = index.add %length, %length : offset %capacity = index.add %twice, %c2 : offset %output = buffer.alloca<private> align(1) %capacity : buffer buffer.store.i8 %quote, %output[%c0] %end = func.call @json_escape(%text, %c0, %length, %output, %c1) : (buffer, offset, offset, buffer, offset) -> (offset) buffer.store.i8 %quote, %output[%end] %used = index.add %end, %c1 : offset %result = func.call @slice(%output, %c0, %used) : (buffer, offset, offset) -> (buffer) func.return %result : buffer } // Case-sensitive a < b on decoded UTF-8 keys (matches Python sort_keys=True and // Jinja dictsort(case_sensitive=true)). func.def @ink_key_less(%a: buffer, %b: buffer) -> (i1) { %c0 = index.constant 0 : offset %c1 = index.constant 1 : offset %a_length = buffer.length %a %b_length = buffer.length %b %a_shorter = index.cmp ult, %a_length, %b_length : offset %common = scf.select %a_shorter, %a_length, %b_length : offset %stop, %less = scf.while(%position = %c0 : offset, %result = %a_shorter : i1) -> (offset, i1) { %inside = index.cmp ult, %position, %common : offset %continue = scf.if %inside -> (i1) { %x = buffer.load.i8.u %a[%position] %y = buffer.load.i8.u %b[%position] %same = scalar.cmpi eq, %x, %y : i32 scf.yield %same : i1 } else { %false = scalar.constant false : i1 scf.yield %false : i1 } %value = scf.if %inside -> (i1) { %x = buffer.load.i8.u %a[%position] %y = buffer.load.i8.u %b[%position] %lt = scalar.cmpi ult, %x, %y : i32 scf.yield %lt : i1 } else { scf.yield %a_shorter : i1 } scf.condition %continue, %position, %value : i1, offset, i1 } do(%position: offset, %result: i1) { %next = index.add %position, %c1 : offset scf.yield %next, %result : offset, i1 } func.return %less : i1 } // Member order of the JSON object at text[begin, begin + length), sorted ascending // case-sensitively by decoded key. Returns (fields, count, order[count] as i64). func.def @ink_sorted(%text: buffer, %begin: i64, %length: i64) -> (buffer, i64, buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %fields, %type, %raw_count = func.call @members_range(%text, %begin, %length) : (buffer, i64, i64) -> (buffer, i32, i64) %object = scalar.constant 2 : i32 %is_object = scalar.cmpi eq, %type, %object : i32 %count = scf.select %is_object, %raw_count, %zero64 : i64 %bounded = scalar.assume %count [range(%count, 0, 65536)] : i64 %n = index.cast %bounded : i64 to index %records = buffer.view %fields[%origin] : buffer -> view<[%n]x5xi64> %c8 = index.constant 8 : offset %order_bytes = index.scale %n, %c8 : index, offset -> offset %padded = index.add %order_bytes, %c8 : offset %order = buffer.alloca<private> align(8) %padded : buffer %used = buffer.alloca<private> align(8) %padded : buffer %c0_i32 = scalar.constant 0 : i32 buffer.fill %c0_i32, %used[%origin], %padded : i32 %order_view = buffer.view %order[%origin] : buffer -> view<[%n]xi64> %used_view = buffer.view %used[%origin] : buffer -> view<[%n]xi64> %minus = scalar.constant -1 : i64 %one64 = scalar.constant 1 : i64 scf.for %r = [%c0 to %n step %c1] { %best = scf.for %i = [%c0 to %n step %c1](%current = %minus : i64) -> (i64) { %taken = view.load %used_view[%i] : view<[%n]xi64> -> i64 %free = scalar.cmpi eq, %taken, %zero64 : i64 %next = scf.if %free -> (i64) { %index64 = index.cast %i : index to i64 %none = scalar.cmpi slt, %current, %zero64 : i64 %pick = scf.if %none -> (i64) { scf.yield %index64 : i64 } else { %bounded_current = scalar.assume %current [range(%current, 0, 65535)] : i64 %ci = index.cast %bounded_current : i64 to index %cj = index.assume %ci [lt(%ci, %n)] : index %kb = view.load %records[%i, 0] : view<[%n]x5xi64> -> i64 %kl = view.load %records[%i, 1] : view<[%n]x5xi64> -> i64 %cb = view.load %records[%cj, 0] : view<[%n]x5xi64> -> i64 %cl = view.load %records[%cj, 1] : view<[%n]x5xi64> -> i64 %key = func.call @json_key(%text, %kb, %kl) : (buffer, i64, i64) -> (buffer) %other = func.call @json_key(%text, %cb, %cl) : (buffer, i64, i64) -> (buffer) %less = func.call @ink_key_less(%key, %other) : (buffer, buffer) -> (i1) %chosen = scf.select %less, %index64, %current : i64 scf.yield %chosen : i64 } scf.yield %pick : i64 } else { scf.yield %current : i64 } scf.yield %next : i64 } %bounded_best = scalar.assume %best [range(%best, 0, 65535)] : i64 %bi = index.cast %bounded_best : i64 to index %bj = index.assume %bi [lt(%bi, %n)] : index view.store %one64, %used_view[%bj] : i64, view<[%n]xi64> view.store %best, %order_view[%r] : i64, view<[%n]xi64> } func.return %fields, %count, %order : buffer, i64, buffer } // Recursive compact JSON with sorted keys: tojson(sort_keys=true, separators=(",", ":")). func.def @ink_json_sorted(%text: buffer, %begin: i64, %length: i64, %type: i64) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %empty = global.load @empty : buffer %comma = global.load @ink_comma : buffer %colon = global.load @ink_colon : buffer %string_type = scalar.constant 0 : i64 %object_type = scalar.constant 2 : i64 %array_type = scalar.constant 3 : i64 %is_string = scalar.cmpi eq, %type, %string_type : i64 %is_object = scalar.cmpi eq, %type, %object_type : i64 %is_array = scalar.cmpi eq, %type, %array_type : i64 %result = scf.if %is_string -> (buffer) { %q = func.call @ink_quote_raw(%text, %begin, %length) : (buffer, i64, i64) -> (buffer) scf.yield %q : buffer } else { %other = scf.if %is_object -> (buffer) { %fields, %count, %order = func.call @ink_sorted(%text, %begin, %length) : (buffer, i64, i64) -> (buffer, i64, buffer) %bounded = scalar.assume %count [range(%count, 0, 65536)] : i64 %n = index.cast %bounded : i64 to index %records = buffer.view %fields[%origin] : buffer -> view<[%n]x5xi64> %order_view = buffer.view %order[%origin] : buffer -> view<[%n]xi64> %open = global.load @ink_lbrace : buffer %close = global.load @ink_rbrace : buffer %body = scf.for %r = [%c0 to %n step %c1](%acc = %open : buffer) -> (buffer) { %k64 = view.load %order_view[%r] : view<[%n]xi64> -> i64 %kb64 = scalar.assume %k64 [range(%k64, 0, 65535)] : i64 %ki = index.cast %kb64 : i64 to index %k = index.assume %ki [lt(%ki, %n)] : index %kb = view.load %records[%k, 0] : view<[%n]x5xi64> -> i64 %kl = view.load %records[%k, 1] : view<[%n]x5xi64> -> i64 %vb = view.load %records[%k, 2] : view<[%n]x5xi64> -> i64 %vl = view.load %records[%k, 3] : view<[%n]x5xi64> -> i64 %vt = view.load %records[%k, 4] : view<[%n]x5xi64> -> i64 %key_q = func.call @ink_quote_raw(%text, %kb, %kl) : (buffer, i64, i64) -> (buffer) %first = index.cmp eq, %r, %c0 : index %lead = scf.select %first, %empty, %comma : buffer %val = func.call @ink_json_sorted(%text, %vb, %vl, %vt) : (buffer, i64, i64, i64) -> (buffer) %head = func.call @join4(%acc, %lead, %key_q, %colon) : (buffer, buffer, buffer, buffer) -> (buffer) %next = func.call @join(%head, %val) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } %closed = func.call @join(%body, %close) : (buffer, buffer) -> (buffer) scf.yield %closed : buffer } else { %listed = scf.if %is_array -> (buffer) { %elements, %etype, %ecount = func.call @members_range(%text, %begin, %length) : (buffer, i64, i64) -> (buffer, i32, i64) %bounded = scalar.assume %ecount [range(%ecount, 0, 65536)] : i64 %n = index.cast %bounded : i64 to index %records = buffer.view %elements[%origin] : buffer -> view<[%n]x5xi64> %open = global.load @ink_lbracket : buffer %close = global.load @ink_rbracket : buffer %body = scf.for %i = [%c0 to %n step %c1](%acc = %open : buffer) -> (buffer) { %eb = view.load %records[%i, 2] : view<[%n]x5xi64> -> i64 %el = view.load %records[%i, 3] : view<[%n]x5xi64> -> i64 %et = view.load %records[%i, 4] : view<[%n]x5xi64> -> i64 %first = index.cmp eq, %i, %c0 : index %lead = scf.select %first, %empty, %comma : buffer %item = func.call @ink_json_sorted(%text, %eb, %el, %et) : (buffer, i64, i64, i64) -> (buffer) %next = func.call @join4(%acc, %lead, %item, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %next : buffer } %closed = func.call @join(%body, %close) : (buffer, buffer) -> (buffer) scf.yield %closed : buffer } else { %raw = func.call @json_range(%text, %begin, %length) : (buffer, i64, i64) -> (buffer) scf.yield %raw : buffer } scf.yield %listed : buffer } scf.yield %other : buffer } func.return %result : buffer } // Tool-call arguments object: top-level keys sorted case-sensitively (dictsort(case_sensitive=true)), // each value compact JSON (separators=(",", ":") without sorting nested object keys). func.def @ink_args_sorted(%arguments: buffer) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %string_type = scalar.constant 0 : i64 %empty = global.load @empty : buffer %comma = global.load @ink_comma : buffer %colon = global.load @ink_colon : buffer %open = global.load @ink_lbrace : buffer %close = global.load @ink_rbrace : buffer %arg_len = buffer.length %arguments %has_args = index.cmp ne, %arg_len, %origin : offset %result = scf.if %has_args -> (buffer) { %compacted = func.call @compact(%arguments) : (buffer) -> (buffer) %clen_off = buffer.length %compacted %clen = index.cast %clen_off : offset to i64 %fields, %count, %order = func.call @ink_sorted(%compacted, %zero64, %clen) : (buffer, i64, i64) -> (buffer, i64, buffer) %bounded = scalar.assume %count [range(%count, 0, 65536)] : i64 %n = index.cast %bounded : i64 to index %records = buffer.view %fields[%origin] : buffer -> view<[%n]x5xi64> %order_view = buffer.view %order[%origin] : buffer -> view<[%n]xi64> %body = scf.for %r = [%c0 to %n step %c1](%acc = %open : buffer) -> (buffer) { %k64 = view.load %order_view[%r] : view<[%n]xi64> -> i64 %kb64 = scalar.assume %k64 [range(%k64, 0, 65535)] : i64 %ki = index.cast %kb64 : i64 to index %k = index.assume %ki [lt(%ki, %n)] : index %kb = view.load %records[%k, 0] : view<[%n]x5xi64> -> i64 %kl = view.load %records[%k, 1] : view<[%n]x5xi64> -> i64 %vb = view.load %records[%k, 2] : view<[%n]x5xi64> -> i64 %vl = view.load %records[%k, 3] : view<[%n]x5xi64> -> i64 %vt = view.load %records[%k, 4] : view<[%n]x5xi64> -> i64 %key_q = func.call @ink_quote_raw(%compacted, %kb, %kl) : (buffer, i64, i64) -> (buffer) %is_string = scalar.cmpi eq, %vt, %string_type : i64 %val = scf.if %is_string -> (buffer) { %q = func.call @ink_quote_raw(%compacted, %vb, %vl) : (buffer, i64, i64) -> (buffer) scf.yield %q : buffer } else { %raw = func.call @json_range(%compacted, %vb, %vl) : (buffer, i64, i64) -> (buffer) scf.yield %raw : buffer } %first = index.cmp eq, %r, %c0 : index %lead = scf.select %first, %empty, %comma : buffer %head = func.call @join4(%acc, %lead, %key_q, %colon) : (buffer, buffer, buffer, buffer) -> (buffer) %next = func.call @join(%head, %val) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } %closed = func.call @join(%body, %close) : (buffer, buffer) -> (buffer) scf.yield %closed : buffer } else { %empty_obj = global.load @ink_empty_obj : buffer scf.yield %empty_obj : buffer } func.return %result : buffer } // Tool declarations block at the very start of the prompt: // <|message_system|>tool_declare<|content_xml|>[...]<|end_message|> func.def @ink_tools_block(%tools: buffer) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %string_type = scalar.constant 0 : i64 %object_type = scalar.constant 2 : i64 %empty = global.load @empty : buffer %tool_fields, %tool_type, %tool_count = func.call @members(%tools) : (buffer) -> (buffer, i32, i64) %has_tools = scalar.cmpi ne, %tool_count, %zero64 : i64 %result = scf.if %has_tools -> (buffer) { %bounded = scalar.assume %tool_count [range(%tool_count, 1, 4096)] : i64 %extent = index.cast %bounded : i64 to index %records = buffer.view %tool_fields[%origin] : buffer -> view<[%extent]x5xi64> %function_key = global.load @function_key : buffer %name_key = global.load @ink_k_name : buffer %desc_key = global.load @ink_k_desc : buffer %params_key = global.load @ink_k_params : buffer %type_key = global.load @ink_k_type : buffer %spec_desc = global.load @ink_spec_desc : buffer %spec_name = global.load @ink_spec_name : buffer %spec_params = global.load @ink_spec_params : buffer %spec_type = global.load @ink_spec_type : buffer %default_type = global.load @ink_default_type : buffer %empty_str = global.load @ink_empty_str : buffer %empty_obj = global.load @ink_empty_obj : buffer %comma = global.load @ink_comma : buffer %rbrace = global.load @ink_rbrace : buffer %lbracket = global.load @ink_lbracket : buffer %rbracket = global.load @ink_rbracket : buffer %body = scf.for %i = [%c0 to %extent step %c1](%acc = %lbracket : buffer) -> (buffer) { %tb = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %tl = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %ff, %fb, %fl, %ft = func.call @json_lookup(%tools, %tb, %tl, %function_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %fn_obj = scalar.cmpi eq, %ft, %object_type : i64 %use_fn = scalar.andi %ff, %fn_obj : i1 %b = scf.select %use_fn, %fb, %tb : i64 %l = scf.select %use_fn, %fl, %tl : i64 // description (default "") %fd, %db, %dl, %dt = func.call @json_lookup(%tools, %b, %l, %desc_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %d_str = scalar.cmpi eq, %dt, %string_type : i64 %d_nonempty = scalar.cmpi ne, %dl, %zero64 : i64 %d_ok0 = scalar.andi %fd, %d_str : i1 %d_ok = scalar.andi %d_ok0, %d_nonempty : i1 %desc_json = scf.if %d_ok -> (buffer) { %q = func.call @ink_quote_raw(%tools, %db, %dl) : (buffer, i64, i64) -> (buffer) scf.yield %q : buffer } else { scf.yield %empty_str : buffer } // name %fn_f, %nb, %nl, %nt = func.call @json_lookup(%tools, %b, %l, %name_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %name_json = scf.if %fn_f -> (buffer) { %q = func.call @ink_quote_raw(%tools, %nb, %nl) : (buffer, i64, i64) -> (buffer) scf.yield %q : buffer } else { scf.yield %empty_str : buffer } // parameters (default {}, recursively sorted compact JSON) %fp, %pb, %pl, %pt = func.call @json_lookup(%tools, %b, %l, %params_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %p_obj = scalar.cmpi eq, %pt, %object_type : i64 %p_ok = scalar.andi %fp, %p_obj : i1 %params_json = scf.if %p_ok -> (buffer) { %s = func.call @ink_json_sorted(%tools, %pb, %pl, %pt) : (buffer, i64, i64, i64) -> (buffer) scf.yield %s : buffer } else { scf.yield %empty_obj : buffer } // type (from outer tool object, default "function") %fty, %tyb, %tyl, %tyt = func.call @json_lookup(%tools, %tb, %tl, %type_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %ty_str = scalar.cmpi eq, %tyt, %string_type : i64 %ty_nonempty = scalar.cmpi ne, %tyl, %zero64 : i64 %ty_ok0 = scalar.andi %fty, %ty_str : i1 %ty_ok = scalar.andi %ty_ok0, %ty_nonempty : i1 %type_json = scf.if %ty_ok -> (buffer) { %q = func.call @ink_quote_raw(%tools, %tyb, %tyl) : (buffer, i64, i64) -> (buffer) scf.yield %q : buffer } else { scf.yield %default_type : buffer } %first = index.cmp eq, %i, %c0 : index %lead = scf.select %first, %empty, %comma : buffer %s1 = func.call @join4(%acc, %lead, %spec_desc, %desc_json) : (buffer, buffer, buffer, buffer) -> (buffer) %s2 = func.call @join4(%s1, %spec_name, %name_json, %spec_params) : (buffer, buffer, buffer, buffer) -> (buffer) %s3 = func.call @join4(%s2, %params_json, %spec_type, %type_json) : (buffer, buffer, buffer, buffer) -> (buffer) %next = func.call @join(%s3, %rbrace) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } %declare_open = global.load @ink_tool_declare : buffer %end_msg = global.load @ink_end_msg : buffer %block = func.call @join4(%declare_open, %body, %rbracket, %end_msg) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %block : buffer } else { scf.yield %empty : buffer } func.return %result : buffer } // Thinking effort block: // <|message_system|><|content_text|>Thinking effort level: <num><|end_message|> // Resolves enable_thinking / chat_template_kwargs.enable_thinking / // chat_template_kwargs.reasoning_effort / reasoning_effort (string or numeric). func.def @ink_effort_block(%request: buffer) -> (buffer) { %zero64 = scalar.constant 0 : i64 %string_type = scalar.constant 0 : i64 %number_type = scalar.constant 1 : i64 %false = scalar.constant false : i1 %true = scalar.constant true : i1 %empty = global.load @empty : buffer %request_bytes = buffer.length %request %request_length = index.cast %request_bytes : offset to i64 %thinking_key = global.load @thinking_key : buffer %kwargs_key = global.load @kwargs_key : buffer %effort_key = global.load @effort_key : buffer // 1. enable_thinking (top-level, then chat_template_kwargs.enable_thinking) %f1, %b1, %l1, %t1 = func.call @json_lookup(%request, %zero64, %request_length, %thinking_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %think1 = func.call @thinking_value(%request, %f1, %b1, %l1, %t1, %false, %true) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) %fk, %bk, %lk, %tk = func.call @json_lookup(%request, %zero64, %request_length, %kwargs_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %think2, %fke, %bke, %lke, %tke = scf.if %fk -> (i1, i1, i64, i64, i64) { %f2, %b2, %l2, %t2 = func.call @json_lookup(%request, %bk, %lk, %thinking_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %tv = func.call @thinking_value(%request, %f2, %b2, %l2, %t2, %false, %think1) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) %fe1, %be1, %le1, %te1 = func.call @json_lookup(%request, %bk, %lk, %effort_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) scf.yield %tv, %fe1, %be1, %le1, %te1 : i1, i1, i64, i64, i64 } else { scf.yield %think1, %false, %zero64, %zero64, %zero64 : i1, i1, i64, i64, i64 } // 2. reasoning_effort string in kwargs, then top-level (overrides enable_thinking) %kw_str = scalar.cmpi eq, %tke, %string_type : i64 %use_kw_str = scalar.andi %fke, %kw_str : i1 %think3 = func.call @thinking_value(%request, %use_kw_str, %bke, %lke, %tke, %true, %think2) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) %fe2, %be2, %le2, %te2 = func.call @json_lookup(%request, %zero64, %request_length, %effort_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %top_str = scalar.cmpi eq, %te2, %string_type : i64 %use_top_str = scalar.andi %fe2, %top_str : i1 %thinking = func.call @thinking_value(%request, %use_top_str, %be2, %le2, %te2, %true, %think3) : (buffer, i1, i64, i64, i64, i1, i1) -> (i1) // 3. Check numeric reasoning_effort (top-level else kwargs) %top_num = scalar.cmpi eq, %te2, %number_type : i64 %use_top_num = scalar.andi %fe2, %top_num : i1 %kw_num = scalar.cmpi eq, %tke, %number_type : i64 %use_kw_num = scalar.andi %fke, %kw_num : i1 %has_num = scalar.ori %use_top_num, %use_kw_num : i1 %zero_text = global.load @ink_effort_zero : buffer %num = scf.if %has_num -> (buffer) { %nb = scf.select %use_top_num, %be2, %bke : i64 %nl = scf.select %use_top_num, %le2, %lke : i64 %raw = func.call @json_range(%request, %nb, %nl) : (buffer, i64, i64) -> (buffer) %zero_f = global.load @ink_effort_zero_f : buffer %is_zf = func.call @equal(%raw, %zero_f) : (buffer, buffer) -> (i1) %v = scf.select %is_zf, %zero_text, %raw : buffer scf.yield %v : buffer } else { %v = scf.if %thinking -> (buffer) { %inst = func.call @reasoning_instructions(%request, %true) : (buffer, i1) -> (buffer) scf.yield %inst : buffer } else { scf.yield %zero_text : buffer } scf.yield %v : buffer } %effort_open = global.load @ink_effort_open : buffer %end_msg = global.load @ink_end_msg : buffer %result = func.call @join4(%effort_open, %num, %end_msg, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) func.return %result : buffer } // Look up function name(s) in assistant tool_calls matching %tcid (empty if not found). func.def @ink_call_name(%r: buffer, %elements: buffer, %count: i64, %tcid: buffer) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %array_type = scalar.constant 3 : i64 %empty = global.load @empty : buffer %bounded = scalar.assume %count [range(%count, 0, 2147483647)] : i64 %extent = index.cast %bounded : i64 to index %records = buffer.view %elements[%origin] : buffer -> view<[%extent]x5xi64> %role_key = global.load @role_key : buffer %assistant_role = global.load @assistant : buffer %calls_key = global.load @ink_k_tool_calls : buffer %id_key = global.load @ink_k_id : buffer %function_key = global.load @function_key : buffer %name_key = global.load @ink_k_name : buffer %result = scf.for %i = [%c0 to %extent step %c1](%acc = %empty : buffer) -> (buffer) { %mb = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %ml = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %role = func.call @ink_string(%r, %mb, %ml, %role_key) : (buffer, i64, i64, buffer) -> (buffer) %is_asst = func.call @equal(%role, %assistant_role) : (buffer, buffer) -> (i1) %f, %cb, %cl, %ct = func.call @json_lookup(%r, %mb, %ml, %calls_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %is_array = scalar.cmpi eq, %ct, %array_type : i64 %use0 = scalar.andi %is_asst, %f : i1 %use = scalar.andi %use0, %is_array : i1 %next = scf.if %use -> (buffer) { %calls, %t2, %n = func.call @members_range(%r, %cb, %cl) : (buffer, i64, i64) -> (buffer, i32, i64) %nb = scalar.assume %n [range(%n, 0, 2147483647)] : i64 %ne = index.cast %nb : i64 to index %call_records = buffer.view %calls[%origin] : buffer -> view<[%ne]x5xi64> %found = scf.for %k = [%c0 to %ne step %c1](%inner = %acc : buffer) -> (buffer) { %qb = view.load %call_records[%k, 2] : view<[%ne]x5xi64> -> i64 %ql = view.load %call_records[%k, 3] : view<[%ne]x5xi64> -> i64 %id = func.call @ink_string(%r, %qb, %ql, %id_key) : (buffer, i64, i64, buffer) -> (buffer) %match = func.call @equal(%id, %tcid) : (buffer, buffer) -> (i1) %picked = scf.if %match -> (buffer) { %ff, %fb, %fl, %ft = func.call @json_lookup(%r, %qb, %ql, %function_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %name = func.call @ink_string(%r, %fb, %fl, %name_key) : (buffer, i64, i64, buffer) -> (buffer) %joined = func.call @join(%inner, %name) : (buffer, buffer) -> (buffer) scf.yield %joined : buffer } else { scf.yield %inner : buffer } scf.yield %picked : buffer } scf.yield %found : buffer } else { scf.yield %acc : buffer } scf.yield %next : buffer } func.return %result : buffer } // Render content field of a message: string (when non-empty or %allow_empty_string), // or array of parts (each part becomes its own rtok + <|content_text|> + text + <|end_message|>). func.def @ink_render_content(%r: buffer, %rtok: buffer, %fc: i1, %bc: i64, %lc: i64, %tc: i64, %allow_empty_string: i1) -> (buffer) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %string_type = scalar.constant 0 : i64 %object_type = scalar.constant 2 : i64 %array_type = scalar.constant 3 : i64 %false = scalar.constant false : i1 %empty = global.load @empty : buffer %ct_text = global.load @ink_ct_text : buffer %end_msg = global.load @ink_end_msg : buffer %is_string = scalar.cmpi eq, %tc, %string_type : i64 %use_string = scalar.andi %fc, %is_string : i1 %result = scf.if %use_string -> (buffer) { %nonempty = scalar.cmpi ne, %lc, %zero64 : i64 %emit = scalar.ori %nonempty, %allow_empty_string : i1 %v = scf.if %emit -> (buffer) { %text = func.call @json_key(%r, %bc, %lc) : (buffer, i64, i64) -> (buffer) %block = func.call @join4(%rtok, %ct_text, %text, %end_msg) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %block : buffer } else { scf.yield %empty : buffer } scf.yield %v : buffer } else { %is_array = scalar.cmpi eq, %tc, %array_type : i64 %use_array = scalar.andi %fc, %is_array : i1 %w = scf.if %use_array -> (buffer) { %parts, %pt, %pc = func.call @members_range(%r, %bc, %lc) : (buffer, i64, i64) -> (buffer, i32, i64) %bounded = scalar.assume %pc [range(%pc, 0, 2147483647)] : i64 %extent = index.cast %bounded : i64 to index %records = buffer.view %parts[%origin] : buffer -> view<[%extent]x5xi64> %type_key = global.load @ink_k_type : buffer %text_key = global.load @ink_k_text : buffer %input_text_key = global.load @ink_k_input_text : buffer %joined = scf.for %i = [%c0 to %extent step %c1](%acc = %empty : buffer) -> (buffer) { %pb = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %pl = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %pty = view.load %records[%i, 4] : view<[%extent]x5xi64> -> i64 %part_str = scalar.cmpi eq, %pty, %string_type : i64 %piece = scf.if %part_str -> (buffer) { %s = func.call @json_key(%r, %pb, %pl) : (buffer, i64, i64) -> (buffer) %b = func.call @join4(%rtok, %ct_text, %s, %end_msg) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %b : buffer } else { %part_obj = scalar.cmpi eq, %pty, %object_type : i64 %ob = scf.if %part_obj -> (buffer) { %ft, %tb, %tl, %tt = func.call @json_lookup(%r, %pb, %pl, %type_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %no_type = scalar.cmpi eq, %ft, %false : i1 %is_text_kind = scf.if %ft -> (i1) { %kind = func.call @json_key(%r, %tb, %tl) : (buffer, i64, i64) -> (buffer) %t1 = func.call @equal(%kind, %text_key) : (buffer, buffer) -> (i1) %t2 = func.call @equal(%kind, %input_text_key) : (buffer, buffer) -> (i1) %any = scalar.ori %t1, %t2 : i1 scf.yield %any : i1 } else { scf.yield %false : i1 } %emit_text = scalar.ori %no_type, %is_text_kind : i1 func.call @check(%emit_text) : (i1) %txt = func.call @ink_string(%r, %pb, %pl, %text_key) : (buffer, i64, i64, buffer) -> (buffer) %b = func.call @join4(%rtok, %ct_text, %txt, %end_msg) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %b : buffer } else { scf.yield %empty : buffer } scf.yield %ob : buffer } %next = func.call @join(%acc, %piece) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } scf.yield %joined : buffer } else { scf.yield %empty : buffer } scf.yield %w : buffer } func.return %result : buffer } // One tool call as history renders it: // <|message_model|><name><|content_invoke_tool_json|>{"name":"<name>","args":{<sorted compact>}}<|end_message|> func.def public @render_tool(%name: buffer, %arguments: buffer, %ordinal: i64) -> (buffer, i64) { %empty = global.load @empty : buffer %msg_model = global.load @ink_msg_model : buffer %ct_invoke = global.load @ink_ct_invoke : buffer %name_open = global.load @ink_call_name_open : buffer %args_mid = global.load @ink_call_args_mid : buffer %rbrace = global.load @ink_rbrace : buffer %end_msg = global.load @ink_end_msg : buffer %quoted_name = func.call @ink_quote_str(%name) : (buffer) -> (buffer) %sorted_args = func.call @ink_args_sorted(%arguments) : (buffer) -> (buffer) %h1 = func.call @join4(%msg_model, %name, %ct_invoke, %name_open) : (buffer, buffer, buffer, buffer) -> (buffer) %h2 = func.call @join4(%h1, %quoted_name, %args_mid, %sorted_args) : (buffer, buffer, buffer, buffer) -> (buffer) %result = func.call @join4(%h2, %rbrace, %end_msg, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) %length_bytes = buffer.length %result %length = index.cast %length_bytes : offset to i64 func.return %result, %length : buffer, i64 } // Render all tool_calls of an assistant message. Returns (rendered_text, has_any_calls). func.def @ink_render_calls(%r: buffer, %cb: i64, %cl: i64) -> (buffer, i1) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %string_type = scalar.constant 0 : i64 %empty = global.load @empty : buffer %function_key = global.load @function_key : buffer %name_key = global.load @ink_k_name : buffer %arguments_key = global.load @ink_k_arguments : buffer %calls, %t, %n = func.call @members_range(%r, %cb, %cl) : (buffer, i64, i64) -> (buffer, i32, i64) %nb = scalar.assume %n [range(%n, 0, 2147483647)] : i64 %ne = index.cast %nb : i64 to index %records = buffer.view %calls[%origin] : buffer -> view<[%ne]x5xi64> %text = scf.for %k = [%c0 to %ne step %c1](%acc = %empty : buffer) -> (buffer) { %qb = view.load %records[%k, 2] : view<[%ne]x5xi64> -> i64 %ql = view.load %records[%k, 3] : view<[%ne]x5xi64> -> i64 %ff, %fb, %fl, %ft = func.call @json_lookup(%r, %qb, %ql, %function_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %name = func.call @ink_string(%r, %fb, %fl, %name_key) : (buffer, i64, i64, buffer) -> (buffer) %fa, %ab, %al, %at = func.call @json_lookup(%r, %fb, %fl, %arguments_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %arguments = scf.if %fa -> (buffer) { %is_string = scalar.cmpi eq, %at, %string_type : i64 %v = scf.if %is_string -> (buffer) { %decoded = func.call @json_key(%r, %ab, %al) : (buffer, i64, i64) -> (buffer) scf.yield %decoded : buffer } else { %raw = func.call @json_range(%r, %ab, %al) : (buffer, i64, i64) -> (buffer) scf.yield %raw : buffer } scf.yield %v : buffer } else { scf.yield %empty : buffer } %ordinal = index.cast %k : index to i64 %one, %length = func.call @render_tool(%name, %arguments, %ordinal) : (buffer, buffer, i64) -> (buffer, i64) %next = func.call @join(%acc, %one) : (buffer, buffer) -> (buffer) scf.yield %next : buffer } %has = scalar.cmpi ne, %n, %zero64 : i64 func.return %text, %has : buffer, i1 } // Render the full conversation in chat_begin. func.def public @chat_begin(%request: buffer, %tools: buffer) -> (buffer, i64) { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %zero64 = scalar.constant 0 : i64 %array_type = scalar.constant 3 : i64 %false = scalar.constant false : i1 %true = scalar.constant true : i1 %empty = global.load @empty : buffer %request_bytes = buffer.length %request %request_length = index.cast %request_bytes : offset to i64 %messages_key = global.load @messages_key : buffer %fm, %bm, %lm, %tm = func.call @json_lookup(%request, %zero64, %request_length, %messages_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %is_array = scalar.cmpi eq, %tm, %array_type : i64 %has_messages = scalar.andi %fm, %is_array : i1 func.call @check(%has_messages) : (i1) %elements, %etype, %ecount = func.call @members_range(%request, %bm, %lm) : (buffer, i64, i64) -> (buffer, i32, i64) %bounded = scalar.assume %ecount [range(%ecount, 0, 2147483647)] : i64 %extent = index.cast %bounded : i64 to index %records = buffer.view %elements[%origin] : buffer -> view<[%extent]x5xi64> // Tools declaration block (at the very beginning before messages). %tools_block = func.call @ink_tools_block(%tools) : (buffer) -> (buffer) %effort_block = func.call @ink_effort_block(%request) : (buffer) -> (buffer) %role_key = global.load @role_key : buffer %content_key = global.load @content_key : buffer %name_key = global.load @ink_k_name : buffer %tcid_key = global.load @ink_k_tool_call_id : buffer %reasoning_key = global.load @ink_k_reasoning : buffer %calls_key = global.load @ink_k_tool_calls : buffer %system_role = global.load @system : buffer %user_role = global.load @user : buffer %assistant_role = global.load @assistant : buffer %tool_role = global.load @tool_role : buffer %msg_sys = global.load @ink_msg_sys : buffer %msg_user = global.load @ink_msg_user : buffer %msg_model = global.load @ink_msg_model : buffer %msg_tool = global.load @ink_msg_tool : buffer %ct_think = global.load @ink_ct_think : buffer %end_msg = global.load @ink_end_msg : buffer %end_sampling = global.load @ink_end_sampling : buffer %body, %emitted = scf.for %i = [%c0 to %extent step %c1](%acc = %tools_block : buffer, %effort_done = %false : i1) -> (buffer, i1) { %mb = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64 %ml = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64 %role = func.call @ink_string(%request, %mb, %ml, %role_key) : (buffer, i64, i64, buffer) -> (buffer) %is_system = func.call @equal(%role, %system_role) : (buffer, buffer) -> (i1) %is_user = func.call @equal(%role, %user_role) : (buffer, buffer) -> (i1) %is_assistant = func.call @equal(%role, %assistant_role) : (buffer, buffer) -> (i1) %is_tool = func.call @equal(%role, %tool_role) : (buffer, buffer) -> (i1) %known1 = scalar.ori %is_system, %is_user : i1 %known2 = scalar.ori %is_assistant, %is_tool : i1 %known = scalar.ori %known1, %known2 : i1 func.call @check(%known) : (i1) // Emit thinking effort block before the first non-system message. %not_done = scalar.cmpi eq, %effort_done, %false : i1 %not_sys = scalar.cmpi eq, %is_system, %false : i1 %emit_effort = scalar.andi %not_done, %not_sys : i1 %pre = scf.select %emit_effort, %effort_block, %empty : buffer %next_done = scalar.ori %effort_done, %emit_effort : i1 %acc1 = func.call @join(%acc, %pre) : (buffer, buffer) -> (buffer) // Header token rtok. %rtok = scf.if %is_system -> (buffer) { scf.yield %msg_sys : buffer } else { %r1 = scf.if %is_user -> (buffer) { scf.yield %msg_user : buffer } else { %r2 = scf.if %is_tool -> (buffer) { %given = func.call @ink_string(%request, %mb, %ml, %name_key) : (buffer, i64, i64, buffer) -> (buffer) %given_len = buffer.length %given %named = index.cmp ne, %given_len, %origin : offset %tname = scf.if %named -> (buffer) { scf.yield %given : buffer } else { %tcid = func.call @ink_string(%request, %mb, %ml, %tcid_key) : (buffer, i64, i64, buffer) -> (buffer) %tcid_len = buffer.length %tcid %has_tcid = index.cmp ne, %tcid_len, %origin : offset %looked = scf.if %has_tcid -> (buffer) { %v = func.call @ink_call_name(%request, %elements, %ecount, %tcid) : (buffer, buffer, i64, buffer) -> (buffer) scf.yield %v : buffer } else { scf.yield %empty : buffer } scf.yield %looked : buffer } %h = func.call @join(%msg_tool, %tname) : (buffer, buffer) -> (buffer) scf.yield %h : buffer } else { // assistant (+ optional message.name) %aname = func.call @ink_string(%request, %mb, %ml, %name_key) : (buffer, i64, i64, buffer) -> (buffer) %h = func.call @join(%msg_model, %aname) : (buffer, buffer) -> (buffer) scf.yield %h : buffer } scf.yield %r2 : buffer } scf.yield %r1 : buffer } %fc, %bc, %lc, %tc = func.call @json_lookup(%request, %mb, %ml, %content_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %piece = scf.if %is_assistant -> (buffer) { // Optional reasoning_content %reasoning = func.call @ink_string(%request, %mb, %ml, %reasoning_key) : (buffer, i64, i64, buffer) -> (buffer) %rlen = buffer.length %reasoning %has_reasoning = index.cmp ne, %rlen, %origin : offset %rblock = scf.if %has_reasoning -> (buffer) { %v = func.call @join4(%rtok, %ct_think, %reasoning, %end_msg) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %v : buffer } else { scf.yield %empty : buffer } // Optional tool_calls %fk, %bk, %lk, %tk = func.call @json_lookup(%request, %mb, %ml, %calls_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %calls_arr = scalar.cmpi eq, %tk, %array_type : i64 %use_calls = scalar.andi %fk, %calls_arr : i1 %calls_text, %has_calls = scf.if %use_calls -> (buffer, i1) { %ct, %hc = func.call @ink_render_calls(%request, %bk, %lk) : (buffer, i64, i64) -> (buffer, i1) scf.yield %ct, %hc : buffer, i1 } else { scf.yield %empty, %false : buffer, i1 } %has_other = scalar.ori %has_calls, %has_reasoning : i1 %allow_empty = scalar.cmpi eq, %has_other, %false : i1 %content_block = func.call @ink_render_content(%request, %rtok, %fc, %bc, %lc, %tc, %allow_empty) : (buffer, buffer, i1, i64, i64, i64, i1) -> (buffer) %all = func.call @join4(%rblock, %content_block, %calls_text, %end_sampling) : (buffer, buffer, buffer, buffer) -> (buffer) scf.yield %all : buffer } else { %content_block = func.call @ink_render_content(%request, %rtok, %fc, %bc, %lc, %tc, %true) : (buffer, buffer, i1, i64, i64, i64, i1) -> (buffer) scf.yield %content_block : buffer } %next_acc = func.call @join(%acc1, %piece) : (buffer, buffer) -> (buffer) scf.yield %next_acc, %next_done : buffer, i1 } // If all messages were system (or messages was empty), emit thinking effort at the end. %tail = scf.select %emitted, %empty, %effort_block : buffer %rendered = func.call @join(%body, %tail) : (buffer, buffer) -> (buffer) func.return %rendered, %zero64 : buffer, i64 } // Every message was already rendered by chat_begin. func.def public @chat_message(%raw: buffer, %role: buffer, %content: buffer, %parts: buffer, %frames: buffer, %reasoning: buffer, %calls: buffer, %state: i64, %ordinal: i64, %fields: i64, %part_count: i64) -> (buffer, i64, buffer, i64) { %zero64 = scalar.constant 0 : i64 %empty = global.load @empty : buffer func.return %empty, %state, %empty, %zero64 : buffer, i64, buffer, i64 } // Generation prompt is empty; output_next starts in state 1 (expecting <|message_model|> header). func.def public @chat_end(%state: i64) -> (buffer, i64) { %empty = global.load @empty : buffer %header = scalar.constant 1 : i64 func.return %empty, %header : buffer, i64 } // Classify the header right after <|message_model|> at %hfrom: // tag 2 -> <|content_thinking|>, hend = after tag // tag 3 -> <|content_text|>, hend = after tag // tag 4 -> <name><|content_invoke_tool_json|> before next <|end_message|>/<|message_model|> // tag 0 -> incomplete header func.def @ink_classify_header(%response: buffer, %hfrom: offset, %length: offset) -> (i64, offset) { %c0_i64 = scalar.constant 0 : i64 %c2_i64 = scalar.constant 2 : i64 %c3_i64 = scalar.constant 3 : i64 %c4_i64 = scalar.constant 4 : i64 %ct_think = global.load @ink_ct_think : buffer %ct_text = global.load @ink_ct_text : buffer %is_think = func.call @starts_at(%response, %hfrom, %ct_think) : (buffer, offset, buffer) -> (i1) %tag, %hend = scf.if %is_think -> (i64, offset) { %tlen = buffer.length %ct_think %after = index.add %hfrom, %tlen : offset scf.yield %c2_i64, %after : i64, offset } else { %is_text = func.call @starts_at(%response, %hfrom, %ct_text) : (buffer, offset, buffer) -> (i1) %t2, %e2 = scf.if %is_text -> (i64, offset) { %xlen = buffer.length %ct_text %after = index.add %hfrom, %xlen : offset scf.yield %c3_i64, %after : i64, offset } else { %end_msg = global.load @ink_end_msg : buffer %msg_model = global.load @ink_msg_model : buffer %ct_invoke = global.load @ink_ct_invoke : buffer %stop_e = func.call @find(%response, %hfrom, %length, %end_msg) : (buffer, offset, offset, buffer) -> (offset) %stop_m = func.call @find(%response, %hfrom, %length, %msg_model) : (buffer, offset, offset, buffer) -> (offset) %e_first = index.cmp ule, %stop_e, %stop_m : offset %block_end = scf.select %e_first, %stop_e, %stop_m : offset %inv = func.call @find(%response, %hfrom, %block_end, %ct_invoke) : (buffer, offset, offset, buffer) -> (offset) %has_inv = index.cmp ult, %inv, %block_end : offset %tag_v = scf.select %has_inv, %c4_i64, %c0_i64 : i64 scf.yield %tag_v, %hfrom : i64, offset } scf.yield %t2, %e2 : i64, offset } func.return %tag, %hend : i64, offset } // Handle a complete <|message_model|> starting at %previous (with %hfrom right after it). // Never changes state on a zero-length span. func.def @ink_handle_model_header(%response: buffer, %previous: i64, %hfrom: i64, %length: i64, %state: i64, %phase: i32, %wait_kind: i64) -> (i64, i64, i64) { %c0_i64 = scalar.constant 0 : i64 %c1_i64 = scalar.constant 1 : i64 %c2_i64 = scalar.constant 2 : i64 %c3_i64 = scalar.constant 3 : i64 %c4_i64 = scalar.constant 4 : i64 %zero32 = scalar.constant 0 : i32 %hfrom_b = scalar.assume %hfrom [range(%hfrom, 0, 9223372036854775807)] : i64 %len_b = scalar.assume %length [range(%length, 0, 9223372036854775807)] : i64 %hfrom_off = index.cast %hfrom_b : i64 to offset %len_off = index.cast %len_b : i64 to offset %tag, %hend_off = func.call @ink_classify_header(%response, %hfrom_off, %len_off) : (buffer, offset, offset) -> (i64, offset) %hend = index.cast %hend_off : offset to i64 %is_think = scalar.cmpi eq, %tag, %c2_i64 : i64 %is_text = scalar.cmpi eq, %tag, %c3_i64 : i64 %is_channel = scalar.ori %is_think, %is_text : i1 %k, %e, %n = scf.if %is_channel -> (i64, i64, i64) { // Complete thinking or text header: consume it (strictly > previous) and enter state 2 or 3. scf.yield %c0_i64, %hend, %tag : i64, i64, i64 } else { %is_tool = scalar.cmpi eq, %tag, %c4_i64 : i64 %k2, %e2, %n2 = scf.if %is_tool -> (i64, i64, i64) { // Tool call header: stop emitting (zero length, same state); reserve suffix for parse_tools. scf.yield %c1_i64, %previous, %state : i64, i64, i64 } else { // Incomplete header: wait while streaming, drop dangling header at final flush. %final = scalar.cmpi ne, %phase, %zero32 : i32 %drop_end = scf.select %final, %length, %previous : i64 %drop_kind = scf.select %final, %c0_i64, %wait_kind : i64 scf.yield %drop_kind, %drop_end, %state : i64, i64, i64 } scf.yield %k2, %e2, %n2 : i64, i64, i64 } func.return %k, %e, %n : i64, i64, i64 } // Output demuxer: // state 1 = between blocks / expecting <|message_model|> header // state 2 = inside <|content_thinking|> (emits kind 2) // state 3 = inside <|content_text|> (emits kind 1) // Tool calls (<|message_model|><name><|content_invoke_tool_json|>...) halt emission with // zero length and unchanged state so the host reserves the full tool-call suffix for parse_tools. func.def public @output_next(%response: buffer, %previous: i64, %state: i64, %phase: i32) -> (i64, i64, i64, i64) { %c0_i64 = scalar.constant 0 : i64 %c1_i64 = scalar.constant 1 : i64 %c2_i64 = scalar.constant 2 : i64 %length_off = buffer.length %response %length = index.cast %length_off : offset to i64 %msg_model = global.load @ink_msg_model : buffer %end_msg = global.load @ink_end_msg : buffer %in_header = scalar.cmpi eq, %state, %c1_i64 : i64 %kind, %end, %next = scf.if %in_header -> (i64, i64, i64) { %stop_m = func.call @text_until(%response, %msg_model, %previous, %phase) : (buffer, buffer, i64, i32) -> (i64) %stop_e = func.call @text_until(%response, %end_msg, %previous, %phase) : (buffer, buffer, i64, i32) -> (i64) %m_first = scalar.cmpi ule, %stop_m, %stop_e : i64 %stop = scf.select %m_first, %stop_m, %stop_e : i64 %marker = scf.select %m_first, %msg_model, %end_msg : buffer %at_marker = scalar.cmpi eq, %stop, %previous : i64 %remaining = scalar.subi %length, %stop : i64 %mlen_off = buffer.length %marker %mlen = index.cast %mlen_off : offset to i64 %complete = scalar.cmpi uge, %remaining, %mlen : i64 %consume = scalar.andi %at_marker, %complete : i1 %rk, %re, %rn = scf.if %consume -> (i64, i64, i64) { %after = scalar.addi %stop, %mlen : i64 %k2, %e2, %n2 = scf.if %m_first -> (i64, i64, i64) { %hk, %he, %hn = func.call @ink_handle_model_header(%response, %previous, %after, %length, %state, %phase, %c0_i64) : (buffer, i64, i64, i64, i64, i32, i64) -> (i64, i64, i64) scf.yield %hk, %he, %hn : i64, i64, i64 } else { // Stray or trailing <|end_message|>: consume cleanly, stay in state 1. scf.yield %c0_i64, %after, %c1_i64 : i64, i64, i64 } scf.yield %k2, %e2, %n2 : i64, i64, i64 } else { %has_text = scalar.cmpi ugt, %stop, %previous : i64 %wk = scf.select %has_text, %c1_i64, %c0_i64 : i64 scf.yield %wk, %stop, %state : i64, i64, i64 } scf.yield %rk, %re, %rn : i64, i64, i64 } else { %is_thinking = scalar.cmpi eq, %state, %c2_i64 : i64 %channel = scf.select %is_thinking, %c2_i64, %c1_i64 : i64 %stop_e = func.call @text_until(%response, %end_msg, %previous, %phase) : (buffer, buffer, i64, i32) -> (i64) %stop_m = func.call @text_until(%response, %msg_model, %previous, %phase) : (buffer, buffer, i64, i32) -> (i64) %e_first = scalar.cmpi ule, %stop_e, %stop_m : i64 %stop = scf.select %e_first, %stop_e, %stop_m : i64 %marker = scf.select %e_first, %end_msg, %msg_model : buffer %at_marker = scalar.cmpi eq, %stop, %previous : i64 %remaining = scalar.subi %length, %stop : i64 %mlen_off = buffer.length %marker %mlen = index.cast %mlen_off : offset to i64 %complete = scalar.cmpi uge, %remaining, %mlen : i64 %consume = scalar.andi %at_marker, %complete : i1 %rk, %re, %rn = scf.if %consume -> (i64, i64, i64) { %after = scalar.addi %stop, %mlen : i64 %k2, %e2, %n2 = scf.if %e_first -> (i64, i64, i64) { // Complete <|end_message|>: consume it (non-zero length) and return to state 1. scf.yield %c0_i64, %after, %c1_i64 : i64, i64, i64 } else { // Direct <|message_model|> without <|end_message|>: inspect header. %hk, %he, %hn = func.call @ink_handle_model_header(%response, %previous, %after, %length, %state, %phase, %channel) : (buffer, i64, i64, i64, i64, i32, i64) -> (i64, i64, i64) scf.yield %hk, %he, %hn : i64, i64, i64 } scf.yield %k2, %e2, %n2 : i64, i64, i64 } else { scf.yield %channel, %stop, %state : i64, i64, i64 } scf.yield %rk, %re, %rn : i64, i64, i64 } func.return %kind, %previous, %end, %next : i64, i64, i64, i64 } // Parse one or more <|message_model|><name><|content_invoke_tool_json|>{"name":...,"args":{...}}<|end_message|> // blocks into OpenAI tool_calls JSON array: // [{"type":"function","function":{"name":"...","arguments":"<json string>"}}] func.def public @parse_tools(%schemas: buffer, %response: buffer, %truncated: i64) -> (buffer) { %origin = index.constant 0 : offset %c1 = index.constant 1 : offset %c128 = index.constant 128 : offset %zero64 = scalar.constant 0 : i64 %string_type = scalar.constant 0 : i64 %object_type = scalar.constant 2 : i64 %true = scalar.constant true : i1 %false = scalar.constant false : i1 %none = global.load @no_tools : buffer %length = buffer.length %response %has_text = index.cmp ne, %length, %origin : offset %schema_fields, %schema_type, %schema_count = func.call @members(%schemas) : (buffer) -> (buffer, i32, i64) %has_tools = scalar.cmpi ne, %schema_count, %zero64 : i64 %do_parse = scalar.andi %has_text, %has_tools : i1 %result = scf.if %do_parse -> (buffer) { %twice = index.add %length, %length : offset %quad = index.add %twice, %twice : offset %capacity = index.add %quad, %c128 : offset %output = buffer.alloca<private> align(1) %capacity : buffer %open_bracket = scalar.constant 91 : i32 %close_bracket = scalar.constant 93 : i32 %comma = scalar.constant 44 : i32 buffer.store.i8 %open_bracket, %output[%origin] %ct_invoke = global.load @ink_ct_invoke : buffer %end_msg = global.load @ink_end_msg : buffer %msg_model = global.load @ink_msg_model : buffer %invoke_len = buffer.length %ct_invoke %end_len = buffer.length %end_msg %name_key = global.load @ink_k_name : buffer %args_key = global.load @ink_k_args : buffer %arguments_key = global.load @ink_k_arguments : buffer %record_open = global.load @record_open : buffer %record_middle = global.load @record_middle : buffer %record_close = global.load @record_close : buffer %empty_obj = global.load @ink_empty_obj : buffer %allow_eof = scalar.cmpi eq, %truncated, %zero64 : i64 %final_pos, %final_out, %final_first = scf.while(%pos = %origin : offset, %out = %c1 : offset, %first = %true : i1) -> (offset, offset, i1) { %inv = func.call @find(%response, %pos, %length, %ct_invoke) : (buffer, offset, offset, buffer) -> (offset) %has_inv = index.cmp ult, %inv, %length : offset %body_b0 = index.add %inv, %invoke_len : offset %body_b = scf.select %has_inv, %body_b0, %length : offset %end_p = func.call @find(%response, %body_b, %length, %end_msg) : (buffer, offset, offset, buffer) -> (offset) %has_end = index.cmp ult, %end_p, %length : offset %usable_end = scalar.ori %has_end, %allow_eof : i1 %more = scalar.andi %has_inv, %usable_end : i1 scf.condition %more, %pos, %out, %first : i1, offset, offset, i1 } do(%pos: offset, %out: offset, %first: i1) { %inv = func.call @find(%response, %pos, %length, %ct_invoke) : (buffer, offset, offset, buffer) -> (offset) %body_b = index.add %inv, %invoke_len : offset %end_p = func.call @find(%response, %body_b, %length, %end_msg) : (buffer, offset, offset, buffer) -> (offset) %has_end = index.cmp ult, %end_p, %length : offset %next_m = func.call @find(%response, %body_b, %length, %msg_model) : (buffer, offset, offset, buffer) -> (offset) %e_first = index.cmp ule, %end_p, %next_m : offset %raw_end = scf.select %e_first, %end_p, %next_m : offset %jb = func.call @skip_space(%response, %body_b, %raw_end) : (buffer, offset, offset) -> (offset) %je = func.call @trim_end(%response, %jb, %raw_end) : (buffer, offset, offset) -> (offset) %jl = index.sub %je, %jb : offset %jb64 = index.cast %jb : offset to i64 %jl64 = index.cast %jl : offset to i64 %fn_f, %nb, %nl, %nt = func.call @json_lookup(%response, %jb64, %jl64, %name_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %name_str = scalar.cmpi eq, %nt, %string_type : i64 %name_nonempty = scalar.cmpi ne, %nl, %zero64 : i64 %name_ok0 = scalar.andi %fn_f, %name_str : i1 %name_ok = scalar.andi %name_ok0, %name_nonempty : i1 %written, %next_first = scf.if %name_ok -> (offset, i1) { %fa1, %ab1, %al1, %at1 = func.call @json_lookup(%response, %jb64, %jl64, %args_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) %fa, %ab, %al, %at = scf.if %fa1 -> (i1, i64, i64, i64) { scf.yield %fa1, %ab1, %al1, %at1 : i1, i64, i64, i64 } else { %fa2, %ab2, %al2, %at2 = func.call @json_lookup(%response, %jb64, %jl64, %arguments_key) : (buffer, i64, i64, buffer) -> (i1, i64, i64, i64) scf.yield %fa2, %ab2, %al2, %at2 : i1, i64, i64, i64 } %separated = scf.if %first -> (offset) { scf.yield %out : offset } else { buffer.store.i8 %comma, %output[%out] %after = index.add %out, %c1 : offset scf.yield %after : offset } %o1 = func.call @tool_append(%output, %separated, %record_open) : (buffer, offset, buffer) -> (offset) %nb_b = scalar.assume %nb [range(%nb, 0, 9223372036854775807)] : i64 %nl_b = scalar.assume %nl [range(%nl, 0, 9223372036854775807)] : i64 %nb_off = index.cast %nb_b : i64 to offset %nl_off = index.cast %nl_b : i64 to offset buffer.copy %response[%nb_off], %output[%o1], %nl_off %o2 = index.add %o1, %nl_off : offset %o3 = func.call @tool_append(%output, %o2, %record_middle) : (buffer, offset, buffer) -> (offset) %is_obj = scalar.cmpi eq, %at, %object_type : i64 %args_obj = scalar.andi %fa, %is_obj : i1 %o4 = scf.if %args_obj -> (offset) { %ab_b = scalar.assume %ab [range(%ab, 0, 9223372036854775807)] : i64 %ae64 = scalar.addi %ab, %al : i64 %ae_b = scalar.assume %ae64 [range(%ae64, 0, 9223372036854775807)] : i64 %ab_off = index.cast %ab_b : i64 to offset %ae_off = index.cast %ae_b : i64 to offset %moved = func.call @json_escape(%response, %ab_off, %ae_off, %output, %o3) : (buffer, offset, offset, buffer, offset) -> (offset) scf.yield %moved : offset } else { %is_str = scalar.cmpi eq, %at, %string_type : i64 %args_str = scalar.andi %fa, %is_str : i1 %o4b = scf.if %args_str -> (offset) { %ab_b = scalar.assume %ab [range(%ab, 0, 9223372036854775807)] : i64 %al_b = scalar.assume %al [range(%al, 0, 9223372036854775807)] : i64 %ab_off = index.cast %ab_b : i64 to offset %al_off = index.cast %al_b : i64 to offset buffer.copy %response[%ab_off], %output[%o3], %al_off %moved = index.add %o3, %al_off : offset scf.yield %moved : offset } else { %moved = func.call @tool_append(%output, %o3, %empty_obj) : (buffer, offset, buffer) -> (offset) scf.yield %moved : offset } scf.yield %o4b : offset } %o5 = func.call @tool_append(%output, %o4, %record_close) : (buffer, offset, buffer) -> (offset) scf.yield %o5, %false : offset, i1 } else { scf.yield %out, %first : offset, i1 } %after_end = index.add %end_p, %end_len : offset %next_pos = scf.select %has_end, %after_end, %raw_end : offset scf.yield %next_pos, %written, %next_first : offset, offset, i1 } buffer.store.i8 %close_bracket, %output[%final_out] %used = index.add %final_out, %c1 : offset %parsed = func.call @slice(%output, %origin, %used) : (buffer, offset, offset) -> (buffer) scf.yield %parsed : buffer } else { scf.yield %none : buffer } func.return %result : buffer } // Canonical history of this turn: prompt + response + <|content_model_end_sampling|>. // Reusable when output_next returned to state 1 (no open thinking/text block) and the // response closed cleanly with <|end_message|>. func.def public @complete_text(%prompt: buffer, %response: buffer, %content: buffer, %reasoning: buffer, %tools: buffer, %initial_state: i64, %final_state: i64) -> (buffer, i32) { %c1_i64 = scalar.constant 1 : i64 %false = scalar.constant false : i1 %empty = global.load @empty : buffer %terminal = global.load @ink_end_sampling : buffer %end_msg = global.load @ink_end_msg : buffer %result = func.call @join4(%prompt, %response, %terminal, %empty) : (buffer, buffer, buffer, buffer) -> (buffer) %closed_state = scalar.cmpi eq, %final_state, %c1_i64 : i64 %rlen = buffer.length %response %elen = buffer.length %end_msg %long_enough = index.cmp uge, %rlen, %elen : offset %ends_clean = scf.if %long_enough -> (i1) { %tail_pos = index.sub %rlen, %elen : offset %ok = func.call @starts_at(%response, %tail_pos, %end_msg) : (buffer, offset, buffer) -> (i1) scf.yield %ok : i1 } else { scf.yield %false : i1 } %valid = scalar.andi %closed_state, %ends_clean : i1 %reusable = scalar.extui %valid : i1 to i32 func.return %result, %reusable : buffer, i32 } // ---- Speculative decoding (generated by draft/build_draft.py) ---- // Speculative spans reserve draft_depth + 1 packed inputs but the host supplies only the anchor; inputs // therefore move to packed cursors (the sum of earlier span lengths), placeholders are zeroed, and the // device plan copy carries the cursor in word 4, flag bit 4 (speculative: verify, do not publish // recurrent state) and the output credit in bits 8..15. Spans are moved last to first, so a move never // overwrites inputs not yet moved (cursors never precede their host offsets). func.def public @encode_epoch(%control_state: buffer, %plan: buffer, %inputs: buffer, %media_plan: buffer, %first_count: i32, %next_count: i32, %input_count: i32, %first_media_count: i32, %next_media_count: i32, %first_packet_capacity: i32, %next_packet_capacity: i32, %eos: i32, %metadata: buffer, %tokens: buffer) -> (i32, buffer, i64, i64) { %count, %state, %transfer_begin, %transfer_length = func.call @plain_encode_epoch(%control_state, %plan, %inputs, %media_plan, %first_count, %next_count, %input_count, %first_media_count, %next_media_count, %first_packet_capacity, %next_packet_capacity, %eos, %metadata, %tokens) : (buffer, buffer, buffer, buffer, i32, i32, i32, i32, i32, i32, i32, i32, buffer, buffer) -> (i32, buffer, i64, i64) %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %c0_i32 = scalar.constant 0 : i32 %c4_i32 = scalar.constant 4 : i32 %c8_i32 = scalar.constant 8 : i32 %header_bytes = index.constant 16 : offset %bounded_first = scalar.assume %first_count [range(%first_count, 1, 8192)] : i32 %spans = index.cast %bounded_first : i32 to index %plan_extent = func.call @plan_capacity(%plan) : (buffer) -> (index) %requests = buffer.view %plan[%origin] : buffer -> view<[%plan_extent]x9xi32> %device = buffer.view %metadata[%header_bytes] : buffer -> view<[%spans]x9xi32> %media_total = scalar.addi %first_media_count, %next_media_count : i32 %mixed = scalar.cmpi ne, %media_total, %c0_i32 : i32 %one = index.constant 1 : index %eight = index.constant 8 : index %stride = scf.select %mixed, %eight, %one : index %token_bytes = buffer.length %tokens %token_words = index.cast %token_bytes : offset to index %four = index.constant 4 : index %words = index.div %token_words, %four : index %flat = buffer.view %tokens[%origin] : buffer -> view<[%words]xi32> // occupancy / context gate: all spans speculative and at least 2 of them -> verify the anchors only %c1_i32 = scalar.constant 1 : i32 %try_below = scalar.constant 1 : i32 %try = scalar.cmpi ugt, %first_count, %try_below : i32 %flag_value = scf.if %try -> (i32) { %nspec = scf.for %g = [%c0 to %spans step %c1](%acc = %c0_i32 : i32) -> (i32) { %gr = index.assume %g [lt(%g, %plan_extent)] : index %gcredit = view.load %requests[%gr, 7] : view<[%plan_extent]x9xi32> -> i32 %gspec = scalar.cmpi ne, %gcredit, %c0_i32 : i32 %gadd = scalar.extui %gspec : i1 to i32 %gnext = scalar.addi %acc, %gadd : i32 scf.yield %gnext : i32 } %all_spec = scalar.cmpi eq, %nspec, %first_count : i32 %occ_below = scalar.constant 1 : i32 %occupied = scalar.cmpi ugt, %first_count, %occ_below : i32 %gate_limit = scalar.constant 32 : i32 %few_spans = scalar.cmpi ult, %first_count, %gate_limit : i32 %gate_max = scalar.constant 31 : i32 %gate_slot32 = scalar.minui %first_count, %gate_max : i32 %gate_slot_b = scalar.assume %gate_slot32 [range(%gate_slot32, 0, 31)] : i32 %gate_slot = index.cast %gate_slot_b : i32 to index %gate_at = index.constant 1700 : offset %gate_view = buffer.view %control_state[%gate_at] : buffer -> view<32xi32> %gate_raw = view.load %gate_view[%gate_slot] : view<32xi32> -> i32 %gate_shape = scf.select %few_spans, %gate_raw, %c0_i32 : i32 %has_shape = scalar.cmpi ne, %gate_shape, %c0_i32 : i32 %ctx_over = scalar.cmpi ne, %c0_i32, %c0_i32 : i32 %g0 = scalar.ori %occupied, %ctx_over : i1 %g1 = scalar.andi %all_spec, %g0 : i1 %gate_in = scalar.andi %g1, %has_shape : i1 %flag_sel = scf.select %gate_in, %gate_shape, %c0_i32 : i32 scf.yield %flag_sel : i32 } else { scf.yield %c0_i32 : i32 } %gate = scalar.cmpi ne, %flag_value, %c0_i32 : i32 %flag_at = index.constant 1676 : offset %flag_view = buffer.view %control_state[%flag_at] : buffer -> view<1xi32> view.store %flag_value, %flag_view[0] : i32, view<1xi32> %not_gate_pre = scalar.cmpi eq, %flag_value, %c0_i32 : i32 // adaptive draft depth: per-row running acceptance selects depth=1 vs full draft_depth %adapt_hdr_at = index.constant 1828 : offset %adapt_hdr = buffer.view %control_state[%adapt_hdr_at] : buffer -> view<5xi32> %adapt_en = view.load %adapt_hdr[0] : view<5xi32> -> i32 %adapt_on = scalar.cmpi ne, %adapt_en, %c0_i32 : i32 %c1_ad_i32 = scalar.constant 1 : i32 %c2_ad_i32 = scalar.constant 2 : i32 %one_span_ad = scalar.cmpi eq, %first_count, %c1_ad_i32 : i32 %try_ad0 = scalar.andi %adapt_on, %one_span_ad : i1 %try_ad = scalar.andi %try_ad0, %not_gate_pre : i1 %adapt_d1_i32 = scf.if %try_ad -> (i32) { %sr0 = index.assume %c0 [lt(%c0, %plan_extent)] : index %ad_len = view.load %requests[%sr0, 0] : view<[%plan_extent]x9xi32> -> i32 %ad_pos = view.load %requests[%sr0, 1] : view<[%plan_extent]x9xi32> -> i32 %ad_row = view.load %requests[%sr0, 3] : view<[%plan_extent]x9xi32> -> i32 %ad_cred = view.load %requests[%sr0, 7] : view<[%plan_extent]x9xi32> -> i32 %deep_c = scalar.cmpi uge, %ad_cred, %c2_ad_i32 : i32 %deep_l = scalar.cmpi ugt, %ad_len, %c2_ad_i32 : i32 %is_deep = scalar.andi %deep_c, %deep_l : i1 %sel_d1 = scf.if %is_deep -> (i32) { %rmask = scalar.constant 15 : i32 %rslot32 = scalar.andi %ad_row, %rmask : i32 %rslot_b = scalar.assume %rslot32 [range(%rslot32, 0, 15)] : i32 %rslot = index.cast %rslot_b : i32 to index %ar_at = index.constant 1848 : offset %ar_v = buffer.view %control_state[%ar_at] : buffer -> view<16x3xi32> %cur_mode = view.load %ar_v[%rslot, 1] : view<16x3xi32> -> i32 %last_pos = view.load %ar_v[%rslot, 2] : view<16x3xi32> -> i32 %no_prev = scalar.cmpi eq, %last_pos, %c0_i32 : i32 %rewound = scalar.cmpi ne, %ad_pos, %last_pos : i32 %is_new = scalar.ori %no_prev, %rewound : i1 %eff_mode = scf.select %is_new, %c0_i32, %cur_mode : i32 %auto_d1 = scalar.cmpi eq, %eff_mode, %c0_i32 : i32 // Per-request draft_depth override (field 4 of 20-byte sampling record): -1=auto, 1=fixed d1, >=2=fixed full %ad_rb = scalar.assume %ad_row [range(%ad_row, 0, 1048575)] : i32 %ad_ri = index.cast %ad_rb : i32 to index %ad_sh = index.constant 2048 : offset %ad_ss = index.constant 20 : offset %ad_srel = index.scale %ad_ri, %ad_ss : index, offset -> offset %ad_sat = index.add %ad_sh, %ad_srel : offset %ad_send = index.add %ad_sat, %ad_ss : offset %ad_slen = buffer.length %control_state %ad_sok = index.cmp ule, %ad_send, %ad_slen : offset %want_d1 = scf.if %ad_sok -> (i1) { %ad_srec = buffer.view %control_state[%ad_sat] : buffer -> view<5xi32> %req_dd_ad = view.load %ad_srec[4] : view<5xi32> -> i32 %dd_is_1 = scalar.cmpi eq, %req_dd_ad, %c1_ad_i32 : i32 %dd_ge_2 = scalar.cmpi sge, %req_dd_ad, %c2_ad_i32 : i32 %false_ad = scalar.constant false : i1 %true_ad = scalar.constant true : i1 %wd0 = scf.select %dd_ge_2, %false_ad, %auto_d1 : i1 %wd1 = scf.select %dd_is_1, %true_ad, %wd0 : i1 scf.yield %wd1 : i1 } else { scf.yield %auto_d1 : i1 } %w32 = scalar.extui %want_d1 : i1 to i32 scf.yield %w32 : i32 } else { scf.yield %c0_i32 : i32 } scf.yield %sel_d1 : i32 } else { scf.yield %c0_i32 : i32 } view.store %adapt_d1_i32, %adapt_hdr[4] : i32, view<5xi32> %adapt_d1 = scalar.cmpi ne, %adapt_d1_i32, %c0_i32 : i32 // cursors, flags and the total packed length %total = scf.for %s = [%c0 to %spans step %c1](%cursor = %c0_i32 : i32) -> (i32) { %sr = index.assume %s [lt(%s, %plan_extent)] : index %length_host = view.load %requests[%sr, 0] : view<[%plan_extent]x9xi32> -> i32 %length_g = scf.select %gate, %c1_i32, %length_host : i32 %length = scf.select %adapt_d1, %c2_ad_i32, %length_g : i32 %ov_len = scalar.ori %gate, %adapt_d1 : i1 scf.if %ov_len { view.store %length, %device[%s, 0] : i32, view<[%spans]x9xi32> } %flags = view.load %requests[%sr, 6] : view<[%plan_extent]x9xi32> -> i32 %credit = view.load %requests[%sr, 7] : view<[%plan_extent]x9xi32> -> i32 %speculative = scalar.cmpi ne, %credit, %c0_i32 : i32 %not_gate = scalar.cmpi eq, %flag_value, %c0_i32 : i32 %mark_spec = scalar.andi %speculative, %not_gate : i1 %ad_c2_min = scalar.minui %credit, %c2_ad_i32 : i32 %eff_credit = scf.select %adapt_d1, %ad_c2_min, %credit : i32 %credit_bits = scalar.shli %eff_credit, %c8_i32 : i32 %spec_bits = scalar.ori %credit_bits, %c4_i32 : i32 %marked = scalar.ori %flags, %spec_bits : i32 %device_flags = scf.select %mark_spec, %marked, %flags : i32 view.store %cursor, %device[%s, 4] : i32, view<[%spans]x9xi32> view.store %device_flags, %device[%s, 6] : i32, view<[%spans]x9xi32> %next = scalar.addi %cursor, %length : i32 scf.yield %next : i32 } // move inputs to their cursors (last span first), zero placeholders scf.for %r = [%c0 to %spans step %c1] { %last = index.sub %spans, %c1 : index %s = index.sub %last, %r : index %sr = index.assume %s [lt(%s, %plan_extent)] : index %sd = index.assume %s [lt(%s, %spans)] : index %length32_host = view.load %requests[%sr, 0] : view<[%plan_extent]x9xi32> -> i32 %length32_g = scf.select %gate, %c1_i32, %length32_host : i32 %length32 = scf.select %adapt_d1, %c2_ad_i32, %length32_g : i32 %begin32 = view.load %requests[%sr, 4] : view<[%plan_extent]x9xi32> -> i32 %known32 = view.load %requests[%sr, 5] : view<[%plan_extent]x9xi32> -> i32 %cursor32 = view.load %device[%sd, 4] : view<[%spans]x9xi32> -> i32 %moves = scalar.cmpi ne, %begin32, %cursor32 : i32 %length_b = scalar.assume %length32 [range(%length32, 0, 8192)] : i32 %known_b = scalar.assume %known32 [range(%known32, 0, 8192)] : i32 %begin_b = scalar.assume %begin32 [range(%begin32, 0, 8191)] : i32 %cursor_b = scalar.assume %cursor32 [range(%cursor32, 0, 8191)] : i32 %length = index.cast %length_b : i32 to index %known = index.cast %known_b : i32 to index %begin = index.cast %begin_b : i32 to index %cursor = index.cast %cursor_b : i32 to index scf.if %moves { scf.for %q = [%c0 to %known step %c1] { %kl = index.sub %known, %c1 : index %j = index.sub %kl, %q : index %src_t = index.add %begin, %j : index %dst_t = index.add %cursor, %j : index %src0 = index.mul %src_t, %stride : index %dst0 = index.mul %dst_t, %stride : index scf.for %w = [%c0 to %stride step %c1] { %si = index.add %src0, %w : index %di = index.add %dst0, %w : index %sb = index.assume %si [lt(%si, %words)] : index %db = index.assume %di [lt(%di, %words)] : index %v = view.load %flat[%sb] : view<[%words]xi32> -> i32 view.store %v, %flat[%db] : i32, view<[%words]xi32> } } } // placeholders after the known inputs (speculative spans): zero ids; media records keep text // positions continuing the anchor's (t = h = w) scf.for %j = [%known to %length step %c1] { %dst_t = index.add %cursor, %j : index %dst0 = index.mul %dst_t, %stride : index scf.for %w = [%c0 to %stride step %c1] { %di = index.add %dst0, %w : index %db = index.assume %di [lt(%di, %words)] : index view.store %c0_i32, %flat[%db] : i32, view<[%words]xi32> } scf.if %mixed { %anchor0 = index.mul %cursor, %stride : index %anchor4 = index.add %anchor0, %four : index %ab = index.assume %anchor4 [lt(%anchor4, %words)] : index %t0 = view.load %flat[%ab] : view<[%words]xi32> -> i32 %j32 = index.cast %j : index to i32 %tj = scalar.addi %t0, %j32 : i32 %five = index.constant 5 : index %six = index.constant 6 : index %p4 = index.add %dst0, %four : index %p5 = index.add %dst0, %five : index %p6 = index.add %dst0, %six : index %b4 = index.assume %p4 [lt(%p4, %words)] : index %b5 = index.assume %p5 [lt(%p5, %words)] : index %b6 = index.assume %p6 [lt(%p6, %words)] : index view.store %tj, %flat[%b4] : i32, view<[%words]xi32> view.store %tj, %flat[%b5] : i32, view<[%words]xi32> view.store %tj, %flat[%b6] : i32, view<[%words]xi32> } } } // the input transfer covers every packed slot %total64 = scalar.extui %total : i32 to i64 %stride_index = index.cast %stride : index to i64 %word64 = scalar.constant 4 : i64 %record_bytes = scalar.muli %stride_index, %word64 : i64 %input_length = scalar.muli %total64, %record_bytes : i64 %transfer_offset = index.constant 8 : offset %transfers = buffer.view %state[%transfer_offset] : buffer -> view<2x5xi64> view.store %input_length, %transfers[1, 4] : i64, view<2x5xi64> func.return %count, %state, %transfer_begin, %transfer_length : i32, buffer, i64, i64 } // Verified epochs: semantic progress comes from device acceptance (records of 10 i32: // {consumed, output_count, token[8]}); a speculative span's known count is zero. func.def public @publish_epoch(%control_state: buffer, %plan: buffer, %media_plan: buffer, %count: i32, %first_media_count: i32, %next_media_count: i32, %verified: i32, %continues: i32, %next_speculative: buffer, %ordinary: buffer, %first_results: buffer, %next_results: buffer, %progress: buffer) { func.call @plain_publish_epoch(%control_state, %plan, %media_plan, %count, %first_media_count, %next_media_count, %verified, %continues, %next_speculative, %ordinary, %first_results, %next_results, %progress) : (buffer, buffer, buffer, i32, i32, i32, i32, i32, buffer, buffer, buffer, buffer, buffer) %c0_i32 = scalar.constant 0 : i32 %verifies = scalar.cmpi ne, %verified, %c0_i32 : i32 scf.if %verifies { %origin = index.constant 0 : offset %c0 = index.constant 0 : index %c1 = index.constant 1 : index %c1_i32 = scalar.constant 1 : i32 %step = scalar.constant 2 : i32 %bounded_count = scalar.assume %count [range(%count, 1, 2147483647)] : i32 %length = index.cast %bounded_count : i32 to index %plan_extent = func.call @plan_capacity(%plan) : (buffer) -> (index) %requests = buffer.view %plan[%origin] : buffer -> view<[%plan_extent]x9xi32> %records = buffer.view %first_results[%origin] : buffer -> view<[%length]x10xi32> %published = buffer.view %progress[%origin] : buffer -> view<[%length]x21xi32> scf.for %i = [%c0 to %length step %c1] { %ir = index.assume %i [lt(%i, %plan_extent)] : index %consumed = view.load %records[%i, 0] : view<[%length]x10xi32> -> i32 %outputs = view.load %records[%i, 1] : view<[%length]x10xi32> -> i32 %credit = view.load %requests[%ir, 7] : view<[%plan_extent]x9xi32> -> i32 %speculative = scalar.cmpi ne, %credit, %c0_i32 : i32 %known = scf.select %speculative, %c0_i32, %consumed : i32 %verification = scalar.extui %speculative : i1 to i32 view.store %consumed, %published[%i, 0] : i32, view<[%length]x21xi32> view.store %known, %published[%i, 1] : i32, view<[%length]x21xi32> view.store %outputs, %published[%i, 2] : i32, view<[%length]x21xi32> view.store %verification, %published[%i, 3] : i32, view<[%length]x21xi32> scf.if %speculative { %source = view.load %requests[%ir, 2] : view<[%plan_extent]x9xi32> -> i32 %advance = scalar.muli %consumed, %step : i32 %next_source = scalar.addi %source, %advance : i32 view.store %next_source, %published[%i, 4] : i32, view<[%length]x21xi32> } // update adaptive draft depth EMA on single-stream verified speculative span %ad_hdr_at = index.constant 1828 : offset %ad_hdr = buffer.view %control_state[%ad_hdr_at] : buffer -> view<5xi32> %ad_en = view.load %ad_hdr[0] : view<5xi32> -> i32 %ad_on0 = scalar.cmpi ne, %ad_en, %c0_i32 : i32 %ad_one = scalar.cmpi eq, %count, %c1_i32 : i32 %ad_c2 = scalar.constant 2 : i32 %ad_deep = scalar.cmpi ugt, %credit, %ad_c2 : i32 %ad_u0 = scalar.andi %ad_on0, %ad_one : i1 %ad_u1 = scalar.andi %ad_u0, %ad_deep : i1 scf.if %ad_u1 { %ad_act = view.load %ad_hdr[4] : view<5xi32> -> i32 %ad_was_d1 = scalar.cmpi ne, %ad_act, %c0_i32 : i32 %ad_len_h = view.load %requests[%ir, 0] : view<[%plan_extent]x9xi32> -> i32 %ad_cap = scalar.minui %ad_len_h, %credit : i32 %ad_full_d = scalar.subi %ad_cap, %c1_i32 : i32 %ad_used = scf.select %ad_was_d1, %c1_i32, %ad_full_d : i32 %ad_pos = view.load %requests[%ir, 1] : view<[%plan_extent]x9xi32> -> i32 %ad_row = view.load %requests[%ir, 3] : view<[%plan_extent]x9xi32> -> i32 %ad_rmask = scalar.constant 15 : i32 %ad_rs32 = scalar.andi %ad_row, %ad_rmask : i32 %ad_rsb = scalar.assume %ad_rs32 [range(%ad_rs32, 0, 15)] : i32 %ad_rs = index.cast %ad_rsb : i32 to index %ad_rat = index.constant 1848 : offset %ad_rv = buffer.view %control_state[%ad_rat] : buffer -> view<16x3xi32> %old_ema = view.load %ad_rv[%ad_rs, 0] : view<16x3xi32> -> i32 %old_mode = view.load %ad_rv[%ad_rs, 1] : view<16x3xi32> -> i32 %old_pos = view.load %ad_rv[%ad_rs, 2] : view<16x3xi32> -> i32 %no_p = scalar.cmpi eq, %old_pos, %c0_i32 : i32 %rew = scalar.cmpi ne, %ad_pos, %old_pos : i32 %rst = scalar.ori %no_p, %rew : i1 %c512 = scalar.constant 512 : i32 %base_ema = scf.select %rst, %c512, %old_ema : i32 %base_mode = scf.select %rst, %c0_i32, %old_mode : i32 // exact per-trial MLE EMA: trials = min(consumed, used_depth), succ = max(0, consumed - 1) %c_gt0 = scalar.cmpi ugt, %consumed, %c0_i32 : i32 %c_sub1 = scalar.subi %consumed, %c1_i32 : i32 %succ = scf.select %c_gt0, %c_sub1, %c0_i32 : i32 %tr0 = scalar.minui %consumed, %ad_used : i32 %c7_ad = scalar.constant 7 : i32 %tr = scalar.minui %tr0, %c7_ad : i32 %c8_ad = scalar.constant 8 : i32 %keep = scalar.subi %c8_ad, %tr : i32 %c1024 = scalar.constant 1024 : i32 %c3_sh = scalar.constant 3 : i32 %term_old = scalar.muli %keep, %base_ema : i32 %term_new = scalar.muli %succ, %c1024 : i32 %ema_sum = scalar.addi %term_old, %term_new : i32 %new_ema = scalar.shrui %ema_sum, %c3_sh : i32 // hysteresis thresholds (stricter at long context >= 16384) %c_ctx_th = scalar.constant 16384 : i32 %is_lctx = scalar.cmpi uge, %ad_pos, %c_ctx_th : i32 %up_s = scalar.constant 640 : i32 %dn_s = scalar.constant 560 : i32 %up_l = scalar.constant 850 : i32 %dn_l = scalar.constant 770 : i32 %th_up = scf.select %is_lctx, %up_l, %up_s : i32 %th_dn = scf.select %is_lctx, %dn_l, %dn_s : i32 %go_up = scalar.cmpi uge, %new_ema, %th_up : i32 %go_dn = scalar.cmpi ult, %new_ema, %th_dn : i32 %m_up = scf.select %go_up, %c1_i32, %base_mode : i32 %new_mode = scf.select %go_dn, %c0_i32, %m_up : i32 %end_pos = scalar.addi %ad_pos, %consumed : i32 view.store %new_ema, %ad_rv[%ad_rs, 0] : i32, view<16x3xi32> view.store %new_mode, %ad_rv[%ad_rs, 1] : i32, view<16x3xi32> view.store %end_pos, %ad_rv[%ad_rs, 2] : i32, view<16x3xi32> } %bounded_outputs = scalar.assume %outputs [range(%outputs, 0, 8)] : i32 %output_extent = index.cast %bounded_outputs : i32 to index %c2 = index.constant 2 : index %c5 = index.constant 5 : index scf.for %j = [%c0 to %output_extent step %c1] { %src = index.add %j, %c2 : index %dst = index.add %j, %c5 : index %src_b = index.assume %src [range(%src, 2, 9)] : index %dst_b = index.assume %dst [range(%dst, 5, 12)] : index %token = view.load %records[%i, %src_b] : view<[%length]x10xi32> -> i32 view.store %token, %published[%i, %dst_b] : i32, view<[%length]x21xi32> } } } func.return } // propose (when any span proposes) -> verify or advance -> drafter catch-up. Stage tables: advance at // 128, verify at 640, warm at 1152; first proposal stage, level count and draft kind at 1664. func.def public @epoch(%control_state: buffer, %shape: i32, %proposes: i32, %verifies: i32, %span_count: i32, %output_count: i32, %epoch_count: i32, %continuation_shape: i32, %continuation_proposes: i32, %first_media_count: i32, %next_media_count: i32, %residual: hal.buffer, %metadata: hal.buffer, %origins: hal.buffer, %state: hal.buffer, %tokens: hal.buffer, %outputs: hal.buffer, %carry: hal.buffer, %committed: hal.buffer, %results: hal.buffer, %draft_cache: hal.buffer, %draft_origins: hal.buffer, %next_results: hal.buffer) -> (i64) { %c0 = scalar.constant 0 : i32 %c1 = scalar.constant 1 : i32 %c0_i64 = scalar.constant 0 : i64 %bounded_shape = scalar.assume %shape [range(%shape, 0, 127)] : i32 %shape_index = index.cast %bounded_shape : i32 to index %advance_at = index.constant 128 : offset %verify_at = index.constant 640 : offset %warm_at = index.constant 1152 : offset %draft_at = index.constant 1664 : offset %advance_view = buffer.view %control_state[%advance_at] : buffer -> view<128xi32> %verify_view = buffer.view %control_state[%verify_at] : buffer -> view<128xi32> %warm_view = buffer.view %control_state[%warm_at] : buffer -> view<128xi32> %draft_view = buffer.view %control_state[%draft_at] : buffer -> view<3xi32> %propose_first = view.load %draft_view[0] : view<3xi32> -> i32 %levels = view.load %draft_view[1] : view<3xi32> -> i32 %kind = view.load %draft_view[2] : view<3xi32> -> i32 %drafting = scalar.cmpi ne, %kind, %c0 : i32 %propose = scalar.cmpi ne, %proposes, %c0 : i32 %verify = scalar.cmpi ne, %verifies, %c0 : i32 %flag_at = index.constant 1676 : offset %flag_view = buffer.view %control_state[%flag_at] : buffer -> view<1xi32> %flag = view.load %flag_view[0] : view<1xi32> -> i32 %gated = scalar.cmpi ne, %flag, %c0 : i32 %flag_low_mask = scalar.constant 255 : i32 %flag_low = scalar.andi %flag, %flag_low_mask : i32 %flag_nz = scf.select %gated, %flag_low, %c1 : i32 %flag_idx = scalar.subi %flag_nz, %c1 : i32 %flag_shift = scalar.constant 8 : i32 %gated_stage = scalar.shrui %flag, %flag_shift : i32 %flag_b = scalar.assume %flag_idx [range(%flag_idx, 0, 127)] : i32 %flag_shape = index.cast %flag_b : i32 to index %eff_index = scf.select %gated, %flag_shape, %shape_index : index %ungated = scalar.cmpi eq, %flag, %c0 : i32 %propose_on = scalar.andi %propose, %ungated : i1 %ad_hdr_at = index.constant 1828 : offset %ad_hdr = buffer.view %control_state[%ad_hdr_at] : buffer -> view<5xi32> %ad_pstage_d1 = view.load %ad_hdr[1] : view<5xi32> -> i32 %ad_vstage_d1 = view.load %ad_hdr[2] : view<5xi32> -> i32 %ad_wstage_d1 = view.load %ad_hdr[3] : view<5xi32> -> i32 %ad_act = view.load %ad_hdr[4] : view<5xi32> -> i32 %ad_d1 = scalar.cmpi ne, %ad_act, %c0 : i32 scf.if %propose_on { %bits = scalar.constant 32 : i32 %below = scalar.subi %span_count, %c1 : i32 %leading = scalar.ctlzi %below : i32 %level = scalar.subi %bits, %leading : i32 %last = scalar.subi %levels, %c1 : i32 %chosen = scalar.minui %level, %last : i32 %pstage_raw = scalar.addi %propose_first, %chosen : i32 %pstage = scf.select %ad_d1, %ad_pstage_d1, %pstage_raw : i32 %proposal = func.call @execute_5(%pstage, %metadata, %tokens, %draft_origins, %draft_cache, %carry) : (i32, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer) -> (i64) } %target = scf.if %verify -> (i64) { %vstage_table = view.load %verify_view[%eff_index] : view<128xi32> -> i32 %vstage_g = scf.select %gated, %gated_stage, %vstage_table : i32 %vstage = scf.select %ad_d1, %ad_vstage_d1, %vstage_g : i32 %completion = func.call @execute_7(%vstage, %residual, %metadata, %origins, %state, %tokens, %committed, %results) : (i32, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer) -> (i64) %count64 = scalar.extui %span_count : i32 to i64 %record = scalar.constant 40 : i64 %length = scalar.muli %count64, %record : i64 %feedback = func.call @feedback(%c1, %results, %c0_i64, %length) : (i32, hal.buffer, i64, i64) -> (i64) scf.yield %completion : i64 } else { %astage = view.load %advance_view[%eff_index] : view<128xi32> -> i32 %completion = func.call @execute_6(%astage, %residual, %metadata, %origins, %state, %tokens, %outputs) : (i32, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer) -> (i64) %count64 = scalar.extui %span_count : i32 to i64 %word = scalar.constant 4 : i64 %length = scalar.muli %count64, %word : i64 %feedback = func.call @feedback(%c0, %outputs, %c0_i64, %length) : (i32, hal.buffer, i64, i64) -> (i64) scf.yield %completion : i64 } %done = scf.if %drafting -> (i64) { %accepted = scf.if %verify -> (hal.buffer) { scf.yield %committed : hal.buffer } else { scf.yield %metadata : hal.buffer } %wstage_raw = view.load %warm_view[%eff_index] : view<128xi32> -> i32 %wstage = scf.select %ad_d1, %ad_wstage_d1, %wstage_raw : i32 %warm = func.call @execute_6(%wstage, %residual, %accepted, %draft_origins, %draft_cache, %carry, %tokens) : (i32, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer) -> (i64) scf.yield %warm : i64 } else { scf.yield %target : i64 } func.return %done : i64 }
func.decl import("input") @require(%condition: i32, %diagnostic: buffer)
func.decl import("input") @encode(%text: buffer, %flags: i32, %capacity: i64) -> (buffer, i64)
func.decl import("json") @members(%json: buffer) -> (buffer, i32, i64)
func.decl import("json") @members_range(%json: buffer, %offset: i64, %length: i64) -> (buffer, i32, i64)
func.decl import("json") @compact(%text: buffer) -> (buffer)
func.decl import("json") @unescape(%source: buffer, %offset: i64, %length: i64, %target: buffer, %target_offset: i64) -> (i64)
func.decl import("tools") @parse_xml(%schemas: buffer, %response: buffer, %truncated: i64) -> (buffer)
func.decl import("runner") @execute_5(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer) -> (i64)
func.decl import("runner") @execute_7(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer, %b5: hal.buffer, %b6: hal.buffer) -> (i64)
func.decl import("runner") @execute_4(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer) -> (i64)
func.decl import("runner") @execute_6(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer, %b5: hal.buffer) -> (i64)
func.decl import("runner") @feedback(%slot: i32, %source: hal.buffer, %offset: i64, %length: i64) -> (i64)
global.rodata.def @name = bytes("496e6b6c696e672d322d4d696e69")
global.rodata.def @empty = bytes("")
global.rodata.def @no_tools = bytes("5b5d")
global.rodata.def @assistant = bytes("617373697374616e74")
global.rodata.def @user = bytes("75736572")
global.rodata.def @system = bytes("73797374656d")
global.rodata.def @tool_role = bytes("746f6f6c")
// "role", "content", "messages", "enable_thinking", "chat_template_kwargs", "reasoning_effort", "none"
global.rodata.def @role_key = bytes("726f6c65")
global.rodata.def @content_key = bytes("636f6e74656e74")
global.rodata.def @messages_key = bytes("6d65737361676573")
global.rodata.def @thinking_key = bytes("656e61626c655f7468696e6b696e67")
global.rodata.def @kwargs_key = bytes("636861745f74656d706c6174655f6b7761726773")
global.rodata.def @effort_key = bytes("726561736f6e696e675f6566666f7274")
global.rodata.def @effort_none = bytes("6e6f6e65")
global.rodata.def @newline = bytes("0a")
// "unsupported message: roles are system/user/assistant/tool, content is text (no images)"
global.rodata.def @invalid = bytes("756e737570706f72746564206d6573736167653a20726f6c6573206172652073797374656d2f757365722f617373697374616e742f746f6f6c2c20636f6e74656e74206973207465787420286e6f20696d6167657329")
// "llm requires pooled epochs without MTP"
global.rodata.def @unsupported = bytes("6c6c6d20726571756972657320706f6f6c65642065706f63687320776974686f7574204d5450")
// "input exceeds the available context size"
global.rodata.def @overflow = bytes("696e70757420657863656564732074686520617661696c61626c6520636f6e746578742073697a65")
func.def @check(%condition: i1) {
%value = scalar.extui %condition : i1 to i32
%error = global.load @invalid : buffer
func.call @require(%value, %error) : (i32, buffer)
func.return
}
func.def @equal(%a: buffer, %b: buffer) -> (i1) {
%a_length = buffer.length %a
%b_length = buffer.length %b
%same_length = index.cmp eq, %a_length, %b_length : offset
%same = scf.if %same_length -> (i1) {
%c0 = index.constant 0 : offset
%compared = buffer.compare %a[%c0], %b[%c0], %a_length
%c0_i32 = scalar.constant 0 : i32
%matches = scalar.cmpi eq, %compared, %c0_i32 : i32
scf.yield %matches : i1
} else {
%false = scalar.constant false : i1
scf.yield %false : i1
}
func.return %same : i1
}
func.def @join(%a: buffer, %b: buffer) -> (buffer) {
%c0 = index.constant 0 : offset
%a_length = buffer.length %a
%b_length = buffer.length %b
%length = index.add %a_length, %b_length : offset
%output = buffer.alloca<private> align(1) %length : buffer
buffer.copy %a[%c0], %output[%c0], %a_length
buffer.copy %b[%c0], %output[%a_length], %b_length
func.return %output : buffer
}
func.def @join4(%a: buffer, %b: buffer, %c: buffer, %d: buffer) -> (buffer) {