-
Notifications
You must be signed in to change notification settings - Fork 865
Implement auto for C++11-style type deduction
#8452
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ccb6bf
[WIP] Implement `auto` for C++11-style type deduction
llvm-beanz 5756d3a
Doh! Fix broken test.
llvm-beanz c5c4908
Fix tests
llvm-beanz 9c93f9a
disable auto in HLSL 2015
llvm-beanz 588cbcd
Remove stale comment
llvm-beanz a75c33d
Improve test cases
llvm-beanz ef469a2
Add two tests covering spirv generation
llvm-beanz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // RUN: %dxc -T cs_6_0 -E main -HV 202x -fcgl %s -spirv | FileCheck %s | ||
|
|
||
| // Test that the 'auto' keyword can be used to declare variables with inferred | ||
| // types from initialization expressions when targeting SPIR-V. | ||
|
|
||
| // CHECK: [[INT:%[a-zA-Z0-9_]+]] = OpTypeInt 32 1 | ||
| // CHECK: [[INT_1:%[a-zA-Z0-9_]+]] = OpConstant [[INT]] 1 | ||
| // CHECK: [[FLOAT:%[a-zA-Z0-9_]+]] = OpTypeFloat 32 | ||
| // CHECK: [[FLOAT_2:%[a-zA-Z0-9_]+]] = OpConstant [[FLOAT]] 2 | ||
| // CHECK: [[BOOL:%[a-zA-Z0-9_]+]] = OpTypeBool | ||
| // CHECK: [[TRUE:%[a-zA-Z0-9_]+]] = OpConstantTrue [[BOOL]] | ||
| // CHECK: [[V4FLOAT:%[a-zA-Z0-9_]+]] = OpTypeVector [[FLOAT]] 4 | ||
| // CHECK: [[VEC_CONST:%[a-zA-Z0-9_]+]] = OpConstantComposite [[V4FLOAT]] {{%[a-zA-Z0-9_]+}} [[FLOAT_2]] {{%[a-zA-Z0-9_]+}} {{%[a-zA-Z0-9_]+}} | ||
|
|
||
| // CHECK: [[PTR_INT:%_ptr_Function_int]] = OpTypePointer Function [[INT]] | ||
| // CHECK: [[PTR_FLOAT:%_ptr_Function_float]] = OpTypePointer Function [[FLOAT]] | ||
| // CHECK: [[PTR_BOOL:%_ptr_Function_bool]] = OpTypePointer Function [[BOOL]] | ||
| // CHECK: [[PTR_V4FLOAT:%_ptr_Function_v4float]] = OpTypePointer Function [[V4FLOAT]] | ||
|
|
||
| // CHECK: %a = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %b = OpVariable [[PTR_FLOAT]] Function | ||
| // CHECK: %c = OpVariable [[PTR_BOOL]] Function | ||
| // CHECK: %d = OpVariable [[PTR_V4FLOAT]] Function | ||
| // CHECK: %sum = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %product = OpVariable [[PTR_FLOAT]] Function | ||
|
|
||
| // CHECK: OpStore %a [[INT_1]] | ||
| // CHECK: OpStore %b [[FLOAT_2]] | ||
| // CHECK: OpStore %c [[TRUE]] | ||
| // CHECK: OpStore %d [[VEC_CONST]] | ||
|
|
||
| RWBuffer<float> output : register(u0); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| // Auto deduces int from integer literal | ||
| auto a = 1; | ||
| // Auto deduces float from float literal | ||
| auto b = 2.0f; | ||
| // Auto deduces bool from bool literal | ||
| auto c = true; | ||
| // Auto deduces float4 from vector type | ||
| auto d = float4(1.0f, 2.0f, 3.0f, 4.0f); | ||
|
|
||
| // Auto from arithmetic expressions | ||
| auto sum = a + a; | ||
| auto product = b * b; | ||
|
|
||
| // Use the values to prevent dead-code elimination | ||
| output[0] = (float)sum + product + d.x + (float)c; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // RUN: %dxc -T cs_6_0 -E main -HV 202x -fcgl %s -spirv | FileCheck %s | ||
|
|
||
| // Test that the 'auto' keyword works correctly in template contexts when | ||
| // targeting SPIR-V: | ||
| // - auto variables inside template function bodies | ||
| // - auto variables assigned from results of template function instantiations | ||
|
|
||
| RWBuffer<float> output : register(u0); | ||
|
|
||
| // Template with explicit return type; auto is used inside the body. | ||
| template<typename T> | ||
| T square(T val) { | ||
| auto result = val * val; | ||
| return result; | ||
| } | ||
|
|
||
| // Template with explicit return type using a conditional expression. | ||
| template<typename T> | ||
| T clampPositive(T val) { | ||
| auto zero = (T)0; | ||
| auto clamped = val < zero ? zero : val; | ||
| return clamped; | ||
| } | ||
|
|
||
| // CHECK: [[INT:%[a-zA-Z0-9_]+]] = OpTypeInt 32 1 | ||
| // CHECK: [[FLOAT:%[a-zA-Z0-9_]+]] = OpTypeFloat 32 | ||
| // CHECK: [[PTR_INT:%_ptr_Function_int]] = OpTypePointer Function [[INT]] | ||
| // CHECK: [[PTR_FLOAT:%_ptr_Function_float]] = OpTypePointer Function [[FLOAT]] | ||
|
|
||
| // Auto-deduced locals in main() instantiated for int and float. | ||
| // CHECK: %a = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %b = OpVariable [[PTR_FLOAT]] Function | ||
| // CHECK: %c = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %d = OpVariable [[PTR_FLOAT]] Function | ||
|
|
||
| // Auto inside template bodies retains correct types after instantiation. | ||
| // CHECK: %result = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %result_0 = OpVariable [[PTR_FLOAT]] Function | ||
| // CHECK: %zero = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %clamped = OpVariable [[PTR_INT]] Function | ||
| // CHECK: %zero_0 = OpVariable [[PTR_FLOAT]] Function | ||
| // CHECK: %clamped_0 = OpVariable [[PTR_FLOAT]] Function | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| // auto variables assigned from template instantiation results. | ||
| auto a = square(5); // instantiated as int | ||
| auto b = square(2.5f); // instantiated as float | ||
| auto c = clampPositive(-3); // instantiated as int | ||
| auto d = clampPositive(1.5f); // instantiated as float | ||
|
|
||
| output[0] = (float)a + b + (float)c + d; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-pointer.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // RUN: %dxc -T cs_6_0 -HV 202x -verify %s | ||
|
|
||
| // Test that 'auto' cannot be used to declare pointer types in HLSL. | ||
| // Pointers are unsupported in HLSL. | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| int x = 1; | ||
| auto* ptr = &x; // expected-error {{pointers are unsupported in HLSL}} | ||
| } |
11 changes: 11 additions & 0 deletions
11
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // RUN: %dxc -T cs_6_0 -HV 202x -verify %s | ||
|
|
||
| // Test that 'auto' cannot be used to declare reference types in HLSL. | ||
| // References are unsupported in HLSL. | ||
|
|
||
| int gVal = 42; | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| auto& ref = gVal; // expected-error {{references are unsupported in HLSL}} | ||
| } |
41 changes: 41 additions & 0 deletions
41
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-template.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // RUN: %dxc -T cs_6_0 -HV 202x -fcgl %s | FileCheck %s | ||
|
|
||
| // Test that the 'auto' keyword works correctly in template contexts in HLSL: | ||
| // - auto variables inside template function bodies | ||
| // - auto variables assigned from results of template function instantiations | ||
|
|
||
|
|
||
|
|
||
| RWBuffer<float> output : register(u0); | ||
|
|
||
| // Template with explicit return type; auto is used inside the body. | ||
| template<typename T> | ||
| T square(T val) { | ||
| auto result = val * val; | ||
| return result; | ||
| } | ||
|
|
||
| // Template with explicit return type using a conditional expression. | ||
| template<typename T> | ||
| T clampPositive(T val) { | ||
| auto zero = (T)0; | ||
| auto clamped = val < zero ? zero : val; | ||
| return clamped; | ||
| } | ||
|
|
||
| // CHECK-LABEL: define void @main() | ||
| // CHECK: {{.*}} = alloca i32 | ||
| // CHECK: {{.*}} = alloca float | ||
| // CHECK: {{.*}} = alloca i32 | ||
| // CHECK: {{.*}} = alloca float | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| // auto variables assigned from template instantiation results. | ||
| auto a = square(5); // instantiated as int | ||
| auto b = square(2.5f); // instantiated as float | ||
| auto c = clampPositive(-3); // instantiated as int, result = 0 | ||
| auto d = clampPositive(1.5f); // instantiated as float | ||
|
|
||
| output[0] = (float)a + b + (float)c + d; | ||
| } |
55 changes: 55 additions & 0 deletions
55
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-variable-deduction.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // RUN: %dxc -T cs_6_0 -E main - %s -verify | ||
| // RUN: %dxc -T cs_6_0 -E main -HV 202x -fcgl %s | FileCheck %s | ||
|
|
||
| // Test that the 'auto' keyword can be used to declare variables with inferred | ||
| // types from initialization expressions in HLSL. | ||
|
|
||
| // CHECK-LABEL: define void @main() | ||
| // CHECK: [[A:%[a-zA-Z0-9_]+]] = alloca i32 | ||
| // CHECK: [[B:%[a-zA-Z0-9_]+]] = alloca float | ||
| // CHECK: [[C:%[a-zA-Z0-9_]+]] = alloca i32 | ||
| // CHECK: [[D:%[a-zA-Z0-9_]+]] = alloca <4 x float> | ||
| // CHECK: {{.*}} = alloca i32 | ||
| // CHECK: {{.*}} = alloca float | ||
| // CHECK: {{.*}} = alloca <4 x float> | ||
| // CHECK: {{.*}} = alloca i32 | ||
| // CHECK: {{.*}} = alloca float | ||
| // CHECK: store i32 1, i32* [[A]] | ||
| // CHECK: store float 2.000000e+00, float* [[B]] | ||
| // CHECK: store i32 1, i32* [[C]] | ||
|
|
||
| RWBuffer<float> output : register(u0); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| // Auto deduces int from integer literal | ||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto a = 1; | ||
| // Auto deduces float from float literal | ||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto b = 2.0f; | ||
| // Auto deduces bool (stored as i32) from bool literal | ||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto c = true; | ||
| // Auto deduces float4 from vector type | ||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto d = float4(1.0f, 2.0f, 3.0f, 4.0f); | ||
|
|
||
| // Auto from arithmetic expressions | ||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto sum = a + a; | ||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto product = b * b; | ||
|
|
||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto mulAdd = mad(d, d, d); | ||
|
|
||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto m = min(sum, c); | ||
|
|
||
| // expected-warning@+1 {{'auto' type specifier is a HLSL 202x extension}} | ||
| auto dP = dot(d, d); | ||
|
|
||
| // Use the values to prevent dead-code elimination | ||
| output[0] = (float)sum + product + d.x + (float)c; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.