AnyDSL / impala

An imperative and functional programming language

Home Page:https://anydsl.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fixed size arrays broken with -O3

stlemme opened this issue · comments

The following code leads to correct results with -O2 but wrong with -O3

fn main() -> () {
    let N = 4;
    let mut A = [N:i32];
    let mut B = ~[N:i32];
    let mut C = [0, 0, 0, 0];
    let mut D = [N:i32];
    
    for i in $range(0, N) {
        A(i) = i*i;
        B(i) = i*i;
        C(i) = i*i;
    }
    
    D(0) = 0;
    D(1) = 1;
    D(2) = 4;
    D(3) = 9;
    // D(4) = 16; // should break, but doesn't
    
    print_string("A: "); print_array(bitcast[&[i32]](&A), N); print_string("\n");
    print_string("B: "); print_array(bitcast[&[i32]]( B), N); print_string("\n");
    print_string("C: "); print_array(bitcast[&[i32]](&C), N); print_string("\n");
    print_string("D: "); print_array(bitcast[&[i32]](&D), N); print_string("\n");
}

fn print_array(arr: &[i32], num: i32) -> () {
    print_string("["); print_int(arr(0));
    for i in $range(1, num) {
        print_string(", "); print_int(arr(i));
    }
    print_string("]");
}

Output with -O3:

A: [0, 0, 0, 0]
B: [0, 1, 4, 9]
C: [0, 1, 4, 9]
D: [0, 0, 0, 0]
let mut A = [N:i32];

creates an indefinite array on the stack. Code generation is currently broken for this case. This is a known issue. You either have to use a heap allocated indefinite array or a stack-allocated definite array.

I'll keep this bug report open, until this issue is fixed. But it will take a while.