OSDN Git Service

Subzero: Update for LLVM 3.9 (trunk).
authorJim Stichnoth <stichnot@chromium.org>
Mon, 9 May 2016 19:24:36 +0000 (12:24 -0700)
committerJim Stichnoth <stichnot@chromium.org>
Mon, 9 May 2016 19:24:36 +0000 (12:24 -0700)
The purpose is to allow pnacl-sz to be built within an LLVM checkout (version 3.9 or later), and not require the complex PNaCl build environment.

Within an LLVM checkout, one would do something like the following:
  cd projects
  git clone https://chromium.googlesource.com/native_client/pnacl-subzero
  # change to LLVM build directory
  cmake -G "Ninja" <path_to_llvm_source_dir>
  ninja

A follow-on CL will add in the PNaCl bitcode reader source files that are needed for compiling and linking.

BUG= none
R=jpp@chromium.org

Review URL: https://codereview.chromium.org/1961743002 .

17 files changed:
CMakeLists.txt
Makefile
Makefile.standalone
src/IceBitVector.h
src/IceCfg.cpp
src/IceCfgNode.cpp
src/IceClFlags.cpp
src/IceClFlags.h
src/IceCompileServer.cpp
src/IceCompiler.cpp
src/IceConverter.cpp
src/IceInst.h
src/IceTargetLowering.cpp
src/IceTargetLowering.h
src/IceTargetLoweringARM32.cpp
src/PNaClTranslator.cpp
src/SZTargets.def

index 64315b7..b786204 100644 (file)
@@ -35,4 +35,11 @@ else()
   add_compile_options(-Wno-undefined-var-template)
 endif()
 
+# Define PNACL_LLVM for LLVM_VERSION <= 3.7
+if((NOT LLVM_VERSION_MAJOR GREATER 3) AND (NOT LLVM_VERSION_MINOR GREATER 7))
+  add_definitions(
+    -DPNACL_LLVM
+    )
+endif()
+
 target_link_libraries(pnacl-sz pthread)
index d8f17a9..52bfba3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -29,3 +29,5 @@ else
     -DALLOW_WASM=0 -DPNACL_BROWSER_TRANSLATOR=0
   CXX.Flags += -Wno-undefined-var-template
 endif
+
+CPP.Defines += -DPNACL_LLVM
index deb8ab5..d6bfb8e 100644 (file)
@@ -110,6 +110,8 @@ ifdef SZTARGET
   BASE_CXX_DEFINES += -DSZTARGET=$(SZTARGET)
 endif
 
+BASE_CXX_DEFINES += -DPNACL_LLVM
+
 CXX_DEFINES := $(BASE_CXX_DEFINES) -DPNACL_BROWSER_TRANSLATOR=0
 
 ifdef NOASSERT
index cdb4f33..3a5f8e6 100644 (file)
@@ -260,6 +260,14 @@ template <template <typename> class AT> class BitVectorTmpl {
   unsigned Capacity; // Size of allocated memory in BitWord.
   Allocator Alloc;
 
+  uint64_t alignTo(uint64_t Value, uint64_t Align) {
+#ifdef PNACL_LLVM
+    return llvm::RoundUpToAlignment(Value, Align);
+#else // !PNACL_LLVM
+    return llvm::alignTo(Value, Align);
+#endif // !PNACL_LLVM
+  }
+
 public:
   typedef unsigned size_type;
   // Encapsulation of a single bit.
@@ -469,7 +477,7 @@ public:
 
     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
     Bits[I / BITWORD_SIZE] |= PrefixMask;
-    I = llvm::RoundUpToAlignment(I, BITWORD_SIZE);
+    I = alignTo(I, BITWORD_SIZE);
 
     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
       Bits[I / BITWORD_SIZE] = ~0UL;
@@ -509,7 +517,7 @@ public:
 
     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
     Bits[I / BITWORD_SIZE] &= ~PrefixMask;
-    I = llvm::RoundUpToAlignment(I, BITWORD_SIZE);
+    I = alignTo(I, BITWORD_SIZE);
 
     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
       Bits[I / BITWORD_SIZE] = 0UL;
index 9a47dd6..f717379 100644 (file)
@@ -1102,7 +1102,7 @@ void Cfg::materializeVectorShuffles() {
 
       Instr.setDeleted();
       auto &LoweringContext = getTarget()->getContext();
-      LoweringContext.setInsertPoint(Instr);
+      LoweringContext.setInsertPoint(instToIterator(&Instr));
       LoweringContext.insert(ShuffleVector);
     }
   }
