Skip to content

Commit 2079b94

Browse files
authored
Merge pull request shader-slang#371 from csyonghe/master
All compiler fixes to get ir branch work with falcor feature demo.
2 parents 68f529a + 9eb8b4e commit 2079b94

12 files changed

+184
-16
lines changed

source/slang/ast-legalize.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5091,6 +5091,14 @@ struct FindIRDeclUsedByASTVisitor
50915091
// TODO: need to walk the lookup result too
50925092
}
50935093

5094+
void visitOverloadedExpr2(OverloadedExpr2* expr)
5095+
{
5096+
walkExpr(expr->base);
5097+
for (auto & candidate : expr->candidiateExprs)
5098+
walkExpr(candidate);
5099+
}
5100+
5101+
50945102
void visitConstantExpr(ConstantExpr*)
50955103
{}
50965104

source/slang/check.cpp

+55-9
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,9 @@ namespace Slang
14661466
// For now we will do this in a completely ad hoc fashion,
14671467
// but it would be nice to have some generic routine to
14681468
// do the needed type checking/coercion.
1469-
if(getText(hlslUncheckedAttribute->getName()) == "numthreads")
1469+
auto attribText = getText(hlslUncheckedAttribute->getName());
1470+
1471+
if(attribText == "numthreads")
14701472
{
14711473
if(hlslUncheckedAttribute->args.Count() != 3)
14721474
return m;
@@ -1490,8 +1492,33 @@ namespace Slang
14901492

14911493
return hlslNumThreadsAttribute;
14921494
}
1493-
}
1495+
else if (attribText == "maxvertexcount")
1496+
{
1497+
if (hlslUncheckedAttribute->args.Count() != 1)
1498+
return m;
1499+
auto val = checkConstantIntVal(hlslUncheckedAttribute->args[0]);
1500+
auto hlslMaxVertexCountAttrib = new HLSLMaxVertexCountAttribute();
1501+
1502+
hlslMaxVertexCountAttrib->loc = hlslUncheckedAttribute->loc;
1503+
hlslMaxVertexCountAttrib->name = hlslUncheckedAttribute->getName();
1504+
hlslMaxVertexCountAttrib->args = hlslUncheckedAttribute->args;
1505+
hlslMaxVertexCountAttrib->value = (int32_t)val->value;
1506+
return hlslMaxVertexCountAttrib;
1507+
}
1508+
else if (attribText == "instance")
1509+
{
1510+
if (hlslUncheckedAttribute->args.Count() != 1)
1511+
return m;
1512+
auto val = checkConstantIntVal(hlslUncheckedAttribute->args[0]);
1513+
auto attrib = new HLSLInstanceAttribute();
14941514

1515+
attrib->loc = hlslUncheckedAttribute->loc;
1516+
attrib->name = hlslUncheckedAttribute->getName();
1517+
attrib->args = hlslUncheckedAttribute->args;
1518+
attrib->value = (int32_t)val->value;
1519+
return attrib;
1520+
}
1521+
}
14951522
// Default behavior is to leave things as they are,
14961523
// and assume that modifiers are mostly already checked.
14971524
//
@@ -2202,9 +2229,10 @@ namespace Slang
22022229
// to avoid recursion here.
22032230
if (functionNode->Body)
22042231
{
2232+
auto oldFunc = function;
22052233
this->function = functionNode;
22062234
checkStmt(functionNode->Body);
2207-
this->function = nullptr;
2235+
this->function = oldFunc;
22082236
}
22092237
}
22102238
}
@@ -2630,6 +2658,7 @@ namespace Slang
26302658
{
26312659
if (functionNode->IsChecked(DeclCheckState::CheckedHeader)) return;
26322660
functionNode->SetCheckState(DeclCheckState::CheckingHeader);
2661+
auto oldFunc = this->function;
26332662
this->function = functionNode;
26342663
auto returnType = CheckProperType(functionNode->ReturnType);
26352664
functionNode->ReturnType = returnType;
@@ -2648,7 +2677,7 @@ namespace Slang
26482677
else
26492678
paraNames.Add(para->getName());
26502679
}
2651-
this->function = NULL;
2680+
this->function = oldFunc;
26522681
functionNode->SetCheckState(DeclCheckState::CheckedHeader);
26532682

