Index: clang/lib/CodeGen/CodeGenPGO.cpp =================================================================== --- clang/lib/CodeGen/CodeGenPGO.cpp +++ clang/lib/CodeGen/CodeGenPGO.cpp @@ -747,13 +747,21 @@ return Working; // Check for remaining work in Working. - if (Working) - MD5.update(Working); + if (Working) { + // Keep the buggy behavior from v1 for backward-compatibility. This is buggy + // because it implicitly converts a uint64_t into an array of uint8_t. + if (HashVersion == PGO_HASH_V1) { + MD5.update(Working); + } else { + using namespace llvm::support; + uint64_t Swapped = endian::byte_swap(Working); + MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped))); + } + } // Finalize the MD5 and return the hash. llvm::MD5::MD5Result Result; MD5.final(Result); - using namespace llvm::support; return Result.low(); } Index: clang/test/Profile/Inputs/c-counter-overflows.proftext =================================================================== --- clang/test/Profile/Inputs/c-counter-overflows.proftext +++ clang/test/Profile/Inputs/c-counter-overflows.proftext @@ -1,5 +1,5 @@ main -10111551811706059223 +7779561829442898616 8 1 68719476720 Index: clang/test/Profile/Inputs/c-general.proftext =================================================================== --- clang/test/Profile/Inputs/c-general.proftext +++ clang/test/Profile/Inputs/c-general.proftext @@ -7,7 +7,7 @@ 75 conditionals -4190663230902537370 +4904767535850050386 11 1 100 @@ -22,7 +22,7 @@ 100 early_exits -8265526549255474475 +2880354649761471549 9 1 0 @@ -35,7 +35,7 @@ 0 jumps -15872630527555456493 +15051420506203462683 22 1 1 @@ -61,7 +61,7 @@ 9 switches -11892326508727782373 +43242458792028222 19 1 1 @@ -84,7 +84,7 @@ 0 big_switch -16933280399284440835 +13144136522122330070 17 1 32 @@ -117,7 +117,7 @@ 50 boolop_loops -11270260636676715317 +12402604614320574815 9 1 50 @@ -137,7 +137,7 @@ 1 do_fallthrough -6898770640283947069 +8714614136504380050 4 1 10 Index: clang/test/Profile/Inputs/c-unprofiled-blocks.proftext =================================================================== --- clang/test/Profile/Inputs/c-unprofiled-blocks.proftext +++ clang/test/Profile/Inputs/c-unprofiled-blocks.proftext @@ -1,5 +1,5 @@ never_called -5644096560937528444 +6820425066224770721 9 0 0 @@ -17,7 +17,7 @@ 1 dead_code -9636018207904213947 +5254464978620792806 10 1 0 Index: clang/test/Profile/Inputs/cxx-rangefor.proftext =================================================================== --- clang/test/Profile/Inputs/cxx-rangefor.proftext +++ clang/test/Profile/Inputs/cxx-rangefor.proftext @@ -1,5 +1,5 @@ _Z9range_forv -6169071350249721981 +8789831523895825398 5 1 4 Index: clang/test/Profile/Inputs/cxx-throws.proftext =================================================================== --- clang/test/Profile/Inputs/cxx-throws.proftext +++ clang/test/Profile/Inputs/cxx-throws.proftext @@ -1,5 +1,5 @@ _Z6throwsv -340120998528097520 +18172607911962830854 9 1 100 Index: clang/test/Profile/Inputs/misexpect-switch-default.proftext =================================================================== --- clang/test/Profile/Inputs/misexpect-switch-default.proftext +++ clang/test/Profile/Inputs/misexpect-switch-default.proftext @@ -1,6 +1,6 @@ main # Func Hash: -8712453512413296413 +8734802134600123338 # Num Counters: 9 # Counter Values: Index: clang/test/Profile/Inputs/misexpect-switch-nonconst.proftext =================================================================== --- clang/test/Profile/Inputs/misexpect-switch-nonconst.proftext +++ clang/test/Profile/Inputs/misexpect-switch-nonconst.proftext @@ -1,6 +1,6 @@ main # Func Hash: -1965403898329309329 +3721743393642630379 # Num Counters: 10 # Counter Values: Index: clang/test/Profile/Inputs/misexpect-switch.proftext =================================================================== --- clang/test/Profile/Inputs/misexpect-switch.proftext +++ clang/test/Profile/Inputs/misexpect-switch.proftext @@ -1,6 +1,6 @@ main # Func Hash: -1965403898329309329 +872687477373597607 # Num Counters: 9 # Counter Values: Index: clang/test/Profile/c-collision.c =================================================================== --- /dev/null +++ clang/test/Profile/c-collision.c @@ -0,0 +1,22 @@ +// Test that a slight change in the code leads to a different hash. +// RUN: %clang_cc1 -UEXTRA -triple x86_64-unknown-linux-gnu -main-file-name c-collision.c %s -o - -emit-llvm -fprofile-instrument=clang | FileCheck %s --check-prefix=CHECK-NOEXTRA +// RUN: %clang_cc1 -DEXTRA -triple x86_64-unknown-linux-gnu -main-file-name c-collision.c %s -o - -emit-llvm -fprofile-instrument=clang | FileCheck %s --check-prefix=CHECK-EXTRA + +// CHECK-NOEXTRA: @__profd_foo = private global { {{.*}} } { i64 6699318081062747564, i64 7156072912471487002, +// CHECK-EXTRA: @__profd_foo = private global { {{.*}} } { i64 6699318081062747564, i64 -4383447408116050035, + +extern int bar; +void foo() { + if (bar) { + } + if (bar) { + } + if (bar) { + if (bar) { +#ifdef EXTRA + if (bar) { + } +#endif + } + } +}