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

ASR: Bug in explicit loop var #2236

Closed
Shaikh-Ubaid opened this issue Jul 31, 2023 · 2 comments · Fixed by #2237
Closed

ASR: Bug in explicit loop var #2236

Shaikh-Ubaid opened this issue Jul 31, 2023 · 2 comments · Fixed by #2237

Comments

@Shaikh-Ubaid
Copy link
Collaborator

          >Does it work with llvm?

It does not work with LLVM yet. I will create an issue for it soon.

Originally posted by @Shaikh-Ubaid in #1840 (comment)

@Shaikh-Ubaid
Copy link
Collaborator Author

Shaikh-Ubaid commented Jul 31, 2023

Minimal Reproducible Example:

$ cat examples/expr2.py                 
from lpython import i32

def main0():
    points: list[tuple[i32, i32]] = [(445, 193), (138, 28), (418, 279)]
    point: tuple[i32, i32]
    for point in points:
        print(point)


main0()
$ python examples/expr2.py                 
(445, 193)
(138, 28)
(418, 279)
$ lpython examples/expr2.py            
; ModuleID = 'LFortran'
source_filename = "LFortran"

%tuple = type { i32, i32 }
%list = type { i32, i32, %tuple* }

@0 = private unnamed_addr constant [22 x i8] c"IndexError: %s%d%s%d\0A\00", align 1
@1 = private unnamed_addr constant [48 x i8] c"List index is out of range. Index range is (0, \00", align 1
@2 = private unnamed_addr constant [27 x i8] c"), but the given index is \00", align 1
@3 = private unnamed_addr constant [2 x i8] c" \00", align 1
@4 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@5 = private unnamed_addr constant [2 x i8] c"(\00", align 1
@6 = private unnamed_addr constant [5 x i8] c"%s%s\00", align 1
@7 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@8 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@9 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@10 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@11 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@12 = private unnamed_addr constant [3 x i8] c", \00", align 1
@13 = private unnamed_addr constant [5 x i8] c"%s%s\00", align 1
@14 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@15 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@16 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@17 = private unnamed_addr constant [2 x i8] c" \00", align 1
@18 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@19 = private unnamed_addr constant [2 x i8] c")\00", align 1
@20 = private unnamed_addr constant [5 x i8] c"%s%s\00", align 1

define void @__module___main_____main____global_statements() {
.entry:
  call void @__module___main___main0()
  br label %return

return:                                           ; preds = %.entry
  ret void
}

