chsasank / llama.lisp

Lisp dialect designed for HPC and AI
GNU Lesser General Public License v2.1
15 stars 6 forks source link

Variable redeclaration with a different type doesn't take effect #7

Closed GlowingScrewdriver closed 5 months ago

GlowingScrewdriver commented 5 months ago

Currently, llvm.py creates an alloca instruction the first time it encounters every variable, and stores a pointer to the allocated memory in the symbol table. This means that if a variable is re-declared with a different type, the compiler runs into an error since the variable's type doesn't get updated in the symbol table.

For example, consider the following bril program:

@print(n: int): int {
}
@main: int {
  tmp: bool = const false;
  tmp: int = const 2;
  tmp: void = call @print tmp;
  ret tmp;
}

This will cause a error since the symbol entry for variable tmp is never updated to hold an integer pointer (i32* in LLVM), and the following error is thrown by the LLVMLite IR builder:

TypeError: cannot store i32 to i1*: mismatching types
chsasank commented 5 months ago

Can you check how pykaleidoscope folks deal with this? Or see other documentation

chsasank commented 5 months ago

If the type is new, may be create new alloca? Check what C is doing.

GlowingScrewdriver commented 5 months ago

Can you check how pykaleidoscope folks deal with this? Or see other documentation

Kaleidoscope has only one type, which is double. So no issue there.

If the type is new, may be create new alloca? Check what C is doing.

C spec doesn't allow redeclaration at all (regardless of type). The following code won't compile:

int main () {
    int a = 5;
    int a = 6;     // error: redefinition of ‘a’
    char a = '5';  // error: conflicting types for ‘a’; have ‘char’
    return a;
}
GlowingScrewdriver commented 5 months ago

If the type is new, may be create new alloca? Check what C is doing.

I suppose we can create a new alloca and number the variable name.

GlowingScrewdriver commented 5 months ago

One solution is to overwrite the symbol table entry with the new pointer, while letting LLVMLite handle numbering