From 3088d901fee6447b6d80fa67f258626ece4408dc Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 5 May 2022 10:48:14 -0700 Subject: Various vulkan/glsl fixes. (#2222) * Various vulkan/glsl fixes. * Fix. * Fix. * Canonicalize type constraints for name mangling. Co-authored-by: Yong He Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> --- source/slang/hlsl.meta.slang | 21 +++++++++++++++-- source/slang/slang-check-decl.cpp | 47 +++++++++++++++++++++++++++++++++++++++ source/slang/slang-check.h | 3 +++ source/slang/slang-mangle.cpp | 31 +++++++++++++++++++------- 4 files changed, 92 insertions(+), 10 deletions(-) (limited to 'source') diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 2c969e174..7d107888a 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -1582,7 +1582,7 @@ T distance(vector x, vector y) // Vector dot product -__generic +__generic __target_intrinsic(hlsl) __target_intrinsic(glsl) T dot(vector x, vector y) @@ -1593,6 +1593,17 @@ T dot(vector x, vector y) return result; } +__generic +__target_intrinsic(hlsl) +T dot(vector x, vector y) +{ + T result = T(0); + for(int i = 0; i < N; ++i) + result += x[i] * y[i]; + return result; +} + + // Helper for computing distance terms for lighting (obsolete) __generic vector dst(vector x, vector y); @@ -2568,13 +2579,19 @@ __intrinsic_op($(kIROp_Mul)) matrix mul(T x, matrix y); // vector-vector (dot product) -__generic +__generic __target_intrinsic(hlsl) __target_intrinsic(glsl, "dot") T mul(vector x, vector y) { return dot(x, y); } +__generic +__target_intrinsic(hlsl) +T mul(vector x, vector y) +{ + return dot(x, y); +} // vector-matrix __generic diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 8db157a37..3c654a48b 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -12,6 +12,8 @@ #include "slang-lookup.h" +#include "slang-syntax.h" + namespace Slang { /// Visitor to transition declarations to `DeclCheckState::CheckedModifiers` @@ -5342,4 +5344,49 @@ namespace Slang } } + static void _getCanonicalConstraintTypes(List& outTypeList, Type* type) + { + if (auto andType = as(type)) + { + _getCanonicalConstraintTypes(outTypeList, andType->left); + _getCanonicalConstraintTypes(outTypeList, andType->right); + } + else + { + outTypeList.add(type); + } + } + OrderedDictionary> getCanonicalGenericConstraints( + DeclRef genericDecl) + { + OrderedDictionary> genericConstraints; + for (auto mm : getMembersOfType(genericDecl)) + { + genericConstraints[mm.getDecl()] = List(); + } + for (auto genericTypeConstraintDecl : getMembersOfType(genericDecl)) + { + assert( + genericTypeConstraintDecl.getDecl()->sub.type->astNodeType == + ASTNodeType::DeclRefType); + auto typeParamDecl = as(genericTypeConstraintDecl.getDecl()->sub.type)->declRef.getDecl(); + List* constraintTypes = genericConstraints.TryGetValue(typeParamDecl); + assert(constraintTypes); + constraintTypes->add(genericTypeConstraintDecl.getDecl()->getSup().type); + } + + OrderedDictionary> result; + for (auto& constraints : genericConstraints) + { + List typeList; + for (auto type : constraints.Value) + { + _getCanonicalConstraintTypes(typeList, type); + } + // TODO: we also need to sort the types within the list for each generic type param. + result[constraints.Key] = typeList; + } + return result; + } + } diff --git a/source/slang/slang-check.h b/source/slang/slang-check.h index e84ad0bc5..a7d51f951 100644 --- a/source/slang/slang-check.h +++ b/source/slang/slang-check.h @@ -22,4 +22,7 @@ namespace Slang bool isFromStdLib(Decl* decl); void registerBuiltinDecls(Session* session, Decl* decl); + + OrderedDictionary> getCanonicalGenericConstraints( + DeclRef genericDecl); } diff --git a/source/slang/slang-mangle.cpp b/source/slang/slang-mangle.cpp index e6c0a8e7f..ca6dcecd7 100644 --- a/source/slang/slang-mangle.cpp +++ b/source/slang/slang-mangle.cpp @@ -2,6 +2,7 @@ #include "../compiler-core/slang-name.h" #include "slang-syntax.h" +#include "slang-check.h" namespace Slang { @@ -330,6 +331,12 @@ namespace Slang return; } + // TODO: we should special case GenericTypeParamDecl and GenericValueParamDecl nodes + // instead of just dumping their names here to avoid the name of a generic parameter + // to have affect the binary signature. + // For each generic parameter, we should assign it a unique ID (i, j), where i is the + // nesting level of the generic, and j is the sequential order of the parameter within + // its generic parent, and use this 2D ID to refer to such a parameter. emitName(context, declRef.getName()); // Special case: accessors need some way to distinguish themselves @@ -392,26 +399,34 @@ namespace Slang } emit(context, genericParameterCount); - for( auto mm : getMembers(parentGenericDeclRef) ) + + OrderedDictionary> genericConstraints; + for (auto mm : getMembers(parentGenericDeclRef)) { - if(auto genericTypeParamDecl = mm.as()) + if (auto genericTypeParamDecl = mm.as()) { emitRaw(context, "T"); } - else if(auto genericValueParamDecl = mm.as()) + else if (auto genericValueParamDecl = mm.as()) { emitRaw(context, "v"); emitType(context, getType(context->astBuilder, genericValueParamDecl)); } - else if(mm.as()) - { - emitRaw(context, "C"); - // TODO: actually emit info about the constraint - } else + {} + } + + auto canonicalizedConstraints = getCanonicalGenericConstraints(parentGenericDeclRef); + for (auto& constraint : canonicalizedConstraints) + { + for (auto type : constraint.Value) { + emitRaw(context, "C"); + emitQualifiedName(context, DeclRef(constraint.Key, nullptr)); + emitType(context, type); } } + } } -- cgit v1.2.3