Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor InitMemoryPass to FinalizeMemoryInfoPass #245

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace cheerp

using namespace llvm;

class MemoryInitPass : public PassInfoMixin<MemoryInitPass> {
class FinalizeMemoryInfoPass : public PassInfoMixin<FinalizeMemoryInfoPass> {
private:

public:
Expand Down
7 changes: 3 additions & 4 deletions llvm/include/llvm/Cheerp/LinearMemoryHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,17 @@ class LinearMemoryHelper
this->module = &module;
globalDeps = GDA;
builtinIds.fill(std::numeric_limits<uint32_t>::max());
addFunctions();
addStack();
addGlobals();
checkMemorySize();
addMemoryInfo();
populateGlobalData();

return false;
}

void addFunctions();
void populateGlobalData();

uint32_t getGlobalVariableAddress(const llvm::GlobalVariable* G) const;
const llvm::GlobalVariable* getGlobalVariableFromAddress(llvm::Value* C) const;
uint32_t getFunctionAddress(const llvm::Function* F) const;
Expand Down Expand Up @@ -392,11 +393,9 @@ class LinearMemoryHelper

void setGlobalPtrIfPresent(llvm::StringRef name, uint32_t ptr);
void addGlobals();
void addFunctions();
void addStack();
void addMemoryInfo();
void checkMemorySize();
void populateGlobalData();

llvm::Module* module;
GlobalDepsAnalyzer* globalDeps;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CheerpUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ add_llvm_component_library(LLVMCheerpUtils
SIMDTransform.cpp
BitCastLowering.cpp
JSStringLiteralLowering.cpp
MemoryInit.cpp
FinalizeMemoryInfo.cpp
CheerpLowerAtomic.cpp
JsExport.cpp
)
Expand Down
77 changes: 77 additions & 0 deletions llvm/lib/CheerpUtils/FinalizeMemoryInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===-- MemoryInit.cpp - Populate the __memory_init function with init intrinsics------===//
//
// Cheerp: The C++ compiler for the Web
//
// This file is distributed under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT for details.
//
// Copyright 2022-2023 Leaning Technologies
//
//===----------------------------------------------------------------------===//

#include "llvm/Cheerp/FinalizeMemoryInfo.h"
#include "llvm/Cheerp/LinearMemoryHelper.h"
#include "llvm/Cheerp/AllocaMerging.h"
#include "llvm/Cheerp/Registerize.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Intrinsics.h"

using namespace llvm;

namespace cheerp
{

PreservedAnalyses FinalizeMemoryInfoPass::run(Module& M, ModuleAnalysisManager& MAM)
{
PointerAnalyzer &PA = MAM.getResult<PointerAnalysis>(M);
Registerize &registerize = MAM.getResult<RegisterizeAnalysis>(M);
LinearMemoryHelper &linearHelper = MAM.getResult<LinearMemoryAnalysis>(M);
AllocaStoresExtractor &allocaStoresExtractor = MAM.getResult<AllocaStoresExtractorAnalysis>(M);

PA.fullResolve();
PA.computeConstantOffsets(M);
linearHelper.addFunctions();
linearHelper.populateGlobalData();

if (LinearOutput == LinearOutputTy::Wasm)
{
Function* initFunc = M.getFunction("__memory_init");
if (!initFunc)
{
// We print a warning and return. This is to satisfy some llvm tests.
llvm::errs() << "warning: __memory_init function point not found\n";
return PreservedAnalyses::all();
}
if (!initFunc->empty())
llvm::report_fatal_error("__memory_init function already has a body.");

BasicBlock* Entry = BasicBlock::Create(M.getContext(), "entry", initFunc);
IRBuilder<> Builder(Entry);

// Loop over the chunks and populate the function.
Function* memoryInit = Intrinsic::getDeclaration(&M, Intrinsic::cheerp_memory_init);
Function* dataDrop = Intrinsic::getDeclaration(&M, Intrinsic::cheerp_data_drop);

uint32_t amountOfChunks = linearHelper.getAmountChunks();
for (uint32_t i = 0; i < amountOfChunks; i++)
{
auto chunk = linearHelper.getGlobalDataChunk(i);
Builder.CreateCall(memoryInit, {Builder.getInt32(i), Builder.getInt32(chunk.address), Builder.getInt32(0), Builder.getInt32(chunk.view.size())});
Builder.CreateCall(dataDrop, {Builder.getInt32(i)});
}

Builder.CreateRetVoid();
}

// Destroy the stores here, we need them to properly compute the pointer kinds, but we want to optimize them away before registerize
allocaStoresExtractor.unlinkStores();

registerize.assignRegisters(M, PA);
#ifdef REGISTERIZE_STATS
cheerp::reportRegisterizeStatistics();
#endif

return PreservedAnalyses::all();
}

}
55 changes: 0 additions & 55 deletions llvm/lib/CheerpUtils/MemoryInit.cpp

This file was deleted.

16 changes: 4 additions & 12 deletions llvm/lib/Target/WebAssembly/CheerpWritePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "llvm/Cheerp/DTSWriter.h"
#include "llvm/Cheerp/I64Lowering.h"
#include "llvm/Cheerp/JSStringLiteralLowering.h"
#include "llvm/Cheerp/MemoryInit.h"
#include "llvm/Cheerp/FinalizeMemoryInfo.h"
#include "llvm/Cheerp/PassRegistry.h"
#include "llvm/Cheerp/PassUtility.h"
#include "llvm/Cheerp/SIMDLowering.h"
Expand Down Expand Up @@ -60,15 +60,6 @@ PreservedAnalyses cheerp::CheerpWritePassImpl::run(Module& M, ModuleAnalysisMana
return PreservedAnalyses::none();
}
}
PA.fullResolve();
PA.computeConstantOffsets(M);
// Destroy the stores here, we need them to properly compute the pointer kinds, but we want to optimize them away before registerize
allocaStoresExtractor.unlinkStores();

