Skip to content

Commit

Permalink
CheerpWritePass moved to /CheerpWriter/
Browse files Browse the repository at this point in the history
  • Loading branch information
ZyuN0cUka1 committed Jun 12, 2024
1 parent 4d38664 commit fdf6a7d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- CheerpBackend.cpp - Backend wrapper for CheerpWriter---------------===//
//===-- CheerpWritePass.h - Pass writer for CheerpWriter ------------------===//
//
// The LLVM Compiler Infrastructure
//
Expand Down Expand Up @@ -47,15 +47,12 @@ class CheerpWritePass : public ModulePass {
private:
raw_ostream &Out;
static char ID;
void getAnalysisUsage(AnalysisUsage& AU) const override;
void getAnalysisUsage(AnalysisUsage& AU) const override { }
TargetMachine* TM;
public:
explicit CheerpWritePass(raw_ostream &o, TargetMachine* TM) :
ModulePass(ID), Out(o), TM(TM) { }
explicit CheerpWritePass(raw_ostream &o, TargetMachine* TM) :ModulePass(ID), Out(o), TM(TM) { }
bool runOnModule(Module &M) override;
StringRef getPassName() const override {
return "CheerpWritePass";
}
StringRef getPassName() const override { return "CheerpWritePass"; }
};
} // end anonymous namespace.

Expand Down Expand Up @@ -180,135 +177,4 @@ bool CheerpWritePass::runOnModule(Module& M)
return false;
}

PreservedAnalyses cheerp::CheerpWritePassImpl::run(Module& M, ModuleAnalysisManager& MAM)
{
cheerp::Registerize &registerize = MAM.getResult<cheerp::RegisterizeAnalysis>(M);
cheerp::GlobalDepsAnalyzer &GDA = MAM.getResult<cheerp::GlobalDepsAnalysis>(M);
cheerp::PointerAnalyzer &PA = MAM.getResult<cheerp::PointerAnalysis>(M);
cheerp::InvokeWrapping &IW = MAM.getResult<cheerp::InvokeWrappingAnalysis>(M);
cheerp::AllocaStoresExtractor &allocaStoresExtractor = MAM.getResult<cheerp::AllocaStoresExtractorAnalysis>(M);
cheerp::LinearMemoryHelper &linearHelper = MAM.getResult<LinearMemoryAnalysis>(M);
std::unique_ptr<cheerp::SourceMapGenerator> sourceMapGenerator;
GDA.forceTypedArrays = ForceTypedArrays;
if (!SourceMap.empty())
{
std::error_code ErrorCode;
sourceMapGenerator.reset(new cheerp::SourceMapGenerator(SourceMap, SourceMapPrefix, SourceMapStandAlone, ErrorCode));
if (ErrorCode)
{
// An error occurred opening the source map file, bail out
llvm::report_fatal_error(StringRef(ErrorCode.message()), false);
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;
std::error_code ErrorCode;
llvm::ToolOutputFile secondaryFile(SecondaryOutputFile, ErrorCode, sys::fs::OF_None);
std::unique_ptr<llvm::formatted_raw_ostream> secondaryOut;
if (!SecondaryOutputFile.empty())
{
secondaryOut.reset(new formatted_raw_ostream(secondaryFile.os()));
}
else if (WasmOnly && LinearOutput != AsmJs)
{
secondaryOut.reset(new formatted_raw_ostream(Out));
}

std::error_code dtsErrorCode;
llvm::ToolOutputFile dtsFile(DTSOutputFile, dtsErrorCode, sys::fs::OF_None);
std::unique_ptr<llvm::formatted_raw_ostream> dtsOut;
if (!DTSOutputFile.empty())
{
dtsOut.reset(new formatted_raw_ostream(dtsFile.os()));
}

// Build the ordered list of reserved names
std::vector<std::string> reservedNames(ReservedNames.begin(), ReservedNames.end());
std::sort(reservedNames.begin(), reservedNames.end());

cheerp::NameGenerator namegen(M, GDA, registerize, PA, linearHelper, reservedNames, PrettyCode, WasmExportedMemory);

std::string wasmFile;
std::string asmjsMemFile;
llvm::formatted_raw_ostream* memOut = nullptr;
switch (LinearOutput)
{
case Wasm:
if (!SecondaryOutputPath.empty())
wasmFile = SecondaryOutputPath.getValue();
else if (!SecondaryOutputFile.empty())
wasmFile = std::string(llvm::sys::path::filename(SecondaryOutputFile.getValue()));
break;
case AsmJs:
if (!SecondaryOutputPath.empty())
asmjsMemFile = SecondaryOutputPath.getValue();
else if (!SecondaryOutputFile.empty())
asmjsMemFile = std::string(llvm::sys::path::filename(SecondaryOutputFile.getValue()));
memOut = secondaryOut.get();
break;
}

MODULE_TYPE makeModule = getModuleType(MakeModule);

if (MakeDTS && dtsOut)
{
cheerp::CheerpDTSWriter dtsWriter(M, *dtsOut, sourceMapGenerator.get(), PrettyCode, makeModule);
dtsWriter.makeDTS();
}

if (!WasmOnly)
{
cheerp::CheerpWriter writer(M, MAM, Out, PA, registerize, GDA, linearHelper, namegen, allocaStoresExtractor, IW.getLandingPadTable(), memOut, asmjsMemFile,
sourceMapGenerator.get(), PrettyCode, makeModule, !NoNativeJavaScriptMath,
!NoJavaScriptMathImul, !NoJavaScriptMathFround, !NoCredits, MeasureTimeToMain, CheerpHeapSize,
BoundsCheck, SymbolicGlobalsAsmJS, wasmFile, ForceTypedArrays);
writer.makeJS();
}

if (LinearOutput != AsmJs && secondaryOut)
{
cheerp::CheerpWasmWriter wasmWriter(M, MAM, *secondaryOut, PA, registerize, GDA, linearHelper, IW.getLandingPadTable(), namegen,
M.getContext(), CheerpHeapSize, !WasmOnly,
PrettyCode, WasmSharedMemory,
WasmExportedTable);
wasmWriter.makeWasm();
}

allocaStoresExtractor.destroyStores();

if (!SecondaryOutputFile.empty() && ErrorCode)
{
// An error occurred opening the asm.js memory file, bail out
llvm::report_fatal_error(StringRef(ErrorCode.message()), false);
return PreservedAnalyses::none();
}
if (!DTSOutputFile.empty() && dtsErrorCode)
{
llvm::report_fatal_error(StringRef(dtsErrorCode.message()), false);
return PreservedAnalyses::none();
}
if (!WasmOnly)
secondaryFile.keep();
if (MakeDTS)
dtsFile.keep();


return PreservedAnalyses::none();
}

void CheerpWritePass::getAnalysisUsage(AnalysisUsage& AU) const
{
}

char CheerpWritePass::ID = 0;
1 change: 1 addition & 0 deletions llvm/lib/CheerpWriter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_llvm_component_library(LLVMCheerpWriter
PreExecute.cpp
CFGStackifier.cpp
PartialExecuter.cpp
CheerpWritePass.cpp

ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_SRC_DIR}/ExecutionEngine/
Expand Down
145 changes: 145 additions & 0 deletions llvm/lib/CheerpWriter/CheerpWritePass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//===-- CheerpWritePass.cpp - Pass writer for CheerpWriter ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT for details.
//
// Copyright 2011-2023 Leaning Technologies
//
//===----------------------------------------------------------------------===//

#include "llvm/Cheerp/CheerpWritePass.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Cheerp/WasmWriter.h"
#include "llvm/Cheerp/DTSWriter.h"
using namespace llvm;

PreservedAnalyses cheerp::CheerpWritePassImpl::run(Module& M, ModuleAnalysisManager& MAM)
{
cheerp::Registerize &registerize = MAM.getResult<cheerp::RegisterizeAnalysis>(M);
cheerp::GlobalDepsAnalyzer &GDA = MAM.getResult<cheerp::GlobalDepsAnalysis>(M);
cheerp::PointerAnalyzer &PA = MAM.getResult<cheerp::PointerAnalysis>(M);
cheerp::InvokeWrapping &IW = MAM.getResult<cheerp::InvokeWrappingAnalysis>(M);
cheerp::AllocaStoresExtractor &allocaStoresExtractor = MAM.getResult<cheerp::AllocaStoresExtractorAnalysis>(M);
cheerp::LinearMemoryHelper &linearHelper = MAM.getResult<LinearMemoryAnalysis>(M);
std::unique_ptr<cheerp::SourceMapGenerator> sourceMapGenerator;
GDA.forceTypedArrays = ForceTypedArrays;
if (!SourceMap.empty())
{
std::error_code ErrorCode;
sourceMapGenerator.reset(new cheerp::SourceMapGenerator(SourceMap, SourceMapPrefix, SourceMapStandAlone, ErrorCode));
if (ErrorCode)
{
// An error occurred opening the source map file, bail out
llvm::report_fatal_error(StringRef(ErrorCode.message()), false);
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;
std::error_code ErrorCode;
llvm::ToolOutputFile secondaryFile(SecondaryOutputFile, ErrorCode, sys::fs::OF_None);
std::unique_ptr<llvm::formatted_raw_ostream> secondaryOut;
if (!SecondaryOutputFile.empty())
{
secondaryOut.reset(new formatted_raw_ostream(secondaryFile.os()));
}
else if (WasmOnly && LinearOutput != AsmJs)
{
secondaryOut.reset(new formatted_raw_ostream(Out));
}

std::error_code dtsErrorCode;
llvm::ToolOutputFile dtsFile(DTSOutputFile, dtsErrorCode, sys::fs::OF_None);
std::unique_ptr<llvm::formatted_raw_ostream> dtsOut;
if (!DTSOutputFile.empty())
{
dtsOut.reset(new formatted_raw_ostream(dtsFile.os()));
}

// Build the ordered list of reserved names
std::vector<std::string> reservedNames(ReservedNames.begin(), ReservedNames.end());
std::sort(reservedNames.begin(), reservedNames.end());

cheerp::NameGenerator namegen(M, GDA, registerize, PA, linearHelper, reservedNames, PrettyCode, WasmExportedMemory);

std::string wasmFile;
std::string asmjsMemFile;
llvm::formatted_raw_ostream* memOut = nullptr;
switch (LinearOutput)
{
case Wasm:
if (!SecondaryOutputPath.empty())
wasmFile = SecondaryOutputPath.getValue();
else if (!SecondaryOutputFile.empty())
wasmFile = std::string(llvm::sys::path::filename(SecondaryOutputFile.getValue()));
break;
case AsmJs:
if (!SecondaryOutputPath.empty())
asmjsMemFile = SecondaryOutputPath.getValue();
else if (!SecondaryOutputFile.empty())
asmjsMemFile = std::string(llvm::sys::path::filename(SecondaryOutputFile.getValue()));
memOut = secondaryOut.get();
break;
}

MODULE_TYPE makeModule = getModuleType(MakeModule);

if (MakeDTS && dtsOut)
{
cheerp::CheerpDTSWriter dtsWriter(M, *dtsOut, sourceMapGenerator.get(), PrettyCode, makeModule);
dtsWriter.makeDTS();
}

if (!WasmOnly)
{
cheerp::CheerpWriter writer(M, MAM, Out, PA, registerize, GDA, linearHelper, namegen, allocaStoresExtractor, IW.getLandingPadTable(), memOut, asmjsMemFile,
sourceMapGenerator.get(), PrettyCode, makeModule, !NoNativeJavaScriptMath,
!NoJavaScriptMathImul, !NoJavaScriptMathFround, !NoCredits, MeasureTimeToMain, CheerpHeapSize,
BoundsCheck, SymbolicGlobalsAsmJS, wasmFile, ForceTypedArrays);
writer.makeJS();
}

if (LinearOutput != AsmJs && secondaryOut)
{
cheerp::CheerpWasmWriter wasmWriter(M, MAM, *secondaryOut, PA, registerize, GDA, linearHelper, IW.getLandingPadTable(), namegen,
M.getContext(), CheerpHeapSize, !WasmOnly,
PrettyCode, WasmSharedMemory,
WasmExportedTable);
wasmWriter.makeWasm();
}

allocaStoresExtractor.destroyStores();

if (!SecondaryOutputFile.empty() && ErrorCode)
{
// An error occurred opening the asm.js memory file, bail out
llvm::report_fatal_error(StringRef(ErrorCode.message()), false);
return PreservedAnalyses::none();
}
if (!DTSOutputFile.empty() && dtsErrorCode)
{
llvm::report_fatal_error(StringRef(dtsErrorCode.message()), false);
return PreservedAnalyses::none();
}
if (!WasmOnly)
secondaryFile.keep();
if (MakeDTS)
dtsFile.keep();


return PreservedAnalyses::none();
}

6 changes: 4 additions & 2 deletions llvm/lib/Target/CheerpBackend/CheerpBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
//
//===----------------------------------------------------------------------===//

#include "CheerpWritePass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Function.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Cheerp/CheerpWritePass.h"
#include "CheerpTargetMachine.h"
#include "CheerpTargetTransformInfo.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/MC/TargetRegistry.h"

using namespace llvm;

extern "C" void LLVMInitializeCheerpBackendTarget() {
Expand Down

0 comments on commit fdf6a7d

Please sign in to comment.