[flang-commits] [flang] [WIP] [flang] Always lower ALLOCATE/DEALLOCATE to runtime calls. (PR #133238)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Mon Apr 7 09:00:51 PDT 2025


clementval wrote:

> > They don't get called that's why you can force the allocation via runtime with `useAllocateRuntime`. There is no need to update the code to make it a default since the option is already present.
> 
> I'm sorry, but I do not see that:
> 
> ```fortran
> module alloc
> 
> contains
>     subroutine alloc_array(array, n)
>         implicit none
>         real, dimension(:), allocatable :: array
>         integer :: n
>         allocate(array(n))
>     end subroutine alloc_array
> 
>     subroutine dealloc_array(array)
>         implicit none
>         real, dimension(:), allocatable :: array
>         deallocate(array)
>     end subroutine dealloc_array
> end module alloc
> ```
> 
> gives me these pieces of LLVM IR:
> 
> ```
> define void @_QMallocPalloc_array(ptr captures(none) %0, ptr captures(none) %1) #0 {
>   %3 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
>   %4 = load i32, ptr %1, align 4
>   %5 = sext i32 %4 to i64
>   %6 = icmp sgt i64 %5, 0
>   %7 = select i1 %6, i64 %5, i64 0
>   %8 = mul i64 4, %7
>   %9 = call ptr @malloc(i64 %8)
>   %10 = insertvalue { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr undef, i64 4, i32 20240719, i8 1, i8 27, i8 2, i8 0, [1 x [3 x i64]] [[3 x i64] [i64 1, i64 undef, i64 undef]] }, i64 %7, 7, 0, 1
>   %11 = insertvalue { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %10, i64 4, 7, 0, 2
>   %12 = mul i64 4, %7
>   %13 = mul i64 1, %7
>   %14 = insertvalue { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %11, ptr %9, 0
>   store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %14, ptr %3, align 8
>   call void @llvm.memcpy.p0.p0.i32(ptr %0, ptr %3, i32 48, i1 false)
>   ret void
> }
> 
> define void @_QMallocPdealloc_array(ptr captures(none) %0) #0 {
>   %2 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
>   %3 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
>   call void @llvm.memcpy.p0.p0.i32(ptr %3, ptr %0, i32 48, i1 false)
>   %4 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %3, i32 0, i32 0
>   %5 = load ptr, ptr %4, align 8
>   call void @free(ptr %5)
>   store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr null, i64 4, i32 20240719, i8 1, i8 27, i8 2, i8 0, [1 x [3 x i64]] [[3 x i64] [i64 1, i64 0, i64 4]] }, ptr %2, align 8
>   call void @llvm.memcpy.p0.p0.i32(ptr %0, ptr %2, i32 48, i1 false)
>   ret void
> }
> ```
> 
> This seems to indicate to me that the compiler indeed uses the default `malloc` implementation. Of course, I might be missing something completely obvious here (wouldn't be the first time :-))

Have you used the flag to force runtime allocation? What's your command line? 

https://github.com/llvm/llvm-project/pull/133238


More information about the flang-commits mailing list