'Finalize module and return the HSAIL code'

def finalize(self):

    assert (not self._finalized), 'Module    finalized    already'
    main = self._llvm_modules[0]
    for dep in self._llvm_modules[1:]:
        self._hlc.link(main, dep)
    with open(BUILTIN_PATH, 'rb') as builtin_fin:
        builtin_buf = builtin_fin.read()
    builtin_mod = self._hlc.parse_bitcode(builtin_buf)
    self._hlc.link(main, builtin_mod)
    self._hlc.optimize(main)
    if config.DUMP_OPTIMIZED:
        print(self._hlc.to_string(main))
    hsail = self._hlc.to_hsail(main)
    brig = self._hlc.to_brig(main)
    self._finalized = True
    self._hlc.destroy_module(main)
    if config.DUMP_ASSEMBLY:
        print(hsail)
    return namedtuple('FinalizerResult', ['hsail', 'brig'])(hsail, brig)
