From 0472dc1198382275f34500c2647c23818795db1e Mon Sep 17 00:00:00 2001 From: Allison Vacanti Date: Fri, 25 Aug 2017 12:49:32 -0400 Subject: [PATCH] Fix warning on Cuda. assert(false && ""); emitted a "warning : controlling expression is constant" Replace the assertion with an exception, which is more appropriate here anyway. --- vtkm/cont/ArrayHandleDiscard.h | 7 +-- vtkm/internal/CMakeLists.txt | 1 + vtkm/internal/Unreachable.h | 102 +++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 vtkm/internal/Unreachable.h diff --git a/vtkm/cont/ArrayHandleDiscard.h b/vtkm/cont/ArrayHandleDiscard.h index dd7b21916..7af888428 100644 --- a/vtkm/cont/ArrayHandleDiscard.h +++ b/vtkm/cont/ArrayHandleDiscard.h @@ -22,6 +22,7 @@ #include #include +#include #include @@ -66,11 +67,9 @@ public: VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; } - ValueType Get(vtkm::Id index) const + ValueType Get(vtkm::Id) const { - VTKM_ASSERT(index < this->GetNumberOfValues()); - VTKM_ASSERT("Method not supported for ArrayPortalDiscard." && false); - (void)index; + VTKM_UNREACHABLE("Cannot read from ArrayHandleDiscard."); return vtkm::TypeTraits::ZeroInitialization(); } diff --git a/vtkm/internal/CMakeLists.txt b/vtkm/internal/CMakeLists.txt index fc2c4107f..ba04cb604 100755 --- a/vtkm/internal/CMakeLists.txt +++ b/vtkm/internal/CMakeLists.txt @@ -59,6 +59,7 @@ set(headers IntegerSequence.h Invocation.h ListTagDetail.h + Unreachable.h Windows.h ) diff --git a/vtkm/internal/Unreachable.h b/vtkm/internal/Unreachable.h new file mode 100644 index 000000000..99d443043 --- /dev/null +++ b/vtkm/internal/Unreachable.h @@ -0,0 +1,102 @@ +//============================================================================ +// Copyright (c) Kitware, Inc. +// All rights reserved. +// See LICENSE.txt for details. +// This software is distributed WITHOUT ANY WARRANTY; without even +// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the above copyright notice for more information. +// +// Copyright 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS). +// Copyright 2017 UT-Battelle, LLC. +// Copyright 2017 Los Alamos National Security. +// +// Under the terms of Contract DE-NA0003525 with NTESS, +// the U.S. Government retains certain rights in this software. +// +// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National +// Laboratory (LANL), the U.S. Government retains certain rights in +// this software. +//============================================================================ + +#ifndef vtk_m_Unreachable_h +#define vtk_m_Unreachable_h + +/// VTKM_UNREACHABLE is similar to VTK_ASSUME, with the significant difference +/// that it is not conditional. Control should never reach a path containing +/// a VTKM_UNREACHABLE statement under any circumstances. +/// +/// If assertions are enabled (e.g. neither NDEBUG nor VTKM_NO_ASSERT is +/// defined), the following steps are taken: +/// 1. Print an error message containing the macro argument and location of the +/// VTKM_UNREACHABLE call. +/// 2. Abort the kernel (if CUDA) or process. +/// +/// This allows bad code paths to be identified during development and +/// debugging. +/// +/// If assertions are disabled and the compiler has some sort of 'unreachable' +/// intrinsic used to provide optimization hints, the intrinsic is used to +/// notify the compiler that this is a dead code path. +/// +#define VTKM_UNREACHABLE(msg) \ + VTKM_SWALLOW_SEMICOLON_PRE_BLOCK \ + { \ + VTKM_UNREACHABLE_IMPL(); \ + VTKM_UNREACHABLE_PRINT(msg); \ + VTKM_UNREACHABLE_ABORT(); \ + } \ + VTKM_SWALLOW_SEMICOLON_POST_BLOCK + +// VTKM_UNREACHABLE_IMPL is compiler-specific: +#if defined(__CUDA_ARCH__) + +#define VTKM_UNREACHABLE_IMPL() (void)0 /* no-op, no known intrinsic */ + +#if defined(NDEBUG) || defined(VTKM_NO_ASSERT) + +#define VTKM_UNREACHABLE_PRINT(msg) (void)0 /* no-op */ +#define VTKM_UNREACHABLE_ABORT() (void)0 /* no-op */ + +#else // NDEBUG || VTKM_NO_ASSERT + +#define VTKM_UNREACHABLE_PRINT(msg) \ + printf("Unreachable location reached: %s\nLocation: %s:%d\n", msg, __FILE__, __LINE__) +#define VTKM_UNREACHABLE_ABORT() \ + asm("trap;") /* Triggers kernel exit with CUDA error 73: Illegal inst */ + +#endif // NDEBUG || VTKM_NO_ASSERT + +#else // !CUDA + + +#if defined(NDEBUG) || defined(VTKM_NO_ASSERT) + +#define VTKM_UNREACHABLE_PRINT(msg) (void)0 /* no-op */ +#define VTKM_UNREACHABLE_ABORT() (void)0 /* no-op */ + +#if defined(VTKM_MSVC) +#define VTKM_UNREACHABLE_IMPL() __assume(false) +#elif defined(VTKM_ICC) +#define VTKM_UNREACHABLE_IMPL() __assume(false) +#elif defined(VTKM_GCC) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) +// Added in 4.5.0: +#define VTKM_UNREACHABLE_IMPL() __builtin_unreachable() +#elif defined(VTKM_CLANG) +#define VTKM_UNREACHABLE_IMPL() __builtin_unreachable() +#else +#define VTKM_UNREACHABLE_IMPL() (void)0 /* no-op */ +#endif + +#else // NDEBUG || VTKM_NO_ASSERT + +#define VTKM_UNREACHABLE_IMPL() (void)0 +#define VTKM_UNREACHABLE_PRINT(msg) \ + std::cerr << "Unreachable location reached: " << msg << "\n" \ + << "Location: " << __FILE__ << ":" << __LINE__ << "\n" +#define VTKM_UNREACHABLE_ABORT() abort() + +#endif // NDEBUG && !VTKM_NO_ASSERT + +#endif // !CUDA + +#endif //vtk_m_Unreachable_h