Skip to content

Commit 805a839

Browse files
committed
[wasm] Add a thunk cache
1 parent bd6bc1f commit 805a839

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

clang/lib/CodeGen/Targets/WebAssembly.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "ABIInfoImpl.h"
1010
#include "TargetInfo.h"
11+
#include "llvm/ADT/StringMap.h"
1112

1213
#include "clang/AST/ParentMapContext.h"
1314

@@ -56,6 +57,7 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
5657
: TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {
5758
SwiftInfo =
5859
std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
60+
ThunkCache = llvm::StringMap<llvm::Function *>();
5961
}
6062

6163
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
@@ -151,6 +153,16 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
151153
// Construct the Thunk function with the Target (destination) signature
152154
std::string ThunkName = getThunkName(OriginalFnPtr->getName().str(),
153155
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
154166
llvm::Module &M = CGM.getModule();
155167
llvm::Function *Thunk = llvm::Function::Create(
156168
DstFunctionType, llvm::Function::InternalLinkage, ThunkName, M);
@@ -187,10 +199,14 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
187199
LLVM_DEBUG(llvm::dbgs() << "getOrCreateWasmFunctionPointerThunk:"
188200
<< " from " << OriginalFnPtr->getName().str()
189201
<< " to " << ThunkName << "\n");
202+
// Cache the thunk
203+
ThunkCache[ThunkName] = Thunk;
190204
return Thunk;
191205
}
192206

193207
private:
208+
// The thunk cache
209+
mutable llvm::StringMap<llvm::Function *> ThunkCache;
194210
// Build the thunk name: "%s_{OrigName}_{WasmSig}"
195211
std::string getThunkName(std::string OrigName,
196212
const FunctionProtoType *DstProto,

0 commit comments

Comments
 (0)