Skip to content

Commit b0dfb1a

Browse files
Don't emit a warning when implicit casting from known in-range int lit to half. (#5814)
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 945d8dd commit b0dfb1a

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

source/slang/slang-check-conversion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ bool SemanticsVisitor::_coerce(
13271327
// For general types of implicit conversions, we issue a warning, unless `fromExpr`
13281328
// is a known constant and we know it won't cause a problem.
13291329
bool shouldEmitGeneralWarning = true;
1330-
if (isScalarIntegerType(toType))
1330+
if (isScalarIntegerType(toType) || isHalfType(toType))
13311331
{
13321332
if (auto intVal = tryFoldIntegerConstantExpression(
13331333
fromExpr,

source/slang/slang-check-decl.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -7421,6 +7421,15 @@ bool SemanticsVisitor::isScalarIntegerType(Type* type)
74217421
return isIntegerBaseType(baseType) || baseType == BaseType::Bool;
74227422
}
74237423

7424+
bool SemanticsVisitor::isHalfType(Type* type)
7425+
{
7426+
auto basicType = as<BasicExpressionType>(type);
7427+
if (!basicType)
7428+
return false;
7429+
auto baseType = basicType->getBaseType();
7430+
return baseType == BaseType::Half;
7431+
}
7432+
74247433
bool SemanticsVisitor::isValidCompileTimeConstantType(Type* type)
74257434
{
74267435
return isScalarIntegerType(type) || isEnumType(type);
@@ -7466,6 +7475,9 @@ bool SemanticsVisitor::isIntValueInRangeOfType(IntegerLiteralValue value, Type*
74667475
#endif
74677476
return value >= std::numeric_limits<int64_t>::min() &&
74687477
value <= std::numeric_limits<int64_t>::max();
7478+
7479+
case BaseType::Half:
7480+
return value >= -2048 && value <= 2048;
74697481
default:
74707482
return false;
74717483
}

source/slang/slang-check-impl.h

+3
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,9 @@ struct SemanticsVisitor : public SemanticsContext
19671967
/// Is `type` a scalar integer type.
19681968
bool isScalarIntegerType(Type* type);
19691969

1970+
/// Is `type` a scalar half type.
1971+
bool isHalfType(Type* type);
1972+
19701973
/// Is `type` something we allow as compile time constants, i.e. scalar integer and enum types.
19711974
bool isValidCompileTimeConstantType(Type* type);
19721975

tests/bugs/half-coercion.slang

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -target spirv
2+
3+
// CHECK-NOT: warning
4+
5+
RWStructuredBuffer<half> output;
6+
[numthreads(1,1,1)]
7+
void computeMain()
8+
{
9+
output[0] = 0; // coercion from 0 to half should not result in a warning.
10+
}

0 commit comments

Comments
 (0)