31 "spirv-preserve-auxdata",
32 cl::desc(
"Preserve LLVM attributes and metadata as "
33 "NonSemantic.AuxData ExtInst annotations (requires "
34 "SPV_KHR_non_semantic_info)"),
39 AvailableExternally = 0,
42constexpr unsigned NonSemanticAuxDataSet =
43 static_cast<unsigned>(SPIRV::InstructionSet::NonSemantic_AuxData);
47 return F->getAttributes().getFnAttrs();
63 LinkagePreservedGOs.push_back(&GO);
74 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_non_semantic_info)) {
77 "SPV_KHR_non_semantic_info extension to be enabled.");
88 if (ST.canUseExtension(
89 SPIRV::Extension::SPV_KHR_relaxed_extended_instruction))
91 SPIRV::Extension::SPV_KHR_relaxed_extended_instruction);
97SPIRVAuxDataHandler::getOrEmitString(
StringRef S,
99 auto [It, Inserted] = StringRegs.try_emplace(S);
112void SPIRVAuxDataHandler::collectAttributesFor(
const GlobalObject *GO,
117 if (
A.isStringAttribute() &&
123 if (
A.isStringAttribute()) {
124 Rec.Operands.push_back({getOrEmitString(
A.getKindAsString(), MAI)});
125 StringRef Val =
A.getValueAsString();
127 Rec.Operands.push_back({getOrEmitString(Val, MAI)});
129 Rec.Operands.push_back(
130 {getOrEmitString(StringPool.save(
A.getAsString()), MAI)});
132 PendingRecords.push_back(std::move(Rec));
136void SPIRVAuxDataHandler::collectMetadataFor(
const GlobalObject *GO,
148 auto CollectOperands =
149 [&](MDNode *MD) -> std::optional<SmallVector<Operand, 4>> {
150 SmallVector<Operand, 4>
Out;
151 for (
const MDOperand &MdOp : MD->operands()) {
154 Out.push_back({getOrEmitString(MDStr->getString(), MAI)});
159 Out.push_back({MCRegister(),
C});
166 for (
const auto &MD : AllMD) {
167 if (MD.first == LLVMContext::MD_dbg)
169 StringRef MDName = MDNames[MD.first];
170 if (MDName ==
"spirv.Decorations" || MDName ==
"spirv.ParameterDecorations")
172 auto Operands = CollectOperands(MD.second);
178 Rec.Operands.push_back({getOrEmitString(MDName, MAI)});
180 PendingRecords.push_back(std::move(Rec));
188 constexpr unsigned AllMDKindsSeen =
189 (1u << (
static_cast<unsigned>(
193 unsigned SeenMask = 0;
195 unsigned Bit = 1u <<
static_cast<unsigned>(Rec.Kind);
200 if (SeenMask == AllMDKindsSeen)
208 Mod.getContext().getMDKindNames(MDNames);
212 collectAttributesFor(&GO, MAI);
213 collectMetadataFor(&GO, MDNames, MAI);
222 MCRegister VoidTypeReg = findOrEmitOpTypeVoid(MAI);
224 for (
const ExtInstRecord &Rec : PendingRecords) {
230 for (
const Operand &
Op : Rec.Operands)
231 Operands.push_back(
Op.Const ? emitConstant(
Op.Const, MAI) :
Op.Reg);
232 emitAuxDataExtInst(Rec.Opcode, VoidTypeReg, ExtSetReg,
Operands, MAI);
244 {TargetReg, MDNameReg}, MAI,
true);
247 if (LinkagePreservedGOs.empty())
250 MCRegister UInt32TypeReg = findOrEmitOpTypeUInt32(MAI);
258 emitOpConstantUInt32(AvailableExternally, UInt32TypeReg, MAI);
260 {TargetReg, AEConstReg}, MAI);
264void SPIRVAuxDataHandler::emitAuxDataExtInst(
AuxDataOpcode Opcode,
269 bool UseForwardRefs) {
271 Inst.
setOpcode(UseForwardRefs ? SPIRV::OpExtInstWithForwardRefsKHR
282void SPIRVAuxDataHandler::emitMCInst(
MCInst &Inst) {
289 if (
MI->getOpcode() == SPIRV::OpTypeVoid)
300SPIRVAuxDataHandler::findOrEmitOpTypeInt(
unsigned BitWidth,
304 constexpr int64_t UnsignedSignedness = 0;
306 if (
MI->getOpcode() == SPIRV::OpTypeInt &&
307 MI->getOperand(1).getImm() ==
static_cast<int64_t
>(
BitWidth) &&
308 MI->getOperand(2).getImm() == UnsignedSignedness)
322 return findOrEmitOpTypeInt(32, MAI);
326SPIRVAuxDataHandler::findOrEmitOpTypeFloat(
unsigned BitWidth,
329 if (
MI->getOpcode() == SPIRV::OpTypeFloat &&
330 MI->getOperand(1).getImm() ==
static_cast<int64_t
>(
BitWidth))
343 auto [It,
Inserted] = ConstantRegs.try_emplace(
C);
351 Bits = CI->getValue();
352 Opcode = SPIRV::OpConstantI;
353 TypeReg = findOrEmitOpTypeInt(
Bits.getBitWidth(), MAI);
356 Bits = CF->getValueAPF().bitcastToAPInt();
357 Opcode = SPIRV::OpConstantF;
358 TypeReg = findOrEmitOpTypeFloat(
Bits.getBitWidth(), MAI);
369 for (
unsigned I = 0;
I < NumWords; ++
I)
371 std::min(32u,
Bits.getBitWidth() -
I * 32),
I * 32)));
373 if (Opcode == SPIRV::OpConstantF &&
Bits.getBitWidth() == 16)
379MCRegister SPIRVAuxDataHandler::emitOpConstantUInt32(
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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::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.
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.
Triple - Helper class for working with autoconf configuration names.
LLVM Value Representation.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void addStringImm(StringRef Str, MCInst &Inst)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ GlobalVariableMetadataOpcode
@ FunctionAttributeOpcode
@ GlobalVariableAttributeOpcode
@ InstructionMetadataOpcode
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
auto dyn_cast_or_null(const Y &Val)
bool spirvPreserveAuxData(const Triple &TT)
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...
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
DWARFExpression::Operation Op
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
MCRegister getExtInstSetReg(unsigned SetNum)
DenseMap< unsigned, MCRegister > ExtInstSetMap
InstrList & getMSInstrs(unsigned MSType)
MCRegister getRegisterAlias(const MachineFunction *MF, Register Reg)
SmallVector< InstrAuxDataRecord > InstrAuxDataRecords
static StringRef getAMDGPUAtomicMDName(AMDGPUAtomicMDKind Kind)
MCRegister getGlobalObjReg(const GlobalObject *GO)
MCRegister getNextIDRegister()
void addExtension(Extension::Extension ToAdd)