[llvm] r212305 - Modify LTOModule::isTargetMatch to take a StringRef instead of a MemoryBuffer.

Alp Toker alp at nuanti.com
Thu Jul 3 17:17:26 PDT 2014


On 04/07/2014 02:49, Peter Collingbourne wrote:
> Author: pcc
> Date: Thu Jul  3 18:49:28 2014
> New Revision: 212305
>
> URL:http://llvm.org/viewvc/llvm-project?rev=212305&view=rev
> Log:
> Modify LTOModule::isTargetMatch to take a StringRef instead of a MemoryBuffer.
>
> Modified:
>      llvm/trunk/include/llvm/LTO/LTOModule.h
>      llvm/trunk/lib/LTO/LTOModule.cpp
>
> Modified: llvm/trunk/include/llvm/LTO/LTOModule.h
> URL:http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/LTOModule.h?rev=212305&r1=212304&r2=212305&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/LTO/LTOModule.h (original)
> +++ llvm/trunk/include/llvm/LTO/LTOModule.h Thu Jul  3 18:49:28 2014
> @@ -202,8 +202,8 @@ private:
>     /// Get string that the data pointer points to.
>     bool objcClassNameFromExpression(const Constant *c, std::string &name);
>   
> -  /// Returns 'true' if the memory buffer is for the specified target triple.
> -  static bool isTargetMatch(MemoryBuffer *memBuffer, const char *triplePrefix);
> +  /// Returns 'true' if the bitcode BC is for the specified target triple.
> +  static bool isTargetMatch(StringRef BC, const char *TriplePrefix);
>   
>     /// Create an LTOModule (private version). N.B. This method takes ownership of
>     /// the buffer.
>
> Modified: llvm/trunk/lib/LTO/LTOModule.cpp
> URL:http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOModule.cpp?rev=212305&r1=212304&r2=212305&view=diff
> ==============================================================================
> --- llvm/trunk/lib/LTO/LTOModule.cpp (original)
> +++ llvm/trunk/lib/LTO/LTOModule.cpp Thu Jul  3 18:49:28 2014
> @@ -75,7 +75,7 @@ bool LTOModule::isBitcodeFileForTarget(c
>     MemoryBuffer *buffer = makeBuffer(mem, length);
>     if (!buffer)
>       return false;
> -  return isTargetMatch(buffer, triplePrefix);
> +  return isTargetMatch(StringRef((const char *)mem, length), triplePrefix);

This creates a memory buffer, then doesn't use it.

>   }
>   
>   bool LTOModule::isBitcodeFileForTarget(const char *path,
> @@ -83,15 +83,15 @@ bool LTOModule::isBitcodeFileForTarget(c
>     std::unique_ptr<MemoryBuffer> buffer;
>     if (MemoryBuffer::getFile(path, buffer))
>       return false;
> -  return isTargetMatch(buffer.release(), triplePrefix);
> +  return isTargetMatch(buffer->getBuffer(), triplePrefix);

This creates a memory buffer, then creates another in isTargetMatch() 
when a single one would do.

I've attached a nicer way to clean this up.

OK to commit this over yours?

(Ultimately we should remove the the two wrapper functions because they 
just encourage inefficient use by statting, mmapping and reading the 
file twice -- once to test and then again to use it.)

Alp.


>   }
>   
> -/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
> -/// target triple.
> -bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
> -  std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
> -  delete buffer;
> -  return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
> +/// Returns 'true' if the bitcode BC is for the specified target triple.
> +bool LTOModule::isTargetMatch(StringRef BC, const char *TriplePrefix) {
> +  std::unique_ptr<MemoryBuffer> Buffer(
> +      MemoryBuffer::getMemBuffer(BC, "", false));
> +  std::string Triple = getBitcodeTargetTriple(Buffer.get(), getGlobalContext());
> +  return strncmp(Triple.c_str(), TriplePrefix, strlen(TriplePrefix)) == 0;
>   }
>   
>   LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

-- 
http://www.nuanti.com
the browser experts

-------------- next part --------------
commit 7e07ac769ea7fd2b7cca6fd080da23ccbfe2c424
Author: Alp Toker <alp at nuanti.com>
Date:   Fri Jul 4 02:24:43 2014 +0300

    ownership

diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp
index cda41dc..1baa09c 100644
--- a/lib/LTO/LTOModule.cpp
+++ b/lib/LTO/LTOModule.cpp
@@ -72,10 +72,10 @@ bool LTOModule::isBitcodeFile(const char *path) {
 /// LLVM bitcode for the specified triple.
 bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
                                        const char *triplePrefix) {
-  MemoryBuffer *buffer = makeBuffer(mem, length);
+  std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length));
   if (!buffer)
     return false;
-  return isTargetMatch(buffer, triplePrefix);
+  return isTargetMatch(buffer.get(), triplePrefix);
 }
 
 bool LTOModule::isBitcodeFileForTarget(const char *path,
@@ -83,14 +83,13 @@ bool LTOModule::isBitcodeFileForTarget(const char *path,
   std::unique_ptr<MemoryBuffer> buffer;
   if (MemoryBuffer::getFile(path, buffer))
     return false;
-  return isTargetMatch(buffer.release(), triplePrefix);
+  return isTargetMatch(buffer.get(), triplePrefix);
 }
 
 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
 /// target triple.
 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
   std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
-  delete buffer;
   return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
 }
 


More information about the llvm-commits mailing list