Skip to content

Commit 676fe64

Browse files
authored
[HLSL 2021] Allow dependent non-type parameters (#4100)
Non-type template parameters need more complicated constant resolution because they need to look through layers of templates. This change makes it so that when evaluating integer constants we use C++ 11 constant expression evaluation to look through template instantiations. 'beanz/cbieneman/dependent-constant-int'. ../tools/clang/test/HLSLFileCheck/hlsl/template/DependentNonTypeParam.hl sl
1 parent 9373b34 commit 676fe64

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

tools/clang/lib/AST/ExprConstant.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9393,7 +9393,8 @@ static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx,
93939393

93949394
bool Expr::isIntegerConstantExpr(const ASTContext &Ctx,
93959395
SourceLocation *Loc) const {
9396-
if (Ctx.getLangOpts().CPlusPlus11)
9396+
// HLSL Change - if templates are enabled we need to act like C++11 here
9397+
if (Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().EnableTemplates)
93979398
return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc);
93989399

93999400
ICEDiag D = CheckICE(this, Ctx);
@@ -9406,7 +9407,8 @@ bool Expr::isIntegerConstantExpr(const ASTContext &Ctx,
94069407

94079408
bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx,
94089409
SourceLocation *Loc, bool isEvaluated) const {
9409-
if (Ctx.getLangOpts().CPlusPlus11)
9410+
// HLSL Change - if templates are enabled we need to act like C++11 here
9411+
if (Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().EnableTemplates)
94109412
return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
94119413

94129414
if (!isIntegerConstantExpr(Ctx, Loc))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %dxc -E main -T ps_6_0 -ast-dump -enable-templates %s | FileCheck -check-prefix=AST %s
2+
// RUN: %dxc -E main -T ps_6_0 -ast-dump -HV 2021 %s | FileCheck -check-prefix=AST %s
3+
// RUN: %dxc -E main -T ps_6_0 -enable-templates %s | FileCheck %s
4+
// RUN: %dxc -E main -T ps_6_0 -HV 2021 %s | FileCheck %s
5+
6+
template<uint VSize, typename T>
7+
vector<T, VSize> make_vec(T X) {
8+
return (vector<T, VSize>)X;
9+
}
10+
11+
// Just verify that we got to codegen, generated main, and load the two used
12+
// components from the input. The actual code-gen is less interesting to this
13+
// test case.
14+
15+
// CHECK: define void @main()
16+
// CHECK: @dx.op.loadInput.f32
17+
// CHECK: @dx.op.loadInput.f32
18+
float2 main(float4 a:A) : SV_Target {
19+
float3 M4 = make_vec<4>(a.x).xyz;
20+
return M4.xy * a.y;
21+
}
22+
23+
// AST: | |-NonTypeTemplateParmDecl 0x{{[0-9a-zA-Z]+}} <line:6:10, col:15> col:15 referenced 'uint':'unsigned int' VSize
24+
// AST-NEXT: | |-TemplateTypeParmDecl 0x{{[0-9a-zA-Z]+}} <col:22, col:31> col:31 referenced typename T
25+
// AST-NEXT: | |-FunctionDecl 0x{{[0-9a-zA-Z]+}} <line:7:1, line:9:1> line:7:18 make_vec 'vector<T, VSize> (T)'
26+
// AST-NEXT: | | |-ParmVarDecl 0x{{[0-9a-zA-Z]+}} <col:27, col:29> col:29 referenced X 'T'
27+
// AST-NEXT: | | `-CompoundStmt 0x{{[0-9a-zA-Z]+}} <col:32, line:9:1>
28+
// AST-NEXT: | | `-ReturnStmt 0x{{[0-9a-zA-Z]+}} <line:8:3, col:28>
29+
// AST-NEXT: | | `-CStyleCastExpr 0x{{[0-9a-zA-Z]+}} <col:10, col:28> 'vector<T, VSize>' <Dependent>
30+
// AST-NEXT: | | `-DeclRefExpr 0x{{[0-9a-zA-Z]+}} <col:28> 'T' lvalue ParmVar 0x{{[0-9a-zA-Z]+}} 'X' 'T'
31+
// AST-NEXT: | `-FunctionDecl 0x{{[0-9a-zA-Z]+}} <line:7:1, line:9:1> line:7:18 used make_vec 'vector<float, 4U> (float)'
32+
// AST-NEXT: | |-TemplateArgument integral 4
33+
// AST-NEXT: | |-TemplateArgument type 'float'
34+
// AST-NEXT: | |-ParmVarDecl 0x{{[0-9a-zA-Z]+}} <col:27, col:29> col:29 used X 'float':'float'
35+
// AST-NEXT: | `-CompoundStmt 0x{{[0-9a-zA-Z]+}} <col:32, line:9:1>
36+
// AST-NEXT: | `-ReturnStmt 0x{{[0-9a-zA-Z]+}} <line:8:3, col:28>
37+
// AST-NEXT: | `-CStyleCastExpr 0x{{[0-9a-zA-Z]+}} <col:10, col:28> 'vector<float, 4U>':'vector<float, 4>' <NoOp>
38+
// AST-NEXT: | `-ImplicitCastExpr 0x{{[0-9a-zA-Z]+}} <col:28> 'vector<float, 4>':'vector<float, 4>' <HLSLVectorSplat>
39+
// AST-NEXT: | `-ImplicitCastExpr 0x{{[0-9a-zA-Z]+}} <col:28> 'float':'float' <LValueToRValue>
40+
// AST-NEXT: | `-DeclRefExpr 0x{{[0-9a-zA-Z]+}} <col:28> 'float':'float' lvalue ParmVar 0x{{[0-9a-zA-Z]+}} 'X' 'float':'float'

0 commit comments

Comments
 (0)