runtime icon indicating copy to clipboard operation
runtime copied to clipboard

enable Link Time Optimization for building coreclr on Ubuntu 14.04

Open helloguo opened this issue 10 years ago • 6 comments

Hi,

I want to enable the link time optimization for building coreclr. However, I met some problems that I really appreciate anyone can give me any hint/suggestions.

First, I follow the instructions from http://llvm.org/docs/GoldPlugin.html to update the gold linker and plugin. I download the source code of binutils, llvm-3.5 and clang-3.5 and build/update them. Everything works well in this step.

Second, I add '-flto' flag for building coreclr. In file /src/pal/tools/clang-compiler-override.txt, I add "SET (CMAKE_C_FLAGS_RELEASE_INIT "-g -O3 -flto")" and "SET (CMAKE_CXX_FLAGS_RELEASE_INIT "-g -O3 -flto")". When I build it, the compiler complains that "ld: /home/guo1/llvm-3.5/llvm-3.5.0.src/lib/CodeGen/AsmPrinter/DwarfUnit.cpp:957: llvm::DIE* llvm::DwarfUnit::getOrCreateTypeDIE(const llvm::MDNode*): Assertion `Ty == resolve(Ty.getRef()) && "type was not uniqued, possible ODR violation."' failed. ". After I dump more debug information from LLVM, it seems that type "HolderStackValidation", "CorTokenType", "opcode_t", "PrimeInfo" and "ImmutableFlag" from coreclr's source code cause this assertion. One quick but dirty modification is to remove '-g' flag for clang. Then coreclr can be build again. This is not a right way to go. But right now, I'm not very familiar with the source code of coreclr and I don't know how to solve it. Any ideas?

Third, one another error shows up while building coreclr: "/home/guo1/coreclr/src/debug/daccess/daccess.cpp:7230:46: error: use of undeclared identifier 'DAC_TABLE_RVA' ULONG64 dacTableAddress = m_globalBase + DAC_TABLE_RVA; ". It seems that the script /src/pal/tools/gen-dactable-rva.sh will be executed to add the definition of DAC_TABLE_RVA in dactablerva.h. But somehow, this script doesn't work. In order to make building continue, I manually add the definition of DAC_TABLE_RVA in the source code (of course this is not a right way to go). This way, the building can finish completely and the size of libcoreclr.so is reduced. Also, some benchmarks show that LTO can achieve better performance.

So, I guess I need to fix the second and third problem in order to make LTO work for building coreclr. Any comments/suggestions are appreciated.

Thanks.

Mark

helloguo avatar Jun 06 '16 18:06 helloguo

For the second problem, if I use the Release build of LLVM instead of Debug build, the problem disappears. The reason is that Release build of LLVM can disable all its assertions. So is it safe to use the Release build of LLVM? I mean, do these assertions really matter for building a correct CoreCLR?

helloguo avatar Jun 07 '16 00:06 helloguo

Looks like the LLVM assert is in debug info emission, so the generated code should still be correct even if the debuginfo is wrong on a Release LLVM build.

swgillespie avatar Jun 07 '16 05:06 swgillespie

@helloguo I have noticed some time ago that the dependency chain created for the dactablerva.h is strange - although I never get a problem with the file not being generated, often it gets generated even if there was no change. So I have locally tried to fix it by creating a proper dependency chain instead of using POSTBUILD phase of coreclr. Could you please try to replace the add_custom_command for gen-dactable-rva.sh in src/dlls/mscoree/coreclr/CMakeLists.txt by the following to see if it fixes your issue? Please delete the bin folder before making this change, it seems that cmake doesn't do the right thing if you change this kind of things in a CMakeLists.txt without deleting the bin folder.

    add_custom_command(
        COMMAND sh ${CLR_DIR}/src/pal/tools/gen-dactable-rva.sh $<TARGET_FILE:coreclr> ${GENERATED_INCLUDE_DIR}/dactablerva.h
        OUTPUT ${GENERATED_INCLUDE_DIR}/dactablerva.h
        VERBATIM
        DEPENDS coreclr
        COMMENT Generating ${GENERATED_INCLUDE_DIR}/dactablerva.h
    )

    set_source_files_properties("${GENERATED_INCLUDE_DIR}/dactablerva.h" PROPERTIES GENERATED TRUE)

    add_custom_target(
      DacTableRva
      DEPENDS ${GENERATED_INCLUDE_DIR}/dactablerva.h
    )

    # The DacTableRva will be built as a dependency of the daccess target
    add_dependencies(daccess DacTableRva)

janvorli avatar Jun 07 '16 10:06 janvorli

llvm-3.5 and clang-3.5

When we have tried enabling it a while ago, we have run into issues that looked like LLVM bug. It is fine for LTO to require more recent LLVM version to pick up the required bug fixes.

jkotas avatar Jun 11 '16 03:06 jkotas

When we have tried enabling it a while ago, we have run into issues that looked like LLVM bug.

We saw a couple of failed PAL tests which appears to be due to compiler bug(s). Unfortunately, the same tests still fail with 3.6 and 3.8, and we are looking into them more closely. We will keep you posted.

hanblee avatar Jun 13 '16 15:06 hanblee

Enabling LTO status update:

The issues resulting in two failed PAL tests are being tracked by dotnet/coreclr#6801 and dotnet/coreclr#7069. Also, there was a performance regression with LTO enabled which was fixed by dotnet/coreclr#7018.

We plan to evaluate PGO next.

hanblee avatar Sep 06 '16 22:09 hanblee