Compiler projects using llvm
RUN: rm -rf %t
REQUIRES: x86-registered-target

RUN: %clang_cc1 -no-opaque-pointers -triple=x86_64-linux-gnu -fmodules-codegen -x c++ -fmodules -emit-module -fmodule-name=foo %S/Inputs/codegen-opt/foo.modulemap -o %t/foo.pcm
RUN: %clang_cc1 -no-opaque-pointers -triple=x86_64-linux-gnu -fmodules-codegen -x c++ -fmodules -emit-module -fmodule-name=bar %S/Inputs/codegen-opt/bar.modulemap -o %t/bar.pcm -fmodule-file=%t/foo.pcm

RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -emit-llvm -o - %t/foo.pcm | FileCheck --check-prefix=FOO %s
RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -emit-llvm -o - %t/bar.pcm -fmodule-file=%t/foo.pcm | FileCheck --check-prefix=BAR-CMN --check-prefix=BAR %s
RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -emit-llvm -o - -fmodules -fmodule-file=%t/foo.pcm -fmodule-file=%t/bar.pcm %S/Inputs/codegen-opt/use.cpp | FileCheck --check-prefix=USE-CMN --check-prefix=USE %s

RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -emit-llvm -o - -O2 -disable-llvm-passes %t/foo.pcm | FileCheck --check-prefix=FOO %s
RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -emit-llvm -o - -O2 -disable-llvm-passes %t/bar.pcm -fmodule-file=%t/foo.pcm | FileCheck --check-prefix=BAR-CMN --check-prefix=BAR-OPT %s
RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -emit-llvm -o - -O2 -disable-llvm-passes -fmodules -fmodule-file=%t/foo.pcm -fmodule-file=%t/bar.pcm %S/Inputs/codegen-opt/use.cpp | FileCheck --check-prefix=USE-CMN --check-prefix=USE-OPT %s

FOO-NOT: comdat
FOO: $_Z3foov = comdat any
FOO: $_Z4foo2v = comdat any
FOO: $_ZZ3foovE1i = comdat any
FOO: @_ZZ3foovE1i = linkonce_odr global i32 0, comdat
FOO-NOT: {{comdat|define|declare}}
FOO: define{{.*}} void @_Z7foo_extv()
FOO-NOT: {{define|declare}}
FOO: define weak_odr void @_Z3foov() #{{[0-9]+}} comdat
FOO-NOT: {{define|declare}}
FOO: declare void @_Z2f1Ri(i32*
FOO-NOT: {{define|declare}}

Internal functions are not modularly code generated - they are
internal wherever they're used. This might not be ideal, but
continues to workaround/support some oddities that backwards
compatible modules have seen and supported in the wild.  To remove
the duplication here, the internal functions would need to be
promoted to weak_odr, placed in comdat and given a new mangling -
this would be needed for the C++ Modules TS anyway.
FOO: define internal void @_ZL2f2v() #{{[0-9]+}}
FOO-NOT: {{define|declare}}

FOO: define weak_odr void @_Z4foo2v() #{{[0-9]+}} comdat
FOO-NOT: {{define|declare}}


BAR-CMN-NOT: comdat
BAR-CMN: $_Z3barv = comdat any
BAR-OPT: @_ZZ3foovE1i = linkonce_odr global i32 0, comdat
BAR-CMN-NOT: {{comdat|define|declare}}
BAR-CMN: define weak_odr void @_Z3barv() #{{[0-9]+}} comdat
BAR-CMN-NOT: {{define|declare}}
BAR: declare void @_Z3foov()
Include all the available_externally definitions required for bar (foo -> f2)
BAR-OPT: define available_externally void @_Z3foov()
BAR-CMN-NOT: {{define|declare}}
BAR-OPT: declare void @_Z2f1Ri(i32*
BAR-OPT-NOT: {{define|declare}}
BAR-OPT: define internal void @_ZL2f2v()
BAR-OPT-NOT: {{define|declare}}


USE-OPT: @_ZZ3foovE1i = linkonce_odr global i32 0, comdat
USE-CMN-NOT: {{comdat|define|declare}}
USE-CMN: define{{.*}} i32 @main()
USE-CMN-NOT: {{define|declare}}
USE: declare void @_Z3barv()
Include all the available_externally definitions required for main (bar -> foo -> f2)
USE-OPT: define available_externally void @_Z3barv()
USE-CMN-NOT: {{define|declare}}
USE-OPT: define available_externally void @_Z3foov()
USE-OPT-NOT: {{define|declare}}
USE-OPT: declare void @_Z2f1Ri(i32*
USE-OPT-NOT: {{define|declare}}
USE-OPT: define internal void @_ZL2f2v()
USE-OPT-NOT: {{define|declare}}