Skip to content

Implement auto for C++11-style type deduction#8452

Open
llvm-beanz wants to merge 2 commits into
microsoft:mainfrom
llvm-beanz:cbieneman/auto
Open

Implement auto for C++11-style type deduction#8452
llvm-beanz wants to merge 2 commits into
microsoft:mainfrom
llvm-beanz:cbieneman/auto

Conversation

@llvm-beanz
Copy link
Copy Markdown
Collaborator

Partial implementation of C++11-stlye type deduction added to HLSL under the 202x language mode.

This is still missing special handling for non-deducible special HLSL types. The main one that comes to mind is the type of the ResourceDescriptorHeap and SamplerDescriptorHeap.

I think this PR can stand on its own because using it to infer those special types won't technically break anything it just might expose some bits and pieces of the implementation that we don't want.

This is a partial proof of concept of C++11-stlye type deduction being
added to HLSL under the 202x language mode.

This is still missing special handling for non-deducable special HLSL
types. The main one that comes to mind is the type of the
`ResourceDescriptorHeap` and `SamplerDescriptorHeap`.
Copy link
Copy Markdown
Collaborator

@bob80905 bob80905 left a comment

Choose a reason for hiding this comment

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

My only concern is that this seems to add auto for all language modes, only emitting a warning for < 202x, like C++. But the description suggests that auto is only being added for >= 202x.
Should we, unlike the C++ scenario, emit a hard error for auto < 202x?

@llvm-beanz
Copy link
Copy Markdown
Collaborator Author

My only concern is that this seems to add auto for all language modes, only emitting a warning for < 202x, like C++. But the description suggests that auto is only being added for >= 202x. Should we, unlike the C++ scenario, emit a hard error for auto < 202x?

We followed the same pattern for the groupshared argument feature in 202x, where we exposed it in earlier versions with a warning. The subtle thing that makes this different from say the change in #8453, is that this isn't actually a change to the parsing grammar, it's an additive change to the semantics. The additive change doesn't break anything in existing because we always treated auto as a reserved word.

The benefit of adding this in older language modes is that users can start using it right away without needing to deal with any of the other behavioral changes in 202x, and we can start using it right away in library code and to make library code more readable without forcing users to move to 202x in order to use the libraries.

Copy link
Copy Markdown
Contributor

@tex3d tex3d left a comment

Choose a reason for hiding this comment

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

Shouldn't we test auto with some HLSL-specific object types, plus make sure you can't use it to capture the hidden intermediate resource/sampler type from ResourceDescriptorHeap/SamplerDescriptorHeap used for dynamic resources?

typeof(g_int) g_typeof_int; // expected-error {{HLSL requires a type specifier for all declarations}} expected-error {{expected ';' after top level declarator}} expected-error {{unknown type name 'typeof'; did you mean 'typedef'?}}
typedef int (*fn_int)(int); // expected-error {{pointers are unsupported in HLSL}}
auto g_auto = 3; // expected-error {{'auto' is a reserved keyword in HLSL}} expected-error {{HLSL requires a type specifier for all declarations}}
auto g_auto = 3; // auto is now supported in HLSL via type deduction; no error expected
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.

The purpose of HLSL 2015 is best-effort alignment with fxc compatibility for Intellisense API purposes only (non-compilation).

Technically, we should block this on HLSL 2015. I recognize that it's not a priority to maintain that use case at this point, but we still support this language version, and it should be easy to block based on that.

// Test that 'auto' cannot be used to declare pointer types in HLSL.
// Pointers are unsupported in HLSL.

// CHECK: error: pointers are unsupported in HLSL
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 these be tested in a -verify test? For example in tools/clang/test/HLSL/cpp-errors.hlsl?

// Test that 'auto' cannot be used to declare reference types in HLSL.
// References are unsupported in HLSL.

// CHECK: error: references are unsupported in HLSL
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.

Here too (-verify test)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

3 participants