index fb3708b..78e3d18 100644 (file)
@@ -1342,7 +1342,7 @@ void CfgNode::emitIAS(Cfg *Func) const {
       I->emitIAS(Func);
       // Only update stats during the final pass.
       if (Retrying)
-        updateStats(Func, I);
+        updateStats(Func, iteratorToInst(I));
     } else {
       // Treat it as though there were an implicit bundle_lock and
       // bundle_unlock wrapping the instruction.
@@ -1352,7 +1352,7 @@ void CfgNode::emitIAS(Cfg *Func) const {
       Helper.rollback();
       Helper.padToNextBundle();
       I->emitIAS(Func);
-      updateStats(Func, I);
+      updateStats(Func, iteratorToInst(I));
       Helper.leaveBundleLockRegion();
     }
   }
index 257c6f1..4982cbf 100644 (file)
@@ -163,6 +163,10 @@ template <> struct ToSetterParam<cl::list<Ice::VerboseItem>> {
   using ReturnType = Ice::VerboseMask;
 };
 
+template <> struct ToSetterParam<cl::list<std::string>> {
+  using ReturnType = std::vector<std::string>;
+};
+
 template <typename T>
 typename ToSetterParam<T>::ReturnType toSetterParam(const T &Param) {
   return Param;
@@ -181,6 +185,12 @@ toSetterParam(const cl::list<Ice::VerboseItem> &Param) {
   return VMask;
 }
 
+template <>
+ToSetterParam<cl::list<std::string>>::ReturnType
+toSetterParam(const cl::list<std::string> &Param) {
+  return *&Param;
+}
+
 } // end of anonymous namespace
 
 void ClFlags::getParsedClFlags(ClFlags &OutFlags) {
index 19785bf..8c43471 100644 (file)
 #include <utility>
 #include <vector>
 
+#ifndef PNACL_LLVM
+namespace llvm {
+// \brief Define the expected format of the file.
+enum NaClFileFormat {
+  // LLVM IR source or bitcode file (as appropriate).
+  LLVMFormat,
+  // PNaCl bitcode file.
+  PNaClFormat,
+  // Autodetect if PNaCl or LLVM format.
+  AutodetectFileFormat
+};
+} // end of namespace llvm
+#endif // !PNACL_LLVM
+
 namespace Ice {
 // detail defines the type cl_type_traits, which is used to define the
 // getters/setters for the ClFlags class. It converts the cl_detail::*_flag
index e700b1e..15a86c3 100644 (file)
@@ -50,7 +50,12 @@ class TextDataStreamer : public llvm::DataStreamer {
 public:
   TextDataStreamer() = default;
   ~TextDataStreamer() final = default;
-  static TextDataStreamer *create(const std::string &Filename,
+#ifdef PNACL_LLVM
+  using CreateType = TextDataStreamer *;
+#else // !PNACL_LLVM
+  using CreateType = std::unique_ptr<TextDataStreamer>;
+#endif // !PNACL_LLVM
+  static CreateType create(const std::string &Filename,
                                   std::string *Err);
   size_t GetBytes(unsigned char *Buf, size_t Len) final;
 
@@ -59,8 +64,9 @@ private:
   size_t Cursor = 0;
 };
 
-TextDataStreamer *TextDataStreamer::create(const std::string &Filename,
+TextDataStreamer::CreateType TextDataStreamer::create(const std::string &Filename,
                                            std::string *Err) {
+#ifdef PNACL_LLVM
   TextDataStreamer *Streamer = new TextDataStreamer();
   llvm::raw_string_ostream ErrStrm(*Err);
   if (std::error_code EC = llvm::readNaClRecordTextAndBuildBitcode(
@@ -72,6 +78,9 @@ TextDataStreamer *TextDataStreamer::create(const std::string &Filename,
   }
   ErrStrm.flush();
   return Streamer;
+#else // !PNACL_LLVM
+  return CreateType();
+#endif // !PNACL_LLVM
 }
 
 size_t TextDataStreamer::GetBytes(unsigned char *Buf, size_t Len) {
index fbe0577..08e1a14 100644 (file)
@@ -86,8 +86,11 @@ void Compiler::run(const Ice::ClFlags &Flags, GlobalContext &Ctx,
   const bool WasmBuildOnRead = Flags.getBuildOnRead() && wasmInput(IRFilename);
   if (BuildOnRead) {
     std::unique_ptr<PNaClTranslator> PTranslator(new PNaClTranslator(&Ctx));
-    std::unique_ptr<llvm::StreamingMemoryObject> MemObj(
-        new llvm::StreamingMemoryObjectImpl(InputStream.release()));
+#ifdef PNACL_LLVM
+    std::unique_ptr<llvm::StreamingMemoryObject> MemObj(new llvm::StreamingMemoryObjectImpl(InputStream.release()));
+#else // !PNACL_LLVM
+    std::unique_ptr<llvm::StreamingMemoryObject> MemObj(new llvm::StreamingMemoryObject(std::move(InputStream)));
+#endif // !PNACL_LLVM
     PTranslator->translate(IRFilename, std::move(MemObj));
     Translator.reset(PTranslator.release());
   } else if (WasmBuildOnRead) {
@@ -120,6 +123,7 @@ void Compiler::run(const Ice::ClFlags &Flags, GlobalContext &Ctx,
     // Parse the input LLVM IR file into a module.
     llvm::SMDiagnostic Err;
     TimerMarker T1(Ice::TimerStack::TT_parse, &Ctx);
+#ifdef PNACL_LLVM
     llvm::DiagnosticHandlerFunction DiagnosticHandler =
         Flags.getLLVMVerboseErrors()
             ? redirectNaClDiagnosticToStream(llvm::errs())
@@ -127,6 +131,12 @@ void Compiler::run(const Ice::ClFlags &Flags, GlobalContext &Ctx,
     std::unique_ptr<llvm::Module> Mod =
         NaClParseIRFile(IRFilename, Flags.getInputFileFormat(), Err,
                         llvm::getGlobalContext(), DiagnosticHandler);
+#else // !PNACL_LLVM
+    llvm::DiagnosticHandlerFunction DiagnosticHandler = nullptr;
+    llvm::LLVMContext Context;
+    std::unique_ptr<llvm::Module> Mod =
+        parseIRFile(IRFilename, Err, Context);
+#endif // !PNACL_LLVM
     if (!Mod) {
       Err.print(Flags.getAppName().c_str(), llvm::errs());
       Ctx.getErrorStatus()->assign(EC_Bitcode);
index 0729b73..7d192e6 100644 (file)
@@ -111,7 +111,7 @@ public:
       // The initial definition/use of each arg is the entry node.
       for (auto ArgI = F->arg_begin(), ArgE = F->arg_end(); ArgI != ArgE;
            ++ArgI) {
-        Func->addArg(mapValueToIceVar(ArgI));
+        Func->addArg(mapValueToIceVar(&*ArgI));
       }
 
       // Make an initial pass through the block list just to resolve the blocks
@@ -708,7 +708,7 @@ void LLVM2ICEGlobalsConverter::convertGlobalsToIce(Module *Mod) {
                                      E = Mod->global_end();
        I != E; ++I) {
 
-    const GlobalVariable *GV = I;
+    const GlobalVariable *GV = &*I;
 
     Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV);
     auto *VarDecl = cast<Ice::VariableDeclaration>(Var);
@@ -888,7 +888,7 @@ void Converter::installGlobalDeclarations(Module *Mod) {
   for (Module::const_global_iterator I = Mod->global_begin(),
                                      E = Mod->global_end();
        I != E; ++I) {
-    const GlobalVariable *GV = I;
+    const GlobalVariable *GV = &*I;
     constexpr bool NoSuppressMangling = false;
     auto *Var = VariableDeclaration::create(
         GlobalDeclarationsPool.get(), NoSuppressMangling, GV->getLinkage());
index 352a5d4..769d405 100644 (file)
@@ -1131,4 +1131,24 @@ private:
 
 } // end of namespace llvm
 
+namespace Ice {
+
+inline InstList::iterator instToIterator(Inst *Instr) {
+#ifdef PNACL_LLVM
+  return Instr;
+#else // !PNACL_LLVM
+  return Instr->getIterator();
+#endif // !PNACL_LLVM
+}
+
+inline Inst *iteratorToInst(InstList::iterator Iter) {
+  return &*Iter;
+}
+
+inline const Inst *iteratorToInst(InstList::const_iterator Iter) {
+  return &*Iter;
+}
+
+} // end of namespace Ice
+
 #endif // SUBZERO_SRC_ICEINST_H
index 275516e..207a76c 100644 (file)
@@ -338,7 +338,7 @@ void TargetLowering::genTargetHelperCalls() {
     Context.init(Node);
     while (!Context.atEnd()) {
       PostIncrLoweringContext _(Context);
-      genTargetHelperCallFor(Context.getCur());
+      genTargetHelperCallFor(iteratorToInst(Context.getCur()));
     }
   }
 }
@@ -353,7 +353,7 @@ void TargetLowering::doAddressOpt() {
 }
 
 void TargetLowering::doNopInsertion(RandomNumberGenerator &RNG) {
-  Inst *I = Context.getCur();
+  Inst *I = iteratorToInst(Context.getCur());
   bool ShouldSkip = llvm::isa<InstFakeUse>(I) || llvm::isa<InstFakeDef>(I) ||
                     llvm::isa<InstFakeKill>(I) || I->isRedundantAssign() ||
                     I->isDeleted();
@@ -378,7 +378,7 @@ void TargetLowering::doNopInsertion(RandomNumberGenerator &RNG) {
 // should delete any additional instructions it consumes.
 void TargetLowering::lower() {
   assert(!Context.atEnd());
-  Inst *Instr = Context.getCur();
+  Inst *Instr = iteratorToInst(Context.getCur());
   Instr->deleteIfDead();
   if (!Instr->isDeleted() && !llvm::isa<InstFakeDef>(Instr) &&
       !llvm::isa<InstFakeUse>(Instr)) {
@@ -473,7 +473,7 @@ void TargetLowering::lowerInst(CfgNode *Node, InstList::iterator Next,
   Context.setNext(Next);
   Context.insert(Instr);
   --Next;
-  assert(&*Next == Instr);
+  assert(iteratorToInst(Next) == Instr);
   Context.setCur(Next);
   lower();
 }
index 88de3d5..dcba5ce 100644 (file)
@@ -79,13 +79,13 @@ public:
   Inst *getNextInst() const {
     if (Next == End)
       return nullptr;
-    return Next;
+    return iteratorToInst(Next);
   }
   Inst *getNextInst(InstList::iterator &Iter) const {
     advanceForward(Iter);
     if (Iter == End)
       return nullptr;
-    return Iter;
+    return iteratorToInst(Iter);
   }
   CfgNode *getNode() const { return Node; }
   bool atEnd() const { return Cur == End; }
index 22d4c0a..f83eb59 100644 (file)
@@ -842,7 +842,7 @@ void TargetARM32::findMaxStackOutArgsSize() {
     Context.init(Node);
     while (!Context.atEnd()) {
       PostIncrLoweringContext PostIncrement(Context);
-      Inst *CurInstr = Context.getCur();
+      Inst *CurInstr = iteratorToInst(Context.getCur());
       if (auto *Call = llvm::dyn_cast<InstCall>(CurInstr)) {
         SizeT OutArgsSizeBytes = getCallStackArgumentsSizeBytes(Call);
         MaxOutArgsSizeBytes = std::max(MaxOutArgsSizeBytes, OutArgsSizeBytes);
@@ -933,7 +933,7 @@ void TargetARM32::materializeGotAddr(CfgNode *Node) {
 
   // The got addr needs to be materialized at the same point where DefGotPtr
   // lives.
-  Context.setInsertPoint(DefGotPtr);
+  Context.setInsertPoint(instToIterator(DefGotPtr));
   assert(DefGotPtr->getSrcSize() == 1);
   auto *T = llvm::cast<Variable>(DefGotPtr->getSrc(0));
   loadNamedConstantRelocatablePIC(Ctx->getGlobalString(GlobalOffsetTable), T,
@@ -2039,7 +2039,7 @@ void TargetARM32::postLowerLegalization() {
     PostLoweringLegalizer Legalizer(this);
     while (!Context.atEnd()) {
       PostIncrLoweringContext PostIncrement(Context);
-      Inst *CurInstr = Context.getCur();
+      Inst *CurInstr = iteratorToInst(Context.getCur());
 
       // Check if the previous TempBaseReg is clobbered, and reset if needed.
       Legalizer.resetTempBaseIfClobberedBy(CurInstr);
@@ -5750,7 +5750,7 @@ OperandARM32Mem *TargetARM32::formAddressingMode(Type Ty, Cfg *Func,
 }
 
 void TargetARM32::doAddressOptLoad() {
-  Inst *Instr = Context.getCur();
+  Inst *Instr = iteratorToInst(Context.getCur());
   assert(llvm::isa<InstLoad>(Instr));
   Variable *Dest = Instr->getDest();
   Operand *Addr = Instr->getSrc(0);
@@ -5912,7 +5912,7 @@ void TargetARM32::lowerStore(const InstStore *Instr) {
 }
 
 void TargetARM32::doAddressOptStore() {
-  Inst *Instr = Context.getCur();
+  Inst *Instr = iteratorToInst(Context.getCur());
   assert(llvm::isa<InstStore>(Instr));
   Operand *Src = Instr->getSrc(0);
   Operand *Addr = Instr->getSrc(1);
index 69498ce..95619a0 100644 (file)
@@ -2970,6 +2970,9 @@ void FunctionValuesymtabParser::setBbName(NaClBcIndexSize_t Index,
 }
 
 bool FunctionParser::ParseBlock(unsigned BlockID) {
+#ifndef PNACL_LLVM
+  constexpr bool PNaClAllowLocalSymbolTables = true;
+#endif // !PNACL_LLVM
   switch (BlockID) {
   case naclbitc::CONSTANTS_BLOCK_ID: {
     ConstantsParser Parser(BlockID, this);
index b07ac47..15fcb96 100644 (file)
@@ -14,6 +14,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#ifdef PNACL_LLVM
+
 #ifdef SZTARGET
 
 #ifndef SUBZERO_TARGET
@@ -29,3 +31,12 @@ SUBZERO_TARGET(SZTARGET)
 #include "llvm/Config/SZTargets.def"
 
 #endif // !SZTARGET
+
+#else // !PNACL_LLVM
+
+SUBZERO_TARGET(ARM32)
+SUBZERO_TARGET(MIPS32)
+SUBZERO_TARGET(X8632)
+SUBZERO_TARGET(X8664)
+
+#endif // !PNACL_LLVM