26542683
// One last bit of validation: check if we are redeclaring an existing function
@@ -5745,6 +5774,13 @@ namespace Slang
57455774
AddDeclRefOverloadCandidates(item, context);
57465775
}
57475776
}
5777+
else if (auto overloadedExpr2 = funcExpr.As<OverloadedExpr2>())
5778+
{
5779+
for (auto item : overloadedExpr2->candidiateExprs)
5780+
{
5781+
AddOverloadCandidates(item, context);
5782+
}
5783+
}
57485784
else if (auto typeType = funcExprType->As<TypeType>())
57495785
{
57505786
// If none of the above cases matched, but we are
@@ -5944,6 +5980,10 @@ namespace Slang
59445980
{
59455981
context.baseExpr = funcOverloadExpr->base;
59465982
}
5983+
else if (auto funcOverloadExpr2 = funcExpr.As<OverloadedExpr2>())
5984+
{
5985+
context.baseExpr = funcOverloadExpr2->base;
5986+
}
59475987
AddOverloadCandidates(funcExpr, context);
59485988

59495989
if (context.bestCandidates.Count() > 0)
@@ -6172,15 +6212,14 @@ namespace Slang
61726212
// There were multiple viable candidates, but that isn't an error: we just need
61736213
// to complete all of them and create an overloaded expression as a result.
61746214

6175-
LookupResult result;
6215+
auto overloadedExpr = new OverloadedExpr2();
6216+
overloadedExpr->base = context.baseExpr;
61766217
for (auto candidate : context.bestCandidates)
61776218
{
61786219
auto candidateExpr = CompleteOverloadCandidate(context, candidate);
6220+
overloadedExpr->candidiateExprs.Add(candidateExpr);
61796221
}
6180-
6181-
throw "what now?";
6182-
// auto overloadedExpr = new OverloadedExpr();
6183-
// return overloadedExpr;
6222+
return overloadedExpr;
61846223
}
61856224
}
61866225
else if (context.bestCandidate)
@@ -6400,6 +6439,13 @@ namespace Slang
64006439
return expr;
64016440
}
64026441

6442+
RefPtr<Expr> visitOverloadedExpr2(OverloadedExpr2* expr)
6443+
{
6444+
SLANG_DIAGNOSE_UNEXPECTED(getSink(), expr, "should not appear in input syntax");
6445+
return expr;
6446+
}
6447+
6448+
64036449
RefPtr<Expr> visitAggTypeCtorExpr(AggTypeCtorExpr* expr)
64046450
{
64056451
SLANG_DIAGNOSE_UNEXPECTED(getSink(), expr, "should not appear in input syntax");

source/slang/emit.cpp

+49-3
Original file line numberDiff line numberDiff line change
@@ -2315,7 +2315,7 @@ struct EmitVisitor
23152315
CASE(kIRPseudoOp_Pos, +);
23162316
CASE(kIROp_Neg, -);
23172317
CASE(kIROp_Not, !);
2318-
CASE(kIRPseudoOp_BitNot, ~);
2318+
CASE(kIROp_BitNot, ~);
23192319
#undef CASE
23202320

23212321
#define CASE(NAME, OP) case kIRPseudoOp_##NAME: EmitUnaryAssignExpr(outerPrec, kEOp_Prefix, #OP, "", callExpr); return
@@ -2554,6 +2554,11 @@ struct EmitVisitor
25542554
emitName(expr->lookupResult2.getName());
25552555
}
25562556

