From adf758c8c4032afcd96d995840bd697d2adef34c Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Fri, 19 Jul 2024 13:42:31 -0500 Subject: Fix the issue of name mangling (#4691) * Fix the issue of name mangle During our name mangling, we should add the direction of the parameter in the name, otherwise it could have the name collision which will result in invalid code generation: e.g. // in slang-module.slang export func(float a) { ...} // in test.slang extern func(inout float a); when we compile test.slang, slang will pass a pointer type to the 'func', however, in the slang-module.slang, `func` expects a value instead of pointer. This will lead the wrong spirv code. So we should add the parameter direction into the mangle name such that above two symbols will have the different mangled names, and we will catch this during IR-link stage. * Change to use to get param direction * Address few comments --- source/slang/slang-mangle.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source/slang') diff --git a/source/slang/slang-mangle.cpp b/source/slang/slang-mangle.cpp index ad1a9caca..779a6f090 100644 --- a/source/slang/slang-mangle.cpp +++ b/source/slang/slang-mangle.cpp @@ -547,6 +547,33 @@ namespace Slang for(auto paramDeclRef : parameters) { + // parameter modifier makes big difference in the spirv code generation, for example + // "out"/"inout" parameter will be passed by pointer. Therefore, we need to + // distinguish them in the mangled name to avoid name collision. + ParameterDirection paramDirection = getParameterDirection(paramDeclRef.getDecl()); + switch (paramDirection) + { + case kParameterDirection_Ref: + emitRaw(context, "r_"); + break; + case kParameterDirection_ConstRef: + emitRaw(context, "c_"); + break; + case kParameterDirection_Out: + emitRaw(context, "o_"); + break; + case kParameterDirection_InOut: + emitRaw(context, "io_"); + break; + case kParameterDirection_In: + emitRaw(context, "i_"); + break; + default: + StringBuilder errMsg; + errMsg << "Unknown parameter direction: " << paramDirection; + SLANG_ABORT_COMPILATION(errMsg.toString().begin()); + break; + } emitType(context, getType(context->astBuilder, paramDeclRef)); } -- cgit v1.2.3