Skip to content

Support Unsigned for shorthand vectors/matrices - #106

Merged
Young Kim (yskim1501) merged 4 commits into
microsoft:dxil-v1.0from
yskim1501:unsigned
Mar 15, 2017
Merged

Young Kim (yskim1501) merged 4 commits into
microsoft:dxil-v1.0from
yskim1501:unsigned

Conversation

@yskim1501

Copy link
Copy Markdown
Contributor
  1. enable unsigned keyword for int vectors/matrices
    (for example, "unsigned int2" will be converted to "vector<unsigned int, 2>")
  2. disable unsigned keyword as a complete type specifier to be consistent with old compiler.

1. enable unsigned keyword for int vectors/matrices
   (e.g. unsigned int2 -> vector<unsigned int, 2>)
2. disable unsigned keyword as a complete type specifier
@msftclas

Youngkim93,
Thanks for your contribution as a Microsoft full-time employee or intern. You do not need to sign a CLA.
Thanks,
Microsoft Pull Request Bot


// This function reads the given declaration TSS and returns the corresponding parsedType with the
// corresponding type. Replaces the given parsed type with the new type
clang::ParsedType ApplyTypeSpecSignToParsedType(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to operate on QualType rather than ParsedType at this level, since that's all we actually care about.

// signed/unsigned are only valid with int/char/wchar_t.
if (TypeSpecSign != TSS_unspecified) {
if (TypeSpecType == TST_unspecified)
TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of deleting the code that defaults to TST_int, can you "#if 0 // HLSL Change: ..." it out (makes integration with newer code easier).

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
bool isStatic = storage == DeclSpec::SCS::SCS_static;
bool isExtern = storage == DeclSpec::SCS::SCS_extern;

bool isSigned = D.getDeclSpec().getTypeSpecSign() != DeclSpec::TSS::TSS_unspecified;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSigned is a misleading name. Perhaps hasSignSpec instead?

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
if (isSigned) {
HLSLExternalSource *hlslSource = HLSLExternalSource::FromSema(this);
ArTypeObjectKind objKind = hlslSource->GetTypeObjectKind(qt);
if (objKind != AR_TOBJ_MATRIX && objKind != AR_TOBJ_VECTOR && objKind != AR_TOBJ_BASIC) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should additionally get the component type and verify that it is an int/uint type, since I don't think we want to allow you to use unsigned with bool, float, double, etc... Or does fxc allow this and ignore it?


void vector_unsigned() {
unsigned int4x2 intvector;
unsigned float2x4 floatvector; /* expected-error {{'float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be vector tests (as this is vector-syntax.hlsl)?

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
if (qts == nullptr) {
qts = CreateVectorSpecializationShorthand(*m_context, qt, scalarType,
colCount);
m_vectorTypedefs[scalarType][colCount - 1] = qts;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a good time to encapsulate this lookup + create shorthand if it doesn't exist block in a method.

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
if (qts == nullptr) {
qts = CreateVectorSpecializationShorthand(*m_context, qt, scalarType,
colCount);
m_matrixShorthandTypes[scalarType][rowCount - 1][colCount - 1] = qts;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encapsulation for matrix case as well.


void matrix_unsigned() {
unsigned int4x2 unsignedMatrix;
unsigned float1x4 unsignedFloatMatrix; /* expected-error {{'float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add more component types?

// CHECK: uint4x4

unsigned int2 a;
unsigned int4x4 b;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for signed as well? Also, try a all legal component types to ensure they translate (or pass through) properly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will not add signed cases because it is not supported in HLSL

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
_In_ clang::ParsedType &parsedType,
_In_ TypeSpecifierSign TSS,
_In_ SourceLocation Loc,
_In_ const PrintingPolicy &policy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need PrintingPolicy (since you should remove Diag - but you aren't even using it here)

 - allow other int types (min16int, uint64_t) to have unsigned shorthand vectors
 - more tests for different component types
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated

HRESULT CombineDimensions(QualType leftType, QualType rightType, QualType *resultType);

clang::TypedefDecl *HLSLExternalSource::GetMatrixShorthandTypes(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is being defined within the HLSLExternalSource class definition, there's no need for the "HLSLExternalSource::" prefixing the method name. Same with the next method below.
Also, GetMatrixShorthandTypes should probably not be plural, change to: GetMatrixShorthandType.

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
}

clang::TypedefDecl *HLSLExternalSource::GetVectorShorthandType(
QualType type, HLSLScalarType scalarType, UINT colCount) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should incorporate Lookup[Vector|Matrix]Type, eliminating the need for the QualType parameter supplied by the caller. Further, this call is only necessary when creating the specialization shorthand, so it can be placed within the if (qts == nullptr) block.

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
UINT colCount = GetHLSLVecSize(type);
QualType qt = LookupVectorType(newScalarType, colCount);
TypedefDecl *qts = GetVectorShorthandType(qt, newScalarType, colCount);
return qt;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not return the new shorthand type? Use return "m_context->getTypeDeclType(qts);" to return the shorthand type.

unsigned bool3x4 boolvector; /* expected-error {{'bool' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
unsigned half4x1 halfvector; /* expected-error {{'float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
unsigned double1x2 doublevector; /* expected-error {{'double' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
unsigned min12int2x3 min12intvector; /* expected-warning {{min12int is promoted to min16int}} fxc-error {{X3085: unsigned can not be used with type}} */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we would emit an error in this case (like fxc), not just the warning about type promotion.

 - throw an error for shorthand min12int vectors/matrices
@yskim1501
Young Kim (yskim1501) merged commit efe8227 into microsoft:dxil-v1.0 Mar 15, 2017
@yskim1501
Young Kim (yskim1501) deleted the unsigned branch March 15, 2017 00:31
Gregory Roth (pow2clk) pushed a commit to pow2clk/DirectXShaderCompiler that referenced this pull request Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants