From 063cbeaaea2fb00a10c6058ea4a9632092772ea5 Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Thu, 7 Aug 2025 00:22:22 -0700 Subject: Initial copy elision pass (#8042) Fixes #7574 Changes: * Add an initial (fairly simple) optimization pass which is able to eliminate redundant copies. * Our current existing optimizer passes remove redundant load/store very robustly, this pass will focus on other cases of copy elimination * Primary approach is to make all functions which are `in T` and `T` is trivial to copy into a `__constref T`. We then (depending on scenario) manually insert a variable+load if a pass-by-reference is not possible; otherwise we pass by `constref`. * Added optimizations to eliminate redundant code which causes `constref` to fail to compile --------- Co-authored-by: Harsh Aggarwal Co-authored-by: Claude Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/slang/slang-emit-cpp.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'source/slang/slang-emit-cpp.cpp') diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp index ffc4b97ef..66829308d 100644 --- a/source/slang/slang-emit-cpp.cpp +++ b/source/slang/slang-emit-cpp.cpp @@ -307,6 +307,25 @@ SlangResult CPPSourceEmitter::calcTypeName(IRType* type, CodeGenTarget target, S case kIROp_PtrType: case kIROp_ConstRefType: { + // Special note on `constref` types and why they are not emitted + // as a `const` pointer: + // + // We currently do not propegate/manage "constness" for locals. + // This is important since it means that we rely on opimization + // passes to remove all temporary pointer-variables created from + // our constref, otherwise we will generate invalid code like + // `T* var = const_ptr` or `T* var = &const_ptr->member`. + // + // If emitting `constref` fails due to this error, it is likely + // a missing compiler-optimization. + // + // Additionally, for C++/CUDA, downstream methods are required + // to be `const` if we want to use const pointers. This is currently + // not handled robustly. + // + // Due to these cascading issues, we do not emit const and instead + // emit as a regular pointer for the time being. + auto elementType = (IRType*)type->getOperand(0); SLANG_RETURN_ON_FAIL(calcTypeName(elementType, target, out)); out << "*"; @@ -599,6 +618,7 @@ CPPSourceEmitter::CPPSourceEmitter(const Desc& desc) void CPPSourceEmitter::emitParamTypeImpl(IRType* type, String const& name) { + // For use the CPP-specific emitType implementation emitType(type, name); } -- cgit v1.2.3