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

1

2

3

4

5

6

7

8

29#include

30#include

31#include

32

33using namespace llvm;

34

35namespace {

36

37std::once_flag InitOnceFlag;

38void InitializeSPIRVTarget() {

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

43 LLVMInitializeSPIRVAsmPrinter();

44 });

45}

46}

47

48namespace llvm {

49

50

51

52

53

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

58

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

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

61

62 std::setSPIRV::Extension::Extension AllowedExtIds;

65 if (!UnknownExt.empty()) {

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

67 return false;

68 }

69

70

71 InitializeSPIRVTarget();

72

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

74 TargetTriple.setTriple(DefaultTriple);

75 M->setTargetTriple(TargetTriple);

76 }

77 const Target *TheTarget =

79 if (!TheTarget)

80 return false;

81

82

83

84

85

87 std::optionalReloc::Model RM;

88 std::optionalCodeModel::Model CM;

90 TargetTriple, "", "", Options, RM, CM, OLevel));

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

93 return false;

94 }

95

96

100

101 if (M->getCodeModel())

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

103

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

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

107 : DLStr);

108 if (!MaybeDL) {

110 return false;

111 }

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

113

117 std::unique_ptr MMIWP(

119 Target->getObjFileLowering()->Initialize(MMIWP->getMMI().getContext(),

121

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

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

127 return false;

128 }

129

130 PM.run(*M);

131 SpirvObj = OutBuffer.str();

132

133 return true;

134}

135

136

137

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

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

142

143

144 Triple TargetTriple = Opts.empty() || Opts[0].empty()

145 ? M->getTargetTriple()

147

148

150 if (Opts.size() > 1 && !Opts[1].empty()) {

152 OLevel = *Level;

153 } else {

154 ErrMsg = "Invalid optimization level!";

155 return false;

156 }

157 }

158 return SPIRVTranslate(M, SpirvObj, ErrMsg, AllowExtNames, OLevel,

159 std::move(TargetTriple));

160}

161

162}

#define LLVM_EXTERNAL_VISIBILITY

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

LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetMC()

LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetInfo()

LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget()

Target-Independent Code Generator Pass Configuration Options pass.

static LLVM_ABI 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.

void initAvailableExtensions(const std::set< SPIRV::Extension::Extension > &AllowedExtIds)

const SPIRVSubtarget * getSubtargetImpl() const

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(const Triple &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.

LLVM_ABI void setTriple(const Twine &Str)

Set all components to the new triple Str.

static LLVM_ABI 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.

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.

This is an optimization pass for GlobalISel generic memory operations.

LLVM_EXTERNAL_VISIBILITY bool SPIRVTranslate(Module *M, std::string &SpirvObj, std::string &ErrMsg, const std::vector< std::string > &AllowExtNames, llvm::CodeGenOptLevel OLevel, Triple TargetTriple)

Definition SPIRVAPI.cpp:55

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)

Definition SPIRVAPI.cpp:139

std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)

static 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 TripleStr, std::string &Error)

lookupTarget - Lookup a target based on a target triple.