Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler fails to hoist loads -> bad codegen #4620

Open
Waqar144 opened this issue Dec 24, 2024 · 0 comments
Open

Compiler fails to hoist loads -> bad codegen #4620

Waqar144 opened this issue Dec 24, 2024 · 0 comments

Comments

@Waqar144
Copy link
Contributor

Context

Consider the following simple code:

@require
test_fn :: proc(s1: string, s2: string) -> int #no_bounds_check {
    if len(s1) != len(s2) do return 0

    val := make([]int, len(s1))
    for i := 0; i < len(s1); i += 1 {
        for j := 0; j < len(s2); j += 1 {
            val[i] = s1[i] != s2[j] ? 1 : 0
        }
    }
    return val[len(s1) - 1];
}

The line val[i] = s1[i] != s2[j] ? 1 : 0 loads s1[i] and s2[j] from memory every single time even though s1[i] can be hoisted

Link to compiler explorer: https://compiler-explorer.com/z/bsM33rc35

  • Operating System & Odin Version: Linux odin-dev-2012
  • Please paste odin report output:
Odin:    dev-2024-12-nightly
OS:      Manjaro Linux, Linux 6.6.63-1-MANJARO
CPU:     AMD Ryzen 7 4700U with Radeon Graphics         
RAM:     15355 MiB
Backend: LLVM 18.1.6

Expected Behavior

Loads are hoisted such that the code becomes equivalent to:

    for i := 0; i < len(s1); i += 1 {
        ch := s1[i]
        for j := 0; j < len(s2); j += 1 {
            val[i] = ch != s2[j] ? 1 : 0
        }
    }

Current Behavior

Loads are not hoisted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant