; LLVM IR from ggml-org/ggml :: src/ggml-opencl (ggml-opencl)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.ggml_backend_opencl_device_context = type { ptr, %"class.std::__cxx11::basic_string", ptr, %"class.std::__cxx11::basic_string" }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }
%struct.ggml_backend_device = type { %struct.ggml_backend_device_i, ptr, ptr }
%struct.ggml_backend_device_i = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.ggml_backend_reg = type { i32, %struct.ggml_backend_reg_i, ptr }
%struct.ggml_backend_reg_i = type { ptr, ptr, ptr, ptr }

@_ZL19g_ggml_ctx_dev_main = external hidden global %struct.ggml_backend_opencl_device_context, align 8
@_ZL28g_ggml_backend_opencl_device = external hidden global %struct.ggml_backend_device, align 8
@_ZZ23ggml_backend_opencl_regE3reg = external hidden global %struct.ggml_backend_reg, align 8
@_ZZ23ggml_backend_opencl_regE11initialized = external hidden global i8, align 1
@_ZL25ggml_backend_opencl_reg_i = external hidden global %struct.ggml_backend_reg_i, align 8
@_ZL28ggml_backend_opencl_device_i = external hidden global %struct.ggml_backend_device_i, align 8

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
define dso_local ptr @ggml_backend_opencl_reg() #1 {
  %1 = alloca %struct.ggml_backend_reg, align 8
  %2 = alloca %struct.ggml_backend_device, align 8
  %3 = load i8, ptr @_ZZ23ggml_backend_opencl_regE11initialized, align 1, !tbaa !5, !range !9, !noundef !10
  %4 = trunc i8 %3 to i1
  br i1 %4, label %13, label %5

5:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 48, ptr %1) #3
  %6 = getelementptr inbounds %struct.ggml_backend_reg, ptr %1, i32 0, i32 0
  store i32 1, ptr %6, align 8, !tbaa !11
  %7 = getelementptr inbounds %struct.ggml_backend_reg, ptr %1, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 @_ZL25ggml_backend_opencl_reg_i, i64 32, i1 false), !tbaa.struct !16
  %8 = getelementptr inbounds %struct.ggml_backend_reg, ptr %1, i32 0, i32 2
  store ptr null, ptr %8, align 8, !tbaa !18
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 @_ZZ23ggml_backend_opencl_regE3reg, ptr align 8 %1, i64 48, i1 false), !tbaa.struct !19
  call void @llvm.lifetime.end.p0(i64 48, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 136, ptr %2) #3
  %9 = getelementptr inbounds %struct.ggml_backend_device, ptr %2, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %9, ptr align 8 @_ZL28ggml_backend_opencl_device_i, i64 120, i1 false), !tbaa.struct !21
  %10 = getelementptr inbounds %struct.ggml_backend_device, ptr %2, i32 0, i32 1
  store ptr @_ZZ23ggml_backend_opencl_regE3reg, ptr %10, align 8, !tbaa !22
  %11 = getelementptr inbounds %struct.ggml_backend_device, ptr %2, i32 0, i32 2
  store ptr @_ZL19g_ggml_ctx_dev_main, ptr %11, align 8, !tbaa !25
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 @_ZL28g_ggml_backend_opencl_device, ptr align 8 %2, i64 136, i1 false), !tbaa.struct !26
  call void @llvm.lifetime.end.p0(i64 136, ptr %2) #3
  %12 = call noundef ptr @_ZL13ggml_cl2_initP19ggml_backend_device(ptr noundef @_ZL28g_ggml_backend_opencl_device)
  store i8 1, ptr @_ZZ23ggml_backend_opencl_regE11initialized, align 1, !tbaa !5
  br label %13

