Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: src/IceConverter.cpp

Issue 350933002: Refactor llvm2ice so that Ice can be built while reading bitcode. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix issues in patch set 2. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- subzero/src/llvm2ice.cpp - Driver for testing ----------------------===// 1 //===- subzero/src/IceConverter.cpp - Converts LLVM to Ice ---------------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file defines a driver that uses LLVM capabilities to parse a 10 // This file implements the LLVM to ICE converter.
11 // bitcode file and build the LLVM IR, and then convert the LLVM basic
12 // blocks, instructions, and operands into their Subzero equivalents.
13 // 11 //
14 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
15 13
14 #include "IceConverter.h"
15
16 #include "IceCfg.h" 16 #include "IceCfg.h"
17 #include "IceCfgNode.h" 17 #include "IceCfgNode.h"
18 #include "IceDefs.h" 18 #include "IceDefs.h"
19 #include "IceGlobalContext.h" 19 #include "IceGlobalContext.h"
20 #include "IceInst.h" 20 #include "IceInst.h"
21 #include "IceOperand.h" 21 #include "IceOperand.h"
22 #include "IceTargetLowering.h" 22 #include "IceTargetLowering.h"
23 #include "IceTypes.h" 23 #include "IceTypes.h"
24 24
25 #include "llvm/IR/Constant.h" 25 #include "llvm/IR/Constant.h"
26 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Instruction.h" 28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h" 29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/LLVMContext.h" 30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Module.h" 31 #include "llvm/IR/Module.h"
32 #include "llvm/IRReader/IRReader.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_os_ostream.h"
36 #include "llvm/Support/SourceMgr.h"
37 32
38 #include <fstream>
39 #include <iostream> 33 #include <iostream>
40 34
41 using namespace llvm; 35 using namespace llvm;
42 36
37 namespace {
38
43 // Debugging helper 39 // Debugging helper
44 template <typename T> static std::string LLVMObjectAsString(const T *O) { 40 template <typename T> static std::string LLVMObjectAsString(const T *O) {
45 std::string Dump; 41 std::string Dump;
46 raw_string_ostream Stream(Dump); 42 raw_string_ostream Stream(Dump);
47 O->print(Stream); 43 O->print(Stream);
48 return Stream.str(); 44 return Stream.str();
49 } 45 }
50 46
51 // Converter from LLVM to ICE. The entry point is the convertFunction method. 47 // Converter from LLVM to ICE. The entry point is the convertFunction method.
52 // 48 //
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 private: 609 private:
614 // Data 610 // Data
615 Ice::GlobalContext *Ctx; 611 Ice::GlobalContext *Ctx;
616 Ice::Cfg *Func; 612 Ice::Cfg *Func;
617 Ice::CfgNode *CurrentNode; 613 Ice::CfgNode *CurrentNode;
618 Ice::Type SubzeroPointerType; 614 Ice::Type SubzeroPointerType;
619 std::map<const Value *, Ice::Variable *> VarMap; 615 std::map<const Value *, Ice::Variable *> VarMap;
620 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; 616 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap;
621 }; 617 };
622 618
623 static cl::list<Ice::VerboseItem> VerboseList( 619 }
624 "verbose", cl::CommaSeparated,
625 cl::desc("Verbose options (can be comma-separated):"),
626 cl::values(
627 clEnumValN(Ice::IceV_Instructions, "inst", "Print basic instructions"),
628 clEnumValN(Ice::IceV_Deleted, "del", "Include deleted instructions"),
629 clEnumValN(Ice::IceV_InstNumbers, "instnum",
630 "Print instruction numbers"),
631 clEnumValN(Ice::IceV_Preds, "pred", "Show predecessors"),
632 clEnumValN(Ice::IceV_Succs, "succ", "Show successors"),
633 clEnumValN(Ice::IceV_Liveness, "live", "Liveness information"),
634 clEnumValN(Ice::IceV_RegManager, "rmgr", "Register manager status"),
635 clEnumValN(Ice::IceV_RegOrigins, "orig", "Physical register origins"),
636 clEnumValN(Ice::IceV_LinearScan, "regalloc", "Linear scan details"),
637 clEnumValN(Ice::IceV_Frame, "frame", "Stack frame layout details"),
638 clEnumValN(Ice::IceV_Timing, "time", "Pass timing details"),
639 clEnumValN(Ice::IceV_All, "all", "Use all verbose options"),
640 clEnumValN(Ice::IceV_None, "none", "No verbosity"), clEnumValEnd));
641 static cl::opt<Ice::TargetArch> TargetArch(
642 "target", cl::desc("Target architecture:"), cl::init(Ice::Target_X8632),
643 cl::values(
644 clEnumValN(Ice::Target_X8632, "x8632", "x86-32"),
645 clEnumValN(Ice::Target_X8632, "x86-32", "x86-32 (same as x8632)"),
646 clEnumValN(Ice::Target_X8632, "x86_32", "x86-32 (same as x8632)"),
647 clEnumValN(Ice::Target_X8664, "x8664", "x86-64"),
648 clEnumValN(Ice::Target_X8664, "x86-64", "x86-64 (same as x8664)"),
649 clEnumValN(Ice::Target_X8664, "x86_64", "x86-64 (same as x8664)"),
650 clEnumValN(Ice::Target_ARM32, "arm", "arm32"),
651 clEnumValN(Ice::Target_ARM32, "arm32", "arm32 (same as arm)"),
652 clEnumValN(Ice::Target_ARM64, "arm64", "arm64"), clEnumValEnd));
653 static cl::opt<Ice::OptLevel>
654 OptLevel(cl::desc("Optimization level"), cl::init(Ice::Opt_m1),
655 cl::value_desc("level"),
656 cl::values(clEnumValN(Ice::Opt_m1, "Om1", "-1"),
657 clEnumValN(Ice::Opt_m1, "O-1", "-1"),
658 clEnumValN(Ice::Opt_0, "O0", "0"),
659 clEnumValN(Ice::Opt_1, "O1", "1"),
660 clEnumValN(Ice::Opt_2, "O2", "2"), clEnumValEnd));
661 static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"),
662 cl::init("-"));
663 static cl::opt<std::string> OutputFilename("o",
664 cl::desc("Override output filename"),
665 cl::init("-"),
666 cl::value_desc("filename"));
667 static cl::opt<std::string> LogFilename("log", cl::desc("Set log filename"),
668 cl::init("-"),
669 cl::value_desc("filename"));
670 static cl::opt<std::string>
671 TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"),
672 cl::init(""), cl::value_desc("prefix"));
673 static cl::opt<bool>
674 DisableInternal("external",
675 cl::desc("Disable 'internal' linkage type for testing"));
676 static cl::opt<bool>
677 DisableTranslation("notranslate", cl::desc("Disable Subzero translation"));
678 620
679 static cl::opt<bool> SubzeroTimingEnabled( 621 int Llvm2Ice::Converter::convertToIce(llvm::Module *Mod) {
680 "timing", cl::desc("Enable breakdown timing of Subzero translation"));
681
682 static cl::opt<NaClFileFormat> InputFileFormat(
683 "bitcode-format", cl::desc("Define format of input file:"),
684 cl::values(clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"),
685 clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"),
686 clEnumValEnd),
687 cl::init(LLVMFormat));
688
689 int main(int argc, char **argv) {
690 int ExitStatus = 0; 622 int ExitStatus = 0;
691 623
692 cl::ParseCommandLineOptions(argc, argv);
693
694 // Parse the input LLVM IR file into a module.
695 SMDiagnostic Err;
696 Module *Mod;
697
698 {
699 Ice::Timer T;
700 Mod = NaClParseIRFile(IRFilename, InputFileFormat, Err, getGlobalContext());
701
702 if (SubzeroTimingEnabled) {
703 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec()
704 << " sec\n";
705 }
706 }
707
708 if (!Mod) {
709 Err.print(argv[0], errs());
710 return 1;
711 }
712
713 Ice::VerboseMask VMask = Ice::IceV_None;
714 for (unsigned i = 0; i != VerboseList.size(); ++i)
715 VMask |= VerboseList[i];
716
717 std::ofstream Ofs;
718 if (OutputFilename != "-") {
719 Ofs.open(OutputFilename.c_str(), std::ofstream::out);
720 }
721 raw_os_ostream *Os =
722 new raw_os_ostream(OutputFilename == "-" ? std::cout : Ofs);
723 Os->SetUnbuffered();
724 std::ofstream Lfs;
725 if (LogFilename != "-") {
726 Lfs.open(LogFilename.c_str(), std::ofstream::out);
727 }
728 raw_os_ostream *Ls = new raw_os_ostream(LogFilename == "-" ? std::cout : Lfs);
729 Ls->SetUnbuffered();
730
731 // Ideally, Func would be declared inside the loop and its object 624 // Ideally, Func would be declared inside the loop and its object
732 // would be automatically deleted at the end of the loop iteration. 625 // would be automatically deleted at the end of the loop iteration.
733 // However, emitting the constant pool requires a valid Cfg object, 626 // However, emitting the constant pool requires a valid Cfg object,
734 // so we need to defer deleting the last non-empty Cfg object until 627 // so we need to defer deleting the last non-empty Cfg object until
735 // outside the loop and after emitting the constant pool. TODO: 628 // outside the loop and after emitting the constant pool. TODO:
736 // Since all constants are globally pooled in the Ice::GlobalContext 629 // Since all constants are globally pooled in the Ice::GlobalContext
737 // object, change all Ice::Constant related functions to use 630 // object, change all Ice::Constant related functions to use
738 // GlobalContext instead of Cfg, and then clean up this loop. 631 // GlobalContext instead of Cfg, and then clean up this loop.
739 OwningPtr<Ice::Cfg> Func; 632 OwningPtr<Ice::Cfg> Func;
740 Ice::GlobalContext Ctx(Ls, Os, VMask, TargetArch, OptLevel, TestPrefix);
741 633
742 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { 634 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) {
743 if (I->empty()) 635 if (I->empty())
744 continue; 636 continue;
745 LLVM2ICEConverter FunctionConverter(&Ctx); 637 LLVM2ICEConverter FunctionConverter(Ctx);
746 638
747 Ice::Timer TConvert; 639 Ice::Timer TConvert;
748 Func.reset(FunctionConverter.convertFunction(I)); 640 Func.reset(FunctionConverter.convertFunction(I));
749 if (DisableInternal) 641 if (DisableInternal)
750 Func->setInternal(false); 642 Func->setInternal(false);
751 643
752 if (SubzeroTimingEnabled) { 644 if (SubzeroTimingEnabled) {
753 std::cerr << "[Subzero timing] Convert function " 645 std::cerr << "[Subzero timing] Convert function "
754 << Func->getFunctionName() << ": " << TConvert.getElapsedSec() 646 << Func->getFunctionName() << ": " << TConvert.getElapsedSec()
755 << " sec\n"; 647 << " sec\n";
(...skipping 22 matching lines...) Expand all
778 << " sec\n"; 670 << " sec\n";
779 } 671 }
780 } 672 }
781 } 673 }
782 674
783 if (!DisableTranslation && Func) 675 if (!DisableTranslation && Func)
784 Func->getTarget()->emitConstants(); 676 Func->getTarget()->emitConstants();
785 677
786 return ExitStatus; 678 return ExitStatus;
787 } 679 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698