summaryrefslogtreecommitdiffstats
path: root/source/slang/check.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2017-11-17 21:26:21 -0500
committerTim Foley <tfoleyNV@users.noreply.github.com>2017-11-17 18:26:21 -0800
commit54bf54bd0dda378f8400860b25855558f39cb52b (patch)
tree955931f37df819f3c6e22bc981089f644c1141e1 /source/slang/check.cpp
parent0298a0427bbfe19700169c4e239a1b9e91baa410 (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/check.cpp')
-rw-r--r--source/slang/check.cpp102
1 files changed, 101 insertions, 1 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp
index 233a82eef..4b8f4f4c1 100644
--- a/source/slang/check.cpp
+++ b/source/slang/check.cpp
@@ -148,7 +148,6 @@ namespace Slang
return expr->type->As<DeclRefType>();
}
-
RefPtr<Expr> ConstructDeclRefExpr(
DeclRef<Decl> declRef,
RefPtr<Expr> baseExpr,
@@ -1998,6 +1997,22 @@ namespace Slang
decl->SetCheckState(DeclCheckState::Checked);
}
+ void visitGlobalGenericParamDecl(GlobalGenericParamDecl * decl)
+ {
+ if (decl->IsChecked(DeclCheckState::Checked)) return;
+ decl->SetCheckState(DeclCheckState::CheckedHeader);
+ // global generic param only allowed in global scope
+ auto program = decl->ParentDecl->As<ModuleDecl>();
+ if (!program)
+ getSink()->diagnose(decl, Slang::Diagnostics::globalGenParamInGlobalScopeOnly);
+ // Now check all of the member declarations.
+ for (auto member : decl->Members)
+ {
+ checkDecl(member);
+ }
+ decl->SetCheckState(DeclCheckState::Checked);
+ }
+
void visitAssocTypeDecl(AssocTypeDecl* decl)
{
if (decl->IsChecked(DeclCheckState::Checked)) return;
@@ -3703,6 +3718,19 @@ namespace Slang
return true;
}
}
+ // if an inheritance decl is not found, try to find a GenericTypeConstraintDecl
+ for (auto genConstraintDeclRef : getMembersOfType<GenericTypeConstraintDecl>(aggTypeDeclRef))
+ {
+ EnsureDecl(genConstraintDeclRef.getDecl());
+ auto inheritedType = GetSup(genConstraintDeclRef);
+ TypeWitnessBreadcrumb breadcrumb;
+ breadcrumb.prev = inBreadcrumbs;
+ breadcrumb.declRef = genConstraintDeclRef;
+ if (doesTypeConformToInterfaceImpl(originalType, inheritedType, interfaceDeclRef, outWitness, &breadcrumb))
+ {
+ return true;
+ }
+ }
}
else if( auto genericTypeParamDeclRef = declRef.As<GenericTypeParamDecl>() )
{
@@ -6582,6 +6610,78 @@ namespace Slang
// that we don't have to re-do this effort again later.
entryPoint->decl = entryPointFuncDecl;
+ // Lookup generic parameter types in global scope
+ for (auto name : entryPoint->genericParameterTypeNames)
+ {
+ if (!translationUnitSyntax->memberDictionary.TryGetValue(name, firstDeclWithName))
+ {
+ // If there doesn't appear to be any such declaration, then
+ // we need to diagnose it as an error, and then bail out.
+ sink->diagnose(translationUnitSyntax, Diagnostics::entryPointTypeParameterNotFound, name);
+ return;
+ }
+ RefPtr<Type> type;
+ if (auto aggType = firstDeclWithName->As<AggTypeDecl>())
+ {
+ type = DeclRefType::Create(entryPoint->compileRequest->mSession, DeclRef<Decl>(aggType, nullptr));
+ }
+ else if (auto typeDefDecl = firstDeclWithName->As<TypeDefDecl>())
+ {
+ type = GetType(DeclRef<TypeDefDecl>(typeDefDecl, nullptr));
+ }
+ else
+ {
+ sink->diagnose(firstDeclWithName, Diagnostics::entryPointTypeSymbolNotAType, name);
+ return;
+ }
+ entryPoint->genericParameterTypes.Add(type);
+ }
+ // check that user-provioded type arguments conforms to the generic type
+ // parameter declaration of this translation unit
+
+ // collect global generic parameters from all imported modules
+ List<RefPtr<GlobalGenericParamDecl>> globalGenericParams;
+ // add current translation unit first
+ {
+ auto globalGenParams = translationUnit->SyntaxNode->getMembersOfType<GlobalGenericParamDecl>();
+ for (auto p : globalGenParams)
+ globalGenericParams.Add(p);
+ }
+ // add imported modules
+ for (auto moduleDecl : entryPoint->compileRequest->loadedModulesList)
+ {
+ auto globalGenParams = moduleDecl->getMembersOfType<GlobalGenericParamDecl>();
+ for (auto p : globalGenParams)
+ globalGenericParams.Add(p);
+ }
+ if (globalGenericParams.Count() != entryPoint->genericParameterTypes.Count())
+ {
+ sink->diagnose(entryPoint->decl, Diagnostics::mismatchEntryPointTypeArgument, globalGenericParams.Count(),
+ entryPoint->genericParameterTypes.Count());
+ return;
+ }
+ // if number of entry-point type arguments matches parameters, try find
+ // SubtypeWitness for each argument
+ int index = 0;
+ for (auto & gParam : globalGenericParams)
+ {
+ for (auto constraint : gParam->getMembersOfType<GenericTypeConstraintDecl>())
+ {
+ auto interfaceType = GetSup(DeclRef<GenericTypeConstraintDecl>(constraint, nullptr));
+ SemanticsVisitor visitor(sink, entryPoint->compileRequest, translationUnit);
+ auto witness = visitor.tryGetSubtypeWitness(entryPoint->genericParameterTypes[index], interfaceType);
+ if (!witness)
+ {
+ sink->diagnose(gParam,
+ Diagnostics::typeArgumentDoesNotConformToInterface, gParam->nameAndLoc.name, entryPoint->genericParameterTypes[index],
+ interfaceType);
+ }
+ entryPoint->genericParameterWitnesses.Add(witness);
+ }
+ index++;
+ }
+ if (sink->errorCount != 0)
+ return;
// TODO: after all that work, we are now in a position to start
// validating the declaration itself. E.g., we should check if
// the declared input/output parameters have suitable semantics,