From dac49e8ddd5dc0104c238f09cdd532e6fb5c4f1d Mon Sep 17 00:00:00 2001 From: Momchil Velikov Date: Fri, 24 Jan 2025 10:57:23 +0000 Subject: [PATCH] [Arm] Fix generating code with UB in NeonEmitter (#121802) When generating `arm_neon.h`, NeonEmitter outputs code that violates strict aliasing rules (C23 6.5 Expressions #7, C++23 7.2.1 Value category [basic.lval] #11), for example: bfloat16_t __reint = __p0; uint32_t __reint1 = (uint32_t)(*(uint16_t *) &__reint) << 16; __ret = *(float32_t *) &__reint1; This patch fixed the offending code by replacing it with a call to `__builtin_bit_cast`. --- clang/utils/TableGen/NeonEmitter.cpp | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/clang/utils/TableGen/NeonEmitter.cpp b/clang/utils/TableGen/NeonEmitter.cpp index d7d649dd2456d..49633bb7b7f58 100644 --- a/clang/utils/TableGen/NeonEmitter.cpp +++ b/clang/utils/TableGen/NeonEmitter.cpp @@ -1592,24 +1592,10 @@ Intrinsic::DagEmitter::emitDagCast(const DagInit *DI, bool IsBitCast) { } std::string S; - if (IsBitCast) { - // Emit a reinterpret cast. The second operand must be an lvalue, so create - // a temporary. - std::string N = "reint"; - unsigned I = 0; - while (Intr.Variables.find(N) != Intr.Variables.end()) - N = "reint" + utostr(++I); - Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix); - - Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = " - << R.second << ";"; - Intr.emitNewLine(); - - S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + ""; - } else { - // Emit a normal (static) cast. + if (IsBitCast) + S = "__builtin_bit_cast(" + castToType.str() + ", " + R.second + ")"; + else S = "(" + castToType.str() + ")(" + R.second + ")"; - } return std::make_pair(castToType, S); }