From 6e591ada0eb652c320bba4bd8a46cd579946df01 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 7 Nov 2017 19:09:40 -0500 Subject: Support generic interface methods (#251) * improve diagnostic messages and prevent fatal errors from crashing the compiler. * fix top level exception catching. * spelling fix * change wording of invalidSwizzleExpr diagnostic * add speculative GenericsApp expr parsing * add new test case of cascading generics call. * Fixing bugs in compiling cascaded generic function calls. Add implementation of DeclaredSubTypeWitness::SubstituteImpl() This is not needed by the type checker, but needed by IR specialization. When input source contains cascading generic function call, the arguments to `specialize` instruction is currently represented as a substitution. The arg values of this subsittution can be a `DeclaredSubTypeWitness` when a generic function uses one of its generic parameter to specialize another generic function. When the top level generics function is being specialized, this substitution argument, which is a `DeclaredSubTypeWitness`, needs to be substituted with the witness that used to specialize the top level function in the specialized specialize instruction as well. * add a test case for cascading generic function call. * parser bug fix * fixes #255 * add test case for issue #255 * Generate missing `specialize` instruction when calling a generic method from an interface constraint. When calling a generic method via an interface, we should be generating the following ir: ... f = lookup_interface_method(...) f_s = specailize(f, declRef) ... This commit fixes this `emitFuncRef` function to emit the needed `specialize` instruction. * fixes #260 This fix follows the second apporach in the disucssion. It generated mangled name for specialized functions by appending new substitution type names to the original mangled name. * Disabling removing and re-inserting specailized functions in getSpecalizeFunc() I am not sure why it is needed, it seems HLSL and GLSL backends are generating forward declarations anyways, so the order of functions in IRModule shouldn't matter. * cleanup and complete test cases. * fix warnings --- source/slang/lower-to-ir.cpp | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 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 2c64c539b..4fabd6a81 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -314,6 +314,13 @@ LoweredValInfo emitDeclRef( IRGenContext* context, DeclRef declRef); +// Emit necessary `specialize` instruction needed by a declRef. +// This is currently used by emitDeclRef() and emitFuncRef() +LoweredValInfo maybeEmitSpecializeInst(IRGenContext* context, + LoweredValInfo loweredDecl, // the lowered value of the inner decl + DeclRef declRef // the full decl ref containing substitutions +); + IRValue* getSimpleVal(IRGenContext* context, LoweredValInfo lowered); @@ -488,10 +495,11 @@ LoweredValInfo emitFuncRef( RefPtr type = funcExpr->type; - return LoweredValInfo::simple(context->irBuilder->emitLookupInterfaceMethodInst( + auto loweredVal = LoweredValInfo::simple(context->irBuilder->emitLookupInterfaceMethodInst( type, baseMemberDeclRef, funcDeclRef)); + return maybeEmitSpecializeInst(context, loweredVal, funcDeclRef); } } } @@ -2893,6 +2901,13 @@ struct DeclLoweringVisitor : DeclVisitor return LoweredValInfo::simple(irFunc); } + LoweredValInfo visitGenericDecl(GenericDecl * genDecl) + { + if (auto innerFuncDecl = genDecl->inner->As()) + return lowerFuncDecl(innerFuncDecl); + SLANG_RELEASE_ASSERT(false); + UNREACHABLE_RETURN(LoweredValInfo()); + } LoweredValInfo visitFunctionDeclBase(FunctionDeclBase* decl) { @@ -2997,6 +3012,11 @@ RefPtr lowerSubstitutionArg( } else if (auto declaredSubtypeWitness = dynamic_cast(val)) { + // We do not have a concrete witness table yet for a GenericTypeConstraintDecl witness + + if (declaredSubtypeWitness->declRef.As()) + return val; + // We need to look up the IR-level representation of the witness // (which is a witness table). @@ -3073,9 +3093,16 @@ LoweredValInfo emitDeclRef( // unspecialized declaration. LoweredValInfo loweredDecl = ensureDecl(context, declRef.getDecl()); + return maybeEmitSpecializeInst(context, loweredDecl, declRef); +} + +LoweredValInfo maybeEmitSpecializeInst(IRGenContext* context, + LoweredValInfo loweredDecl, + DeclRef declRef) +{ // If this declaration reference doesn't involve any specializations, // then we are done at this point. - if(!hasGenericSubstitutions(declRef.substitutions)) + if (!hasGenericSubstitutions(declRef.substitutions)) return loweredDecl; auto val = getSimpleVal(context, loweredDecl); @@ -3089,7 +3116,7 @@ LoweredValInfo emitDeclRef( RefPtr type; - if(auto declType = val->getType()) + if (auto declType = val->getType()) { type = declType->Substitute(declRef.substitutions).As(); } @@ -3102,6 +3129,7 @@ LoweredValInfo emitDeclRef( declRef)); } + static void lowerEntryPointToIR( IRGenContext* context, EntryPointRequest* entryPointRequest) -- cgit v1.2.3