|
| 1 | +#include "GIL/InstVisitor.hpp" |
| 2 | +#include "GIL/Module.hpp" |
| 3 | +#include "GILGen/Context.hpp" |
| 4 | +#include "Instructions/LoadInst.hpp" |
| 5 | +#include "Instructions/ReturnInst.hpp" |
| 6 | +#include "PassManager.hpp" |
| 7 | + |
| 8 | +namespace glu::optimizer { |
| 9 | + |
| 10 | +class CopyLoweringPass : public gil::InstVisitor<CopyLoweringPass> { |
| 11 | +private: |
| 12 | + gil::Module *module; |
| 13 | + std::optional<gilgen::Context> ctx = std::nullopt; |
| 14 | + llvm::BumpPtrAllocator &arena; |
| 15 | + llvm::SmallVector<gil::InstBase *, 8> toErase; |
| 16 | + bool inCopyFunction = false; // Track if we're inside a copy function |
| 17 | + |
| 18 | +public: |
| 19 | + CopyLoweringPass(gil::Module *module, llvm::BumpPtrAllocator &arena) |
| 20 | + : module(module), arena(arena) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + ~CopyLoweringPass() |
| 25 | + { |
| 26 | + for (auto *inst : toErase) { |
| 27 | + inst->eraseFromParent(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + void visitLoadInst(gil::LoadInst *loadInst) |
| 32 | + { |
| 33 | + // Don't transform loads inside copy functions (would cause infinite |
| 34 | + // recursion) |
| 35 | + if (inCopyFunction) |
| 36 | + return; |
| 37 | + |
| 38 | + // Only handle load [copy] instructions |
| 39 | + if (loadInst->getOwnershipKind() != gil::LoadOwnershipKind::Copy) |
| 40 | + return; |
| 41 | + |
| 42 | + if (!ctx) |
| 43 | + return; |
| 44 | + |
| 45 | + // Generate code to call the copy function if it exists |
| 46 | + auto *structure = llvm::dyn_cast<types::StructTy>( |
| 47 | + loadInst->getResultType(0).getType() |
| 48 | + ); |
| 49 | + if (structure && structure->getDecl()->hasOverloadedCopyFunction()) { |
| 50 | + // Change the load to trivial ownership (no copy semantics) |
| 51 | + loadInst->setOwnershipKind(gil::LoadOwnershipKind::Trivial); |
| 52 | + |
| 53 | + // Insert a call to the copy function after the load |
| 54 | + auto *bb = loadInst->getParent(); |
| 55 | + auto it = std::next(loadInst->getIterator()); |
| 56 | + gil::InstBase *nextInst |
| 57 | + = (it != bb->getInstructions().end()) ? &*it : nullptr; |
| 58 | + |
| 59 | + ctx->setInsertionPoint(bb, nextInst); |
| 60 | + ctx->setSourceLoc(loadInst->getLocation()); |
| 61 | + |
| 62 | + // Call the copy function with the loaded value |
| 63 | + auto *callInst = ctx->buildCall( |
| 64 | + structure->getDecl()->getCopyFunction(), |
| 65 | + { loadInst->getResult(0) } |
| 66 | + ); |
| 67 | + |
| 68 | + // Find the store that uses this load's result and update it |
| 69 | + if (nextInst && llvm::isa<gil::StoreInst>(nextInst)) { |
| 70 | + auto *storeInst = llvm::cast<gil::StoreInst>(nextInst); |
| 71 | + if (storeInst->getSource() == loadInst->getResult(0)) { |
| 72 | + // Replace the store's source with the call result |
| 73 | + storeInst->setSource(callInst->getResult(0)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void visitCopyInst(gil::CopyInst *copyInst) |
| 80 | + { |
| 81 | + if (!ctx) |
| 82 | + return; |
| 83 | + |
| 84 | + auto *bb = copyInst->getParent(); |
| 85 | + ctx->setInsertionPoint(bb, copyInst); |
| 86 | + ctx->setSourceLoc(copyInst->getLocation()); |
| 87 | + |
| 88 | + // Generate code to call the copy function if it exists |
| 89 | + if (auto *structure = llvm::dyn_cast<types::StructTy>( |
| 90 | + copyInst->getSource().getType().getType() |
| 91 | + )) { |
| 92 | + if (structure->getDecl()->hasOverloadedCopyFunction()) { |
| 93 | + auto *callInst = ctx->buildCall( |
| 94 | + structure->getDecl()->getCopyFunction(), |
| 95 | + { copyInst->getSource() } |
| 96 | + ); |
| 97 | + // Replace the copy instruction with the call instruction |
| 98 | + bb->replaceInstruction(copyInst, callInst); |
| 99 | + return; // Don't erase, we replaced it |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Remove the original copy instruction if no custom copy function |
| 104 | + toErase.push_back(copyInst); |
| 105 | + } |
| 106 | + |
| 107 | + void beforeVisitFunction(gil::Function *func) |
| 108 | + { |
| 109 | + // Check if this is a copy function |
| 110 | + if (func->getName() == "copy") { |
| 111 | + inCopyFunction = true; |
| 112 | + } |
| 113 | + |
| 114 | + // Create context for this function |
| 115 | + ctx.emplace(module, func, arena); |
| 116 | + } |
| 117 | + |
| 118 | + void afterVisitFunction(gil::Function *) |
| 119 | + { |
| 120 | + ctx.reset(); |
| 121 | + inCopyFunction = false; |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +void PassManager::runCopyLoweringPass() |
| 126 | +{ |
| 127 | + CopyLoweringPass pass(_module, _gilArena); |
| 128 | + pass.visit(_module); |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace glu::optimizer |
0 commit comments