2557+
void visitOverloadedExpr2(OverloadedExpr2* expr, ExprEmitArg const& arg)
2558+
{
2559+
ExprVisitorWithArg<Slang::EmitVisitor, Slang::ExprEmitArg>::dispatch(expr->candidiateExprs[0].Ptr(), arg);
2560+
}
2561+
25572562
void setSampleRateFlag()
25582563
{
25592564
context->shared->entryPointLayout->flags |= EntryPointLayout::Flag::usesAnySampleRateInput;
@@ -4928,6 +4933,10 @@ emitDeclImpl(decl, nullptr);
49284933
// types.
49294934
return true;
49304935
}
4936+
else if (type->As<HLSLStreamOutputType>())
4937+
{
4938+
return true;
4939+
}
49314940
else if(type->As<TextureTypeBase>())
49324941
{
49334942
// GLSL doesn't allow texture/resource types to
@@ -5512,7 +5521,12 @@ emitDeclImpl(decl, nullptr);
55125521
emitIROperand(ctx, inst->getArg(0));
55135522
}
55145523
break;
5515-
5524+
case kIROp_BitNot:
5525+
{
5526+
emit("~");
5527+
emitIROperand(ctx, inst->getArg(0));
5528+
}
5529+
break;
55165530
case kIROp_Sample:
55175531
emitIROperand(ctx, inst->getArg(0));
55185532
emit(".Sample(");
@@ -6266,7 +6280,22 @@ emitDeclImpl(decl, nullptr);
62666280
emit(")]\n");
62676281
}
62686282
break;
6269-
6283+
case Stage::Geometry:
6284+
{
6285+
if (auto attrib = entryPointLayout->entryPoint->FindModifier<HLSLMaxVertexCountAttribute>())
6286+
{
6287+
emit("[maxvertexcount(");
6288+
Emit(attrib->value);
6289+
emit(")]\n");
6290+
}
6291+
if (auto attrib = entryPointLayout->entryPoint->FindModifier<HLSLInstanceAttribute>())
6292+
{
6293+
emit("[instance(");
6294+
Emit(attrib->value);
6295+
emit(")]\n");
6296+
}
6297+
}
6298+
break;
62706299
// TODO: There are other stages that will need this kind of handling.
62716300
default:
62726301
break;
@@ -6286,6 +6315,23 @@ emitDeclImpl(decl, nullptr);
62866315

62876316
auto paramName = getIRName(pp);
62886317
auto paramType = pp->getType();
6318+
if (auto decor = pp->findDecoration<IRHighLevelDeclDecoration>())
6319+
{
6320+
if (decor->decl)
6321+
{
6322+
auto primType = decor->decl->FindModifier<HLSLGeometryShaderInputPrimitiveTypeModifier>();
6323+
if (dynamic_cast<HLSLTriangleModifier*>(primType))
6324+
emit("triangle ");
6325+
else if (dynamic_cast<HLSLPointModifier*>(primType))
6326+
emit("point ");
6327+
else if (dynamic_cast<HLSLLineModifier*>(primType))
6328+
emit("line ");
6329+
else if (dynamic_cast<HLSLLineAdjModifier*>(primType))
6330+
emit("lineadj ");
6331+
else if (dynamic_cast<HLSLTriangleAdjModifier*>(primType))
6332+
emit("triangleadj ");
6333+
}
6334+
}
62896335
emitIRParamType(ctx, paramType, paramName);
62906336

62916337
emitIRSemantics(ctx, pp);

source/slang/expr-defs.h

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ SYNTAX_CLASS(OverloadedExpr, Expr)
3030
FIELD(LookupResult, lookupResult2)
3131
END_SYNTAX_CLASS()
3232

33+
// An expression that references an overloaded set of declarations
34+
// having the same name.
35+
SYNTAX_CLASS(OverloadedExpr2, Expr)
36+
37+
// Optional: the base expression is this overloaded result
38+
// arose from a member-reference expression.
39+
SYNTAX_FIELD(RefPtr<Expr>, base)
40+
41+
// The lookup result that was ambiguous
42+
FIELD(List<RefPtr<Expr>>, candidiateExprs)
43+
END_SYNTAX_CLASS()
44+
3345
SYNTAX_CLASS(ConstantExpr, Expr)
3446
FIELD(Token, token)
3547

source/slang/ir-inst-defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ INTRINSIC(Pos)
231231

232232
INST(Neg, neg, 1, 0)
233233
INST(Not, not, 1, 0)
234+
INST(BitNot, bitnot, 1, 0)
234235

235236
#if 0
236237
INTRINSIC(PreInc)
@@ -286,7 +287,6 @@ PSEUDO_INST(XorAssign )
286287
PSEUDO_INST(LshAssign)
287288
PSEUDO_INST(RshAssign)
288289
PSEUDO_INST(Assign)
289-
PSEUDO_INST(BitNot)
290290
PSEUDO_INST(And)
291291
PSEUDO_INST(Or)
292292

source/slang/ir.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3591,6 +3591,7 @@ namespace Slang
35913591
IRParam* clonedParam = builder->emitParam(
35923592
context->maybeCloneType(
35933593
originalParam->getType()));
3594+
cloneDecorations(context, clonedParam, originalParam);
35943595
registerClonedValue(context, clonedParam, originalParam);
35953596
}
35963597
}

