[lld] [lld][test] Precommit test for ld -r links with FatLTO PIC objects (PR #92817)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu May 23 10:02:15 PDT 2024


================
@@ -4,13 +4,23 @@
 ; RUN: rm -rf %t && split-file %s %t
 
 ;; Ensure that input files contain .llvm.lto section.
-; RUN: llc %t/a-LTO.ll --filetype=obj -o %t/a-fatLTO.o
+; RUN: llc %t/a-LTO.ll --filetype=obj -o %t/a-fatLTO.o --relocation-model=pic
 ; RUN: opt < %t/a-LTO.ll --module-summary -o %t/a-fatLTO.bc
-; RUN: llvm-objcopy --add-section=.llvm.lto=%t/a-fatLTO.bc --set-section-flags=.llvm.lto=exclude %t/a-fatLTO.o
+; RUN: llvm-objcopy --add-section=.llvm.lto=%t/a-fatLTO.bc --set-section-flags=.llvm.lto=exclude --set-section-type=.llvm.lto=0x6fff4c0c %t/a-fatLTO.o
----------------
MaskRay wrote:

I think the test is complex enough that removing some `%t/` occurrences will help readability...

I don't use llvm-lit for one-shot test invocation. I use `ff fatlto.test` and it's quite straightforward to copy & paste one single command...

```python
#!/usr/bin/env python3
# Simplified lit runner for a single test.
import argparse
import os, re, sys
from ipdb import set_trace as bp

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('-b', '--build', default='/tmp/Debug', help='build directory')
    ap.add_argument('-d', '--debug', type=int, default=-1, help='rr record the specified command')
    ap.add_argument('-r', '--run', type=int, default=-1, help='run the specified command')
    ap.add_argument('--release', action='store_true', help='use /tmp/Rel as the build directory')
    ap.add_argument('--binutils', action='store_true')
    ap.add_argument('-n', action='store_true', help='dry run')
    ap.add_argument('file')
    args = ap.parse_args()
    if args.debug >= 0:
        args.run = args.debug

    home = os.path.expanduser('~')
    build_dir = '/tmp/Rel' if args.release else args.build
    os.environ["PATH"] = os.path.expanduser(build_dir + '/bin') + os.pathsep + os.environ['PATH']
    os.environ['LLVM_DISABLE_SYMBOLIZATION'] = '1'
    os.environ['LLD_IN_TEST'] = '1'
    os.environ['LLD_VERSION'] = 'LLD 1.0'

    # GNU binutils
    if args.binutils:
        del sys.argv[1]
        os.environ["PATH"] = os.path.expanduser('~/Dev/binutils-gdb/out/release/ld') + os.pathsep + os.environ['PATH']

    cwd = os.getcwd()
    fname = os.path.realpath(args.file)
    tmp = os.path.join(os.path.dirname(fname), 'a')
    dirname = os.path.dirname(fname).replace(home, '$HOME')
    lineno = 0
    last = ''
    try:
       fh = open(fname)
    except FileNotFoundError:
        print('not exist', file=sys.stderr)
        return
    substs = sorted([
        ('/p', lambda: os.path.relpath(dirname, cwd)),
        ('/t', lambda: os.path.relpath(tmp, cwd)),
        # See llvm/utils/lit/lit/llvm/config.py
        ('clang', lambda: 'clang'),
        ('clang_cc1', lambda: f'clang -cc1 -internal-isystem {build_dir}/lib/clang/17/include -nostdsysteminc'),
        ('errc_EACCES', lambda: '"Permission denied"'),
        ('itanium_abi_triple', lambda: 'x86_64'),
        ('llc_dwarf', lambda: 'llc'),
        # See lld/test/MachO/lit.local.cfg
        ('lld', lambda: os.path.expanduser('ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -syslibroot ~/llvm/lld/test/MachO/Inputs/MacOSX.sdk -lSystem -fatal_warnings')),
        ('no-fatal-warnings-lld', lambda: os.path.expanduser('ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -syslibroot ~/llvm/lld/test/MachO/Inputs/MacOSX.sdk -lSystem')),
        ('p', lambda: os.path.relpath(dirname, cwd)),
        ('python', lambda: 'python3'),
        ('run', lambda: ''),
        ('s', lambda: os.path.relpath(fname, cwd)),
        ('S', lambda: os.path.relpath(dirname, cwd)),
        ('t', lambda: os.path.relpath(tmp, cwd)),
    ], key=lambda x: -len(x[0]))

    with fh as f:
        for line in f.readlines():
            line = line.strip()
            lineno += 1
            if 'END.' in line: break
            match = re.search(r'RUN:[ \t]', line)
            if not match:
                last = ''
                continue
            line = line[match.end():]
            if line[-1] == '\\':
                last += line[:-1]
                continue
            line = last + line
            last = ''

            if 'rm ' in line and ('..' in line or (not re.search(r'rm (-f|-fr|-rf) %t', line))):
                continue

            line1 = ''
            idx = 0
            while (idx1 := line.find('%', idx)) != -1:
                line1 += line[idx:idx1]
                idx = idx1+1
                for s in substs:
                    if line[idx:].startswith(s[0]):
                        line1 += s[1]()
                        idx += len(s[0])
                        break
                else:
                    line1 += line[idx-1]
            line = line1 + line[idx:]

            if args.run < 0:
                print(f'{lineno:^2} {line}')
            if not args.n:
                if os.system(line) != 0:
                    sys.exit(1)
            if args.run == lineno:
                if (idx := line.find('|')) >= 0:
                    line = line[:idx]
                line = f'{build_dir}/bin/{line}'
                if args.debug >= 0:
                    line = 'rr record ' + line
                print(f'### {line}')
                if not args.n:
                    if os.system(line) != 0:
                        sys.exit(1)
                    if args.debug >= 0:
                        os.execvp('rr', ('rr', 'replay', '-d', 'cgdb'))
                break

            if match := re.search(r'\bcd ([/.\w]+)', line):
                os.chdir(match.group(1))
                cwd = os.getcwd()

main()
```

https://github.com/llvm/llvm-project/pull/92817


More information about the llvm-commits mailing list