PATCH: C API - Add LLVMAddTargetDependentFunctionAttr(), LLVMGetBufferStart(), LLVMGetBufferSize(), LLVMTargetMachineEmitToMemoryBuffer()

Tom Stellard tom at stellard.net
Mon Apr 8 07:02:13 PDT 2013


Ping.

On Wed, Apr 03, 2013 at 10:44:16AM -0700, Tom Stellard wrote:
> Hi,
> 
> The attached patches add some new functions to the C API.  Please
> review.
> 
> Thanks,
> Tom Stellard
> 

> From 194f0ae6541e6da2ac1e012439d4452f3673039f Mon Sep 17 00:00:00 2001
> From: Tom Stellard <thomas.stellard at amd.com>
> Date: Tue, 2 Apr 2013 11:14:51 -0700
> Subject: [PATCH 1/4] C API: Add LLVMAddTargetDependentFunctionAttr()
> 
> ---
>  include/llvm-c/Core.h |  7 +++++++
>  lib/IR/Core.cpp       | 11 +++++++++++
>  2 files changed, 18 insertions(+)
> 
> diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h
> index e85fb97..9c1d825 100644
> --- a/include/llvm-c/Core.h
> +++ b/include/llvm-c/Core.h
> @@ -1694,6 +1694,13 @@ void LLVMSetGC(LLVMValueRef Fn, const char *Name);
>  void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA);
>  
>  /**
> + * Add a target-dependent attribute to a fuction
> + * @see llvm::AttrBuilder::addAttribute()
> + */
> +void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
> +                                        const char *V);
> +
> +/**
>   * Obtain an attribute from a function.
>   *
>   * @see llvm::Function::getAttributes()
> diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp
> index 983b49c..df23833 100644
> --- a/lib/IR/Core.cpp
> +++ b/lib/IR/Core.cpp
> @@ -1396,6 +1396,17 @@ void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
>    Func->setAttributes(PALnew);
>  }
>  
> +void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
> +                                        const char *V) {
> +  Function *Func = unwrap<Function>(Fn);
> +  int Idx = AttributeSet::FunctionIndex;
> +  AttrBuilder B;
> +
> +  B.addAttribute(A, V);
> +  AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
> +  Func->addAttributes(Idx, Set);
> +}
> +
>  void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
>    Function *Func = unwrap<Function>(Fn);
>    const AttributeSet PAL = Func->getAttributes();
> -- 
> 1.7.11.4
> 

> From 54b5515729aaa484f1a920289d13d117616cf963 Mon Sep 17 00:00:00 2001
> From: Tom Stellard <thomas.stellard at amd.com>
> Date: Tue, 2 Apr 2013 11:16:08 -0700
> Subject: [PATCH 2/4] C API: Add LLVMGetBufferStart()
> 
> ---
>  include/llvm-c/Core.h | 1 +
>  lib/IR/Core.cpp       | 3 +++
>  2 files changed, 4 insertions(+)
> 
> diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h
> index 9c1d825..3b350e4 100644
> --- a/include/llvm-c/Core.h
> +++ b/include/llvm-c/Core.h
> @@ -2567,6 +2567,7 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(const char *InputData,
>  LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData,
>                                                                size_t InputDataLength,
>                                                                const char *BufferName);
> +const char* LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf);
>  void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
>  
>  /**
> diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp
> index df23833..0b13be3 100644
> --- a/lib/IR/Core.cpp
> +++ b/lib/IR/Core.cpp
> @@ -2408,6 +2408,9 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
>        StringRef(BufferName)));
>  }
>  
> +const char* LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
> +  return unwrap(MemBuf)->getBufferStart();
> +}
>  
>  void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
>    delete unwrap(MemBuf);
> -- 
> 1.7.11.4
> 

> From 32ec728e801bc40b52659ea9872cb22ca22b8e2d Mon Sep 17 00:00:00 2001
> From: Tom Stellard <thomas.stellard at amd.com>
> Date: Tue, 2 Apr 2013 11:16:30 -0700
> Subject: [PATCH 3/4] C API: Add LLVMGetBufferSize()
> 
> ---
>  include/llvm-c/Core.h | 1 +
>  lib/IR/Core.cpp       | 4 ++++
>  2 files changed, 5 insertions(+)
> 
> diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h
> index 3b350e4..2ef0af5 100644
> --- a/include/llvm-c/Core.h
> +++ b/include/llvm-c/Core.h
> @@ -2568,6 +2568,7 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputD
>                                                                size_t InputDataLength,
>                                                                const char *BufferName);
>  const char* LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf);
> +size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf);
>  void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
>  
>  /**
> diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp
> index 0b13be3..5777e9e 100644
> --- a/lib/IR/Core.cpp
> +++ b/lib/IR/Core.cpp
> @@ -2412,6 +2412,10 @@ const char* LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
>    return unwrap(MemBuf)->getBufferStart();
>  }
>  
> +size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
> +  return unwrap(MemBuf)->getBufferSize();
> +}
> +
>  void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
>    delete unwrap(MemBuf);
>  }
> -- 
> 1.7.11.4
> 

> From 231f0afbcabe7db04a600d322af3607a44472262 Mon Sep 17 00:00:00 2001
> From: Tom Stellard <thomas.stellard at amd.com>
> Date: Tue, 2 Apr 2013 11:17:00 -0700
> Subject: [PATCH 4/4] C API: Add LLVMTargetMachineEmitToMemoryBuffer()
> 
> ---
>  include/llvm-c/TargetMachine.h |  3 +++
>  lib/Target/TargetMachineC.cpp  | 45 +++++++++++++++++++++++++++++++-----------
>  2 files changed, 36 insertions(+), 12 deletions(-)
> 
> diff --git a/include/llvm-c/TargetMachine.h b/include/llvm-c/TargetMachine.h
> index 691abdf..a02161a 100644
> --- a/include/llvm-c/TargetMachine.h
> +++ b/include/llvm-c/TargetMachine.h
> @@ -114,6 +114,9 @@ LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T);
>  LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
>    char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage);
>  
> +/** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
> +LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M,
> +  LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf);
>  
>  
>  
> diff --git a/lib/Target/TargetMachineC.cpp b/lib/Target/TargetMachineC.cpp
> index 79f74bd..11a5d7a 100644
> --- a/lib/Target/TargetMachineC.cpp
> +++ b/lib/Target/TargetMachineC.cpp
> @@ -149,8 +149,8 @@ LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
>    return wrap(unwrap(T)->getDataLayout());
>  }
>  
> -LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
> -  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
> +static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
> +  formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
>    TargetMachine* TM = unwrap(T);
>    Module* Mod = unwrap(M);
>  
> @@ -176,14 +176,7 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
>        ft = TargetMachine::CGFT_ObjectFile;
>        break;
>    }
> -  raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
> -  formatted_raw_ostream destf(dest);
> -  if (!error.empty()) {
> -    *ErrorMessage = strdup(error.c_str());
> -    return true;
> -  }
> -
> -  if (TM->addPassesToEmitFile(pass, destf, ft)) {
> +  if (TM->addPassesToEmitFile(pass, OS, ft)) {
>      error = "TargetMachine can't emit a file of this type";
>      *ErrorMessage = strdup(error.c_str());
>      return true;
> @@ -191,7 +184,35 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
>  
>    pass.run(*Mod);
>  
> -  destf.flush();
> -  dest.flush();
> +  OS.flush();
>    return false;
>  }
> +
> +LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
> +  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
> +  std::string error;
> +  raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
> +  formatted_raw_ostream destf(dest);
> +  if (!error.empty()) {
> +    *ErrorMessage = strdup(error.c_str());
> +    return true;
> +  }
> +  bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
> +  dest.flush();
> +  return Result;
> +}
> +
> +LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
> +  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
> +  LLVMMemoryBufferRef *OutMemBuf) {
> +  std::string CodeString;
> +  raw_string_ostream OStream(CodeString);
> +  formatted_raw_ostream Out(OStream);
> +  bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
> +  OStream.flush();
> +
> +  std::string &Data = OStream.str();
> +  *OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
> +                                                     Data.length(), "");
> +  return Result;
> +}
> -- 
> 1.7.11.4
> 

> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list