define void @__module___main___main0() {
.entry:
  %0 = alloca i32, align 4
  %__explicit_iterator = alloca i32, align 4
  %point = alloca %tuple, align 8
  %points = alloca %list, align 8
  %1 = call i8* (i32, ...) @_lfortran_malloc(i32 8)
  %2 = bitcast i8* %1 to %tuple*
  %3 = getelementptr %list, %list* %points, i32 0, i32 2
  store %tuple* %2, %tuple** %3, align 8
  %4 = getelementptr %list, %list* %points, i32 0, i32 0
  store i32 0, i32* %4, align 4
  %5 = getelementptr %list, %list* %points, i32 0, i32 1
  store i32 1, i32* %5, align 4
  %const_list = alloca %list, align 8
  %6 = call i8* (i32, ...) @_lfortran_malloc(i32 24)
  %7 = bitcast i8* %6 to %tuple*
  %8 = getelementptr %list, %list* %const_list, i32 0, i32 2
  store %tuple* %7, %tuple** %8, align 8
  %9 = getelementptr %list, %list* %const_list, i32 0, i32 0
  store i32 3, i32* %9, align 4
  %10 = getelementptr %list, %list* %const_list, i32 0, i32 1
  store i32 3, i32* %10, align 4
  %const_tuple = alloca %tuple, align 8
  %11 = getelementptr %tuple, %tuple* %const_tuple, i32 0, i32 0
  store i32 445, i32* %11, align 4
  %12 = getelementptr %tuple, %tuple* %const_tuple, i32 0, i32 1
  store i32 193, i32* %12, align 4
  %13 = getelementptr %list, %list* %const_list, i32 0, i32 2
  %14 = load %tuple*, %tuple** %13, align 8
  %15 = getelementptr inbounds %tuple, %tuple* %14, i32 0
  %16 = getelementptr %tuple, %tuple* %const_tuple, i32 0, i32 0
  %17 = load i32, i32* %16, align 4
  %18 = getelementptr %tuple, %tuple* %15, i32 0, i32 0
  store i32 %17, i32* %18, align 4
  %19 = getelementptr %tuple, %tuple* %const_tuple, i32 0, i32 1
  %20 = load i32, i32* %19, align 4
  %21 = getelementptr %tuple, %tuple* %15, i32 0, i32 1
  store i32 %20, i32* %21, align 4
  %const_tuple1 = alloca %tuple, align 8
  %22 = getelementptr %tuple, %tuple* %const_tuple1, i32 0, i32 0
  store i32 138, i32* %22, align 4
  %23 = getelementptr %tuple, %tuple* %const_tuple1, i32 0, i32 1
  store i32 28, i32* %23, align 4
  %24 = getelementptr %list, %list* %const_list, i32 0, i32 2
  %25 = load %tuple*, %tuple** %24, align 8
  %26 = getelementptr inbounds %tuple, %tuple* %25, i32 1
  %27 = getelementptr %tuple, %tuple* %const_tuple1, i32 0, i32 0
  %28 = load i32, i32* %27, align 4
  %29 = getelementptr %tuple, %tuple* %26, i32 0, i32 0
  store i32 %28, i32* %29, align 4
  %30 = getelementptr %tuple, %tuple* %const_tuple1, i32 0, i32 1
  %31 = load i32, i32* %30, align 4
  %32 = getelementptr %tuple, %tuple* %26, i32 0, i32 1
  store i32 %31, i32* %32, align 4
  %const_tuple2 = alloca %tuple, align 8
  %33 = getelementptr %tuple, %tuple* %const_tuple2, i32 0, i32 0
  store i32 418, i32* %33, align 4
  %34 = getelementptr %tuple, %tuple* %const_tuple2, i32 0, i32 1
  store i32 279, i32* %34, align 4
  %35 = getelementptr %list, %list* %const_list, i32 0, i32 2
  %36 = load %tuple*, %tuple** %35, align 8
  %37 = getelementptr inbounds %tuple, %tuple* %36, i32 2
  %38 = getelementptr %tuple, %tuple* %const_tuple2, i32 0, i32 0
  %39 = load i32, i32* %38, align 4
  %40 = getelementptr %tuple, %tuple* %37, i32 0, i32 0
  store i32 %39, i32* %40, align 4
  %41 = getelementptr %tuple, %tuple* %const_tuple2, i32 0, i32 1
  %42 = load i32, i32* %41, align 4
  %43 = getelementptr %tuple, %tuple* %37, i32 0, i32 1
  store i32 %42, i32* %43, align 4
  %44 = getelementptr %list, %list* %const_list, i32 0, i32 0
  %45 = load i32, i32* %44, align 4
  %46 = getelementptr %list, %list* %const_list, i32 0, i32 1
  %47 = load i32, i32* %46, align 4
  %48 = getelementptr %list, %list* %points, i32 0, i32 0
  %49 = getelementptr %list, %list* %points, i32 0, i32 1
  store i32 %45, i32* %48, align 4
  store i32 %47, i32* %49, align 4
  %50 = getelementptr %list, %list* %const_list, i32 0, i32 2
  %51 = load %tuple*, %tuple** %50, align 8
  %52 = mul i32 8, %47
  %53 = call i8* (i32, ...) @_lfortran_malloc(i32 %52)
  %54 = bitcast i8* %53 to %tuple*
  %55 = getelementptr %list, %list* %points, i32 0, i32 2
  store %tuple* %54, %tuple** %55, align 8
  store i32 0, i32* %0, align 4
  br label %loop.head

loop.head:                                        ; preds = %loop.body, %.entry
  %56 = load i32, i32* %0, align 4
  %57 = icmp sgt i32 %45, %56
  br i1 %57, label %loop.body, label %loop.end

loop.body:                                        ; preds = %loop.head
  %58 = load i32, i32* %0, align 4
  %59 = getelementptr %list, %list* %const_list, i32 0, i32 2
  %60 = load %tuple*, %tuple** %59, align 8
  %61 = getelementptr inbounds %tuple, %tuple* %60, i32 %58
  %62 = getelementptr %list, %list* %points, i32 0, i32 2
  %63 = load %tuple*, %tuple** %62, align 8
  %64 = getelementptr inbounds %tuple, %tuple* %63, i32 %58
  %65 = getelementptr %tuple, %tuple* %61, i32 0, i32 0
  %66 = load i32, i32* %65, align 4
  %67 = getelementptr %tuple, %tuple* %64, i32 0, i32 0
  store i32 %66, i32* %67, align 4
  %68 = getelementptr %tuple, %tuple* %61, i32 0, i32 1
  %69 = load i32, i32* %68, align 4
  %70 = getelementptr %tuple, %tuple* %64, i32 0, i32 1
  store i32 %69, i32* %70, align 4
  %71 = add i32 %58, 1
  store i32 %71, i32* %0, align 4
  br label %loop.head

loop.end:                                         ; preds = %loop.head
  store i32 -1, i32* %__explicit_iterator, align 4
  br label %loop.head3

loop.head3:                                       ; preds = %ifcont, %loop.end
  %72 = load i32, i32* %__explicit_iterator, align 4
  %73 = add i32 %72, 1
  %74 = getelementptr %list, %list* %points, i32 0, i32 0
  %75 = load i32, i32* %74, align 4
  %76 = sub i32 %75, 1
  %77 = icmp sle i32 %73, %76
  br i1 %77, label %loop.body4, label %loop.end5

loop.body4:                                       ; preds = %loop.head3
  %78 = load i32, i32* %__explicit_iterator, align 4
  %79 = add i32 %78, 1
  store i32 %79, i32* %__explicit_iterator, align 4
  %80 = load i32, i32* %__explicit_iterator, align 4
  %81 = getelementptr %list, %list* %points, i32 0, i32 0
  %82 = load i32, i32* %81, align 4
  %83 = icmp sge i32 %80, %82
  %84 = icmp slt i32 %80, 0
  %85 = or i1 %83, %84
  br i1 %85, label %then, label %else

then:                                             ; preds = %loop.body4
  %86 = sub i32 %82, 1
  call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @0, i32 0, i32 0), i8* getelementptr inbounds ([48 x i8], [48 x i8]* @1, i32 0, i32 0), i32 %86, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @2, i32 0, i32 0), i32 %80)
  call void @exit(i32 1)
  br label %ifcont

