mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-09 13:16:26 +00:00
Fix ExpandMemoryOpPass doesn't work properly (#2399)
The old method may not work for some cases. This PR iterates over all instructions in the function, looking for memcpy, memmove and memset instructions, putting them into a set, and finally expands them into a loop one by one. And move this LLVM Pass after building the pipe line of pass builder to ensure that the memcpy/memmove/memset instrinsics are generated before applying the pass.
This commit is contained in:
parent
7db4815e83
commit
10b18d85cd
|
@ -82,43 +82,40 @@ class ExpandMemoryOpPass : public PassInfoMixin<ExpandMemoryOpPass>
|
||||||
PreservedAnalyses
|
PreservedAnalyses
|
||||||
ExpandMemoryOpPass::run(Function &F, FunctionAnalysisManager &AM)
|
ExpandMemoryOpPass::run(Function &F, FunctionAnalysisManager &AM)
|
||||||
{
|
{
|
||||||
Intrinsic::ID ID = F.getIntrinsicID();
|
SmallVector<MemIntrinsic *, 16> MemCalls;
|
||||||
bool Changed = false;
|
|
||||||
|
|
||||||
for (auto I = F.user_begin(), E = F.user_end(); I != E;) {
|
/* Iterate over all instructions in the function, looking for memcpy,
|
||||||
Instruction *Inst = cast<Instruction>(*I);
|
* memmove, and memset. When we find one, expand it into a loop. */
|
||||||
++I;
|
|
||||||
|
|
||||||
switch (ID) {
|
for (auto &BB : F) {
|
||||||
case Intrinsic::memcpy:
|
for (auto &Inst : BB) {
|
||||||
{
|
if (auto *Memcpy = dyn_cast_or_null<MemCpyInst>(&Inst)) {
|
||||||
auto *Memcpy = cast<MemCpyInst>(Inst);
|
MemCalls.push_back(Memcpy);
|
||||||
Function *ParentFunc = Memcpy->getParent()->getParent();
|
|
||||||
const TargetTransformInfo &TTI =
|
|
||||||
AM.getResult<TargetIRAnalysis>(*ParentFunc);
|
|
||||||
expandMemCpyAsLoop(Memcpy, TTI);
|
|
||||||
Memcpy->eraseFromParent();
|
|
||||||
Changed = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Intrinsic::memmove:
|
else if (auto *Memmove = dyn_cast_or_null<MemMoveInst>(&Inst)) {
|
||||||
{
|
MemCalls.push_back(Memmove);
|
||||||
auto *Memmove = cast<MemMoveInst>(Inst);
|
|
||||||
expandMemMoveAsLoop(Memmove);
|
|
||||||
Memmove->eraseFromParent();
|
|
||||||
Changed = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Intrinsic::memset:
|
else if (auto *Memset = dyn_cast_or_null<MemSetInst>(&Inst)) {
|
||||||
{
|
MemCalls.push_back(Memset);
|
||||||
auto *Memset = cast<MemSetInst>(Inst);
|
|
||||||
expandMemSetAsLoop(Memset);
|
|
||||||
Memset->eraseFromParent();
|
|
||||||
Changed = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
default:
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
|
for (MemIntrinsic *MemCall : MemCalls) {
|
||||||
|
if (MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(MemCall)) {
|
||||||
|
Function *ParentFunc = Memcpy->getParent()->getParent();
|
||||||
|
const TargetTransformInfo &TTI =
|
||||||
|
AM.getResult<TargetIRAnalysis>(*ParentFunc);
|
||||||
|
expandMemCpyAsLoop(Memcpy, TTI);
|
||||||
|
Memcpy->eraseFromParent();
|
||||||
|
}
|
||||||
|
else if (MemMoveInst *Memmove = dyn_cast<MemMoveInst>(MemCall)) {
|
||||||
|
expandMemMoveAsLoop(Memmove);
|
||||||
|
Memmove->eraseFromParent();
|
||||||
|
}
|
||||||
|
else if (MemSetInst *Memset = dyn_cast<MemSetInst>(MemCall)) {
|
||||||
|
expandMemSetAsLoop(Memset);
|
||||||
|
Memset->eraseFromParent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,13 +294,6 @@ aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module)
|
||||||
FPM.addPass(SLPVectorizerPass());
|
FPM.addPass(SLPVectorizerPass());
|
||||||
FPM.addPass(LoadStoreVectorizerPass());
|
FPM.addPass(LoadStoreVectorizerPass());
|
||||||
|
|
||||||
/* Run specific passes for AOT indirect mode in last since general
|
|
||||||
optimization may create some intrinsic function calls like
|
|
||||||
llvm.memset, so let's remove these function calls here. */
|
|
||||||
if (comp_ctx->is_indirect_mode) {
|
|
||||||
FPM.addPass(ExpandMemoryOpPass());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (comp_ctx->enable_llvm_pgo || comp_ctx->use_prof_file) {
|
if (comp_ctx->enable_llvm_pgo || comp_ctx->use_prof_file) {
|
||||||
/* LICM pass: loop invariant code motion, attempting to remove
|
/* LICM pass: loop invariant code motion, attempting to remove
|
||||||
as much code from the body of a loop as possible. Experiments
|
as much code from the body of a loop as possible. Experiments
|
||||||
|
@ -341,6 +331,15 @@ aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module)
|
||||||
else {
|
else {
|
||||||
MPM.addPass(PB.buildPerModuleDefaultPipeline(OL));
|
MPM.addPass(PB.buildPerModuleDefaultPipeline(OL));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Run specific passes for AOT indirect mode in last since general
|
||||||
|
optimization may create some intrinsic function calls like
|
||||||
|
llvm.memset, so let's remove these function calls here. */
|
||||||
|
if (comp_ctx->is_indirect_mode) {
|
||||||
|
FunctionPassManager FPM1;
|
||||||
|
FPM1.addPass(ExpandMemoryOpPass());
|
||||||
|
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM1)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MPM.run(*M, MAM);
|
MPM.run(*M, MAM);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user