Skip to content

Commit

Permalink
[SYCL][Sema] Only imply float size conversion warnings in SYCL (#16857)
Browse files Browse the repository at this point in the history
Before this change `-Wimplicit-float-conversion` (part of
`-Wconversion`) enabled `-Wimplicit-float-size-conversion` for all
language modes.

This is problematic because it causes DPC++ to emit warnings where
upstream clang does not, even when used as a plain C++ compiler.
Projects that enable `-Werror` don't expect these (questionable)
floating point size warnings when they enable `-Wconversion`, and as
such cannot be compiled with the DPC++ compiler.

Change `-Wimplicit-float-conversion` to only be emitted in SYCL language
mode and rename it to `-Wsycl-implicit-float-conversion`. This preserves
existing behaviour for SYCL users, but otherwise matches upstream Clang
behaviour.

Fixes: #16393
  • Loading branch information
Maetveis authored Feb 14, 2025
1 parent fdea2cb commit 254dd3b
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 29 deletions.
5 changes: 2 additions & 3 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ def ImplicitIntFloatConversion : DiagGroup<"implicit-int-float-conversion",
[ImplicitConstIntFloatConversion]>;
def ObjCSignedCharBoolImplicitFloatConversion :
DiagGroup<"objc-signed-char-bool-implicit-float-conversion">;
def ImplicitFloatSizeConversion :
DiagGroup<"implicit-float-size-conversion">;
def SyclImplicitFloatSizeConversion : DiagGroup<"sycl-implicit-float-size-conversion">;
def ImplicitFloatConversion : DiagGroup<"implicit-float-conversion",
[ImplicitIntFloatConversion,
ObjCSignedCharBoolImplicitFloatConversion, ImplicitFloatSizeConversion]>;
ObjCSignedCharBoolImplicitFloatConversion, SyclImplicitFloatSizeConversion]>;
def ImplicitFixedPointConversion : DiagGroup<"implicit-fixed-point-conversion">;

def FloatOverflowConversion : DiagGroup<"float-overflow-conversion">;
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def warn_float_compare_literal : Warning<
def warn_double_const_requires_fp64 : Warning<
"double precision constant requires %select{cl_khr_fp64|cl_khr_fp64 and __opencl_c_fp64}0, "
"casting to single precision">;
def warn_imp_float_size_conversion : Warning<
def warn_sycl_imp_float_size_conversion : Warning<
"implicit conversion between floating point types of different sizes">,
InGroup<ImplicitFloatSizeConversion>, DefaultIgnore;
InGroup<SyclImplicitFloatSizeConversion>, DefaultIgnore;
def err_half_const_requires_fp16 : Error<
"half precision constant requires cl_khr_fp16">;

Expand Down
28 changes: 17 additions & 11 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11680,27 +11680,33 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
result.Val,
Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) {
if (getLangOpts().SYCLIsDevice)
SYCL().DiagIfDeviceCode(CC, diag::warn_imp_float_size_conversion);
else
DiagnoseImpCast(*this, E, T, CC,
diag::warn_imp_float_size_conversion);
if (getLangOpts().isSYCL()) {
if (getLangOpts().SYCLIsDevice)
SYCL().DiagIfDeviceCode(
CC, diag::warn_sycl_imp_float_size_conversion);
else
DiagnoseImpCast(*this, E, T, CC,
diag::warn_sycl_imp_float_size_conversion);
}
return;
}
}

