================ @@ -119,6 +123,119 @@ class OpLowerer { }); } + Value *createTmpHandleCast(Value *V, Type *Ty) { + Function *CastFn = Intrinsic::getDeclaration(&M, Intrinsic::dx_cast_handle, + {Ty, V->getType()}); + CallInst *Cast = OpBuilder.getIRB().CreateCall(CastFn, {V}); + CleanupCasts.push_back(Cast); + return Cast; + } + + void cleanupHandleCasts() { + SmallVector<CallInst *> ToRemove; + SmallVector<Function *> CastFns; + + for (CallInst *Cast : CleanupCasts) { + CastFns.push_back(Cast->getCalledFunction()); + // All of the ops should be using `dx.types.Handle` at this point, so if + // we're not producing that we should be part of a pair. Track this so we + // can remove it at the end. + if (Cast->getType() != OpBuilder.getHandleType()) { + ToRemove.push_back(Cast); + continue; + } + // Otherwise, we're the second handle in a pair. Forward the arguments and + // remove the (second) cast. + CallInst *Def = cast<CallInst>(Cast->getOperand(0)); + assert(Def->getIntrinsicID() == Intrinsic::dx_cast_handle && + "Unbalanced pair of temporary handle casts"); + Cast->replaceAllUsesWith(Def->getOperand(0)); + Cast->eraseFromParent(); + } + for (CallInst *Cast : ToRemove) { + assert(Cast->user_empty() && "Temporary handle cast still has users"); + Cast->eraseFromParent(); + } + llvm::sort(CastFns); + CastFns.erase(llvm::unique(CastFns), CastFns.end()); + for (Function *F : CastFns) + F->eraseFromParent(); ---------------- bogner wrote:
Above we add the cast function to the list every time we see a call to a cast - this means we could end up with duplicates if the same cast function is called multiple times. So, we need to deduplicate the list before we start erasing the functions from the module. https://github.com/llvm/llvm-project/pull/104251 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits