Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default type for 1<<x to int. #3400

Merged
merged 2 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -786,4 +786,5 @@ def HLSLAttributeType : DiagGroup<"attribute-type">;
def HLSLSpecifierOverride : DiagGroup<"specifier-override">;
def HLSLPackOffsetOverride : DiagGroup<"packoffset-override">;
def HLSLCommaInInit : DiagGroup<"comma-in-init">;
def HLSLAmbigLitShift : DiagGroup<"ambig-lit-shift">;
// HLSL Change Ends
3 changes: 3 additions & 0 deletions tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -7624,6 +7624,9 @@ def warn_hlsl_narrowing : Warning<
def warn_hlsl_sema_minprecision_promotion : Warning <
"%0 is promoted to %1">,
InGroup<Conversion>, DefaultWarn;
def warn_hlsl_ambiguous_literal_shift : Warning <
"ambiguous type for bit shift; use a type suffix on literal values, like 'L' or 'U', or a cast">,
InGroup<HLSLAmbigLitShift>, DefaultWarn;
def warn_hlsl_implicit_vector_truncation : Warning<
"implicit truncation of vector type">,
InGroup<Conversion>, DefaultWarn;
Expand Down
6 changes: 5 additions & 1 deletion tools/clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9104,7 +9104,11 @@ void HLSLExternalSource::CheckBinOpForHLSL(
rightElementKind != AR_BASIC_LITERAL_INT &&
rightElementKind != AR_BASIC_LITERAL_FLOAT) {
// For case like 1<<x.
resultElementKind = AR_BASIC_UINT32;
m_sema->Diag(OpLoc, diag::warn_hlsl_ambiguous_literal_shift);
if (rightElementKind == AR_BASIC_UINT32)
resultElementKind = AR_BASIC_UINT32;
else
resultElementKind = AR_BASIC_INT32;
} else if (resultElementKind == AR_BASIC_BOOL &&
BinaryOperatorKindRequiresBoolAsNumeric(Opc)) {
resultElementKind = AR_BASIC_INT32;
Expand Down
18 changes: 18 additions & 0 deletions tools/clang/test/HLSL/literals.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ uint64_t overload2(uint64_t v1, uint64_t v2) { return (uint64_t)700; } /* fxc-e
min16float m16f;
min16float4x4 m16f4x4;


int i;
uint u;
min16int m16i;
min16uint m16u;

float test() {
// Ambiguous due to literal int and literal float:
VERIFY_TYPES(float, overload1(1.5)); /* expected-error {{call to 'overload1' is ambiguous}} fxc-error {{X3067: 'overload1': ambiguous function call}} */
Expand Down Expand Up @@ -186,5 +192,17 @@ float test() {
VERIFY_TYPE_CONVERSION(min16int, 1UL); /* fxc-warning {{X3205: conversion from larger type to smaller, possible loss of data}} */
VERIFY_TYPE_CONVERSION(min16int, 1LL); /* fxc-error {{X3000: syntax error: unexpected token 'L'}} */

// Type depends on LHS
VERIFY_TYPES(int, 1L << u);
VERIFY_TYPES(uint, 1U << i);
// FXC behavior: pick uint if RHS is uint, otherwise, pick int
// DXC: Warn for ambiguous literal LHS, then match FXC for type
VERIFY_TYPES(int, 1 << i);/* expected-warning {{ambiguous type for bit shift; use a type suffix on literal values, like 'L' or 'U', or a cast}} */
VERIFY_TYPES(uint, 1 << u);/* expected-warning {{ambiguous type for bit shift; use a type suffix on literal values, like 'L' or 'U', or a cast}} */
VERIFY_TYPES(int, 1 << m16i);/* expected-warning {{ambiguous type for bit shift; use a type suffix on literal values, like 'L' or 'U', or a cast}} */
VERIFY_TYPES(int, 1 << m16u);/* expected-warning {{ambiguous type for bit shift; use a type suffix on literal values, like 'L' or 'U', or a cast}} */

return 0.0f;
}

int half_btf(int w,int bit) { return (w + (1<<bit)) >> bit; } /* expected-warning {{ambiguous type for bit shift; use a type suffix on literal values, like 'L' or 'U', or a cast}} */
26 changes: 26 additions & 0 deletions tools/clang/test/HLSLFileCheck/hlsl/operators/binary/shr.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %dxc -E main -T ps_6_0 -DDEFAULT %s | FileCheck %s -check-prefix=DEFAULT
// RUN: %dxc -E main -T ps_6_0 -DUINT_SHIFT %s | FileCheck %s -check-prefix=UINT
// RUN: %dxc -E main -T ps_6_0 -DINT_SHIFT %s | FileCheck %s -check-prefix=INT

// Make sure generate ashr and lshr
// DEFAULT:ashr
// UINT:lshr
// INT:ashr

int i;

#ifdef DEFAULT
int half_btf(int w,int bit) { return (w + (1<<bit)) >> bit; }
#endif

#ifdef UINT_SHIFT
int half_btf(int w,int bit) { return (w + (1U<<bit)) >> bit; }
#endif

#ifdef INT_SHIFT
int half_btf(int w,int bit) { return (w + (1L<<bit)) >> bit; }
#endif

int main(int w:W) : SV_Target {
return half_btf(i,12);
}