|
8 | 8 |
|
9 | 9 | #include "ABIInfoImpl.h" |
10 | 10 | #include "TargetInfo.h" |
| 11 | +#include "llvm/ADT/StringMap.h" |
11 | 12 |
|
12 | 13 | #include "clang/AST/ParentMapContext.h" |
13 | 14 |
|
@@ -56,6 +57,7 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
56 | 57 | : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) { |
57 | 58 | SwiftInfo = |
58 | 59 | std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false); |
| 60 | + ThunkCache = llvm::StringMap<llvm::Function *>(); |
59 | 61 | } |
60 | 62 |
|
61 | 63 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
@@ -151,6 +153,16 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
151 | 153 | // Construct the Thunk function with the Target (destination) signature |
152 | 154 | std::string ThunkName = getThunkName(OriginalFnPtr->getName().str(), |
153 | 155 | DstProtoType, CGM.getContext()); |
| 156 | + // Check if we already have a thunk for this function |
| 157 | + if (auto It = ThunkCache.find(ThunkName); It != ThunkCache.end()) { |
| 158 | + LLVM_DEBUG(llvm::dbgs() << "getOrCreateWasmFunctionPointerThunk: " |
| 159 | + << "found existing thunk for " |
| 160 | + << OriginalFnPtr->getName().str() << " as " |
| 161 | + << ThunkName << "\n"); |
| 162 | + return It->second; |
| 163 | + } |
| 164 | + |
| 165 | + // Create the thunk function |
154 | 166 | llvm::Module &M = CGM.getModule(); |
155 | 167 | llvm::Function *Thunk = llvm::Function::Create( |
156 | 168 | DstFunctionType, llvm::Function::InternalLinkage, ThunkName, M); |
@@ -187,10 +199,14 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
187 | 199 | LLVM_DEBUG(llvm::dbgs() << "getOrCreateWasmFunctionPointerThunk:" |
188 | 200 | << " from " << OriginalFnPtr->getName().str() |
189 | 201 | << " to " << ThunkName << "\n"); |
| 202 | + // Cache the thunk |
| 203 | + ThunkCache[ThunkName] = Thunk; |
190 | 204 | return Thunk; |
191 | 205 | } |
192 | 206 |
|
193 | 207 | private: |
| 208 | + // The thunk cache |
| 209 | + mutable llvm::StringMap<llvm::Function *> ThunkCache; |
194 | 210 | // Build the thunk name: "%s_{OrigName}_{WasmSig}" |
195 | 211 | std::string getThunkName(std::string OrigName, |
196 | 212 | const FunctionProtoType *DstProto, |
|
0 commit comments