From ddc6f353c0e6746553e7042170e42d715b76a294 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 6 Jan 2007 23:53:43 +0000 Subject: [PATCH] Add C wrappers for the LTO library (PR1081). Patch by Chandler Carruth! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32966 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm-c/LinkTimeOptimizer.h | 58 ++++++++++++++++++++++++++ tools/lto/lto-c.cpp | 66 ++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 include/llvm-c/LinkTimeOptimizer.h create mode 100644 tools/lto/lto-c.cpp diff --git a/include/llvm-c/LinkTimeOptimizer.h b/include/llvm-c/LinkTimeOptimizer.h new file mode 100644 index 00000000000..6de33ca9c20 --- /dev/null +++ b/include/llvm-c/LinkTimeOptimizer.h @@ -0,0 +1,58 @@ +//===-- llvm/LinkTimeOptimizer.h - LTO Public C Interface -------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chandler Carruth and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header provides a C API to use the LLVM link time optimization +// library. This is inteded to be used by linkers which are C-only in +// their implementation for performing LTO. +// +//===----------------------------------------------------------------------===// + +#ifndef __LTO_CAPI_H__ +#define __LTO_CAPI_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + /// This provides a dummy type for pointers to the LTO object. + typedef void* llvm_lto_t; + + /// This provides a C-visible enumerator to manage status codes. + /// This should map exactly onto the C++ enumerator LTOStatus. + typedef enum llvm_lto_status { + LLVM_LTO_UNKNOWN, + LLVM_LTO_OPT_SUCCESS, + LLVM_LTO_READ_SUCCESS, + LLVM_LTO_READ_FAILURE, + LLVM_LTO_WRITE_FAILURE, + LLVM_LTO_NO_TARGET, + LLVM_LTO_NO_WORK, + LLVM_LTO_MODULE_MERGE_FAILURE, + LLVM_LTO_ASM_FAILURE, + + // Added C-specific error codes + LLVM_LTO_NULL_OBJECT + } llvm_lto_status_t; + + /// This provides C interface to initialize link time optimizer. This allows + /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer. + /// extern "C" helps, because dlopen() interface uses name to find the symbol. + extern llvm_lto_t llvm_create_optimizer(void); + extern void llvm_destroy_optimizer(llvm_lto_t lto); + + extern llvm_lto_status_t llvm_read_object_file + (llvm_lto_t lto, const char* input_filename); + extern llvm_lto_status_t llvm_optimize_modules + (llvm_lto_t lto, const char* output_filename); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tools/lto/lto-c.cpp b/tools/lto/lto-c.cpp new file mode 100644 index 00000000000..f89b374e19b --- /dev/null +++ b/tools/lto/lto-c.cpp @@ -0,0 +1,66 @@ +//===- lto-c.cpp - LLVM Link Time Optimizer C Wrappers --------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chandler Carruth and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements a C wrapper API for the Link Time Optimization +// library. +// +//===----------------------------------------------------------------------===// + +#include "llvm-c/LinkTimeOptimizer.h" +#include "llvm/LinkTimeOptimizer.h" +using namespace llvm; + + +/// Create an instance of the LLVM LTO object for performing the link +/// time optimizations. +extern "C" +llvm_lto_t llvm_create_optimizer() { + return new llvm::LTO(); +} + +/// Destroy an instance of the LLVM LTO object +extern "C" +void llvm_destroy_optimizer(llvm_lto_t lto) { + delete (llvm::LTO*)lto; +} + +/// Read an LLVM bytecode file using LTO::readLLVMObjectFile. +extern "C" +llvm_lto_status +llvm_read_object_file(llvm_lto_t lto, const char *input_filename) { + llvm::LTO *l = (llvm::LTO*)lto; + + if (input_filename == NULL) + return LLVM_LTO_READ_FAILURE; + + std::string InputFilename(input_filename); + llvm::LTO::NameToSymbolMap symbols; + std::set references; + return (llvm_lto_status)((int)(l->readLLVMObjectFile(InputFilename, symbols, + references))); +} + + +/// Optimize and output object code using LTO::optimizeModules. +extern "C" +llvm_lto_status +llvm_optimize_modules(llvm_lto_t lto, const char *output_filename) { + llvm::LTO *l = (llvm::LTO*)lto; + + std::string OutputFilename(output_filename); + std::vector exportList; + std::string targetTriple; + + return (llvm_lto_status)((int)( + l->optimizeModules(OutputFilename, exportList, + targetTriple, false, ""))); +} + + +