source/slang/lower-to-ir.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,6 @@ LoweredValInfo emitCallToDeclRef(
705705
CASE(kIRPseudoOp_PostInc, kIROp_Add);
706706
CASE(kIRPseudoOp_PostDec, kIROp_Sub);
707707
#undef CASE
708-
709708
default:
710709
SLANG_UNIMPLEMENTED_X("IR pseudo-op");
711710
break;
@@ -1197,6 +1196,11 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo>
11971196
SLANG_UNEXPECTED("overloaded expressions should not occur in checked AST");
11981197
}
11991198

1199+
LoweredValInfo visitOverloadedExpr2(OverloadedExpr2* /*expr*/)
1200+
{
1201+
SLANG_UNEXPECTED("overloaded expressions should not occur in checked AST");
1202+
}
1203+
12001204
LoweredValInfo visitIndexExpr(IndexExpr* expr)
12011205
{
12021206
auto type = lowerType(context, expr->type);
@@ -3521,7 +3525,6 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
35213525
for( auto paramInfo : parameterLists.params )
35223526
{
35233527
RefPtr<Type> irParamType = lowerSimpleType(context, paramInfo.type);
3524-
35253528
switch( paramInfo.direction )
35263529
{
35273530
case kParameterDirection_In:

source/slang/mangle.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ namespace Slang
124124
{
125125
emitQualifiedName(context, declRefType->declRef);
126126
}
127+
else if (auto arrType = dynamic_cast<ArrayExpressionType*>(type))
128+
{
129+
emitRaw(context, "a");
130+
emitSimpleIntVal(context, arrType->ArrayLength);
131+
emitType(context, arrType->baseType);
132+
}
127133
else
128134
{
129135
SLANG_UNEXPECTED("unimplemented case in mangling");

source/slang/modifier-defs.h

+10
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ SYNTAX_CLASS(HLSLNumThreadsAttribute, HLSLAttribute)
305305
FIELD(int32_t, z)
306306
END_SYNTAX_CLASS()
307307

308+
SYNTAX_CLASS(HLSLMaxVertexCountAttribute, HLSLAttribute)
309+
// The number of max vertex count for geometry shader
310+
FIELD(int32_t, value)
311+
END_SYNTAX_CLASS()
312+
313+
SYNTAX_CLASS(HLSLInstanceAttribute, HLSLAttribute)
314+
// The number of instances to run for geometry shader
315+
FIELD(int32_t, value)
316+
END_SYNTAX_CLASS()
317+
308318
// HLSL modifiers for geometry shader input topology
309319
SIMPLE_SYNTAX_CLASS(HLSLGeometryShaderInputPrimitiveTypeModifier, Modifier)
310320
SIMPLE_SYNTAX_CLASS(HLSLPointModifier , HLSLGeometryShaderInputPrimitiveTypeModifier)

source/slang/slang-stdlib.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ namespace Slang
199199
{ kIRPseudoOp_Pos, "+", ARITHMETIC_MASK },
200200
{ kIROp_Neg, "-", ARITHMETIC_MASK },
201201
{ kIROp_Not, "!", ANY_MASK },
202-
{ kIRPseudoOp_BitNot, "~", INT_MASK },
202+
{ kIROp_BitNot, "~", INT_MASK },
203203
{ kIRPseudoOp_PreInc, "++", ARITHMETIC_MASK | ASSIGNMENT },
204204
{ kIRPseudoOp_PreDec, "--", ARITHMETIC_MASK | ASSIGNMENT },
205205
{ kIRPseudoOp_PostInc, "++", ARITHMETIC_MASK | ASSIGNMENT | POSTFIX },

tests/compute/generics-overload.slang

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//TEST(smoke,compute):COMPARE_COMPUTE:-xslang -use-ir
2+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out
3+
// Confirm that generics syntax can be used in user
4+
// code and generates valid output.
5+
6+
RWStructuredBuffer<float> outputBuffer;
7+
8+
9+
__generic<T>
10+
T test(T val)
11+
{
12+
return val;
13+
}
14+
15+
__generic<T>
16+
T test(T val, int a)
17+
{
18+
return val;
19+
}
20+
21+
22+
[numthreads(4, 1, 1)]
23+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
24+
{
25+
uint tid = dispatchThreadID.x;
26+
27+
float inVal = float(tid);
28+
29+
float outVal = test<float>(inVal, 0);
30+
31+
outputBuffer[tid] = outVal;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
3F800000
3+
40000000
4+
40400000

0 commit comments

Comments
 (0)