|
| 1 | +// SPDX-License-Identifier: MIT License |
| 2 | +// Copyright (c) 2024 Yingwei Zheng |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// See the LICENSE file for more information. |
| 5 | + |
| 6 | +#include "llvm/IR/Argument.h" |
| 7 | +#include "llvm/IR/IntrinsicInst.h" |
| 8 | +#include <llvm/ADT/DenseMap.h> |
| 9 | +#include <llvm/Analysis/TargetLibraryInfo.h> |
| 10 | +#include <llvm/IR/BasicBlock.h> |
| 11 | +#include <llvm/IR/Constants.h> |
| 12 | +#include <llvm/IR/DataLayout.h> |
| 13 | +#include <llvm/IR/DerivedTypes.h> |
| 14 | +#include <llvm/IR/Function.h> |
| 15 | +#include <llvm/IR/GlobalVariable.h> |
| 16 | +#include <llvm/IR/IRBuilder.h> |
| 17 | +#include <llvm/IR/InstVisitor.h> |
| 18 | +#include <llvm/IR/Instruction.h> |
| 19 | +#include <llvm/IR/Instructions.h> |
| 20 | +#include <llvm/IR/Intrinsics.h> |
| 21 | +#include <llvm/IR/LLVMContext.h> |
| 22 | +#include <llvm/IR/Module.h> |
| 23 | +#include <llvm/IR/Operator.h> |
| 24 | +#include <llvm/IR/Type.h> |
| 25 | +#include <llvm/IR/Value.h> |
| 26 | +#include <llvm/IR/Verifier.h> |
| 27 | +#include <llvm/IRPrinter/IRPrintingPasses.h> |
| 28 | +#include <llvm/IRReader/IRReader.h> |
| 29 | +#include <llvm/Support/CommandLine.h> |
| 30 | +#include <llvm/Support/Error.h> |
| 31 | +#include <llvm/Support/ErrorHandling.h> |
| 32 | +#include <llvm/Support/FileSystem.h> |
| 33 | +#include <llvm/Support/FormattedStream.h> |
| 34 | +#include <llvm/Support/InitLLVM.h> |
| 35 | +#include <llvm/Support/MemoryBuffer.h> |
| 36 | +#include <llvm/Support/SourceMgr.h> |
| 37 | +#include <llvm/Support/ToolOutputFile.h> |
| 38 | +#include <llvm/Support/raw_ostream.h> |
| 39 | +#include <llvm/TargetParser/Triple.h> |
| 40 | +#include <cstdint> |
| 41 | +#include <cstdlib> |
| 42 | +#include <filesystem> |
| 43 | +#include <map> |
| 44 | +#include <memory> |
| 45 | +#include <unordered_map> |
| 46 | + |
| 47 | +using namespace llvm; |
| 48 | +namespace fs = std::filesystem; |
| 49 | + |
| 50 | +static cl::opt<std::string> |
| 51 | + InputDir(cl::Positional, cl::desc("<directory for input LLVM IR files>"), |
| 52 | + cl::Required, cl::value_desc("inputdir")); |
| 53 | + |
| 54 | +int main(int argc, char **argv) { |
| 55 | + InitLLVM Init{argc, argv}; |
| 56 | + cl::ParseCommandLineOptions(argc, argv, "scanner\n"); |
| 57 | + |
| 58 | + std::vector<std::string> BlockList{ |
| 59 | + "ruby/optimized/vm.ll", |
| 60 | + "/regexec.ll", |
| 61 | + "quickjs/optimized/quickjs.ll", |
| 62 | + }; |
| 63 | + |
| 64 | + std::vector<fs::path> InputFiles; |
| 65 | + for (auto &Entry : fs::recursive_directory_iterator(std::string(InputDir))) { |
| 66 | + if (Entry.is_regular_file()) { |
| 67 | + auto &Path = Entry.path(); |
| 68 | + if (Path.extension() == ".ll" && |
| 69 | + Path.string().find("/optimized/") != std::string::npos) { |
| 70 | + auto Str = Path.string(); |
| 71 | + bool Blocked = false; |
| 72 | + for (auto &Pattern : BlockList) |
| 73 | + if (Str.find(Pattern) != std::string::npos) { |
| 74 | + Blocked = true; |
| 75 | + break; |
| 76 | + } |
| 77 | + if (!Blocked) |
| 78 | + InputFiles.push_back(Path); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + errs() << "Input files: " << InputFiles.size() << '\n'; |
| 83 | + LLVMContext Context; |
| 84 | + uint32_t Count = 0; |
| 85 | + |
| 86 | + for (auto &Path : InputFiles) { |
| 87 | + SMDiagnostic Err; |
| 88 | + auto M = parseIRFile(Path.string(), Err, Context); |
| 89 | + if (!M) |
| 90 | + continue; |
| 91 | + |
| 92 | + for (auto &F : *M) { |
| 93 | + if (F.empty()) |
| 94 | + continue; |
| 95 | + |
| 96 | + for (auto &BB : F) { |
| 97 | + for (auto &I : BB) { |
| 98 | + if (auto *LI = dyn_cast<LifetimeIntrinsic>(&I)) { |
| 99 | + auto *Arg = LI->getArgOperand(1); |
| 100 | + if (!isa<AllocaInst>(Arg) && !isa<Argument>(Arg)) { |
| 101 | + errs() << *Arg << '\n'; |
| 102 | + std::abort(); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + errs() << "\rProgress: " << ++Count; |
| 110 | + } |
| 111 | + errs() << '\n'; |
| 112 | + |
| 113 | + return EXIT_SUCCESS; |
| 114 | +} |
0 commit comments