Odin icon indicating copy to clipboard operation
Odin copied to clipboard

wrapping `string` in `any` fails codegen

Open Carlyle-Foster opened this issue 5 months ago • 9 comments

package test_case_1

import "core:fmt"

main :: proc(){
    fmt.printfln("{}", true ? any(52) : any("blah"))
}

result from odin build test_case_1:

src/llvm_backend_const.cpp(769): Assertion Failure: `is_type_string(original_type)` 
Illegal instruction (core dumped)
package test_case_2

import "core:fmt"

main :: proc(){
    fmt.printfln("{}", true ? any(52) : any(42))
}

odin build test_case_2:

LLVM CODE GEN FAILED FOR PROCEDURE: test_case_2::main
define void @"test_case_2::main"(ptr noalias nocapture nonnull %__.context_ptr) {
decls:
  %0 = alloca { ptr, i64 }, align 8
  %1 = alloca [16 x i8], align 16
  br label %entry

entry:                                            ; preds = %decls
  br i1 true, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  br label %if.done

if.else:                                          ; preds = %entry
  br label %if.done

if.done:                                          ; preds = %if.else, %if.then
  %2 = phi %..any [ false, %if.then ], [ false, %if.else ]
  call void @llvm.memset.inline.p0.i64(ptr %0, i8 0, i64 16, i1 false)
  call void @llvm.memset.inline.p0.i64(ptr %1, i8 0, i64 16, i1 false)
  %3 = getelementptr [1 x %..any], ptr %1, i64 0, i64 0
  store %..any %2, ptr %3, align 8
  %4 = getelementptr [1 x %..any], ptr %1, i64 0, i64 0
  %5 = getelementptr inbounds { ptr, i64 }, ptr %0, i32 0, i32 0
  store ptr %4, ptr %5, align 8
  %6 = getelementptr inbounds { ptr, i64 }, ptr %0, i32 0, i32 1
  store i64 1, ptr %6, align 8
  %7 = load { ptr, i64 }, ptr %0, align 8
  %8 = call i64 @"fmt::printfln"(%..string { ptr @"csbs$test_case_2-test_case_2$0", i64 2 }, { ptr, i64 } %7, i8 1, ptr %__.context_ptr)
  ret void
}




PHI node operands are not the same type as the result!
  %2 = phi %..any [ false, %if.then ], [ false, %if.else ]

odin report (paraphrased) =

        Odin:    dev-2025-07:a93d3503b
        OS:      Linux Mint 22, Linux 6.8.0-62-generic
        CPU:     12th Gen Intel(R) Core(TM) i5-12400F
        RAM:     15825 MiB
        Backend: LLVM 18.1.3

Carlyle-Foster avatar Jul 26 '25 07:07 Carlyle-Foster

bypassing the assertion mentioned in the output of test_case_1 (src/llvm_backend_const.cpp(769)) results in a codegen failure similar to test_case_2:

LLVM CODE GEN FAILED FOR PROCEDURE: test_case_2::main
define void @"test_case_2::main"(ptr noalias nocapture nonnull %__.context_ptr) {
decls:
  %0 = alloca { ptr, i64 }, align 8
  %1 = alloca [16 x i8], align 16
  br label %entry

entry:                                            ; preds = %decls
  br i1 true, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  br label %if.done

if.else:                                          ; preds = %entry
  br label %if.done

if.done:                                          ; preds = %if.else, %if.then
  %2 = phi %..any [ false, %if.then ], [ false, %if.else ]
  call void @llvm.memset.inline.p0.i64(ptr %0, i8 0, i64 16, i1 false)
  call void @llvm.memset.inline.p0.i64(ptr %1, i8 0, i64 16, i1 false)
  %3 = getelementptr [1 x %..any], ptr %1, i64 0, i64 0
  store %..any %2, ptr %3, align 8
  %4 = getelementptr [1 x %..any], ptr %1, i64 0, i64 0
  %5 = getelementptr inbounds { ptr, i64 }, ptr %0, i32 0, i32 0
  store ptr %4, ptr %5, align 8
  %6 = getelementptr inbounds { ptr, i64 }, ptr %0, i32 0, i32 1
  store i64 1, ptr %6, align 8
  %7 = load { ptr, i64 }, ptr %0, align 8
  %8 = call i64 @"fmt::printfln"(%..string { ptr @"csbs$test_case_2-test_case_2$0", i64 2 }, { ptr, i64 } %7, i8 1, ptr %__.context_ptr)
  ret void
}




