From cfa6da59532d2a3cb9eb34f249c257cdaf4b6260 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 23 Oct 2023 15:19:41 +0200 Subject: [PATCH] COMP: circumvent aggressive compiler branch optimization (clang-17) - it seems that both sides of the ternary are evaluated despite the divide-by-zero protection. Use volatile to force the compiler to use in-order evaluation. --- src/OpenFOAM/meshes/meshShapes/edge/edgeI.H | 9 ++++----- src/OpenFOAM/meshes/meshShapes/face/faceI.H | 8 ++++---- src/OpenFOAM/meshes/primitiveShapes/line/lineI.H | 4 ++++ src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H | 8 ++++---- src/OpenFOAM/primitives/Vector/VectorI.H | 3 +++ src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H | 3 +++ src/OpenFOAM/primitives/complex/complexI.H | 1 + src/OpenFOAM/primitives/quaternion/quaternion.H | 4 ++-- src/OpenFOAM/primitives/quaternion/quaternionI.H | 9 +++++---- src/finiteArea/faMesh/faMeshDemandDrivenData.C | 1 + 10 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H index a3b6c7f4e3..4ac3fa4ca2 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -417,10 +417,9 @@ inline Foam::vector Foam::edge::unitVec(const UList& pts) const } #endif - const vector v = (pts[second()] - pts[first()]); - const scalar s(Foam::mag(v)); - - return s < ROOTVSMALL ? Zero : v/s; + vector v = (pts[second()] - pts[first()]); + (void) v.normalise(ROOTVSMALL); + return v; } diff --git a/src/OpenFOAM/meshes/meshShapes/face/faceI.H b/src/OpenFOAM/meshes/meshShapes/face/faceI.H index c54c33f3cd..73902597a1 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/faceI.H +++ b/src/OpenFOAM/meshes/meshShapes/face/faceI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -103,9 +103,9 @@ inline Foam::pointField Foam::face::points(const UList& pts) const inline Foam::vector Foam::face::unitNormal(const UList& p) const { - const vector n(areaNormal(p)); - const scalar s(Foam::mag(n)); - return s < ROOTVSMALL ? Zero : n/s; + vector n(areaNormal(p)); + (void) n.normalise(ROOTVSMALL); + return n; } diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H index 5707547858..132535f119 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H +++ b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H @@ -129,6 +129,10 @@ template inline Point Foam::line::unitVec() const { const Point v = (b_ - a_); + + #ifdef __clang__ + volatile // Use volatile to avoid aggressive branch optimization + #endif const scalar s(::Foam::mag(v)); return s < ROOTVSMALL ? Zero : v/s; diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H index 21d63cd4c9..0c84feb0b4 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -175,9 +175,9 @@ inline Foam::vector Foam::triangle::unitNormal const Point& p2 ) { - const vector n(areaNormal(p0, p1, p2)); - const scalar s(::Foam::mag(n)); - return s < ROOTVSMALL ? Zero : n/s; + vector n(areaNormal(p0, p1, p2)); + (void) n.normalise(ROOTVSMALL); + return n; } diff --git a/src/OpenFOAM/primitives/Vector/VectorI.H b/src/OpenFOAM/primitives/Vector/VectorI.H index 0b580b7451..2b5449f680 100644 --- a/src/OpenFOAM/primitives/Vector/VectorI.H +++ b/src/OpenFOAM/primitives/Vector/VectorI.H @@ -120,6 +120,9 @@ inline Foam::scalar Foam::Vector::dist(const Vector& v2) const template inline Foam::Vector& Foam::Vector::normalise(const scalar tol) { + #ifdef __clang__ + volatile // Use volatile to avoid aggressive branch optimization + #endif const scalar s = this->mag(); if (s < tol) diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H index 34ba8a1393..812b369986 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H @@ -490,6 +490,9 @@ inline VectorSpace normalised const VectorSpace& vs ) { + #ifdef __clang__ + volatile // Use volatile to avoid aggressive branch optimization + #endif const scalar s(mag(vs)); return s < ROOTVSMALL ? Zero : vs/s; } diff --git a/src/OpenFOAM/primitives/complex/complexI.H b/src/OpenFOAM/primitives/complex/complexI.H index ceda93cb89..06b725495f 100644 --- a/src/OpenFOAM/primitives/complex/complexI.H +++ b/src/OpenFOAM/primitives/complex/complexI.H @@ -210,6 +210,7 @@ inline complex sqr(const complex& c) //- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum inline complex sign(const complex& c) { + // TBD: Use volatile to avoid aggressive branch optimization const scalar s(mag(c)); return s < ROOTVSMALL ? Zero : c/s; } diff --git a/src/OpenFOAM/primitives/quaternion/quaternion.H b/src/OpenFOAM/primitives/quaternion/quaternion.H index 2f3c954b10..677ee5e3fa 100644 --- a/src/OpenFOAM/primitives/quaternion/quaternion.H +++ b/src/OpenFOAM/primitives/quaternion/quaternion.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019-2022 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -215,7 +215,7 @@ public: //- Inplace normalise the quaternion by its magnitude // For small magnitudes (less than ROOTVSMALL) set to zero. - inline quaternion& normalise(); + inline quaternion& normalise(const scalar tol = ROOTVSMALL); // Transform diff --git a/src/OpenFOAM/primitives/quaternion/quaternionI.H b/src/OpenFOAM/primitives/quaternion/quaternionI.H index 84592253e5..bc6d188463 100644 --- a/src/OpenFOAM/primitives/quaternion/quaternionI.H +++ b/src/OpenFOAM/primitives/quaternion/quaternionI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019-2022 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -306,13 +306,14 @@ inline Foam::vector& Foam::quaternion::v() noexcept } -inline Foam::quaternion& Foam::quaternion::normalise() +inline Foam::quaternion& Foam::quaternion::normalise(const scalar tol) { + // TBD: Use volatile to avoid aggressive branch optimization const scalar s(Foam::mag(*this)); - if (s < ROOTVSMALL) + if (s < tol) { - *this = Zero; + *this = Foam::zero{}; } else { diff --git a/src/finiteArea/faMesh/faMeshDemandDrivenData.C b/src/finiteArea/faMesh/faMeshDemandDrivenData.C index 9d7b41c162..3f94292886 100644 --- a/src/finiteArea/faMesh/faMeshDemandDrivenData.C +++ b/src/finiteArea/faMesh/faMeshDemandDrivenData.C @@ -68,6 +68,7 @@ static inline vector areaInvDistSqrWeightedNormal const vector& b ) { + // TBD: Use volatile to avoid aggressive branch optimization const scalar s(2*magSqr(a)*magSqr(b)); return s < ROOTVSMALL ? Zero : (a ^ b) / s;