llvm.org GIT mirror llvm / 7fc162f
Split out the IRReader header and the utility functions it provides into its own library. These functions are bridging between the bitcode reader and the ll parser which are in different libraries. Previously we didn't have any good library to do this, and instead played fast and loose with a "header only" set of interfaces in the Support library. This really doesn't work well as evidenced by the recent attempt to add timing logic to the these routines. As part of this, make them normal functions rather than weird inline functions, and sink the implementation into the library. Also clean up the header to be nice and minimal. This requires updating lots of build system dependencies to specify that the IRReader library is needed, and several source files to not implicitly rely upon the header file to transitively include all manner of other headers. If you are using IRReader.h, this commit will break you (the header moved) and you'll need to also update your library usage to include 'irreader'. I will commit the corresponding change to Clang momentarily. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@177971 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 6 years ago
33 changed file(s) with 205 addition(s) and 137 deletion(s). Raw diff Collapse all Expand all
0 //===---- llvm/IRReader/IRReader.h - Reader for LLVM IR files ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines functions for reading LLVM IR. They support both
10 // Bitcode and Assembly, automatically detecting the input format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IRREADER_IRREADER_H
15 #define LLVM_IRREADER_IRREADER_H
16
17 #include
18
19 namespace llvm {
20
21 class Module;
22 class MemoryBuffer;
23 class SMDiagnostic;
24 class LLVMContext;
25
26 /// If the given MemoryBuffer holds a bitcode image, return a Module for it
27 /// which does lazy deserialization of function bodies. Otherwise, attempt to
28 /// parse it as LLVM Assembly and return a fully populated Module. This
29 /// function *always* takes ownership of the given MemoryBuffer.
30 Module *getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
31 LLVMContext &Context);
32
33 /// If the given file holds a bitcode image, return a Module
34 /// for it which does lazy deserialization of function bodies. Otherwise,
35 /// attempt to parse it as LLVM Assembly and return a fully populated
36 /// Module.
37 Module *getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
38 LLVMContext &Context);
39
40 /// If the given MemoryBuffer holds a bitcode image, return a Module
41 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
42 /// a Module for it. This function *always* takes ownership of the given
43 /// MemoryBuffer.
44 Module *ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, LLVMContext &Context);
45
46 /// If the given file holds a bitcode image, return a Module for it.
47 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
48 /// for it.
49 Module *ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
50 LLVMContext &Context);
51
52 }
53
54 #endif
+0
-112
include/llvm/Support/IRReader.h less more
None //===---- llvm/Support/IRReader.h - Reader for LLVM IR files ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines functions for reading LLVM IR. They support both
10 // Bitcode and Assembly, automatically detecting the input format.
11 //
12 // These functions must be defined in a header file in order to avoid
13 // library dependencies, since they reference both Bitcode and Assembly
14 // functions.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_SUPPORT_IRREADER_H
19 #define LLVM_SUPPORT_IRREADER_H
20
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/Assembly/Parser.h"
23 #include "llvm/Bitcode/ReaderWriter.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/system_error.h"
27
28 namespace llvm {
29
30 /// If the given MemoryBuffer holds a bitcode image, return a Module for it
31 /// which does lazy deserialization of function bodies. Otherwise, attempt to
32 /// parse it as LLVM Assembly and return a fully populated Module. This
33 /// function *always* takes ownership of the given MemoryBuffer.
34 inline Module *getLazyIRModule(MemoryBuffer *Buffer,
35 SMDiagnostic &Err,
36 LLVMContext &Context) {
37 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
38 (const unsigned char *)Buffer->getBufferEnd())) {
39 std::string ErrMsg;
40 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
41 if (M == 0) {
42 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
43 ErrMsg);
44 // ParseBitcodeFile does not take ownership of the Buffer in the
45 // case of an error.
46 delete Buffer;
47 }
48 return M;
49 }
50
51 return ParseAssembly(Buffer, 0, Err, Context);
52 }
53
54 /// If the given file holds a bitcode image, return a Module
55 /// for it which does lazy deserialization of function bodies. Otherwise,
56 /// attempt to parse it as LLVM Assembly and return a fully populated
57 /// Module.
58 inline Module *getLazyIRFileModule(const std::string &Filename,
59 SMDiagnostic &Err,
60 LLVMContext &Context) {
61 OwningPtr File;
62 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
63 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
64 "Could not open input file: " + ec.message());
65 return 0;
66 }
67
68 return getLazyIRModule(File.take(), Err, Context);
69 }
70
71 /// If the given MemoryBuffer holds a bitcode image, return a Module
72 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
73 /// a Module for it. This function *always* takes ownership of the given
74 /// MemoryBuffer.
75 inline Module *ParseIR(MemoryBuffer *Buffer,
76 SMDiagnostic &Err,
77 LLVMContext &Context) {
78 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
79 (const unsigned char *)Buffer->getBufferEnd())) {
80 std::string ErrMsg;
81 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
82 if (M == 0)
83 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
84 ErrMsg);
85 // ParseBitcodeFile does not take ownership of the Buffer.
86 delete Buffer;
87 return M;
88 }
89
90 return ParseAssembly(Buffer, 0, Err, Context);
91 }
92
93 /// If the given file holds a bitcode image, return a Module for it.
94 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
95 /// for it.
96 inline Module *ParseIRFile(const std::string &Filename,
97 SMDiagnostic &Err,
98 LLVMContext &Context) {
99 OwningPtr File;
100 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
101 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
102 "Could not open input file: " + ec.message());
103 return 0;
104 }
105
106 return ParseIR(File.take(), Err, Context);
107 }
108
109 }
110
111 #endif
0 # `Support' and `TableGen' libraries are added on the top-level CMakeLists.txt
11
22 add_subdirectory(IR)
3 add_subdirectory(IRReader)
34 add_subdirectory(CodeGen)
45 add_subdirectory(Bitcode)
56 add_subdirectory(Transforms)
0 add_llvm_library(LLVMIRReader
1 IRReader.cpp
2 )
0 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IRReader/IRReader.h"
10 #include "llvm/ADT/OwningPtr.h"
11 #include "llvm/Assembly/Parser.h"
12 #include "llvm/Bitcode/ReaderWriter.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "llvm/Support/system_error.h"
16
17 using namespace llvm;
18
19 Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
20 LLVMContext &Context) {
21 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
22 (const unsigned char *)Buffer->getBufferEnd())) {
23 std::string ErrMsg;
24 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
25 if (M == 0) {
26 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
27 ErrMsg);
28 // ParseBitcodeFile does not take ownership of the Buffer in the
29 // case of an error.
30 delete Buffer;
31 }
32 return M;
33 }
34
35 return ParseAssembly(Buffer, 0, Err, Context);
36 }
37
38 Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
39 LLVMContext &Context) {
40 OwningPtr File;
41 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
42 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
43 "Could not open input file: " + ec.message());
44 return 0;
45 }
46
47 return getLazyIRModule(File.take(), Err, Context);
48 }
49
50 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
51 LLVMContext &Context) {
52 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
53 (const unsigned char *)Buffer->getBufferEnd())) {
54 std::string ErrMsg;
55 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
56 if (M == 0)
57 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
58 ErrMsg);
59 // ParseBitcodeFile does not take ownership of the Buffer.
60 delete Buffer;
61 return M;
62 }
63
64 return ParseAssembly(Buffer, 0, Err, Context);
65 }
66
67 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
68 LLVMContext &Context) {
69 OwningPtr File;
70 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
71 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
72 "Could not open input file: " + ec.message());
73 return 0;
74 }
75
76 return ParseIR(File.take(), Err, Context);
77 }
0 ;===- ./lib/AsmParser/LLVMBuild.txt ----------------------------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [component_0]
18 type = Library
19 name = IRReader
20 parent = Libraries
21 required_libraries = AsmParser BitReader Core Support
0 ##===- lib/IRReader/Makefile -------------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 LIBRARYNAME := LLVMIRReader
11 BUILD_ARCHIVE = 1
12
13 include $(LEVEL)/Makefile.common
1515 ;===------------------------------------------------------------------------===;
1616
1717 [common]
18 subdirectories = Analysis Archive AsmParser Bitcode CodeGen DebugInfo ExecutionEngine Linker IR MC Object Option Support TableGen Target Transforms
18 subdirectories = Analysis Archive AsmParser Bitcode CodeGen DebugInfo ExecutionEngine Linker IR IRReader MC Object Option Support TableGen Target Transforms
1919
2020 [component_0]
2121 type = Group
1010 include $(LEVEL)/Makefile.config
1111
1212 PARALLEL_DIRS := IR AsmParser Bitcode Archive Analysis Transforms CodeGen \
13 Target ExecutionEngine Linker MC Object Option DebugInfo
13 Target ExecutionEngine Linker MC Object Option DebugInfo \
14 IRReader
1415
1516 include $(LEVEL)/Makefile.common
1617
1515 #include "BugDriver.h"
1616 #include "ToolRunner.h"
1717 #include "llvm/IR/Module.h"
18 #include "llvm/IRReader/IRReader.h"
1819 #include "llvm/Linker.h"
1920 #include "llvm/Pass.h"
2021 #include "llvm/Support/CommandLine.h"
2122 #include "llvm/Support/FileUtilities.h"
2223 #include "llvm/Support/Host.h"
23 #include "llvm/Support/IRReader.h"
2424 #include "llvm/Support/SourceMgr.h"
2525 #include "llvm/Support/raw_ostream.h"
2626 #include
0 set(LLVM_LINK_COMPONENTS asmparser instrumentation scalaropts ipo
1 linker bitreader bitwriter vectorize objcarcopts)
1 linker bitreader bitwriter irreader vectorize objcarcopts)
22
33 add_llvm_tool(bugpoint
44 BugDriver.cpp
1818 type = Tool
1919 name = bugpoint
2020 parent = Tools
21 required_libraries = AsmParser BitReader BitWriter IPO Instrumentation Linker Scalar ObjCARC
21 required_libraries = AsmParser BitReader BitWriter IRReader IPO Instrumentation Linker Scalar ObjCARC
None set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} bitreader asmparser)
0 set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} bitreader asmparser irreader)
11
22 add_llvm_tool(llc
33 llc.cpp
1818 type = Tool
1919 name = llc
2020 parent = Tools
21 required_libraries = AsmParser BitReader all-targets
21 required_libraries = AsmParser BitReader IRReader all-targets
2020 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
2121 #include "llvm/IR/DataLayout.h"
2222 #include "llvm/IR/Module.h"
23 #include "llvm/IRReader/IRReader.h"
2324 #include "llvm/MC/SubtargetFeature.h"
2425 #include "llvm/Pass.h"
2526 #include "llvm/PassManager.h"
2728 #include "llvm/Support/Debug.h"
2829 #include "llvm/Support/FormattedStream.h"
2930 #include "llvm/Support/Host.h"
30 #include "llvm/Support/IRReader.h"
3131 #include "llvm/Support/ManagedStatic.h"
3232 #include "llvm/Support/PluginLoader.h"
3333 #include "llvm/Support/PrettyStackTrace.h"
3434 #include "llvm/Support/Signals.h"
35 #include "llvm/Support/SourceMgr.h"
3536 #include "llvm/Support/TargetRegistry.h"
3637 #include "llvm/Support/TargetSelect.h"
3738 #include "llvm/Support/ToolOutputFile.h"
0
1 set(LLVM_LINK_COMPONENTS mcjit jit interpreter nativecodegen bitreader asmparser selectiondag native)
1 set(LLVM_LINK_COMPONENTS mcjit jit interpreter nativecodegen bitreader asmparser irreader selectiondag native)
22
33 if( LLVM_USE_OPROFILE )
44 set(LLVM_LINK_COMPONENTS
1818 type = Tool
1919 name = lli
2020 parent = Tools
21 required_libraries = AsmParser BitReader Interpreter JIT MCJIT NativeCodeGen SelectionDAG Native
21 required_libraries = AsmParser BitReader IRReader Interpreter JIT MCJIT NativeCodeGen SelectionDAG Native
2828 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2929 #include "llvm/IR/Module.h"
3030 #include "llvm/IR/Type.h"
31 #include "llvm/IRReader/IRReader.h"
3132 #include "llvm/Support/CommandLine.h"
3233 #include "llvm/Support/Debug.h"
3334 #include "llvm/Support/DynamicLibrary.h"
3435 #include "llvm/Support/Format.h"
35 #include "llvm/Support/IRReader.h"
3636 #include "llvm/Support/ManagedStatic.h"
3737 #include "llvm/Support/MathExtras.h"
3838 #include "llvm/Support/Memory.h"
4141 #include "llvm/Support/PrettyStackTrace.h"
4242 #include "llvm/Support/Process.h"
4343 #include "llvm/Support/Signals.h"
44 #include "llvm/Support/SourceMgr.h"
4445 #include "llvm/Support/TargetSelect.h"
4546 #include "llvm/Support/raw_ostream.h"
4647 #include
None set(LLVM_LINK_COMPONENTS support asmparser bitreader)
0 set(LLVM_LINK_COMPONENTS support asmparser bitreader irreader)
11
22 add_llvm_tool(llvm-diff
33 llvm-diff.cpp
1818 type = Tool
1919 name = llvm-diff
2020 parent = Tools
21 required_libraries = AsmParser BitReader
21 required_libraries = AsmParser BitReader IRReader
1818 #include "llvm/IR/LLVMContext.h"
1919 #include "llvm/IR/Module.h"
2020 #include "llvm/IR/Type.h"
21 #include "llvm/IRReader/IRReader.h"
2122 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/IRReader.h"
2323 #include "llvm/Support/MemoryBuffer.h"
2424 #include "llvm/Support/SourceMgr.h"
2525 #include "llvm/Support/raw_ostream.h"
None set(LLVM_LINK_COMPONENTS asmparser ipo bitreader bitwriter)
0 set(LLVM_LINK_COMPONENTS asmparser ipo bitreader bitwriter irreader)
11
22 add_llvm_tool(llvm-extract
33 llvm-extract.cpp
1818 type = Tool
1919 name = llvm-extract
2020 parent = Tools
21 required_libraries = AsmParser BitReader BitWriter IPO
21 required_libraries = AsmParser BitReader BitWriter IRReader IPO
1818 #include "llvm/Bitcode/ReaderWriter.h"
1919 #include "llvm/IR/DataLayout.h"
2020 #include "llvm/IR/Module.h"
21 #include "llvm/IRReader/IRReader.h"
2122 #include "llvm/PassManager.h"
2223 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/IRReader.h"
2424 #include "llvm/Support/ManagedStatic.h"
2525 #include "llvm/Support/PrettyStackTrace.h"
2626 #include "llvm/Support/Regex.h"
2727 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/SourceMgr.h"
2829 #include "llvm/Support/SystemUtils.h"
2930 #include "llvm/Support/ToolOutputFile.h"
3031 #include "llvm/Transforms/IPO.h"
88 debuginfo
99 inteljitevents
1010 interpreter
11 irreader
1112 jit
1213 mcjit
1314 nativecodegen
1818 type = Tool
1919 name = llvm-jitlistener
2020 parent = Tools
21 required_libraries = AsmParser BitReader Interpreter JIT MCJIT NativeCodeGen Object SelectionDAG Native
21 required_libraries = AsmParser BitReader IRReader Interpreter JIT MCJIT NativeCodeGen Object SelectionDAG Native
2121 #include "llvm/ExecutionEngine/MCJIT.h"
2222 #include "llvm/ExecutionEngine/ObjectImage.h"
2323 #include "llvm/IR/Module.h"
24 #include "llvm/IRReader/IRReader.h"
2425 #include "llvm/Support/CommandLine.h"
2526 #include "llvm/Support/Host.h"
26 #include "llvm/Support/IRReader.h"
2727 #include "llvm/Support/ManagedStatic.h"
2828 #include "llvm/Support/MemoryBuffer.h"
2929 #include "llvm/Support/PrettyStackTrace.h"
None set(LLVM_LINK_COMPONENTS linker bitreader bitwriter asmparser)
0 set(LLVM_LINK_COMPONENTS linker bitreader bitwriter asmparser irreader)
11
22 add_llvm_tool(llvm-link
33 llvm-link.cpp
1818 type = Tool
1919 name = llvm-link
2020 parent = Tools
21 required_libraries = AsmParser BitReader BitWriter Linker
21 required_libraries = AsmParser BitReader BitWriter IRReader Linker
1616 #include "llvm/Bitcode/ReaderWriter.h"
1717 #include "llvm/IR/LLVMContext.h"
1818 #include "llvm/IR/Module.h"
19 #include "llvm/IRReader/IRReader.h"
1920 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/IRReader.h"
2121 #include "llvm/Support/ManagedStatic.h"
2222 #include "llvm/Support/Path.h"
2323 #include "llvm/Support/PrettyStackTrace.h"
2424 #include "llvm/Support/Signals.h"
25 #include "llvm/Support/SourceMgr.h"
2526 #include "llvm/Support/SystemUtils.h"
2627 #include "llvm/Support/ToolOutputFile.h"
2728 #include
None set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} bitreader asmparser bitwriter instrumentation scalaropts objcarcopts ipo vectorize)
0 set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} bitreader asmparser bitwriter irreader instrumentation scalaropts objcarcopts ipo vectorize)
11
22 add_llvm_tool(opt
33 AnalysisWrappers.cpp
1818 type = Tool
1919 name = opt
2020 parent = Tools
21 required_libraries = AsmParser BitReader BitWriter IPO Instrumentation Scalar ObjCARC all-targets
21 required_libraries = AsmParser BitReader BitWriter IRReader IPO Instrumentation Scalar ObjCARC all-targets
2525 #include "llvm/DebugInfo.h"
2626 #include "llvm/IR/DataLayout.h"
2727 #include "llvm/IR/Module.h"
28 #include "llvm/IRReader/IRReader.h"
2829 #include "llvm/LinkAllIR.h"
2930 #include "llvm/LinkAllPasses.h"
3031 #include "llvm/MC/SubtargetFeature.h"
3132 #include "llvm/PassManager.h"
3233 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/IRReader.h"
3434 #include "llvm/Support/ManagedStatic.h"
3535 #include "llvm/Support/PassNameParser.h"
3636 #include "llvm/Support/PluginLoader.h"
3737 #include "llvm/Support/PrettyStackTrace.h"
3838 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/SourceMgr.h"
3940 #include "llvm/Support/SystemUtils.h"
4041 #include "llvm/Support/TargetRegistry.h"
4142 #include "llvm/Support/TargetSelect.h"