PHI node operands are not the same type as the result!
  %2 = phi %..any [ false, %if.then ], [ false, %if.else ]

Carlyle-Foster avatar Jul 26 '25 07:07 Carlyle-Foster

Even with just this:

package test_issues

main :: proc(){
    any("asdfa")
}

I get:

src/llvm_backend_const.cpp(769): Assertion Failure: `is_type_string(original_type)`
Illegal instruction (core dumped)

odin report:

        Odin:    dev-2025-07:be3006dbf
        OS:      Ubuntu 24.04.2 LTS, Linux 6.6.87.2-microsoft-standard-WSL2
        CPU:     Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
        RAM:     7843 MiB
        Backend: LLVM 19.1.1

Paul-Andre avatar Jul 28 '25 21:07 Paul-Andre

@Paul-Andre bypassing the assert in that simpler case doesn't fail codegen but instead silently removes the expression, u can tell if u try to use the expression as in:

package test_case

import "core:fmt"

main :: proc() {
    fmt.println(any("blah"))
}

Carlyle-Foster avatar Jul 28 '25 23:07 Carlyle-Foster

@Paul-Andre bypassing the assert in that simpler case doesn't fail codegen but instead silently removes the expression, u can tell if u try to use the expression as in:

When I do that it just doesn't print anything, or perhaps prints the empty string.

It looks like Odin does not currently correctly codegen any(constant)

package test_issues
import "core:fmt"
import "core:reflect"

a_var_global := 123
a_const_global :: 123

main :: proc(){
    {
        a_var_local := 123;
        a:=any(a_var_local)
        a_unpacked,a_valid:=reflect.as_int(a);
        fmt.println(a_unpacked,a_valid); // 123 true
    }
    {
        a:=any(a_var_global)
        a_unpacked,a_valid:=reflect.as_int(a);
        fmt.println(a_unpacked,a_valid); // 123 true
    }
    {
        a_const_local :: 123
        a:=any(a_const_local)
        a_unpacked,a_valid:=reflect.as_int(a);
        fmt.println(a_unpacked,a_valid); // 0 false
    }
    {
        a:=any(a_const_global)
        a_unpacked,a_valid:=reflect.as_int(a);
        fmt.println(a_unpacked,a_valid); // 0 false
    }
    {
        a:=any(123)
        a_unpacked,a_valid:=reflect.as_int(a);
        fmt.println(a_unpacked,a_valid); // 0 false
    }
}

Going through the code with a debugger, I see that any(123) ends up getting a type of any, while having a value of ExactValue_Integer 123. Which then seems to confuse the function gb_internal lbValue lb_build_expr_internal

Paul-Andre avatar Jul 28 '25 23:07 Paul-Andre

When I do that it just doesn't print anything, or perhaps prints the empty string.

that's what happens when u don't pass any args to fmt.println, hence i think it removed the expression

Carlyle-Foster avatar Jul 29 '25 03:07 Carlyle-Foster

i'm pretty sure that an any should never have an ExactValue, even when it's wrapping a comptime-known expression the expression still has to be loaded to a stack variable with a runtime-known address to provide the necessary indirection, that's what i see happening when i look at the LLVM generated by a working use of fmt.println like:

package working

import "core:fmt"

main :: proc() {
    fmt.println("hullo")
}

Carlyle-Foster avatar Jul 29 '25 08:07 Carlyle-Foster

So what exactly should the semantics of any(123) or any("hullo") be?

The way variable:=123; any(variable) works is that it takes the address of the variable, which means the stack address if the variable was declared on the stack.

~I think it's sensible when you do any(123) or any("hullo"), then the 123 and "hullo" should be stored as a global variable, but then the behavior will become inconsistent and potentially confusing.~

Ah... Ok, the Odin overview literally writes what the semantics should be: https://odin-lang.org/docs/overview/#any-type "Note: The any value is only valid for as long as the underlying data is still valid. Passing a literal to an any will allocate the literal in the current stack frame."

Paul-Andre avatar Aug 02 '25 19:08 Paul-Andre

Side note: any is interesting because it's an undercover way of passing something by reference, even though nothing else is passed by reference.

Paul-Andre avatar Aug 02 '25 20:08 Paul-Andre

It should put it on the stack yes.

laytan avatar Aug 03 '25 11:08 laytan