From ce879112cb16e3def1b8673104e7123b8b17ee2a Mon Sep 17 00:00:00 2001 From: Yong He Date: Sun, 21 Jan 2018 07:09:55 -0500 Subject: Improvements and bug fixes for global type parameters 1. allow spReflection_FindTypeByName to accept arbitrary type expression string 2. allow const int generic value to be used as expression value, and as array size 3. various bug fixes in witness table specialization / function cloning during specializeIRForEntryPoint to avoid creating duplicate global values, not copying the right definition of a function from the other module, not cloning witness tables that are required by specializeGenerics etc. --- source/slang/lower-to-ir.cpp | 49 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'source/slang/lower-to-ir.cpp') diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 5e7e05a23..5d710725a 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -1021,6 +1021,7 @@ RefPtr getFuncType( return funcType; } +SubstitutionSet lowerSubstitutions(IRGenContext* context, SubstitutionSet subst); // struct ValLoweringVisitor : ValVisitor @@ -1080,8 +1081,6 @@ struct ValLoweringVisitor : ValVisitordeclRef); - - return LoweredTypeInfo(type); } @@ -3006,6 +3005,11 @@ struct DeclLoweringVisitor : DeclVisitor return globalVal; } + LoweredValInfo visitGenericValueParamDecl(GenericValueParamDecl* decl) + { + return LoweredValInfo::simple(context->irBuilder->getDeclRefVal(DeclRefBase(decl))); + } + LoweredValInfo visitVarDeclBase(VarDeclBase* decl) { // Detect global (or effectively global) variables @@ -3733,7 +3737,10 @@ struct DeclLoweringVisitor : DeclVisitor if (auto innerFuncDecl = genDecl->inner->As()) return lowerFuncDecl(innerFuncDecl); else if (auto innerStructDecl = genDecl->inner->As()) + { + visitAggTypeDecl(innerStructDecl); return LoweredValInfo(); + } SLANG_RELEASE_ASSERT(false); UNREACHABLE_RETURN(LoweredValInfo()); } @@ -3910,6 +3917,32 @@ RefPtr lowerGenericSubstitutions( return result; } +RefPtr lowerGlobalGenericSubstitutions( + IRGenContext* context, + GlobalGenericParamSubstitution* genSubst) +{ + if (!genSubst) + return nullptr; + RefPtr result; + RefPtr newSubst = new GlobalGenericParamSubstitution(); + newSubst->actualType = lowerSubstitutionArg(context, genSubst->actualType); + newSubst->paramDecl = genSubst->paramDecl; + for (auto & tbl : genSubst->witnessTables) + { + auto ntbl = tbl; + ntbl.Value = lowerSubstitutionArg(context, tbl.Value); + newSubst->witnessTables.Add(ntbl); + } + result = newSubst; + if (genSubst->outer) + { + result->outer = lowerGlobalGenericSubstitutions( + context, + genSubst->outer); + } + return result; +} + RefPtr lowerThisTypeSubstitution( IRGenContext* context, ThisTypeSubstitution* thisSubst) @@ -3926,7 +3959,7 @@ SubstitutionSet lowerSubstitutions(IRGenContext* context, SubstitutionSet subst) SubstitutionSet rs; rs.genericSubstitutions = lowerGenericSubstitutions(context, subst.genericSubstitutions); rs.thisTypeSubstitution = lowerThisTypeSubstitution(context, subst.thisTypeSubstitution); - rs.globalGenParamSubstitutions = subst.globalGenParamSubstitutions; + rs.globalGenParamSubstitutions = lowerGlobalGenericSubstitutions(context, subst.globalGenParamSubstitutions); return rs; } @@ -3973,10 +4006,10 @@ LoweredValInfo maybeEmitSpecializeInst(IRGenContext* context, // need to walk through those and replace things in // cases where the `Val`s used for substitution should // lower to something other than their original form. - auto lowedNewSubst = lowerGenericSubstitutions(context, newSubst); - DeclRef newDeclRef = DeclRef(declRef.decl, - SubstitutionSet(lowedNewSubst, declRef.substitutions.thisTypeSubstitution, - declRef.substitutions.globalGenParamSubstitutions)); + SubstitutionSet oldSubst = declRef.substitutions; + oldSubst.genericSubstitutions = newSubst; + auto lowedNewSubst = lowerSubstitutions(context, oldSubst); + DeclRef newDeclRef = DeclRef(declRef.decl, lowedNewSubst); RefPtr type; if (auto declType = val->getType()) @@ -4014,9 +4047,9 @@ static void lowerEntryPointToIR( return; } // we need to lower all global type arguments as well + auto loweredEntryPointFunc = ensureDecl(context, entryPointFuncDecl); for (auto arg : entryPointRequest->genericParameterTypes) lowerType(context, arg); - auto loweredEntryPointFunc = ensureDecl(context, entryPointFuncDecl); } #if 0 -- cgit v1.2.3