Compiler generates invalid LLVM IR for if/else with constant function in then branch #338

Closed
opened 2026-01-26 20:51:37 +00:00 by navicore · 1 comment
navicore commented 2026-01-26 20:51:37 +00:00 (Migrated from github.com)

Summary

Seq 3.0.2 generates invalid LLVM IR when an if/else statement has a simple constant-returning function call as the only expression in the then branch. The generated IR is missing a terminator instruction (branch) before the else label.

Minimal Reproduction

: edit-done ( -- Int ) 0 ;

: simple-loop ( Int Int -- Int )
  over 0 i.< if
    drop drop edit-done
  else
    swap 1 i.add swap
    simple-loop
  then ;

: main ( -- )
  0 0 simple-loop drop ;

Error

Error: Clang compilation failed:
/tmp/seq_bug_repro.ll:367:1: error: expected instruction opcode
  367 | if_else4:
      | ^
1 error generated.

Generated LLVM IR (around the error)

The if_then block ends with a call to seq_edit_done_to_i() but has no terminator (branch instruction) before the if_else label:

if_then4:
  %23 = call ptr @patch_seq_drop_op(ptr %21)
  %24 = call ptr @patch_seq_drop_op(ptr %23)
  %25 = call i64 @seq_edit_done_to_i()
if_else4:                          ; <-- Missing terminator before this
  ...

Expected Behavior

The compiler should generate a branch instruction after the call to return/exit the if block before the else label.

Environment

  • Seq version: 3.0.2
  • OS: macOS Darwin 25.2.0
## Summary Seq 3.0.2 generates invalid LLVM IR when an if/else statement has a simple constant-returning function call as the only expression in the then branch. The generated IR is missing a terminator instruction (branch) before the else label. ## Minimal Reproduction ```seq : edit-done ( -- Int ) 0 ; : simple-loop ( Int Int -- Int ) over 0 i.< if drop drop edit-done else swap 1 i.add swap simple-loop then ; : main ( -- ) 0 0 simple-loop drop ; ``` ## Error ``` Error: Clang compilation failed: /tmp/seq_bug_repro.ll:367:1: error: expected instruction opcode 367 | if_else4: | ^ 1 error generated. ``` ## Generated LLVM IR (around the error) The `if_then` block ends with a call to `seq_edit_done_to_i()` but has no terminator (branch instruction) before the `if_else` label: ```llvm if_then4: %23 = call ptr @patch_seq_drop_op(ptr %21) %24 = call ptr @patch_seq_drop_op(ptr %23) %25 = call i64 @seq_edit_done_to_i() if_else4: ; <-- Missing terminator before this ... ``` ## Expected Behavior The compiler should generate a branch instruction after the call to return/exit the if block before the else label. ## Environment - Seq version: 3.0.2 - OS: macOS Darwin 25.2.0
navicore commented 2026-01-26 21:35:08 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/339
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
navicore/patch-seq#338
No description provided.