Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

basic_functions fixes #24

Merged
merged 1 commit into from
Oct 16, 2019
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
2 changes: 2 additions & 0 deletions include/bcdb/BCDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class BCDB {

llvm::Expected<std::unique_ptr<llvm::Module>>
LoadParts(llvm::StringRef Name, std::map<std::string, std::string> &PartIDs);

llvm::Expected<std::vector<std::string>> FetchBasicFunctions(std::string dest_path);
};

} // end namespace bcdb
Expand Down
31 changes: 31 additions & 0 deletions lib/BCDB/BCDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "bcdb/AlignBitcode.h"
#include "bcdb/Split.h"

#include "llvm/Support/TarWriter.h"

#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/IR/Constants.h>
Expand Down Expand Up @@ -259,6 +261,35 @@ Expected<std::unique_ptr<Module>> BCDB::GetFunctionById(StringRef Id) {
return LoadModuleFromValue(db.get(), value.get(), Id, Context);
}


Expected<std::vector<std::string>> BCDB::FetchBasicFunctions(std::string dest_path) {
Expected<std::unique_ptr<TarWriter>> tar_writer = TarWriter::create(dest_path, "basic_functions");
if(!tar_writer) return tar_writer.takeError();

std::vector<std::string> basic_functions;
Expected<std::vector<std::string>> all_functions = ListAllFunctions();
if(!all_functions) return all_functions.takeError();
for (auto &func_id : *all_functions){
auto M = GetFunctionById(func_id);
if (!M) return M.takeError();
for (Function &F : **M) {
//skip declarations
if (!F.isDeclaration()){
if(F.size() == 1){
SmallVector<char, 0> Buffer;
WriteUnalignedModule(**M, Buffer);
basic_functions.push_back(func_id);
//append file to archive
std::string basic_func(Buffer.begin(), Buffer.end());
(*tar_writer)->append(func_id + ".bc", basic_func);
}
}
}
}
return basic_functions;
}


namespace {
class BCDBSplitLoader : public SplitLoader {
LLVMContext &Context;
Expand Down
22 changes: 22 additions & 0 deletions tools/bcdb/bcdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ static int Mux() {
return WriteModule(*M);
}

//bcdb FetchBasicFunctions

static cl::SubCommand ExtractBasicCommand("extract-basic-functions", "Extract basic functions to destination");

static cl::opt<std::string> DestPath("dest_path", cl::desc("full destination path"),
cl::sub(ExtractBasicCommand), cl::Required);

static int ExtractBasicFunctions(){
ExitOnError Err("bcdb extractbasicfunctions: ");
std::unique_ptr<BCDB> db = Err(BCDB::Open(Uri));
std::string dest_path = DestPath;
std::vector<std::string> basicfuncs = Err(db->FetchBasicFunctions(dest_path));
outs() <<"Extracting "<< basicfuncs.size() <<" functions to "<< dest_path <<"\n";
for (auto &basicfunc : basicfuncs) {
outs() << basicfunc << "\n";
}
return 0;
}


// main

int main(int argc, char **argv) {
Expand Down Expand Up @@ -275,6 +295,8 @@ int main(int argc, char **argv) {
return Merge();
} else if (MuxCommand) {
return Mux();
} else if (ExtractBasicCommand){
return ExtractBasicFunctions();
} else {
cl::PrintHelpMessage(false, true);
return 0;
Expand Down