diff options
| author | Yong He <yonghe@outlook.com> | 2017-11-17 21:26:21 -0500 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2017-11-17 18:26:21 -0800 |
| commit | 54bf54bd0dda378f8400860b25855558f39cb52b (patch) | |
| tree | 955931f37df819f3c6e22bc981089f644c1141e1 /source/slang/syntax.cpp | |
| parent | 0298a0427bbfe19700169c4e239a1b9e91baa410 (diff) | |
Add support for global generic parameters (#285)
* Add support for global generic parameters
(In-progress work)
This commit include:
1. Update Slang API to allow specification of generic type arguments in an `EntryPointRequest`
2. Add parsing of `__generic_param` construct, which becomes a GlobalGenericParamDecl, contains members of `GenericTypeConstraintDecl`.
3. Semantics checking will check whether the provided type arguments conform to the interfaces as defined by the generic parameter, and store SubtypeWitness values in the EntryPointRequest, which will be used by `specializeIRForEntryPoint` when generating final IR.
4. Add a new type of substitution - `GlobalGenericParamSubstitution` for subsittuting references to `__generic_param` decls or to its member `GenericTypeConsraintDecl` with the actual type argument or witness tables.
5. Update `IRSpecContext` to apply `GlobalGenericParamSubstitution` when specializing the IR for an EntryPointRequest.
6. Update `render-test` to take additional `type` inputs, which specifies the type arguments to substitute into the global `__generic_param` types.
This commit does not include ProgramLayout specialization.
* IR: pass through `[unroll]` attribute (#284)
The initial lowering was adding an `IRLoopControlDecoration` to the instruction at the head of a loop, but this was getting dropped when the IR gets cloned for a particular entry point.
The fix was simply to add a case for loop-control decorations to `cloneDecoration`.
* fix warnings
* IR: support `CompileTimeForStmt` (#286)
This statement type is a bit of a hack, to support loops that *must* be unrolled.
The AST-to-AST pass handles them by cloning the AST for the loop body N times, and it was easy enough to do the same thing for the IR: emit the instructions for the body N times.
The only thing that requires a bit of care is that now we might see the same variable declarations multiple times, so we need to play it safe and overwrite existing entries in our map from declarations to their IR values.
Of course a better answer long-term would be to do the actual unrolling in the IR. This is especially true because we might some day want to support compile-time/must-unroll loops in functions, where the loop counter comes in as a parameter (but must still be compile-time-constant at every call site).
* Add support for global generic parameters
(In-progress work)
This commit include:
1. Update Slang API to allow specification of generic type arguments in an `EntryPointRequest`
2. Add parsing of `__generic_param` construct, which becomes a GlobalGenericParamDecl, contains members of `GenericTypeConstraintDecl`.
3. Semantics checking will check whether the provided type arguments conform to the interfaces as defined by the generic parameter, and store SubtypeWitness values in the EntryPointRequest, which will be used by `specializeIRForEntryPoint` when generating final IR.
4. Add a new type of substitution - `GlobalGenericParamSubstitution` for subsittuting references to `__generic_param` decls or to its member `GenericTypeConsraintDecl` with the actual type argument or witness tables.
5. Update `IRSpecContext` to apply `GlobalGenericParamSubstitution` when specializing the IR for an EntryPointRequest.
6. Update `render-test` to take additional `type` inputs, which specifies the type arguments to substitute into the global `__generic_param` types.
progress on parameter binding
* Add a more contrived test case for specializing parameter bindings
* update render-test to align buffers to 256 bytes (to get rid of D3D complains on minimal buffer size).
* adding one more test case for parameter binding specialization.
* Cleanup according to @tfoleyNV 's suggestions.
* fix a bug introduced in the cleanup
Diffstat (limited to 'source/slang/syntax.cpp')
| -rw-r--r-- | source/slang/syntax.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/source/slang/syntax.cpp b/source/slang/syntax.cpp index e5fc8dfa3..fa9c88051 100644 --- a/source/slang/syntax.cpp +++ b/source/slang/syntax.cpp @@ -93,6 +93,7 @@ ABSTRACT_SYNTAX_CLASS(Expr, SyntaxNode); ABSTRACT_SYNTAX_CLASS(Substitutions, SyntaxNode); ABSTRACT_SYNTAX_CLASS(GenericSubstitution, Substitutions); ABSTRACT_SYNTAX_CLASS(ThisTypeSubstitution, Substitutions); +ABSTRACT_SYNTAX_CLASS(GlobalGenericParamSubstitution, Substitutions); #include "expr-defs.h" #include "decl-defs.h" @@ -488,6 +489,20 @@ void Type::accept(IValVisitor* visitor, void* extra) } } } + else if (auto globalGenParam = dynamic_cast<GlobalGenericParamDecl*>(declRef.getDecl())) + { + // search for a substitution that might apply to us + for (auto s = subst; s; s = s->outer.Ptr()) + { + if (auto genericSubst = dynamic_cast<GlobalGenericParamSubstitution*>(s)) + { + if (genericSubst->paramDecl == globalGenParam) + { + return genericSubst->actualType; + } + } + } + } int diff = 0; DeclRef<Decl> substDeclRef = declRef.SubstituteImpl(subst, &diff); @@ -1208,6 +1223,35 @@ void Type::accept(IValVisitor* visitor, void* extra) return false; } + RefPtr<Substitutions> GlobalGenericParamSubstitution::SubstituteImpl(Substitutions* /*subst*/, int* /*ioDiff*/) + { + // we will never replace values for this type of substitution + return this; + } + + bool GlobalGenericParamSubstitution::Equals(Substitutions* subst) + { + if (!subst) + return false; + if (auto genSubst = dynamic_cast<GlobalGenericParamSubstitution*>(subst)) + { + if (paramDecl != genSubst->paramDecl) + return false; + if (!actualType->EqualsVal(genSubst->actualType)) + return false; + if (witnessTables.Count() != genSubst->witnessTables.Count()) + return false; + for (UInt i = 0; i < witnessTables.Count(); i++) + { + if (!witnessTables[i].Key->Equals(genSubst->witnessTables[i].Key)) + return false; + if (!witnessTables[i].Value->EqualsVal(genSubst->witnessTables[i].Value)) + return false; + } + return true; + } + return false; + } // DeclRefBase @@ -1564,6 +1608,24 @@ void Type::accept(IValVisitor* visitor, void* extra) return genericSubst->args[index + ordinaryParamCount]; } } + else if (auto globalGenParamSubst = dynamic_cast<GlobalGenericParamSubstitution*>(s)) + { + // we have a GlobalGenericParamSubstitution, this substitution will provide + // a concrete IRWitnessTable for a generic global variable + auto supType = GetSup(genConstraintDecl); + + // check if the substitution is really about this global generic type parameter + if (globalGenParamSubst->paramDecl != genConstraintDecl.getDecl()->ParentDecl) + continue; + + // find witness table for the required interface + for (auto witness : globalGenParamSubst->witnessTables) + if (witness.Key->EqualsVal(supType)) + { + (*ioDiff)++; + return witness.Value; + } + } } } RefPtr<DeclaredSubtypeWitness> rs = new DeclaredSubtypeWitness(); |
