Skip to content

Commit b6132d1

Browse files
committed
Replace the sil-func-extractor binary with a symlink to swift-frontend
rdar://76551283
1 parent 2e9c241 commit b6132d1

File tree

9 files changed

+115
-110
lines changed

9 files changed

+115
-110
lines changed

include/swift/Driver/Driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class Driver {
167167
Interactive, // swift
168168
Batch, // swiftc
169169
SILOpt, // sil-opt
170+
SILFuncExtractor,// sil-func-extractor
170171
AutolinkExtract, // swift-autolink-extract
171172
SwiftIndent, // swift-indent
172173
SymbolGraph, // swift-symbolgraph

lib/Driver/Driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void Driver::parseDriverKind(ArrayRef<const char *> Args) {
100100
.Case("swift", DriverKind::Interactive)
101101
.Case("swiftc", DriverKind::Batch)
102102
.Case("sil-opt", DriverKind::SILOpt)
103+
.Case("sil-func-extractor", DriverKind::SILFuncExtractor)
103104
.Case("swift-autolink-extract", DriverKind::AutolinkExtract)
104105
.Case("swift-indent", DriverKind::SwiftIndent)
105106
.Case("swift-symbolgraph-extract", DriverKind::SymbolGraph)
@@ -3557,6 +3558,7 @@ void Driver::printHelp(bool ShowHidden) const {
35573558
break;
35583559
case DriverKind::Batch:
35593560
case DriverKind::SILOpt:
3561+
case DriverKind::SILFuncExtractor:
35603562
case DriverKind::AutolinkExtract:
35613563
case DriverKind::SwiftIndent:
35623564
case DriverKind::SymbolGraph:

lib/DriverTool/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(driver_sources_and_options
22
driver.cpp
33
sil_opt_main.cpp
4+
sil_func_extractor_main.cpp
45
autolink_extract_main.cpp
56
modulewrap_main.cpp
67
swift_api_digester_main.cpp

lib/DriverTool/driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ std::string getExecutablePath(const char *FirstArg) {
6666
/// Run 'sil-opt'
6767
extern int sil_opt_main(ArrayRef<const char *> argv, void *MainAddr);
6868

69+
/// Run 'sil-func-extractor'
70+
extern int sil_func_extractor_main(ArrayRef<const char *> argv, void *MainAddr);
71+
6972
/// Run 'swift-autolink-extract'.
7073
extern int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
7174
void *MainAddr);
@@ -286,6 +289,8 @@ static int run_driver(StringRef ExecName,
286289
switch (TheDriver.getDriverKind()) {
287290
case Driver::DriverKind::SILOpt:
288291
return sil_opt_main(argv, (void *)(intptr_t)getExecutablePath);
292+
case Driver::DriverKind::SILFuncExtractor:
293+
return sil_func_extractor_main(argv, (void *)(intptr_t)getExecutablePath);
289294
case Driver::DriverKind::AutolinkExtract:
290295
return autolink_extract_main(
291296
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv),

tools/sil-func-extractor/SILFunctionExtractor.cpp renamed to lib/DriverTool/sil_func_extractor_main.cpp

Lines changed: 101 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- SILFunctionExtractor.cpp - SIL function extraction utility -------===//
1+
//===--- sil_func_extractor_main.cpp --------------------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -49,96 +49,99 @@
4949

5050
using namespace swift;
5151

52-
static llvm::cl::opt<std::string> InputFilename(llvm::cl::desc("input file"),
52+
struct SILFuncExtractorOptions {
53+
llvm::cl::opt<std::string>
54+
InputFilename = llvm::cl::opt<std::string>(llvm::cl::desc("input file"),
5355
llvm::cl::init("-"),
5456
llvm::cl::Positional);
5557

56-
static llvm::cl::opt<std::string>
57-
OutputFilename("o", llvm::cl::desc("output filename"), llvm::cl::init("-"));
58+
llvm::cl::opt<std::string>
59+
OutputFilename = llvm::cl::opt<std::string>("o", llvm::cl::desc("output filename"), llvm::cl::init("-"));
5860

59-
static llvm::cl::opt<bool>
60-
EmitVerboseSIL("emit-verbose-sil",
61+
llvm::cl::opt<bool>
62+
EmitVerboseSIL = llvm::cl::opt<bool>("emit-verbose-sil",
6163
llvm::cl::desc("Emit locations during sil emission."));
6264

63-
static llvm::cl::list<std::string>
64-
CommandLineFunctionNames("func",
65+
llvm::cl::list<std::string>
66+
CommandLineFunctionNames = llvm::cl::list<std::string>("func",
6567
llvm::cl::desc("Function names to extract."));
66-
static llvm::cl::opt<std::string> FunctionNameFile(
68+
llvm::cl::opt<std::string>
69+
FunctionNameFile = llvm::cl::opt<std::string>(
6770
"func-file", llvm::cl::desc("File to load additional function names from"));
6871

69-
static llvm::cl::opt<bool>
70-
EmitSIB("emit-sib",
72+
llvm::cl::opt<bool>
73+
EmitSIB = llvm::cl::opt<bool>("emit-sib",
7174
llvm::cl::desc("Emit a sib file as output instead of a sil file"));
7275

73-
static llvm::cl::opt<bool> InvertMatch(
74-
"invert",
75-
llvm::cl::desc("Match functions whose name do not "
76-
"match the names of the functions to be extracted"));
76+
llvm::cl::opt<bool>
77+
InvertMatch = llvm::cl::opt<bool>(
78+
"invert",
79+
llvm::cl::desc("Match functions whose name do not "
80+
"match the names of the functions to be extracted"));
7781

78-
static llvm::cl::list<std::string>
79-
ImportPaths("I",
82+
llvm::cl::list<std::string>
83+
ImportPaths = llvm::cl::list<std::string>("I",
8084
llvm::cl::desc("add a directory to the import search path"));
8185

82-
static llvm::cl::opt<std::string>
83-
ModuleName("module-name",
86+
llvm::cl::opt<std::string>
87+
ModuleName = llvm::cl::opt<std::string>("module-name",
8488
llvm::cl::desc("The name of the module if processing"
8589
" a module. Necessary for processing "
8690
"stdin."));
8791

88-
static llvm::cl::opt<std::string>
89-
ModuleCachePath("module-cache-path",
92+
llvm::cl::opt<std::string>
93+
ModuleCachePath = llvm::cl::opt<std::string>("module-cache-path",
9094
llvm::cl::desc("Clang module cache path"));
9195

92-
static llvm::cl::opt<std::string> ResourceDir(
93-
"resource-dir",
94-
llvm::cl::desc("The directory that holds the compiler resource files"));
96+
llvm::cl::opt<std::string>
97+
ResourceDir = llvm::cl::opt<std::string>(
98+
"resource-dir",
99+
llvm::cl::desc("The directory that holds the compiler resource files"));
95100

96-
static llvm::cl::opt<std::string>
97-
SDKPath("sdk", llvm::cl::desc("The path to the SDK for use with the clang "
101+
llvm::cl::opt<std::string>
102+
SDKPath = llvm::cl::opt<std::string>("sdk", llvm::cl::desc("The path to the SDK for use with the clang "
98103
"importer."),
99-
llvm::cl::init(""));
104+
llvm::cl::init(""));
100105

101-
static llvm::cl::opt<std::string> Triple("target",
102-
llvm::cl::desc("target triple"));
106+
llvm::cl::opt<std::string>
107+
Triple = llvm::cl::opt<std::string>("target",
108+
llvm::cl::desc("target triple"));
103109

104-
static llvm::cl::opt<bool>
105-
EmitSortedSIL("emit-sorted-sil", llvm::cl::Hidden, llvm::cl::init(false),
110+
llvm::cl::opt<bool>
111+
EmitSortedSIL = llvm::cl::opt<bool>("emit-sorted-sil", llvm::cl::Hidden, llvm::cl::init(false),
106112
llvm::cl::desc("Sort Functions, VTables, Globals, "
107113
"WitnessTables by name to ease diffing."));
108114

109-
static llvm::cl::opt<bool>
110-
DisableASTDump("sil-disable-ast-dump", llvm::cl::Hidden,
111-
llvm::cl::init(false),
112-
llvm::cl::desc("Do not dump AST."));
113-
114-
static llvm::cl::opt<bool> EnableOSSAModules(
115-
"enable-ossa-modules",
116-
llvm::cl::desc("Do we always serialize SIL in OSSA form? If "
117-
"this is disabled we do not serialize in OSSA "
118-
"form when optimizing."));
119-
120-
static llvm::cl::opt<llvm::cl::boolOrDefault> EnableObjCInterop(
121-
"enable-objc-interop",
122-
llvm::cl::desc("Whether the Objective-C interop should be enabled. "
123-
"The value is `true` by default on Darwin platforms."));
124-
125-
// This function isn't referenced outside its translation unit, but it
126-
// can't use the "static" keyword because its address is used for
127-
// getMainExecutable (since some platforms don't support taking the
128-
// address of main, and some platforms can't implement getMainExecutable
129-
// without being given the address of a function in the main executable).
130-
void anchorForGetMainExecutable() {}
131-
132-
static void getFunctionNames(std::vector<std::string> &Names) {
133-
std::copy(CommandLineFunctionNames.begin(), CommandLineFunctionNames.end(),
115+
llvm::cl::opt<bool>
116+
DisableASTDump = llvm::cl::opt<bool>("sil-disable-ast-dump", llvm::cl::Hidden,
117+
llvm::cl::init(false),
118+
llvm::cl::desc("Do not dump AST."));
119+
120+
llvm::cl::opt<bool>
121+
EnableOSSAModules = llvm::cl::opt<bool>(
122+
"enable-ossa-modules",
123+
llvm::cl::desc("Do we always serialize SIL in OSSA form? If "
124+
"this is disabled we do not serialize in OSSA "
125+
"form when optimizing."));
126+
127+
llvm::cl::opt<llvm::cl::boolOrDefault>
128+
EnableObjCInterop = llvm::cl::opt<llvm::cl::boolOrDefault>(
129+
"enable-objc-interop",
130+
llvm::cl::desc("Whether the Objective-C interop should be enabled. "
131+
"The value is `true` by default on Darwin platforms."));
132+
};
133+
134+
static void getFunctionNames(std::vector<std::string> &Names,
135+
const SILFuncExtractorOptions &options) {
136+
std::copy(options.CommandLineFunctionNames.begin(), options.CommandLineFunctionNames.end(),
134137
std::back_inserter(Names));
135138

136-
if (!FunctionNameFile.empty()) {
139+
if (!options.FunctionNameFile.empty()) {
137140
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
138-
llvm::MemoryBuffer::getFileOrSTDIN(FunctionNameFile);
141+
llvm::MemoryBuffer::getFileOrSTDIN(options.FunctionNameFile);
139142
if (!FileBufOrErr) {
140143
fprintf(stderr, "Error! Failed to open file: %s\n",
141-
InputFilename.c_str());
144+
options.InputFilename.c_str());
142145
exit(-1);
143146
}
144147
StringRef Buffer = FileBufOrErr.get()->getBuffer();
@@ -168,7 +171,8 @@ static bool stringInSortedArray(
168171
}
169172

170173
void removeUnwantedFunctions(SILModule *M, ArrayRef<std::string> MangledNames,
171-
ArrayRef<std::string> DemangledNames) {
174+
ArrayRef<std::string> DemangledNames,
175+
const SILFuncExtractorOptions &options) {
172176
assert((!MangledNames.empty() || !DemangledNames.empty()) &&
173177
"Expected names of function we want to retain!");
174178
assert(M && "Expected a SIL module to extract from.");
@@ -191,7 +195,7 @@ void removeUnwantedFunctions(SILModule *M, ArrayRef<std::string> MangledNames,
191195
return str1.substr(0, str1.find(' ')) <
192196
str2.substr(0, str2.find(' '));
193197
});
194-
if ((FoundMangledName || FoundDemangledName) ^ InvertMatch) {
198+
if ((FoundMangledName || FoundDemangledName) ^ options.InvertMatch) {
195199
LLVM_DEBUG(llvm::dbgs() << " Not removing!\n");
196200
continue;
197201
}
@@ -225,57 +229,57 @@ void removeUnwantedFunctions(SILModule *M, ArrayRef<std::string> MangledNames,
225229
performSILDeadFunctionElimination(M);
226230
}
227231

228-
int main(int argc, char **argv) {
229-
PROGRAM_START(argc, argv);
232+
int sil_func_extractor_main(ArrayRef<const char *> argv, void *MainAddr) {
230233
INITIALIZE_LLVM();
231234

232-
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift SIL Extractor\n");
235+
SILFuncExtractorOptions options;
236+
237+
llvm::cl::ParseCommandLineOptions(argv.size(), argv.data(), "Swift SIL Extractor\n");
233238

234239
CompilerInvocation Invocation;
235240

236-
Invocation.setMainExecutablePath(llvm::sys::fs::getMainExecutable(
237-
argv[0], reinterpret_cast<void *>(&anchorForGetMainExecutable)));
241+
Invocation.setMainExecutablePath(llvm::sys::fs::getMainExecutable(argv[0], MainAddr));
238242

239243
// Give the context the list of search paths to use for modules.
240-
Invocation.setImportSearchPaths(ImportPaths);
244+
Invocation.setImportSearchPaths(options.ImportPaths);
241245
// Set the SDK path and target if given.
242-
if (SDKPath.getNumOccurrences() == 0) {
246+
if (options.SDKPath.getNumOccurrences() == 0) {
243247
const char *SDKROOT = getenv("SDKROOT");
244248
if (SDKROOT)
245-
SDKPath = SDKROOT;
249+
options.SDKPath = SDKROOT;
246250
}
247-
if (!SDKPath.empty())
248-
Invocation.setSDKPath(SDKPath);
249-
if (!Triple.empty())
250-
Invocation.setTargetTriple(Triple);
251-
if (!ResourceDir.empty())
252-
Invocation.setRuntimeResourcePath(ResourceDir);
253-
Invocation.getClangImporterOptions().ModuleCachePath = ModuleCachePath;
251+
if (!options.SDKPath.empty())
252+
Invocation.setSDKPath(options.SDKPath);
253+
if (!options.Triple.empty())
254+
Invocation.setTargetTriple(options.Triple);
255+
if (!options.ResourceDir.empty())
256+
Invocation.setRuntimeResourcePath(options.ResourceDir);
257+
Invocation.getClangImporterOptions().ModuleCachePath = options.ModuleCachePath;
254258
Invocation.setParseStdlib();
255259
Invocation.getLangOptions().DisableAvailabilityChecking = true;
256260
Invocation.getLangOptions().EnableAccessControl = false;
257261
Invocation.getLangOptions().EnableObjCAttrRequiresFoundation = false;
258262

259-
if (EnableObjCInterop == llvm::cl::BOU_UNSET) {
263+
if (options.EnableObjCInterop == llvm::cl::BOU_UNSET) {
260264
Invocation.getLangOptions().EnableObjCInterop =
261265
Invocation.getLangOptions().Target.isOSDarwin();
262266
} else {
263267
Invocation.getLangOptions().EnableObjCInterop =
264-
EnableObjCInterop == llvm::cl::BOU_TRUE;
268+
options.EnableObjCInterop == llvm::cl::BOU_TRUE;
265269
}
266270

267271
SILOptions &Opts = Invocation.getSILOptions();
268-
Opts.EmitVerboseSIL = EmitVerboseSIL;
269-
Opts.EmitSortedSIL = EmitSortedSIL;
270-
Opts.EnableOSSAModules = EnableOSSAModules;
272+
Opts.EmitVerboseSIL = options.EmitVerboseSIL;
273+
Opts.EmitSortedSIL = options.EmitSortedSIL;
274+
Opts.EnableOSSAModules = options.EnableOSSAModules;
271275

272276
serialization::ExtendedValidationInfo extendedInfo;
273277
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
274-
Invocation.setUpInputForSILTool(InputFilename, ModuleName,
278+
Invocation.setUpInputForSILTool(options.InputFilename, options.ModuleName,
275279
/*alwaysSetModuleToMain*/ true,
276280
/*bePrimary*/ false, extendedInfo);
277281
if (!FileBufOrErr) {
278-
fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str());
282+
fprintf(stderr, "Error! Failed to open file: %s\n", options.InputFilename.c_str());
279283
exit(-1);
280284
}
281285

@@ -297,13 +301,13 @@ int main(int argc, char **argv) {
297301
auto SILMod = performASTLowering(CI.getMainModule(), CI.getSILTypes(),
298302
CI.getSILOptions());
299303

300-
if (CommandLineFunctionNames.empty() && FunctionNameFile.empty())
304+
if (options.CommandLineFunctionNames.empty() && options.FunctionNameFile.empty())
301305
return CI.getASTContext().hadError();
302306

303307
// For efficient usage, we separate our names into two separate sorted
304308
// lists, one of managled names, and one of unmangled names.
305309
std::vector<std::string> Names;
306-
getFunctionNames(Names);
310+
getFunctionNames(Names, options);
307311

308312
// First partition our function names into mangled/demangled arrays.
309313
auto FirstDemangledName = std::partition(
@@ -339,14 +343,14 @@ int main(int argc, char **argv) {
339343
llvm::errs() << " " << str << '\n';
340344
}));
341345

342-
removeUnwantedFunctions(SILMod.get(), MangledNames, DemangledNames);
346+
removeUnwantedFunctions(SILMod.get(), MangledNames, DemangledNames, options);
343347

344-
if (EmitSIB) {
348+
if (options.EmitSIB) {
345349
llvm::SmallString<128> OutputFile;
346-
if (OutputFilename.size()) {
347-
OutputFile = OutputFilename;
348-
} else if (ModuleName.size()) {
349-
OutputFile = ModuleName;
350+
if (options.OutputFilename.size()) {
351+
OutputFile = options.OutputFilename;
352+
} else if (options.ModuleName.size()) {
353+
OutputFile = options.ModuleName;
350354
llvm::sys::path::replace_extension(
351355
OutputFile, file_types::getExtension(file_types::TY_SIB));
352356
} else {
@@ -365,14 +369,14 @@ int main(int argc, char **argv) {
365369
serialize(CI.getMainModule(), serializationOpts, symbolGraphOpts, SILMod.get());
366370
} else {
367371
const StringRef OutputFile =
368-
OutputFilename.size() ? StringRef(OutputFilename) : "-";
372+
options.OutputFilename.size() ? StringRef(options.OutputFilename) : "-";
369373

370374
auto SILOpts = SILOptions();
371-
SILOpts.EmitVerboseSIL = EmitVerboseSIL;
372-
SILOpts.EmitSortedSIL = EmitSortedSIL;
375+
SILOpts.EmitVerboseSIL = options.EmitVerboseSIL;
376+
SILOpts.EmitSortedSIL = options.EmitSortedSIL;
373377

374378
if (OutputFile == "-") {
375-
SILMod->print(llvm::outs(), CI.getMainModule(), SILOpts, !DisableASTDump);
379+
SILMod->print(llvm::outs(), CI.getMainModule(), SILOpts, !options.DisableASTDump);
376380
} else {
377381
std::error_code EC;
378382
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_None);
@@ -381,7 +385,8 @@ int main(int argc, char **argv) {
381385
<< '\n';
382386
return 1;
383387
}
384-
SILMod->print(OS, CI.getMainModule(), SILOpts, !DisableASTDump);
388+
SILMod->print(OS, CI.getMainModule(), SILOpts, !options.DisableASTDump);
385389
}
386390
}
391+
return 0;
387392
}

test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ function(get_test_dependencies SDK result_var_name)
5252
if (SWIFT_INCLUDE_TOOLS)
5353
list(APPEND deps_binaries
5454
lldb-moduleimport-test
55-
sil-func-extractor
5655
sil-llvm-gen
5756
sil-nm
5857
sil-passpipeline-dumper

tools/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ add_swift_tool_subdirectory(swift-demangle)
2323
add_swift_tool_subdirectory(swift-demangle-yamldump)
2424
add_swift_tool_subdirectory(swift-def-to-strings-converter)
2525
add_swift_tool_subdirectory(swift-serialize-diagnostics)
26-
add_swift_tool_subdirectory(sil-func-extractor)
2726
add_swift_tool_subdirectory(sil-llvm-gen)
2827
add_swift_tool_subdirectory(sil-nm)
2928
add_swift_tool_subdirectory(sil-passpipeline-dumper)

0 commit comments

Comments
 (0)