registerize.assignRegisters(M, PA);
#ifdef REGISTERIZE_STATS
cheerp::reportRegisterizeStatistics();
#endif

Triple TargetTriple(M.getTargetTriple());
bool WasmOnly = TargetTriple.getOS() == Triple::WASI;
Expand Down Expand Up @@ -261,8 +252,6 @@ bool CheerpWritePass::runOnModule(Module& M)
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::PreserveCheerpAnalysisPassWrapper<DCEPass, Function, FunctionAnalysisManager>()));
MPM.addPass(cheerp::RegisterizePass(!NoJavaScriptMathFround, LinearOutput == Wasm));
MPM.addPass(cheerp::LinearMemoryHelperPass(cheerp::LinearMemoryHelperInitializer({functionAddressMode, CheerpHeapSize, CheerpStackSize, CheerpStackOffset, growMem, hasAsmjsMem})));
if (LinearOutput == LinearOutputTy::Wasm)
MPM.addPass(cheerp::MemoryInitPass());
MPM.addPass(cheerp::ConstantExprLoweringPass());
MPM.addPass(cheerp::PointerAnalyzerPass());
MPM.addPass(cheerp::DelayInstsPass());
Expand All @@ -275,6 +264,9 @@ bool CheerpWritePass::runOnModule(Module& M)
// Keep this pass last, it is going to remove stores to memory from the LLVM visible code, so further optimizing afterwards will break
MPM.addPass(cheerp::AllocaStoresExtractorPass());

// This pass needs to be ran after the AllocaStoresExtractorPass and does not do any optimizing.
MPM.addPass(cheerp::FinalizeMemoryInfoPass());

MPM.addPass(cheerp::CheerpWritePassImpl(Out, TM));

// Now that we have all the passes ready, run them.
Expand Down
Loading