LLVM: lib/Target/SPIRV/SPIRVAPI.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

34#include

35#include

36#include

37#include

38

39using namespace llvm;

40

41namespace {

42

43

44

48

49

50void parseSPIRVCommandLineOptions(const std::vectorstd::string &Options,

52 static constexpr const char *Origin = "SPIRVTranslateModule";

54 std::vector<const char *> Argv(1, Origin);

55 for (const auto &Arg : Options)

56 Argv.push_back(Arg.c_str());

58 }

59}

60

61std::once_flag InitOnceFlag;

62void InitializeSPIRVTarget() {

63 std::call_once(InitOnceFlag, []() {

68 });

69}

70}

71

72namespace llvm {

73

74

75

76

77

78

81 const std::vectorstd::string &AllowExtNames,

82 const std::vectorstd::string &Opts) {

83

84 static const std::string DefaultTriple = "spirv64-unknown-unknown";

85 static const std::string DefaultMArch = "";

86

87

88 std::string Errors;

90 parseSPIRVCommandLineOptions(Opts, &ErrorStream);

91 if (!Errors.empty()) {

92 ErrMsg = Errors;

93 return false;

94 }

95

98 OLevel = *Level;

99 } else {

100 ErrMsg = "Invalid optimization level!";

101 return false;

102 }

103

104

105

106 std::setSPIRV::Extension::Extension AllowedExtIds;

109 if (!UnknownExt.empty()) {

110 ErrMsg = "Unknown SPIR-V extension: " + UnknownExt.str();

111 return false;

112 }

114

115

116 InitializeSPIRVTarget();

117

118 Triple TargetTriple(SpvTargetTriple.empty()

119 ? M->getTargetTriple()

121 if (TargetTriple.getTriple().empty()) {

122 TargetTriple.setTriple(DefaultTriple);

123 M->setTargetTriple(DefaultTriple);

124 }

125 const Target *TheTarget =

127 if (!TheTarget)

128 return false;

129

130

131

132

133

135 std::optionalReloc::Model RM;

136 std::optionalCodeModel::Model CM;

137 std::unique_ptr Target =

141 ErrMsg = "Could not allocate target machine!";

142 return false;

143 }

144

145 if (M->getCodeModel())

146 Target->setCodeModel(*M->getCodeModel());

147

148 std::string DLStr = M->getDataLayoutStr();

150 DLStr.empty() ? Target->createDataLayout().getStringRepresentation()

151 : DLStr);

152 if (!MaybeDL) {

154 return false;

155 }

156 M->setDataLayout(MaybeDL.get());

157

165

168 if (Target->addPassesToEmitFile(PM, OutStream, nullptr,

170 ErrMsg = "Target machine cannot emit a file of this type";

171 return false;

172 }

173

174 PM.run(*M);

175 SpirvObj = OutBuffer.str();

176

177 return true;

178}

179

180}

#define LLVM_EXTERNAL_VISIBILITY

Module.h This file contains the declarations for the Module class.

LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVAsmPrinter()

LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetMC()

LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetInfo()

LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget()

Target-Independent Code Generator Pass Configuration Options pass.

static Expected< DataLayout > parse(StringRef LayoutString)

Parse a data layout string and return the layout.

Tagged union holding either a T or a Error.

Error takeError()

Take ownership of the stored error.

reference get()

Returns a reference to the stored T value.

MachineModuleInfo & getMMI()

const MCContext & getContext() const

A Module instance is used to store all the information related to an LLVM module.

static void addExtensionsToClOpt(const std::set< SPIRV::Extension::Extension > &AllowList)

SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...

StringRef str() const

Explicit conversion to StringRef.

StringRef - Represent a constant reference to a string, i.e.

std::string str() const

str - Get the contents as an std::string.

constexpr bool empty() const

empty - Check if the string is empty.

Implementation of the target library information.

Target - Wrapper for Target specific information.

TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM=std::nullopt, CodeGenOptLevel OL=CodeGenOptLevel::Default, bool JIT=false) const

createTargetMachine - Create a target specific machine implementation for the specified Triple.

Triple - Helper class for working with autoconf configuration names.

void setTriple(const Twine &Str)

Set all components to the new triple Str.

static std::string normalize(StringRef Str, CanonicalForm Form=CanonicalForm::ANY)

Turn an arbitrary machine specification into the canonical triple form (or something sensible that th...

const std::string & getTriple() const

PassManager manages ModulePassManagers.

void add(Pass *P) override

Add a pass to the queue of passes to run.

bool run(Module &M)

run - Execute all of the passes scheduled for execution.

This class implements an extremely fast bulk output stream that can only output to a stream.

A raw_ostream that writes to an std::string.

A raw_ostream that writes to an SmallVector or SmallString.

std::optional< CodeGenOptLevel > parseLevel(char C)

Parse C as a single digit integer and get matching CodeGenLevel.

bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", raw_ostream *Errs=nullptr, const char *EnvVar=nullptr, bool LongOptionsUseDoubleDash=false)

initializer< Ty > init(const Ty &Val)

This is an optimization pass for GlobalISel generic memory operations.

CodeGenOptLevel

Code generation optimization level.

LLVM_EXTERNAL_VISIBILITY bool SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg, const std::vector< std::string > &AllowExtNames, const std::vector< std::string > &Opts)

const char * toString(DWARFSectionKind Kind)

static llvm::StringRef checkExtensions(const std::vector< std::string > &ExtNames, std::set< SPIRV::Extension::Extension > &AllowedExtensions)

Validates and converts extension names into internal enum values.

static const Target * lookupTarget(StringRef Triple, std::string &Error)

lookupTarget - Lookup a target based on a target triple.