-
Notifications
You must be signed in to change notification settings - Fork 5
/
preprocess.cpp
61 lines (57 loc) · 2.04 KB
/
preprocess.cpp
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
// SPDX-License-Identifier: MIT License
// Copyright (c) 2024 Yingwei Zheng
// This file is licensed under the MIT License.
// See the LICENSE file for more information.
#include <llvm/IR/Analysis.h>
#include <llvm/IR/DebugInfo.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/PassManager.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Transforms/IPO/GlobalDCE.h>
#include <llvm/Transforms/Scalar/DCE.h>
#include <filesystem>
using namespace llvm;
namespace fs = std::filesystem;
class DumpPass : public PassInfoMixin<DumpPass> {
public:
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
const char *Prefix = getenv("DUMP_PREFIX");
if (!Prefix)
return PreservedAnalyses::none();
std::string FileName = M.getSourceFileName();
M.setModuleIdentifier("");
M.setSourceFileName("");
StripDebugInfo(M);
if (M.empty())
return PreservedAnalyses::none();
std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(new ToolOutputFile(
(Prefix / fs::path(FileName).filename().replace_extension(".ll"))
.string(),
EC, sys::fs::OF_Text));
if (EC)
return PreservedAnalyses::none();
M.print(Out->os(), /*AAW=*/nullptr);
Out->keep();
return PreservedAnalyses::none();
}
};
extern "C" LLVM_ATTRIBUTE_WEAK PassPluginLibraryInfo llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "Preprocessor", LLVM_VERSION_STRING,
[](PassBuilder &PB) {
PB.registerPipelineStartEPCallback(
[](ModulePassManager &PM, OptimizationLevel Level) {
FunctionPassManager FPM;
FPM.addPass(DCEPass());
PM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
PM.addPass(GlobalDCEPass());
PM.addPass(DumpPass());
});
}};
}