|
| 1 | +// slang-ir-lower-generic-function.cpp |
| 2 | +#include "slang-ir-lower-generic-function.h" |
| 3 | +#include "slang-ir-generics-lowering-context.h" |
| 4 | + |
| 5 | +namespace Slang |
| 6 | +{ |
| 7 | + struct GenericCallLoweringContext |
| 8 | + { |
| 9 | + SharedGenericsLoweringContext* sharedContext; |
| 10 | + |
| 11 | + // Translate `callInst` into a call of `newCallee`, and respect the new `funcType`. |
| 12 | + // If `funcType` involve lowered generic parameters or return values, this function |
| 13 | + // also translates the argument list to match with that. |
| 14 | + // If `newCallee` is a lowered generic function, `specializeInst` contains the type |
| 15 | + // arguments used to specialize the callee. |
| 16 | + void translateCallInst( |
| 17 | + IRCall* callInst, |
| 18 | + IRFuncType* funcType, |
| 19 | + IRInst* newCallee, |
| 20 | + IRSpecialize* specializeInst) |
| 21 | + { |
| 22 | + List<IRType*> paramTypes; |
| 23 | + for (UInt i = 0; i < funcType->getParamCount(); i++) |
| 24 | + paramTypes.add(funcType->getParamType(i)); |
| 25 | + |
| 26 | + IRBuilder builderStorage; |
| 27 | + auto builder = &builderStorage; |
| 28 | + builder->sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 29 | + builder->setInsertBefore(callInst); |
| 30 | + |
| 31 | + List<IRInst*> args; |
| 32 | + |
| 33 | + // Indicates whether the caller should allocate space for return value. |
| 34 | + // If the lowered callee returns void and this call inst has a type that is not void, |
| 35 | + // then we are calling a transformed function that expects caller allocated return value |
| 36 | + // as the first argument. |
| 37 | + bool shouldCallerAllocateReturnValue = (funcType->getResultType()->op == kIROp_VoidType && |
| 38 | + callInst->getDataType() != funcType->getResultType()); |
| 39 | + |
| 40 | + IRVar* retVarInst = nullptr; |
| 41 | + int startParamIndex = 0; |
| 42 | + if (shouldCallerAllocateReturnValue) |
| 43 | + { |
| 44 | + // Declare a var for the return value. |
| 45 | + retVarInst = builder->emitVar(callInst->getFullType()); |
| 46 | + args.add(retVarInst); |
| 47 | + startParamIndex = 1; |
| 48 | + } |
| 49 | + |
| 50 | + for (UInt i = 0; i < callInst->getArgCount(); i++) |
| 51 | + { |
| 52 | + auto arg = callInst->getArg(i); |
| 53 | + if (as<IRRawPointerTypeBase>(paramTypes[i] + startParamIndex) && |
| 54 | + !as<IRRawPointerTypeBase>(arg->getDataType()) && |
| 55 | + !as<IRPtrTypeBase>(arg->getDataType())) |
| 56 | + { |
| 57 | + // We are calling a generic function that with an argument of |
| 58 | + // some concrete value type. We need to convert this argument to void*. |
| 59 | + // We do so by defining a local variable, store the SSA value |
| 60 | + // in the variable, and use the pointer of this variable as argument. |
| 61 | + auto localVar = builder->emitVar(arg->getDataType()); |
| 62 | + builder->emitStore(localVar, arg); |
| 63 | + arg = localVar; |
| 64 | + } |
| 65 | + args.add(arg); |
| 66 | + } |
| 67 | + if (specializeInst) |
| 68 | + { |
| 69 | + for (UInt i = 0; i < specializeInst->getArgCount(); i++) |
| 70 | + { |
| 71 | + auto arg = specializeInst->getArg(i); |
| 72 | + // Translate Type arguments into RTTI object. |
| 73 | + if (as<IRType>(arg)) |
| 74 | + { |
| 75 | + // We are using a simple type to specialize a callee. |
| 76 | + // Generate RTTI for this type. |
| 77 | + auto rttiObject = sharedContext->maybeEmitRTTIObject(arg); |
| 78 | + arg = builder->emitGetAddress( |
| 79 | + builder->getPtrType(builder->getRTTIType()), |
| 80 | + rttiObject); |
| 81 | + } |
| 82 | + else if (arg->op == kIROp_Specialize) |
| 83 | + { |
| 84 | + // The type argument used to specialize a callee is itself a |
| 85 | + // specialization of some generic type. |
| 86 | + // TODO: generate RTTI object for specializations of generic types. |
| 87 | + SLANG_UNIMPLEMENTED_X("RTTI object generation for generic types"); |
| 88 | + } |
| 89 | + else if (arg->op == kIROp_RTTIObject) |
| 90 | + { |
| 91 | + // We are inside a generic function and using a generic parameter |
| 92 | + // to specialize another callee. The generic parameter of the caller |
| 93 | + // has already been translated into an RTTI object, so we just need |
| 94 | + // to pass this object down. |
| 95 | + } |
| 96 | + args.add(arg); |
| 97 | + } |
| 98 | + } |
| 99 | + auto callInstType = retVarInst ? builder->getVoidType() : callInst->getFullType(); |
| 100 | + auto newCall = builder->emitCallInst(callInstType, newCallee, args); |
| 101 | + if (retVarInst) |
| 102 | + { |
| 103 | + auto loadInst = builder->emitLoad(retVarInst); |
| 104 | + callInst->replaceUsesWith(loadInst); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + callInst->replaceUsesWith(newCall); |
| 109 | + } |
| 110 | + callInst->removeAndDeallocate(); |
| 111 | + } |
| 112 | + |
| 113 | + void lowerCallToSpecializedFunc(IRCall* callInst, IRSpecialize* specializeInst) |
| 114 | + { |
| 115 | + // If we see a call(specialize(gFunc, Targs), args), |
| 116 | + // translate it into call(gFunc, args, Targs). |
| 117 | + auto loweredFunc = specializeInst->getBase(); |
| 118 | + // All callees should have already been lowered in lower-generic-functions pass. |
| 119 | + // For intrinsic generic functions, they are left as is, and we also need to ignore |
| 120 | + // them here. |
| 121 | + if (loweredFunc->op == kIROp_Generic) |
| 122 | + { |
| 123 | + // This is an intrinsic function, don't transform. |
| 124 | + return; |
| 125 | + } |
| 126 | + IRFuncType* funcType = cast<IRFuncType>(loweredFunc->getDataType()); |
| 127 | + translateCallInst(callInst, funcType, loweredFunc, specializeInst); |
| 128 | + } |
| 129 | + |
| 130 | + void lowerCall(IRCall* callInst) |
| 131 | + { |
| 132 | + if (auto specializeInst = as<IRSpecialize>(callInst->getCallee())) |
| 133 | + lowerCallToSpecializedFunc(callInst, specializeInst); |
| 134 | + } |
| 135 | + |
| 136 | + void processInst(IRInst* inst) |
| 137 | + { |
| 138 | + if (auto callInst = as<IRCall>(inst)) |
| 139 | + { |
| 140 | + lowerCall(callInst); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + void processModule() |
| 145 | + { |
| 146 | + // We start by initializing our shared IR building state, |
| 147 | + // since we will re-use that state for any code we |
| 148 | + // generate along the way. |
| 149 | + // |
| 150 | + SharedIRBuilder* sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 151 | + sharedBuilder->module = sharedContext->module; |
| 152 | + sharedBuilder->session = sharedContext->module->session; |
| 153 | + |
| 154 | + sharedContext->addToWorkList(sharedContext->module->getModuleInst()); |
| 155 | + |
| 156 | + while (sharedContext->workList.getCount() != 0) |
| 157 | + { |
| 158 | + // We will then iterate until our work list goes dry. |
| 159 | + // |
| 160 | + while (sharedContext->workList.getCount() != 0) |
| 161 | + { |
| 162 | + IRInst* inst = sharedContext->workList.getLast(); |
| 163 | + |
| 164 | + sharedContext->workList.removeLast(); |
| 165 | + sharedContext->workListSet.Remove(inst); |
| 166 | + |
| 167 | + processInst(inst); |
| 168 | + |
| 169 | + for (auto child = inst->getLastChild(); child; child = child->getPrevInst()) |
| 170 | + { |
| 171 | + sharedContext->addToWorkList(child); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + }; |
| 177 | + |
| 178 | + void lowerGenericCalls(SharedGenericsLoweringContext* sharedContext) |
| 179 | + { |
| 180 | + GenericCallLoweringContext context; |
| 181 | + context.sharedContext = sharedContext; |
| 182 | + context.processModule(); |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments