Skip to content

Commit

Permalink
merge main into amd-staging
Browse files Browse the repository at this point in the history
Change-Id: I9b7530b0a747ecc4217933773520b2d5eb162e69
  • Loading branch information
ronlieb committed Aug 25, 2024
2 parents 4047a72 + 1193f7d commit c828e74
Show file tree
Hide file tree
Showing 37 changed files with 542 additions and 295 deletions.
4 changes: 3 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class AnnotatingParser {
if (Precedence > prec::Conditional && Precedence < prec::Relational)
return false;
}
if (Prev.is(TT_ConditionalExpr))
if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
SeenTernaryOperator = true;
updateParameterCount(Left, CurrentToken);
if (Style.Language == FormatStyle::LK_Proto) {
Expand Down Expand Up @@ -2875,6 +2875,8 @@ class AnnotatingParser {
// Search for unexpected tokens.
for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous) {
if (Prev->is(tok::r_paren)) {
if (Prev->is(TT_CastRParen))
return false;
Prev = Prev->MatchingParen;
if (!Prev)
return false;
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) {
EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[8], tok::greater, TT_BinaryOperator);

Tokens = annotate("return checklower ? a < b : a > b;");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[8], tok::greater, TT_BinaryOperator);

Tokens = annotate("return A < B ^ A > B;");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
Expand Down Expand Up @@ -747,6 +752,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_CastRParen);
EXPECT_TOKEN(Tokens[10], tok::amp, TT_UnaryOperator);

Tokens = annotate("int result = ((int)a) - b;");
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_CastRParen);
EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[9], tok::minus, TT_BinaryOperator);

auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
Expand Down
9 changes: 5 additions & 4 deletions clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
/// \000\020#pragma-messages\t#warnings\020CFString-literal"
/// };
/// \endcode
static void emitDiagGroupNames(StringToOffsetTable &GroupNames,
static void emitDiagGroupNames(const StringToOffsetTable &GroupNames,
raw_ostream &OS) {
OS << "static const char DiagGroupNames[] = {\n";
GroupNames.EmitString(OS);
Expand All @@ -1656,7 +1656,7 @@ static void emitDiagGroupNames(StringToOffsetTable &GroupNames,
static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
RecordVec &DiagsInPedantic,
RecordVec &GroupsInPedantic,
StringToOffsetTable &GroupNames,
const StringToOffsetTable &GroupNames,
raw_ostream &OS) {
OS << "\n#ifdef GET_DIAG_ARRAYS\n";
emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS);
Expand All @@ -1683,7 +1683,8 @@ static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
RecordVec &DiagsInPedantic,
RecordVec &GroupsInPedantic,
StringToOffsetTable &GroupNames, raw_ostream &OS) {
const StringToOffsetTable &GroupNames,
raw_ostream &OS) {
unsigned MaxLen = 0;

for (auto const &I: DiagsInGroup)
Expand All @@ -1705,7 +1706,7 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
OS << I.first << " */, ";
// Store a pascal-style length byte at the beginning of the string.
std::string Name = char(I.first.size()) + I.first;
OS << GroupNames.GetOrAddStringOffset(Name, false) << ", ";
OS << *GroupNames.GetStringOffset(Name) << ", ";

// Special handling for 'pedantic'.
const bool IsPedantic = I.first == "pedantic";
Expand Down
34 changes: 30 additions & 4 deletions compiler-rt/lib/nsan/nsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,21 @@ __nsan_dump_shadow_mem(const u8 *addr, size_t size_bytes, size_t bytes_per_line,
}
}

alignas(16) SANITIZER_INTERFACE_ATTRIBUTE
alignas(64) SANITIZER_INTERFACE_ATTRIBUTE
thread_local uptr __nsan_shadow_ret_tag = 0;

alignas(16) SANITIZER_INTERFACE_ATTRIBUTE
alignas(64) SANITIZER_INTERFACE_ATTRIBUTE
thread_local char __nsan_shadow_ret_ptr[kMaxVectorWidth *
sizeof(__float128)];

alignas(16) SANITIZER_INTERFACE_ATTRIBUTE
alignas(64) SANITIZER_INTERFACE_ATTRIBUTE
thread_local uptr __nsan_shadow_args_tag = 0;

// Maximum number of args. This should be enough for anyone (tm). An alternate
// scheme is to have the generated code create an alloca and make
// __nsan_shadow_args_ptr point ot the alloca.
constexpr const int kMaxNumArgs = 128;
alignas(16) SANITIZER_INTERFACE_ATTRIBUTE
alignas(64) SANITIZER_INTERFACE_ATTRIBUTE
thread_local char __nsan_shadow_args_ptr[kMaxVectorWidth * kMaxNumArgs *
sizeof(__float128)];

Expand All @@ -445,6 +445,32 @@ int32_t checkFT(const FT value, ShadowFT Shadow, CheckTypeT CheckType,
const InternalFT check_value = value;
const InternalFT check_shadow = Shadow;

// We only check for NaNs in the value, not the shadow.
if (flags().check_nan && isnan(check_value)) {
GET_CALLER_PC_BP;
BufferedStackTrace stack;
stack.Unwind(pc, bp, nullptr, false);
if (GetSuppressionForStack(&stack, CheckKind::Consistency)) {
// FIXME: optionally print.
return flags().resume_after_suppression ? kResumeFromValue
: kContinueWithShadow;
}
Decorator D;
Printf("%s", D.Warning());
Printf("WARNING: NumericalStabilitySanitizer: NaN detected\n");
Printf("%s", D.Default());
stack.Print();
if (flags().halt_on_error) {
if (common_flags()->abort_on_error)
Printf("ABORTING\n");
else
Printf("Exiting\n");
Die();
}
// Performing other tests for NaN values is meaningless when dealing with numbers.
return kResumeFromValue;
}

// See this article for an interesting discussion of how to compare floats:
// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
static constexpr const FT Eps = FTInfo<FT>::kEpsilon;
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/nsan/nsan_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ NSAN_FLAG(bool, enable_loadtracking_stats, false,
"due to invalid or unknown types.")
NSAN_FLAG(bool, poison_in_free, true, "")
NSAN_FLAG(bool, print_stats_on_exit, false, "If true, print stats on exit.")
NSAN_FLAG(bool, check_nan, false,
"If true, check the floating-point number is nan")
25 changes: 25 additions & 0 deletions compiler-rt/test/nsan/nan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clangxx_nsan -O0 -g %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s

// RUN: %clangxx_nsan -O3 -g %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s

// RUN: %clangxx_nsan -O0 -g %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=1 not %run %t

#include <cmath>
#include <cstdio>

// This function returns a NaN value for triggering the NaN detection.
__attribute__((noinline)) float ReturnNaN(float p, float q) {
float ret = p / q;
return ret;
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
}

int main() {
float val = ReturnNaN(0., 0.);
printf("%f\n", val);
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
return 0;
}
54 changes: 54 additions & 0 deletions compiler-rt/test/nsan/softmax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: %clangxx_nsan -O0 -g -DSOFTMAX=softmax %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0,log2_max_relative_error=19 %run %t 2>&1 | FileCheck %s

// RUN: %clangxx_nsan -O3 -g -DSOFTMAX=softmax %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0,log2_max_relative_error=19 %run %t 2>&1 | FileCheck %s

// RUN: %clangxx_nsan -O0 -g -DSOFTMAX=stable_softmax %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=1,log2_max_relative_error=19 %run %t

// RUN: %clangxx_nsan -O3 -g -DSOFTMAX=stable_softmax %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=1,log2_max_relative_error=19 %run %t

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>

// unstable softmax
template <typename T>
__attribute__((noinline)) void softmax(std::vector<T> &values) {
T sum_exp = 0.0;
for (auto &i: values) {
i = std::exp(i);
sum_exp += i;
}
for (auto &i: values) {
i /= sum_exp;
}
}

// use max value to avoid overflow
// \sigma_i exp(x_i) / \sum_j exp(x_j) = \sigma_i exp(x_i - max(x)) / \sum_j exp(x_j - max(x))
template <typename T>
__attribute__((noinline)) void stable_softmax(std::vector<T> &values) {
T sum_exp = 0.0;
T max_values = *std::max_element(values.begin(), values.end());
for (auto &i: values) {
i = std::exp(i - max_values);
sum_exp += i;
}
for (auto &i:values) {
i /= sum_exp;
}
}

int main() {
std::vector<double> data = {1000, 1001, 1002};
SOFTMAX(data);
for (auto i: data) {
printf("%f", i);
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
}
return 0;
}
34 changes: 34 additions & 0 deletions compiler-rt/test/nsan/vec_sqrt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clangxx_nsan -O0 -g -mavx %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s
// RUN: %clangxx_nsan -O3 -g -mavx %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s

#include <cmath>
#include <immintrin.h>
#include <iostream>

void simd_sqrt(const float *input, float *output, size_t size) {
size_t i = 0;
for (; i + 7 < size; i += 8) {
__m256 vec = _mm256_loadu_ps(&input[i]);
__m256 result = _mm256_sqrt_ps(vec);
_mm256_storeu_ps(&output[i], result);
}
for (; i < size; ++i) {
output[i] = std::sqrt(input[i]);
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
}
}

int main() {
float input[] = {1.0, 2.0, -3.0, 4.0, 5.0, 6.0, 7.0,
8.0, 9.0, -10.0, 11.0, 12.0, 13.0, 14.0,
15.0, -16.0, 17.0, -18.0, -19.0, -20.0};
float output[20];
simd_sqrt(input, output, 20);
for (int i = 0; i < 20; ++i) {
std::cout << output[i] << std::endl;
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
}
return 0;
}
25 changes: 25 additions & 0 deletions compiler-rt/test/nsan/vec_sqrt_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clangxx_nsan -O0 -g -mavx %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s
// RUN: %clangxx_nsan -O3 -g -mavx %s -o %t
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s
#include <iostream>
#include <cmath>

typedef float v8sf __attribute__ ((vector_size(32)));

v8sf simd_sqrt(v8sf a) {
return __builtin_elementwise_sqrt(a);
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
}

int main() {
v8sf a = {-1.0, -2.0, -3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
a = simd_sqrt(a);

// This prevents DCE.
for (size_t i = 0; i < 8; ++i) {
std::cout << a[i] << std::endl;
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected
}
return 0;
}
4 changes: 4 additions & 0 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@ void TypeSystemClang::CreateASTContext() {
TargetInfo *target_info = getTargetInfo();
if (target_info)
m_ast_up->InitBuiltinTypes(*target_info);
else if (auto *log = GetLog(LLDBLog::Expressions))
LLDB_LOG(log,
"Failed to initialize builtin ASTContext types for target '{0}'",
m_target_triple);

GetASTMap().Insert(m_ast_up.get(), this);

Expand Down
2 changes: 2 additions & 0 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,8 @@ set(LLVM_USE_STATIC_ZSTD FALSE CACHE BOOL "Use static version of zstd. Can be TR

set(LLVM_ENABLE_CURL "OFF" CACHE STRING "Use libcurl for the HTTP client if available. Can be ON, OFF, or FORCE_ON")

set(LLVM_HAS_LOGF128 "OFF" CACHE STRING "Use logf128 to constant fold fp128 logarithm calls. Can be ON, OFF, or FORCE_ON")

set(LLVM_ENABLE_HTTPLIB "OFF" CACHE STRING "Use cpp-httplib HTTP server library if available. Can be ON, OFF, or FORCE_ON")

set(LLVM_Z3_INSTALL_DIR "" CACHE STRING "Install directory of the Z3 solver.")
Expand Down
18 changes: 11 additions & 7 deletions llvm/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ else()
set(HAVE_LIBEDIT 0)
endif()

if(LLVM_HAS_LOGF128)
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)

if(LLVM_HAS_LOGF128 STREQUAL FORCE_ON AND NOT HAS_LOGF128)
message(FATAL_ERROR "Failed to configure logf128")
endif()

set(LLVM_HAS_LOGF128 "${HAS_LOGF128}")
endif()

# function checks
check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
find_package(Backtrace)
Expand All @@ -259,13 +270,6 @@ if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
endif()

check_cxx_symbol_exists(logf128 cmath HAS_LOGF128)
check_symbol_exists(__powerpc__ "" __PPC64LE)
if(HAS_LOGF128 AND NOT __PPC64LE)
set(LLVM_HAS_LOGF128 On)
add_compile_definitions(HAS_LOGF128)
endif()

# Determine whether we can register EH tables.
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)
Expand Down
15 changes: 12 additions & 3 deletions llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/float128.h"
#include <memory>

#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
Expand Down Expand Up @@ -377,6 +378,9 @@ class IEEEFloat final : public APFloatBase {
Expected<opStatus> convertFromString(StringRef, roundingMode);
APInt bitcastToAPInt() const;
double convertToDouble() const;
#ifdef HAS_IEE754_FLOAT128
float128 convertToQuad() const;
#endif
float convertToFloat() const;

/// @}
Expand Down Expand Up @@ -1270,9 +1274,14 @@ class APFloat : public APFloatBase {
/// shorter semantics, like IEEEsingle and others.
double convertToDouble() const;

/// Return true if this APFloat has quadruple precision floating point
/// semantics
bool isValidIEEEQuad() const;
/// Converts this APFloat to host float value.
///
/// \pre The APFloat must be built using semantics, that can be represented by
/// the host float type without loss of precision. It can be IEEEquad and
/// shorter semantics, like IEEEdouble and others.
#ifdef HAS_IEE754_FLOAT128
float128 convertToQuad() const;
#endif

/// Converts this APFloat to host float value.
///
Expand Down
Loading

0 comments on commit c828e74

Please sign in to comment.