Skip to content

Commit

Permalink
Split Abort definition from targets.cc
Browse files Browse the repository at this point in the history
This allows compilation without the dynamic dispatch machinery and gives
an example of the interface for the `Abort` function should you wish to
write your own.
  • Loading branch information
Mousius committed Apr 6, 2024
1 parent 06d42c6 commit a92d452
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ COMPAT = [
cc_library(
name = "hwy",
srcs = [
"hwy/abort.cc",
"hwy/aligned_allocator.cc",
"hwy/per_target.cc",
"hwy/print.cc",
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Copyright 2019 Google LLC
# Copyright 2024 Arm Limited and/or its affiliates <[email protected]>
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: BSD-3-Clause
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -132,6 +135,7 @@ list(APPEND HWY_CONTRIB_SOURCES
endif() # HWY_ENABLE_CONTRIB

set(HWY_SOURCES
hwy/abort.cc
hwy/aligned_allocator.cc
hwy/aligned_allocator.h
hwy/base.h
Expand Down
40 changes: 40 additions & 0 deletions hwy/abort.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019 Google LLC
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: BSD-3-Clause

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>

#include "hwy/base.h"

namespace hwy {

HWY_DLLEXPORT HWY_NORETURN void HWY_FORMAT(3, 4)
Abort(const char* file, int line, const char* format, ...) {
char buf[800];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);

fprintf(stderr, "Abort at %s:%d: %s\n", file, line, buf);

// If compiled with any sanitizer, they can also print a stack trace.
#if HWY_IS_ASAN || HWY_IS_MSAN || HWY_IS_TSAN
__sanitizer_print_stack_trace();
#endif // HWY_IS_*
fflush(stderr);

// Now terminate the program:
#if HWY_ARCH_RVV
exit(1); // trap/abort just freeze Spike.
#elif HWY_IS_DEBUG_BUILD && !HWY_COMPILER_MSVC
// Facilitates breaking into a debugger, but don't use this in non-debug
// builds because it looks like "illegal instruction", which is misleading.
__builtin_trap();
#else
abort(); // Compile error without this due to HWY_NORETURN.
#endif
}
}
30 changes: 0 additions & 30 deletions hwy/targets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

#include "hwy/targets.h"

#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h> // abort / exit

#include "hwy/detect_targets.h"
#include "hwy/highway.h"
Expand Down Expand Up @@ -545,34 +543,6 @@ int64_t DetectTargets() {

} // namespace

HWY_DLLEXPORT HWY_NORETURN void HWY_FORMAT(3, 4)
Abort(const char* file, int line, const char* format, ...) {
char buf[800];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);

fprintf(stderr, "Abort at %s:%d: %s\n", file, line, buf);

// If compiled with any sanitizer, they can also print a stack trace.
#if HWY_IS_ASAN || HWY_IS_MSAN || HWY_IS_TSAN
__sanitizer_print_stack_trace();
#endif // HWY_IS_*
fflush(stderr);

// Now terminate the program:
#if HWY_ARCH_RVV
exit(1); // trap/abort just freeze Spike.
#elif HWY_IS_DEBUG_BUILD && !HWY_COMPILER_MSVC
// Facilitates breaking into a debugger, but don't use this in non-debug
// builds because it looks like "illegal instruction", which is misleading.
__builtin_trap();
#else
abort(); // Compile error without this due to HWY_NORETURN.
#endif
}

HWY_DLLEXPORT void DisableTargets(int64_t disabled_targets) {
supported_mask_ = static_cast<int64_t>(~disabled_targets);
// This will take effect on the next call to SupportedTargets, which is
Expand Down

0 comments on commit a92d452

Please sign in to comment.