source: icGREP/icgrep-devel/icgrep/IR_Gen/llvm2ptx.h @ 5463

Last change on this file since 5463 was 5463, checked in by lindanl, 2 years ago

Some fixes.

File size: 3.9 KB
Line 
1#ifndef LLVM2PTX_H
2#define LLVM2PTX_H
3
4#include "llvm/Analysis/TargetLibraryInfo.h"
5#include "llvm/CodeGen/CommandFlags.h"
6#include "llvm/CodeGen/MIRParser/MIRParser.h"
7#include "llvm/IR/LegacyPassManager.h"
8#include "llvm/IR/Module.h"
9#include "llvm/IR/Verifier.h"
10#include "llvm/IRReader/IRReader.h"
11#include "llvm/Support/CommandLine.h"
12#include "llvm/Support/FileSystem.h"
13#include "llvm/Support/SourceMgr.h"
14#include "llvm/Support/TargetRegistry.h"
15#include "llvm/Support/TargetSelect.h"
16#include "llvm/Support/ToolOutputFile.h"
17#include "llvm/Target/TargetMachine.h"
18#include <memory>
19using namespace llvm;
20
21static int llvm2ptx(Module * M, std::string PTXFilename) {
22
23  LLVMContext Context;
24  SMDiagnostic Err;
25  std::unique_ptr<MIRParser> MIR;
26  Triple TheTriple;
27
28  TheTriple = Triple(M->getTargetTriple());
29
30  if (TheTriple.getTriple().empty())
31    TheTriple.setTriple(sys::getDefaultTargetTriple());
32
33  // Get the target specific parser.
34  std::string Error;
35  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
36                                                         Error);
37  if (!TheTarget) {
38    errs() << Error;
39    return 1;
40  }
41
42  std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
43
44  CodeGenOpt::Level OLvl = CodeGenOpt::Default;
45
46  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
47
48  std::unique_ptr<TargetMachine> Target(
49      TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, FeaturesStr,
50                                     Options, RelocModel, CMModel, OLvl));
51
52  assert(Target && "Could not allocate target machine!");
53
54  // Figure out where we are going to send the output.
55  std::error_code EC;
56  sys::fs::OpenFlags OpenFlags = sys::fs::F_None | sys::fs::F_Text;
57  std::unique_ptr<tool_output_file> Out = llvm::make_unique<tool_output_file>(PTXFilename, EC, OpenFlags);
58  if (EC) {
59    errs() << EC.message() << '\n';
60    return 1;
61  }
62
63  // Build up all of the passes that we want to do to the module.
64  legacy::PassManager PM;
65
66  // Add an appropriate TargetLibraryInfo pass for the module's triple.
67  TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
68
69  PM.add(new TargetLibraryInfoWrapperPass(TLII));
70
71  // Add the target data from the target machine, if it exists, or the module.
72  M->setDataLayout(Target->createDataLayout());
73
74  // Override function attributes based on CPUStr, FeaturesStr, and command line
75  // flags.
76  setFunctionAttributes(CPUStr, FeaturesStr, *M);
77
78  {
79    raw_pwrite_stream *OS = &Out->os();
80
81    AnalysisID StartBeforeID = nullptr;
82    AnalysisID StartAfterID = nullptr;
83    AnalysisID StopAfterID = nullptr;
84    const PassRegistry *PR = PassRegistry::getPassRegistry();
85    if (!RunPass.empty()) {
86      if (!StartAfter.empty() || !StopAfter.empty()) {
87        errs() << "start-after and/or stop-after passes are redundant when run-pass is specified.\n";
88        return 1;
89      }
90      const PassInfo *PI = PR->getPassInfo(RunPass);
91      if (!PI) {
92        errs() << "run-pass pass is not registered.\n";
93        return 1;
94      }
95      StopAfterID = StartBeforeID = PI->getTypeInfo();
96    } else {
97      if (!StartAfter.empty()) {
98        const PassInfo *PI = PR->getPassInfo(StartAfter);
99        if (!PI) {
100          errs() << "start-after pass is not registered.\n";
101          return 1;
102        }
103        StartAfterID = PI->getTypeInfo();
104      }
105      if (!StopAfter.empty()) {
106        const PassInfo *PI = PR->getPassInfo(StopAfter);
107        if (!PI) {
108          errs() << "stop-after pass is not registered.\n";
109          return 1;
110        }
111        StopAfterID = PI->getTypeInfo();
112      }
113    }
114
115    // Ask the target to add backend passes as necessary.
116    if (Target->addPassesToEmitFile(PM, *OS, FileType, false, StartBeforeID,
117                                    StartAfterID, StopAfterID, MIR.get())) {
118      errs() << " target does not support generation of this file type!\n";
119      return 1;
120    }
121
122    PM.run(*M);
123  }
124  // Declare success.
125  Out->keep();
126
127  return 0;
128}
129
130#endif
Note: See TracBrowser for help on using the repository browser.