Skip to content

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

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

Implement auto for C++11-style type deduction#8452
llvm-beanz wants to merge 6 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?

Comment thread tools/clang/test/HLSL/cpp-errors-hv2015.hlsl Outdated
Comment thread tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-pointer.hlsl Outdated
Comment thread tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl Outdated
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-pointer.hlsl
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl
@llvm-beanz
Copy link
Copy Markdown
Collaborator Author

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?

I explicitly called this out in the description:

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 believe this change stands on its own and is fine to go in with the expectation that we'll prevent deducing some objects in a follow-up change. I think the correct design for marking some built-in types as non-deducible is likely a bigger change than just enabling auto so I thought it best to split the two up.

@hekota hekota assigned hekota and unassigned hekota May 18, 2026
Copy link
Copy Markdown
Member

@hekota hekota left a comment

Choose a reason for hiding this comment

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

LGTM!

auto sum = a + a;
// expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}}
auto product = b * b;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggestion: add test for auto from return value of an intricsic call

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good call! Added a few.

Comment on lines +8 to +9
// CHECK-LABEL: define void @main()
// DXIL: define void @main()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be -verify test with no expected diagnostics?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it's probably worth testing the alloca variable types to make sure the correct types are generated in codegen. I've updated the test.

../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-template.hlsl
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-variable-deduction.h
lsl
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.

4 participants