llvm_bc.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from _common import rootrel_arc_src, sort_by_keywords, skip_build_root
  2. def onllvm_bc(unit, *args):
  3. free_args, kwds = sort_by_keywords(
  4. {'SYMBOLS': -1, 'NAME': 1, 'GENERATE_MACHINE_CODE': 0, 'NO_COMPILE': 0, 'SUFFIX': 1}, args
  5. )
  6. name = kwds['NAME'][0]
  7. symbols = kwds.get('SYMBOLS')
  8. obj_suf = kwds['SUFFIX'][0] if 'SUFFIX' in kwds else '' + unit.get('OBJ_SUF')
  9. skip_compile_step = 'NO_COMPILE' in kwds
  10. merged_bc = name + '_merged' + obj_suf + '.bc'
  11. out_bc = name + '_optimized' + obj_suf + '.bc'
  12. bcs = []
  13. for x in free_args:
  14. rel_path = rootrel_arc_src(x, unit)
  15. bc_path = '${ARCADIA_BUILD_ROOT}/' + skip_build_root(rel_path) + obj_suf + '.bc'
  16. if not skip_compile_step:
  17. if x.endswith('.c'):
  18. llvm_compile = unit.onllvm_compile_c
  19. elif x.endswith('.ll'):
  20. llvm_compile = unit.onllvm_compile_ll
  21. else:
  22. llvm_compile = unit.onllvm_compile_cxx
  23. llvm_compile([rel_path, bc_path])
  24. bcs.append(bc_path)
  25. unit.onllvm_link([merged_bc] + bcs)
  26. passes = ['default<O2>', 'globalopt', 'globaldce']
  27. opt_opts = []
  28. if symbols:
  29. passes += ['internalize']
  30. # XXX: '#' used instead of ',' to overcome ymake tendency to split everything by comma
  31. opt_opts += ['-internalize-public-api-list=' + '#'.join(symbols)]
  32. # Add additional quotes for cmake build.
  33. # Generated final option for cmake looks like: -passes="..."
  34. opt_opts += ['\'-passes="{}"\''.format('${__COMMA__}'.join(passes))]
  35. unit.onllvm_opt([merged_bc, out_bc] + opt_opts)
  36. if 'GENERATE_MACHINE_CODE' in kwds:
  37. unit.onllvm_llc([out_bc, '-O2'])
  38. else:
  39. unit.onresource([out_bc, '/llvm_bc/' + name])