13:                                               ; preds = %5, %0
  ret ptr @_ZZ23ggml_backend_opencl_regE3reg
}

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL13ggml_cl2_initP19ggml_backend_device(ptr noundef) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS16ggml_backend_reg", !13, i64 0, !14, i64 8, !15, i64 40}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_ZTS18ggml_backend_reg_i", !15, i64 0, !15, i64 8, !15, i64 16, !15, i64 24}
!15 = !{!"any pointer", !7, i64 0}
!16 = !{i64 0, i64 8, !17, i64 8, i64 8, !17, i64 16, i64 8, !17, i64 24, i64 8, !17}
!17 = !{!15, !15, i64 0}
!18 = !{!12, !15, i64 40}
!19 = !{i64 0, i64 4, !20, i64 8, i64 8, !17, i64 16, i64 8, !17, i64 24, i64 8, !17, i64 32, i64 8, !17, i64 40, i64 8, !17}
!20 = !{!13, !13, i64 0}
!21 = !{i64 0, i64 8, !17, i64 8, i64 8, !17, i64 16, i64 8, !17, i64 24, i64 8, !17, i64 32, i64 8, !17, i64 40, i64 8, !17, i64 48, i64 8, !17, i64 56, i64 8, !17, i64 64, i64 8, !17, i64 72, i64 8, !17, i64 80, i64 8, !17, i64 88, i64 8, !17, i64 96, i64 8, !17, i64 104, i64 8, !17, i64 112, i64 8, !17}
!22 = !{!23, !15, i64 120}
!23 = !{!"_ZTS19ggml_backend_device", !24, i64 0, !15, i64 120, !15, i64 128}
!24 = !{!"_ZTS21ggml_backend_device_i", !15, i64 0, !15, i64 8, !15, i64 16, !15, i64 24, !15, i64 32, !15, i64 40, !15, i64 48, !15, i64 56, !15, i64 64, !15, i64 72, !15, i64 80, !15, i64 88, !15, i64 96, !15, i64 104, !15, i64 112}
!25 = !{!23, !15, i64 128}
!26 = !{i64 0, i64 8, !17, i64 8, i64 8, !17, i64 16, i64 8, !17, i64 24, i64 8, !17, i64 32, i64 8, !17, i64 40, i64 8, !17, i64 48, i64 8, !17, i64 56, i64 8, !17, i64 64, i64 8, !17, i64 72, i64 8, !17, i64 80, i64 8, !17, i64 88, i64 8, !17, i64 96, i64 8, !17, i64 104, i64 8, !17, i64 112, i64 8, !17, i64 120, i64 8, !17, i64 128, i64 8, !17}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.ggml_backend_opencl_device_context = type { ptr, %"class.std::__cxx11::basic_string", ptr, %"class.std::__cxx11::basic_string" }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }
%struct.ggml_backend_device = type { %struct.ggml_backend_device_i, ptr, ptr }
%struct.ggml_backend_device_i = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.ggml_backend_reg = type { i32, %struct.ggml_backend_reg_i, ptr }
%struct.ggml_backend_reg_i = type { ptr, ptr, ptr, ptr }

@_ZL19g_ggml_ctx_dev_main = external hidden global %struct.ggml_backend_opencl_device_context, align 8
@_ZL28g_ggml_backend_opencl_device = external hidden global %struct.ggml_backend_device, align 8
@_ZZ23ggml_backend_opencl_regE3reg = external hidden global %struct.ggml_backend_reg, align 8
@_ZZ23ggml_backend_opencl_regE11initialized = external hidden local_unnamed_addr global i8, align 1
@_ZL25ggml_backend_opencl_reg_i = external hidden local_unnamed_addr global %struct.ggml_backend_reg_i, align 8
@_ZL28ggml_backend_opencl_device_i = external hidden local_unnamed_addr global %struct.ggml_backend_device_i, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef nonnull ptr @ggml_backend_opencl_reg() local_unnamed_addr #1 {
  %.sroa.42 = alloca [36 x i8], align 4
  %1 = load i8, ptr @_ZZ23ggml_backend_opencl_regE11initialized, align 1, !tbaa !5, !range !9, !noundef !10
  %2 = trunc nuw i8 %1 to i1
  br i1 %2, label %5, label %3

3:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %.sroa.42)
  %.sroa.42.8.sroa_idx = getelementptr inbounds i8, ptr %.sroa.42, i64 4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(32) %.sroa.42.8.sroa_idx, ptr noundef nonnull align 8 dereferenceable(32) @_ZL25ggml_backend_opencl_reg_i, i64 32, i1 false), !tbaa.struct !11
  store i32 1, ptr @_ZZ23ggml_backend_opencl_regE3reg, align 8, !tbaa !14
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) getelementptr inbounds (i8, ptr @_ZZ23ggml_backend_opencl_regE3reg, i64 4), ptr noundef nonnull align 4 dereferenceable(36) %.sroa.42, i64 36, i1 false), !tbaa.struct !16
  store ptr null, ptr getelementptr inbounds (i8, ptr @_ZZ23ggml_backend_opencl_regE3reg, i64 40), align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %.sroa.42)
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(120) @_ZL28g_ggml_backend_opencl_device, ptr noundef nonnull align 8 dereferenceable(120) @_ZL28ggml_backend_opencl_device_i, i64 120, i1 false)
  store ptr @_ZZ23ggml_backend_opencl_regE3reg, ptr getelementptr inbounds (i8, ptr @_ZL28g_ggml_backend_opencl_device, i64 120), align 8, !tbaa !12
  store ptr @_ZL19g_ggml_ctx_dev_main, ptr getelementptr inbounds (i8, ptr @_ZL28g_ggml_backend_opencl_device, i64 128), align 8, !tbaa !12
  %4 = tail call noundef ptr @_ZL13ggml_cl2_initP19ggml_backend_device(ptr noundef nonnull @_ZL28g_ggml_backend_opencl_device)
  store i8 1, ptr @_ZZ23ggml_backend_opencl_regE11initialized, align 1, !tbaa !5
  br label %5

5:                                                ; preds = %3, %0
  ret ptr @_ZZ23ggml_backend_opencl_regE3reg
}

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL13ggml_cl2_initP19ggml_backend_device(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{i64 0, i64 8, !12, i64 8, i64 8, !12, i64 16, i64 8, !12, i64 24, i64 8, !12}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{i64 4, i64 8, !12, i64 12, i64 8, !12, i64 20, i64 8, !12, i64 28, i64 8, !12, i64 36, i64 8, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass