After setting the LTO mode from the -flto option, look at the input files as well. If any of them is an object file containing LLVM bitcode,
set the LTO mode to either thin or full, depending on the input file.
This makes the following sample work:
clang test.c -c -flto clang test.o
Which also works when using GCC. Currently this makes non-lld linkers print an error, for example when using ld.bfd:
$ clang test.o test.o: file not recognized: file format not recognized clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
On line 1133, there is a similar setLTOMode. If we are going to add the logic, probably consider unifying the logic. However, I am concerned with the cost, see my main comment