30 "spirv-preserve-auxdata",
31 cl::desc(
"Preserve LLVM attributes and metadata as "
32 "NonSemantic.AuxData ExtInst annotations (requires "
33 "SPV_KHR_non_semantic_info)"),
38 AvailableExternally = 0,
41constexpr unsigned NonSemanticAuxDataSet =
42 static_cast<unsigned>(SPIRV::InstructionSet::NonSemantic_AuxData);
46 return F->getAttributes().getFnAttrs();
62 LinkagePreservedGOs.push_back(&GO);
71 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_non_semantic_info)) {
74 "SPV_KHR_non_semantic_info extension to be enabled.");
83SPIRVAuxDataHandler::getOrEmitString(
StringRef S,
85 auto [It, Inserted] = StringRegs.try_emplace(S);
98void SPIRVAuxDataHandler::collectAttributesFor(
const GlobalObject *GO,
103 if (
A.isStringAttribute() &&
109 if (
A.isStringAttribute()) {
110 Rec.Operands.push_back({getOrEmitString(
A.getKindAsString(), MAI)});
111 StringRef Val =
A.getValueAsString();
113 Rec.Operands.push_back({getOrEmitString(Val, MAI)});
115 Rec.Operands.push_back(
116 {getOrEmitString(StringPool.save(
A.getAsString()), MAI)});
118 PendingRecords.push_back(std::move(Rec));
122void SPIRVAuxDataHandler::collectMetadataFor(
const GlobalObject *GO,
134 auto CollectOperands =
135 [&](MDNode *MD) -> std::optional<SmallVector<Operand, 4>> {
136 SmallVector<Operand, 4> Out;
137 for (
const MDOperand &MdOp : MD->operands()) {
140 Out.
push_back({getOrEmitString(MDStr->getString(), MAI)});
152 for (
const auto &MD : AllMD) {
153 if (MD.first == LLVMContext::MD_dbg)
155 StringRef MDName = MDNames[MD.first];
156 if (MDName ==
"spirv.Decorations" || MDName ==
"spirv.ParameterDecorations")
158 auto Operands = CollectOperands(MD.second);
164 Rec.Operands.push_back({getOrEmitString(MDName, MAI)});
165 Rec.Operands.append(Operands->begin(), Operands->end());
166 PendingRecords.push_back(std::move(Rec));
176 Mod.getContext().getMDKindNames(MDNames);
180 collectAttributesFor(&GO, MAI);
181 collectMetadataFor(&GO, MDNames, MAI);
190 MCRegister VoidTypeReg = findOrEmitOpTypeVoid(MAI);
192 for (
const ExtInstRecord &Rec : PendingRecords) {
198 for (
const Operand &
Op : Rec.Operands)
200 emitAuxDataExtInst(Rec.Opcode, VoidTypeReg, ExtSetReg, Operands, MAI);
203 if (LinkagePreservedGOs.empty())
206 MCRegister UInt32TypeReg = findOrEmitOpTypeUInt32(MAI);
214 emitOpConstantUInt32(AvailableExternally, UInt32TypeReg, MAI);
216 {TargetReg, AEConstReg}, MAI);
220void SPIRVAuxDataHandler::emitAuxDataExtInst(
AuxDataOpcode Opcode,
236void SPIRVAuxDataHandler::emitMCInst(
MCInst &Inst) {
243 if (
MI->getOpcode() == SPIRV::OpTypeVoid)
254SPIRVAuxDataHandler::findOrEmitOpTypeInt(
unsigned BitWidth,
258 constexpr int64_t UnsignedSignedness = 0;
260 if (
MI->getOpcode() == SPIRV::OpTypeInt &&
261 MI->getOperand(1).getImm() ==
static_cast<int64_t
>(
BitWidth) &&
262 MI->getOperand(2).getImm() == UnsignedSignedness)
276 return findOrEmitOpTypeInt(32, MAI);
280SPIRVAuxDataHandler::findOrEmitOpTypeFloat(
unsigned BitWidth,
283 if (
MI->getOpcode() == SPIRV::OpTypeFloat &&
284 MI->getOperand(1).getImm() ==
static_cast<int64_t
>(
BitWidth))
297 auto [It,
Inserted] = ConstantRegs.try_emplace(
C);
305 Bits = CI->getValue();
306 Opcode = SPIRV::OpConstantI;
307 TypeReg = findOrEmitOpTypeInt(
Bits.getBitWidth(), MAI);
310 Bits = CF->getValueAPF().bitcastToAPInt();
311 Opcode = SPIRV::OpConstantF;
312 TypeReg = findOrEmitOpTypeFloat(
Bits.getBitWidth(), MAI);
322 unsigned NumWords = std::max(1u, (
Bits.getBitWidth() + 31) / 32);
323 for (
unsigned I = 0;
I < NumWords; ++
I)
325 std::min(32u,
Bits.getBitWidth() -
I * 32),
I * 32)));
327 if (Opcode == SPIRV::OpConstantF &&
Bits.getBitWidth() == 16)
333MCRegister SPIRVAuxDataHandler::emitOpConstantUInt32(
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
static cl::opt< bool > SPVPreserveAuxData("spirv-preserve-auxdata", cl::desc("Preserve LLVM attributes and metadata as " "NonSemantic.AuxData ExtInst annotations (requires " "SPV_KHR_non_semantic_info)"), cl::Optional, cl::Hidden, cl::init(false))
static bool wasAvailableExternally(const GlobalObject *GO)
#define SPIRV_WAS_AVAILABLE_EXTERNALLY_ATTR
Represent a constant reference to an array (0 or more elements consecutively in memory),...
This class is intended to be used as a driving class for all asm writers.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
This class holds the attributes for a particular argument, parameter, function, or return value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
This is an important base class in LLVM.
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Instances of this class represent a single low-level machine instruction.
void setFlags(unsigned F)
void addOperand(const MCOperand Op)
void setOpcode(unsigned Op)
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
Wrapper class representing physical registers. Should be passed by value.
constexpr bool isValid() const
A Module instance is used to store all the information related to an LLVM module.
SPIRVAuxDataHandler(AsmPrinter &AP, const Module &M)
void emitAuxDataStrings(SPIRV::ModuleAnalysisInfo &MAI)
Emit OpStrings and stage ExtInst records; call in module section 7.
void emitAuxData(SPIRV::ModuleAnalysisInfo &MAI)
Emit the staged ExtInst records; call in module section 10.
void prepareModuleOutput(const SPIRVSubtarget &ST, SPIRV::ModuleAnalysisInfo &MAI)
Register extension + ext-inst-set; call before output of section 1.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
constexpr bool empty() const
Check if the string is empty.
LLVM Value Representation.
@ C
The default llvm calling convention, compatible with C.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ GlobalVariableMetadataOpcode
@ FunctionAttributeOpcode
@ GlobalVariableAttributeOpcode
auto dyn_cast_or_null(const Y &Val)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
void addStringImm(const StringRef &Str, MCInst &Inst)
MCRegister getExtInstSetReg(unsigned SetNum)
DenseMap< unsigned, MCRegister > ExtInstSetMap
InstrList & getMSInstrs(unsigned MSType)
MCRegister getRegisterAlias(const MachineFunction *MF, Register Reg)
MCRegister getGlobalObjReg(const GlobalObject *GO)
MCRegister getNextIDRegister()
void addExtension(Extension::Extension ToAdd)