Skip to content

-gen llvm: array writes inside a FOR loop don't advance the element address (every element reads back the last iteration's value) #478

Description

@dalibor31

fbc version: 1.10.1 (2023-12-24), linux-x86_64 (official SourceForge binary release, checksum-verified against sha1sums.txt)

Command: fbc -gen llvm -O 0 repro.bas -x repro (also reproduces with -O 2)

Minimal repro

dim shared a(1) as long
dim i as long
for i = 0 to 1
    a(i) = i + 1
next i
print a(0)
print a(1)

Expected output

 1
 2

(this is exactly what -gen gcc produces for the identical source, at both -O 0 and -O 2)

Actual output with -gen llvm

 2
 0

a(0) comes back holding the last iteration's value, and a(1) comes back as 0 (its true zero-initialized value) — meaning both loop iterations wrote to the same element (element 0), and element 1 was never actually written at all.

This reproduces identically at -O 0 and -O 2, so it is not an LLVM optimizer artifact — the IR that fbc itself emits for the array-element address inside the loop does not appear to depend on the loop variable's current value per iteration. It also reproduces the same way with a longer loop (every element ends up holding the last iteration's value) and with a plain, non-shared module-level array.

-gen gcc (the default backend) is unaffected and produces correct output for this exact same source.

Found while benchmarking fbc against another LLVM-emitting BASIC compiler for an unrelated project.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions