-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCheerpWritePass.cpp
More file actions
283 lines (245 loc) · 11.4 KB
/
CheerpWritePass.cpp
File metadata and controls
283 lines (245 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//===-- 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/Support/ToolOutputFile.h"
#include "llvm/Cheerp/BitCastLowering.h"
#include "llvm/Cheerp/DTSWriter.h"
#include "llvm/Cheerp/I64Lowering.h"
#include "llvm/Cheerp/JSStringLiteralLowering.h"
#include "llvm/Cheerp/FinalizeMemoryInfo.h"
#include "llvm/Cheerp/PassRegistry.h"
#include "llvm/Cheerp/PassUtility.h"
#include "llvm/Cheerp/SIMDLowering.h"
#include "llvm/Cheerp/SIMDTransform.h"
#include "llvm/Cheerp/WasmWriter.h"
#include "llvm/Cheerp/Writer.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/PassManager.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Transforms/Scalar/EarlyCSE.h"
#include "CheerpWritePass.h"
using namespace llvm;
cl::opt<bool> VerbosePassManager("cheerp-verbose-pm", cl::init(false), cl::Hidden,
cl::desc("Emit verbose informations"));
PreservedAnalyses cheerp::CheerpWritePassImpl::run(Module& M, ModuleAnalysisManager& MAM)
{
cheerp::Registerize ®isterize = 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();
}
}
Triple TargetTriple(M.getTargetTriple());
bool WasmOnly = TargetTriple.isCheerpWasmStandalone();
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();
}
bool CheerpWritePass::runOnModule(Module& M)
{
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;
PassInstrumentationCallbacks PIC;
PrintPassOptions PrintPassOpts;
PrintPassOpts.Indent = VerbosePassManager;
PrintPassOpts.SkipAnalyses = false;
StandardInstrumentations SI(M.getContext(), VerbosePassManager,
/*VerifyEach*/ false, PrintPassOpts);
SI.registerCallbacks(PIC, &FAM);
llvm::PipelineTuningOptions PTO;
Optional<PGOOptions> PGOOpt;
PassBuilder PB(TM, PTO, PGOOpt, &PIC);
#define HANDLE_EXTENSION(Ext) \
get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
#include "llvm/Support/Extension.def"
Triple TargetTriple(M.getTargetTriple());
std::unique_ptr<TargetLibraryInfoImpl> TLII(
new TargetLibraryInfoImpl(TargetTriple));
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
// Register all the basic analyses with the managers.
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
ModulePassManager MPM;
bool isWasmTarget = Triple(M.getTargetTriple()).isCheerpWasm();
cheerp::GlobalDepsAnalyzer::MATH_MODE mathMode;
if (NoNativeJavaScriptMath)
mathMode = cheerp::GlobalDepsAnalyzer::NO_BUILTINS;
else if(isWasmTarget && LinearOutput != AsmJs)
mathMode = cheerp::GlobalDepsAnalyzer::WASM_BUILTINS;
else
mathMode = cheerp::GlobalDepsAnalyzer::JS_BUILTINS;
auto functionAddressMode = LinearOutput == LinearOutputTy::AsmJs
? cheerp::LinearMemoryHelperInitializer::FunctionAddressMode::AsmJS
: cheerp::LinearMemoryHelperInitializer::FunctionAddressMode::Wasm;
bool growMem = !WasmNoGrowMemory &&
functionAddressMode == cheerp::LinearMemoryHelperInitializer::FunctionAddressMode::Wasm &&
// NOTE: this is not actually required by the spec, but for now chrome
// doesn't like growing shared memory
(!WasmSharedMemory || WasmSharedModule);
bool hasAsmjsMem = functionAddressMode == cheerp::LinearMemoryHelperInitializer::FunctionAddressMode::AsmJS &&
(!SecondaryOutputFile.empty() || !SecondaryOutputPath.empty());
if (FixWrongFuncCasts)
MPM.addPass(cheerp::FixFunctionCastsPass());
{
//Wrap these in a FunctionPassManager
FunctionPassManager FPM;
FPM.addPass(cheerp::I64LoweringPass());
// Run a simple constant elimination pass to clean up suboptimal code left
// by I64Lowering.
FPM.addPass(EarlyCSEPass());
FPM.addPass(cheerp::JSStringLiteralLoweringPass());
FPM.addPass(cheerp::BitCastLoweringPass());
FPM.addPass(cheerp::SIMDTransformPass());
FPM.addPass(cheerp::SIMDLoweringPass());
FPM.addPass(cheerp::CheerpLowerSwitchPass(/*onlyLowerI64*/false));
FPM.addPass(cheerp::LowerAndOrBranchesPass());
FPM.addPass(cheerp::StructMemFuncLoweringPass());
FPM.addPass(cheerp::FreezeAndAssumeRemovalPass());
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
MPM.addPass(cheerp::GlobalDepsAnalyzerPass(mathMode, /*resolveAliases*/true));
MPM.addPass(cheerp::InvokeWrappingPass());
if (isWasmTarget)
MPM.addPass(cheerp::AllocaLoweringPass());
MPM.addPass(cheerp::ThreadLocalLoweringPass());
MPM.addPass(cheerp::FFIWrappingPass());
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::FixIrreducibleControlFlowPass()));
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::PointerArithmeticToArrayIndexingPass()));
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::PointerToImmutablePHIRemovalPass()));
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::GEPOptimizerPass()));
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::StoreMergingPass(LinearOutput == Wasm && !WasmNoUnalignedMem)));
// Remove obviously dead instruction, this avoids problems caused by inlining of effectfull instructions
// inside not used instructions which are then not rendered.
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})));
MPM.addPass(cheerp::ConstantExprLoweringPass());
MPM.addPass(cheerp::PointerAnalyzerPass());
MPM.addPass(cheerp::DelayInstsPass());
MPM.addPass(cheerp::AllocaMergingPass());
MPM.addPass(cheerp::AllocaArraysPass());
MPM.addPass(cheerp::AllocaArraysMergingPass());
MPM.addPass(createModuleToFunctionPassAdaptor(cheerp::RemoveFwdBlocksPass()));
// 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.
{
PrettyStackTraceString CrashInfo("Optimizer");
llvm::TimeTraceScope TimeScope("Optimizer");
MPM.run(M, MAM);
}
return false;
}
char CheerpWritePass::ID = 0;