else:                                             ; preds = %loop.body4
  br label %ifcont

ifcont:                                           ; preds = %else, %then
  %87 = getelementptr %list, %list* %points, i32 0, i32 2
  %88 = load %tuple*, %tuple** %87, align 8
  %89 = getelementptr inbounds %tuple, %tuple* %88, i32 %80
  store %tuple* %89, %tuple* %point, align 8
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @6, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @5, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @4, i32 0, i32 0))
  %90 = getelementptr %tuple, %tuple* %point, i32 0, i32 0
  %91 = load i32, i32* %90, align 4
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @9, i32 0, i32 0), i32 %91, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @8, i32 0, i32 0))
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @13, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @12, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @11, i32 0, i32 0))
  %92 = getelementptr %tuple, %tuple* %point, i32 0, i32 1
  %93 = load i32, i32* %92, align 4
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @16, i32 0, i32 0), i32 %93, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @15, i32 0, i32 0))
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @20, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @19, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @18, i32 0, i32 0))
  br label %loop.head3

loop.end5:                                        ; preds = %loop.head3
  br label %return

return:                                           ; preds = %loop.end5
  ret void
}

declare i8* @_lfortran_malloc(i32, ...)

declare void @_lcompilers_print_error(i8*, ...)

declare void @exit(i32)

declare void @_lfortran_printf(i8*, ...)

define i32 @main(i32 %0, i8** %1) {
.entry:
  call void @_lpython_set_argv(i32 %0, i8** %1)
  call void @__module___main_____main____global_statements()
  ret i32 0
}

declare void @_lpython_set_argv(i32, i8**)
code generation error: asr_to_llvm: module failed verification. Error:
Stored value type does not match pointer operand type!
  store %tuple* %89, %tuple* %point, align 8
 %tuple = type { i32, i32 }


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that must be fixed).

@certik
Copy link
Contributor

certik commented Jul 31, 2023

Great job!

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

Successfully merging a pull request may close this issue.

2 participants