if (SourceMgr.isInSystemMacro(CC))
return;
// If there is a precision conversion between floating point types when
// -Wimplicit-float-size-conversion is passed but
// -Wsycl-implicit-float-size-conversion is passed but
// -Wimplicit-float-conversion is not, make sure we emit at least a size
// warning.
if (Diags.isIgnored(diag::warn_impcast_float_precision, CC)) {
if (getLangOpts().SYCLIsDevice)
SYCL().DiagIfDeviceCode(CC, diag::warn_imp_float_size_conversion);
else
DiagnoseImpCast(*this, E, T, CC,
diag::warn_imp_float_size_conversion);
if (getLangOpts().isSYCL()) {
if (getLangOpts().SYCLIsDevice)
SYCL().DiagIfDeviceCode(
CC, diag::warn_sycl_imp_float_size_conversion);
else
DiagnoseImpCast(*this, E, T, CC,
diag::warn_sycl_imp_float_size_conversion);
}
}
DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_float_precision);
}
Expand Down
1 change: 0 additions & 1 deletion clang/test/Sema/conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,3 @@ float double2float_test4(double a, float b) {
b -= a; // expected-warning {{implicit conversion when assigning computation result loses floating-point precision: 'double' to 'float'}}
return b;
}
float f = 1.0 / 2.0; // expected-warning {{implicit conversion between floating point types of different sizes}}
2 changes: 1 addition & 1 deletion clang/test/Sema/ext_vector_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void splats(int i, long l, __uint128_t t, float f, double d) {

vf = 1 + vf;
vf = l + vf; // expected-warning {{implicit conversion from 'long' to 'float2' (vector of 2 'float' values) may lose precision}}
vf = 2.0 + vf; // expected-warning {{implicit conversion between floating point types of different sizes}}
vf = 2.0 + vf;
vf = d + vf; // expected-warning {{implicit conversion loses floating-point precision}}
vf = vf + 0xffffffff; // expected-warning {{implicit conversion from 'unsigned int' to 'float2' (vector of 2 'float' values) changes value from 4294967295 to 4294967296}}
vf = vf + 2.1; // expected-warning {{implicit conversion loses floating-point precision}}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaHLSL/Types/Arithmetic/literal_suffixes.hlsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -fnative-half-type -Wconversion -Wno-implicit-float-size-conversion -verify %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -fnative-half-type -Wconversion -verify %s

void literal_assignments() {
half h;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -Wconversion -Wno-implicit-float-size-conversion -verify %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -Wconversion -verify %s

void literal_assignments() {
half h;
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaHLSL/standard_conversion_sequences.hlsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -Wconversion -Wno-implicit-float-size-conversion -verify -o - %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -Wno-conversion -Wno-implicit-float-size-conversion -DNO_ERR -ast-dump %s | FileCheck %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -Wconversion -verify -o - %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -Wno-conversion -DNO_ERR -ast-dump %s | FileCheck %s

void test() {

Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaSYCL/implicit-float-size-conversion.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -internal-isystem %S/Inputs -fsyntax-only -sycl-std=2020 -Wimplicit-float-size-conversion -verify=size-only,always-size %s
// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -internal-isystem %S/Inputs -fsyntax-only -sycl-std=2020 -Wsycl-implicit-float-size-conversion -verify=size-only,always-size %s
// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -internal-isystem %S/Inputs -fsyntax-only -sycl-std=2020 -Wimplicit-float-conversion -verify=always-size,precision-only %s
// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -internal-isystem %S/Inputs -fsyntax-only -sycl-std=2020 -Wimplicit-float-conversion -Wno-implicit-float-size-conversion -verify=prefer-precision %s
// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -internal-isystem %S/Inputs -fsyntax-only -sycl-std=2020 -Wimplicit-float-conversion -Wno-sycl-implicit-float-size-conversion -verify=prefer-precision %s
// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -internal-isystem %S/Inputs -fsyntax-only -sycl-std=2020 -Wno-implicit-float-conversion -verify %s

// This test checks that floating point conversion warnings are emitted correctly when used in conjunction.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
// RUN: %clang_cc1 -fsyntax-only -verify=precision-loss,precision-gain,size-change -Wimplicit-float-conversion -Wdouble-promotion -Wimplicit-float-size-conversion \
// RUN: %clang_cc1 -fsyntax-only -verify=precision-loss,precision-gain,size-change -fsycl-is-host -Wimplicit-float-conversion -Wdouble-promotion -Wsycl-implicit-float-size-conversion \
// RUN: -triple x86_64-apple-darwin %s

// RUN: %clang_cc1 -fsyntax-only -verify=size-only,precision-gain,size-change -Wdouble-promotion -Wimplicit-float-size-conversion \
// RUN: %clang_cc1 -fsyntax-only -verify=size-only,precision-gain,size-change -fsycl-is-host -Wdouble-promotion -Wsycl-implicit-float-size-conversion \
// RUN: -triple x86_64-apple-darwin %s

// RUN: %clang_cc1 -fsyntax-only -verify=precision-increase -Wdouble-promotion \
// RUN: %clang_cc1 -fsyntax-only -verify=precision-increase -fsycl-is-host -Wdouble-promotion \
// RUN: -triple x86_64-apple-darwin %s

// RUN: %clang_cc1 -fsyntax-only -verify=precision-loss,size-change -Wimplicit-float-conversion \
// RUN: %clang_cc1 -fsyntax-only -verify=precision-loss,size-change -fsycl-is-host -Wimplicit-float-conversion \
// RUN: -triple x86_64-apple-darwin %s

// RUN: %clang_cc1 -fsyntax-only -verify \
// RUN: %clang_cc1 -fsyntax-only -verify=precision-loss -Wimplicit-float-conversion \
// RUN: -triple x86_64-apple-darwin %s

// RUN: %clang_cc1 -fsyntax-only -verify -fsycl-is-host \
// RUN: -triple x86_64-apple-darwin %s

// RUN: %clang_cc1 -fsyntax-only -verify -Wsycl-implicit-float-size-conversion \
// RUN: -triple x86_64-apple-darwin %s

// This test checks that floating point conversion warnings are emitted correctly when used in conjunction.
Expand Down

0 comments on commit 254dd3b

Please sign in to comment.