Skip to content

Commit

Permalink
[sil-func-extractor] Add support for reading from a file a list of fu…
Browse files Browse the repository at this point in the history
…nctions to preserve.

This will allow for greater scriptability.
  • Loading branch information
gottesmm committed Dec 9, 2016
1 parent 1182426 commit 6a5f54e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
2 changes: 2 additions & 0 deletions test/sil-func-extractor/functions_to_preserve
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
f6
f8
19 changes: 14 additions & 5 deletions test/sil-func-extractor/multiple-functions.sil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-sil-func-extractor -assume-parsing-unqualified-ownership-sil %s -func=f2 -func=f5 -func=f6 | %FileCheck %s
// RUN: %target-sil-func-extractor -assume-parsing-unqualified-ownership-sil %s -invert -func=f2 -func=f5 -func=f6 | %FileCheck -check-prefix=INVERSE %s
// RUN: %target-sil-func-extractor -assume-parsing-unqualified-ownership-sil %s -func=f2 -func=f5 -func=f6 -func=f8 | %FileCheck %s
// RUN: %target-sil-func-extractor -assume-parsing-unqualified-ownership-sil %s -invert -func=f2 -func=f5 -func=f6 -func=f8 | %FileCheck -check-prefix=INVERSE %s
// RUN: %target-sil-func-extractor -assume-parsing-unqualified-ownership-sil %s -func=f2 -func=f5 -func-file=%S/functions_to_preserve | %FileCheck %s

import Builtin

Expand Down Expand Up @@ -59,10 +60,18 @@ bb0:
return %0 : $()
}

// CHECK-NOT: sil @f8 : $@convention(thin) () -> () {
// INVERSE: sil @f8 : $@convention(thin) () -> () {
// CHECK: sil @f8 : $@convention(thin) () -> () {
// INVERSE-NOT: sil @f8 : $@convention(thin) () -> () {
sil @f8 : $@convention(thin) () -> () {
bb0:
%0 = tuple()
return %0 : $()
}
}

// CHECK-NOT: sil @f9 : $@convention(thin) () -> () {
// INVERSE: sil @f9 : $@convention(thin) () -> () {
sil @f9 : $@convention(thin) () -> () {
bb0:
%0 = tuple()
return %0 : $()
}
102 changes: 63 additions & 39 deletions tools/sil-func-extractor/SILFunctionExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ static llvm::cl::opt<bool>
llvm::cl::desc("Emit locations during sil emission."));

static llvm::cl::list<std::string>
FunctionNames("func", llvm::cl::desc("Function names to extract."));
CommandLineFunctionNames("func",
llvm::cl::desc("Function names to extract."));
static llvm::cl::opt<std::string> FunctionNameFile(
"func-file", llvm::cl::desc("File to load additional function names from"));

static llvm::cl::opt<bool>
EmitSIB("emit-sib",
Expand Down Expand Up @@ -119,6 +122,31 @@ static llvm::cl::opt<bool> AssumeUnqualifiedOwnershipWhenParsing(
// without being given the address of a function in the main executable).
void anchorForGetMainExecutable() {}

static void getFunctionNames(std::vector<std::string> &Names) {
std::copy(CommandLineFunctionNames.begin(), CommandLineFunctionNames.end(),
std::back_inserter(Names));

if (!FunctionNameFile.empty()) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(FunctionNameFile);
if (!FileBufOrErr) {
fprintf(stderr, "Error! Failed to open file: %s\n",
InputFilename.c_str());
exit(-1);
}
StringRef Buffer = FileBufOrErr.get()->getBuffer();
while (!Buffer.empty()) {
StringRef Token, NewBuffer;
std::tie(Token, NewBuffer) = llvm::getToken(Buffer, "\n");
if (Token.empty()) {
break;
}
Names.push_back(Token);
Buffer = NewBuffer;
}
}
}

static bool stringInSortedArray(
StringRef str, ArrayRef<std::string> list,
llvm::function_ref<bool(const std::string &, const std::string &)> &&cmp) {
Expand Down Expand Up @@ -282,14 +310,13 @@ int main(int argc, char **argv) {
SL->getAll();
}

if (FunctionNames.empty())
if (CommandLineFunctionNames.empty() && FunctionNameFile.empty())
return CI.getASTContext().hadError();

// For efficient usage, we separate our names into two separate sorted
// lists, one of managled names, and one of unmangled names.
std::vector<std::string> Names;
std::copy(FunctionNames.begin(), FunctionNames.end(),
std::back_inserter(Names));
getFunctionNames(Names);

// First partition our function names into mangled/demangled arrays.
auto FirstDemangledName = std::partition(
Expand Down Expand Up @@ -324,45 +351,42 @@ int main(int argc, char **argv) {
}));

removeUnwantedFunctions(CI.getSILModule(), MangledNames, DemangledNames);
}

if (EmitSIB) {
llvm::SmallString<128> OutputFile;
if (OutputFilename.size()) {
OutputFile = OutputFilename;
} else if (ModuleName.size()) {
OutputFile = ModuleName;
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
} else {
OutputFile = CI.getMainModule()->getName().str();
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
}

SerializationOptions serializationOpts;
serializationOpts.OutputPath = OutputFile.c_str();
serializationOpts.SerializeAllSIL = true;
serializationOpts.IsSIB = true;
if (EmitSIB) {
llvm::SmallString<128> OutputFile;
if (OutputFilename.size()) {
OutputFile = OutputFilename;
} else if (ModuleName.size()) {
OutputFile = ModuleName;
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
} else {
OutputFile = CI.getMainModule()->getName().str();
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
}

serialize(CI.getMainModule(), serializationOpts, CI.getSILModule());
} else {
const StringRef OutputFile = OutputFilename.size() ?
StringRef(OutputFilename) : "-";
SerializationOptions serializationOpts;
serializationOpts.OutputPath = OutputFile.c_str();
serializationOpts.SerializeAllSIL = true;
serializationOpts.IsSIB = true;

if (OutputFile == "-") {
CI.getSILModule()->print(llvm::outs(), EmitVerboseSIL, CI.getMainModule(),
EnableSILSortOutput, !DisableASTDump);
serialize(CI.getMainModule(), serializationOpts, CI.getSILModule());
} else {
std::error_code EC;
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << "while opening '" << OutputFile << "': "
<< EC.message() << '\n';
return 1;
const StringRef OutputFile =
OutputFilename.size() ? StringRef(OutputFilename) : "-";

if (OutputFile == "-") {
CI.getSILModule()->print(llvm::outs(), EmitVerboseSIL, CI.getMainModule(),
EnableSILSortOutput, !DisableASTDump);
} else {
std::error_code EC;
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << "while opening '" << OutputFile << "': " << EC.message()
<< '\n';
return 1;
}
CI.getSILModule()->print(OS, EmitVerboseSIL, CI.getMainModule(),
EnableSILSortOutput, !DisableASTDump);
}
CI.getSILModule()->print(OS, EmitVerboseSIL, CI.getMainModule(),
EnableSILSortOutput, !DisableASTDump);
}
}


}

0 comments on commit 6a5f54e

Please sign in to comment.