From 879e111740b0cc69db61ca334031164496b3adc9 Mon Sep 17 00:00:00 2001 From: Jason Carey Date: Wed, 2 May 2018 18:18:09 +0000 Subject: SERVER-33395 Add configure check for vec_vbpermq output GCC changed the output of vec_vbpermq between 5.4.0 and later. This seems likely to be a bug, probably in earlier versions of gcc. Rather than attempt to enumerate how the compiler versions work, this adds a runtime configure check which will select the lane that works. (cherry picked from commit b989fe4b0cfba4062c5bb7fb68337f188070c1c8) --- SConstruct | 57 ++++++++++++++++++++++++++ src/mongo/SConscript | 1 + src/mongo/config.h.in | 4 ++ src/mongo/db/fts/unicode/byte_vector_altivec.h | 3 +- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index 102717fe5a8..f72671f57e8 100644 --- a/SConstruct +++ b/SConstruct @@ -2924,6 +2924,63 @@ def doConfigure(myenv): # ask each module to configure itself and the build environment. moduleconfig.configure_modules(mongo_modules, conf) + if env['TARGET_ARCH'] == "ppc64le": + # This checks for an altivec optimization we use in full text search. + # Different versions of gcc appear to put output bytes in different + # parts of the output vector produced by vec_vbpermq. This configure + # check looks to see which format the compiler produces. + # + # NOTE: This breaks cross compiles, as it relies on checking runtime functionality for the + # environment we're in. A flag to choose the index, or the possibility that we don't have + # multiple versions to support (after a compiler upgrade) could solve the problem if we + # eventually need them. + def CheckAltivecVbpermqOutput(context, index): + test_body = """ + #include + #include + #include + #include + + int main() {{ + using Native = __vector signed char; + const size_t size = sizeof(Native); + const Native bits = {{ 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0 }}; + + uint8_t inputBuf[size]; + std::memset(inputBuf, 0xFF, sizeof(inputBuf)); + + for (size_t offset = 0; offset <= size; offset++) {{ + Native vec = vec_vsx_ld(0, reinterpret_cast(inputBuf)); + + uint64_t mask = vec_extract(vec_vbpermq(vec, bits), {0}); + + size_t initialZeros = (mask == 0 ? size : __builtin_ctzll(mask)); + if (initialZeros != offset) {{ + return 1; + }} + + if (offset < size) {{ + inputBuf[offset] = 0; // Add an initial 0 for the next loop. + }} + }} + + return 0; + }} + """.format(index) + + context.Message('Checking for vec_vbperm output in index {0}... '.format(index)) + ret = context.TryRun(textwrap.dedent(test_body), ".cpp") + context.Result(ret[0]) + return ret[0] + + conf.AddTest('CheckAltivecVbpermqOutput', CheckAltivecVbpermqOutput) + + outputIndex = next((idx for idx in [0,1] if conf.CheckAltivecVbpermqOutput(idx)), None) + if outputIndex is not None: + conf.env.SetConfigHeaderDefine("MONGO_CONFIG_ALTIVEC_VEC_VBPERMQ_OUTPUT_INDEX", outputIndex) + else: + myenv.ConfError("Running on ppc64le, but can't find a correct vec_vbpermq output index. Compiler or platform not supported") + return conf.Finish() env = doConfigure( env ) diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 1208446d70f..66ba4054a11 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -242,6 +242,7 @@ if env.TargetOSIs('windows'): env.Alias('generated-sources', generatedResourceConstantFile) config_header_substs = ( + ('@mongo_config_altivec_vec_vbpermq_output_index@', 'MONGO_CONFIG_ALTIVEC_VEC_VBPERMQ_OUTPUT_INDEX'), ('@mongo_config_byte_order@', 'MONGO_CONFIG_BYTE_ORDER'), ('@mongo_config_debug_build@', 'MONGO_CONFIG_DEBUG_BUILD'), ('@mongo_config_have___declspec_thread@', 'MONGO_CONFIG_HAVE___DECLSPEC_THREAD'), diff --git a/src/mongo/config.h.in b/src/mongo/config.h.in index c1ef64647b0..011bc5a41ac 100644 --- a/src/mongo/config.h.in +++ b/src/mongo/config.h.in @@ -28,6 +28,10 @@ #pragma once + +// Define altivec vec_vbpermq output index +@mongo_config_altivec_vec_vbpermq_output_index@ + // Define to target byte order (1234 vs 4321) @mongo_config_byte_order@ diff --git a/src/mongo/db/fts/unicode/byte_vector_altivec.h b/src/mongo/db/fts/unicode/byte_vector_altivec.h index f7a07ac0743..0797443f72f 100644 --- a/src/mongo/db/fts/unicode/byte_vector_altivec.h +++ b/src/mongo/db/fts/unicode/byte_vector_altivec.h @@ -34,6 +34,7 @@ #include +#include "mongo/config.h" #include "mongo/platform/bits.h" namespace mongo { @@ -98,7 +99,7 @@ public: // big endian by comparison. const Native bits = {120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0}; - return vec_extract(vec_vbpermq(_data, bits), 0); + return vec_extract(vec_vbpermq(_data, bits), MONGO_CONFIG_ALTIVEC_VEC_VBPERMQ_OUTPUT_INDEX); } /** -- cgit v1.2.1