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

49

50

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

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

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

56 for (const auto &Arg : Options)

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

59 }

60}

61

62std::once_flag InitOnceFlag;

63void InitializeSPIRVTarget() {

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

69 });

70}

71}

72

73namespace llvm {

74

75

76

77

78

79

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

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

84

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

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

87

88

89 std::string Errors;

91 parseSPIRVCommandLineOptions(Opts, &ErrorStream);

92 if (!Errors.empty()) {

93 ErrMsg = Errors;

94 return false;

95 }

96

99 OLevel = *Level;

100 } else {

101 ErrMsg = "Invalid optimization level!";

102 return false;

103 }

104

105

106

107 std::setSPIRV::Extension::Extension AllowedExtIds;

110 if (!UnknownExt.empty()) {

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

112 return false;

113 }

115

116

117 InitializeSPIRVTarget();

118

119 Triple TargetTriple(SpirvTargetTriple.empty()

120 ? M->getTargetTriple()

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

123 TargetTriple.setTriple(DefaultTriple);

124 M->setTargetTriple(DefaultTriple);

125 }

126 const Target *TheTarget =

128 if (!TheTarget)

129 return false;

130

131

132

133

134

136 std::optionalReloc::Model RM;

137 std::optionalCodeModel::Model CM;

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

161 std::unique_ptr MMIWP(

164 ->Initialize(MMIWP.get()->getMMI().getContext(), *Target);

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.

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.