Skip to content

Commit 96ffe11

Browse files
authored
Remove complexity in caching of llvm-nm results. NFC (#13360)
This extra argument (include_internal) and the extra complexity it entailed had only a single call site in the test suite. Calling llvm-nm directly in the test suite is much simpler.
1 parent 88a3b47 commit 96ffe11

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

tests/test_other.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6502,8 +6502,8 @@ def test_disable_inlining(self):
65026502

65036503
# To this test to be successful, foo() shouldn't have been inlined above and
65046504
# foo() should be in the function list
6505-
syms = building.llvm_nm('test2.o', include_internal=True)
6506-
assert 'foo' in syms.defs, 'foo() should not be inlined'
6505+
output = self.run_process([shared.LLVM_NM, 'test2.o'], stdout=PIPE).stdout
6506+
self.assertContained('foo', output)
65076507

65086508
def test_output_eol(self):
65096509
for params in [[], ['--proxy-to-worker'], ['--proxy-to-worker', '-s', 'WASM=0']]:

tools/building.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
multiprocessing_pool = None
4040
binaryen_checked = False
4141

42-
# internal caches
43-
internal_nm_cache = {}
4442
# cache results of nm - it can be slow to run
45-
uninternal_nm_cache = {}
43+
nm_cache = {}
4644
# Stores the object files contained in different archive files passed as input
4745
ar_contents = {}
4846
_is_ar_cache = {}
@@ -150,12 +148,11 @@ def check(value):
150148
return list(filter(check, values))
151149

152150

153-
# clear internal caches. this is not normally needed, except if the clang/LLVM
151+
# clear caches. this is not normally needed, except if the clang/LLVM
154152
# used changes inside this invocation of Building, which can happen in the benchmarker
155153
# when it compares different builds.
156154
def clear():
157-
internal_nm_cache.clear()
158-
uninternal_nm_cache.clear()
155+
nm_cache.clear()
159156
ar_contents.clear()
160157
_is_ar_cache.clear()
161158

@@ -372,7 +369,7 @@ def make_paths_absolute(f):
372369

373370

374371
# Runs llvm-nm in parallel for the given list of files.
375-
# The results are populated in uninternal_nm_cache
372+
# The results are populated in nm_cache
376373
# multiprocessing_pool: An existing multiprocessing pool to reuse for the operation, or None
377374
# to have the function allocate its own.
378375
def parallel_llvm_nm(files):
@@ -383,7 +380,7 @@ def parallel_llvm_nm(files):
383380
for i, file in enumerate(files):
384381
if object_contents[i].returncode != 0:
385382
logger.debug('llvm-nm failed on file ' + file + ': return code ' + str(object_contents[i].returncode) + ', error: ' + object_contents[i].output)
386-
uninternal_nm_cache[file] = object_contents[i]
383+
nm_cache[file] = object_contents[i]
387384
return object_contents
388385

389386

@@ -398,7 +395,7 @@ def read_link_inputs(files):
398395

399396
if absolute_path_f not in ar_contents and is_ar(absolute_path_f):
400397
archive_names.append(absolute_path_f)
401-
elif absolute_path_f not in uninternal_nm_cache and is_bitcode(absolute_path_f):
398+
elif absolute_path_f not in nm_cache and is_bitcode(absolute_path_f):
402399
object_names.append(absolute_path_f)
403400

404401
# Archives contain objects, so process all archives first in parallel to obtain the object files in them.
@@ -419,7 +416,7 @@ def clean_at_exit():
419416

420417
for o in object_names_in_archives:
421418
for f in o['files']:
422-
if f not in uninternal_nm_cache:
419+
if f not in nm_cache:
423420
object_names.append(f)
424421

425422
# Next, extract symbols from all object files (either standalone or inside archives we just extracted)
@@ -730,7 +727,7 @@ def get_command_with_possible_response_file(cmd):
730727
return new_cmd
731728

732729

733-
def parse_symbols(output, include_internal=False):
730+
def parse_symbols(output):
734731
defs = []
735732
undefs = []
736733
commons = []
@@ -753,42 +750,37 @@ def parse_symbols(output, include_internal=False):
753750
undefs.append(symbol)
754751
elif status == 'C':
755752
commons.append(symbol)
756-
elif (not include_internal and status == status.upper()) or \
757-
(include_internal and status in ['W', 't', 'T', 'd', 'D']):
753+
elif status == status.upper():
758754
# FIXME: using WTD in the previous line fails due to llvm-nm behavior on macOS,
759755
# so for now we assume all uppercase are normally defined external symbols
760756
defs.append(symbol)
761757
return ObjectFileInfo(0, None, set(defs), set(undefs), set(commons))
762758

763759

764-
def llvm_nm_uncached(filename, stdout=PIPE, stderr=PIPE, include_internal=False):
760+
def llvm_nm_uncached(filename, stdout=PIPE, stderr=PIPE):
765761
# LLVM binary ==> list of symbols
766762
proc = run_process([LLVM_NM, filename], stdout=stdout, stderr=stderr, check=False)
767763
if proc.returncode == 0:
768-
return parse_symbols(proc.stdout, include_internal)
764+
return parse_symbols(proc.stdout)
769765
else:
770766
return ObjectFileInfo(proc.returncode, str(proc.stdout) + str(proc.stderr))
771767

772768

773-
def llvm_nm(filename, stdout=PIPE, stderr=PIPE, include_internal=False):
769+
def llvm_nm(filename, stdout=PIPE, stderr=PIPE):
774770
# Always use absolute paths to maximize cache usage
775771
filename = os.path.abspath(filename)
776772

777-
if include_internal and filename in internal_nm_cache:
778-
return internal_nm_cache[filename]
779-
elif not include_internal and filename in uninternal_nm_cache:
780-
return uninternal_nm_cache[filename]
773+
if filename in nm_cache:
774+
return nm_cache[filename]
781775

782-
ret = llvm_nm_uncached(filename, stdout, stderr, include_internal)
776+
ret = llvm_nm_uncached(filename, stdout, stderr)
783777

784778
if ret.returncode != 0:
785779
logger.debug('llvm-nm failed on file ' + filename + ': return code ' + str(ret.returncode) + ', error: ' + ret.output)
786780

787-
# Even if we fail, write the results to the NM cache so that we don't keep trying to llvm-nm the failing file again later.
788-
if include_internal:
789-
internal_nm_cache[filename] = ret
790-
else:
791-
uninternal_nm_cache[filename] = ret
781+
# Even if we fail, write the results to the NM cache so that we don't keep trying to llvm-nm the
782+
# failing file again later.
783+
nm_cache[filename] = ret
792784

793785
return ret
794786

0 commit comments

Comments
 (0)