Skip to content

Commit 8d8797d

Browse files
committed
pr-feedback: variable inheritance
1 parent ce1ff6e commit 8d8797d

9 files changed

Lines changed: 50 additions & 41 deletions

File tree

tools/clang/include/clang/SPIRV/SpirvBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ class SpirvBuilder {
636636
/// support a single entry point per module for now.
637637
inline void addEntryPoint(spv::ExecutionModel em, SpirvFunction *target,
638638
llvm::StringRef targetName,
639-
llvm::ArrayRef<SpirvInstruction *> interfaces);
639+
llvm::ArrayRef<SpirvVariableLike *> interfaces);
640640
/// \brief Sets the shader model version, source file name, and source file
641641
/// content. Returns the SpirvString instruction of the file name.
642642
inline SpirvString *setDebugSource(uint32_t major, uint32_t minor,
@@ -994,7 +994,7 @@ void SpirvBuilder::setMemoryModel(spv::AddressingModel addrModel,
994994

995995
void SpirvBuilder::addEntryPoint(
996996
spv::ExecutionModel em, SpirvFunction *target, llvm::StringRef targetName,
997-
llvm::ArrayRef<SpirvInstruction *> interfaces) {
997+
llvm::ArrayRef<SpirvVariableLike *> interfaces) {
998998
mod->addEntryPoint(new (context) SpirvEntryPoint(
999999
target->getSourceLocation(), em, target, targetName, interfaces));
10001000
}

tools/clang/include/clang/SPIRV/SpirvInstruction.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ class SpirvInstruction {
297297
bool isRasterizerOrdered_;
298298
};
299299

300+
/// \brief class wrapping OpVariable and OpUntypedVariableKHR
301+
class SpirvVariableLike : public SpirvInstruction {
302+
303+
protected:
304+
SpirvVariableLike(Kind kind, spv::Op opcode, QualType astResultType,
305+
SourceLocation loc, SourceRange range = {});
306+
};
307+
300308
/// \brief OpCapability instruction
301309
class SpirvCapability : public SpirvInstruction {
302310
public:
@@ -391,7 +399,7 @@ class SpirvEntryPoint : public SpirvInstruction {
391399
public:
392400
SpirvEntryPoint(SourceLocation loc, spv::ExecutionModel executionModel,
393401
SpirvFunction *entryPoint, llvm::StringRef nameStr,
394-
llvm::ArrayRef<SpirvInstruction *> iface);
402+
llvm::ArrayRef<SpirvVariableLike *> iface);
395403

396404
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvEntryPoint)
397405

@@ -405,15 +413,15 @@ class SpirvEntryPoint : public SpirvInstruction {
405413
spv::ExecutionModel getExecModel() const { return execModel; }
406414
SpirvFunction *getEntryPoint() const { return entryPoint; }
407415
llvm::StringRef getEntryPointName() const { return name; }
408-
llvm::ArrayRef<SpirvInstruction *> getInterface() const {
416+
llvm::ArrayRef<SpirvVariableLike *> getInterface() const {
409417
return interfaceVec;
410418
}
411419

412420
private:
413421
spv::ExecutionModel execModel;
414422
SpirvFunction *entryPoint;
415423
std::string name;
416-
llvm::SmallVector<SpirvInstruction *, 8> interfaceVec;
424+
llvm::SmallVector<SpirvVariableLike *, 8> interfaceVec;
417425
};
418426

419427
class SpirvExecutionModeBase : public SpirvInstruction {
@@ -611,7 +619,7 @@ class SpirvDecoration : public SpirvInstruction {
611619
};
612620

613621
/// \brief OpVariable instruction
614-
class SpirvVariable : public SpirvInstruction {
622+
class SpirvVariable : public SpirvVariableLike {
615623
public:
616624
SpirvVariable(QualType resultType, SourceLocation loc, spv::StorageClass sc,
617625
bool isPrecise, bool isNointerp,
@@ -647,7 +655,7 @@ class SpirvVariable : public SpirvInstruction {
647655
};
648656

649657
/// \brief OpUntypedVariableKHR instruction
650-
class SpirvUntypedVariableKHR : public SpirvInstruction {
658+
class SpirvUntypedVariableKHR : public SpirvVariableLike {
651659
public:
652660
SpirvUntypedVariableKHR(QualType resultType, SourceLocation loc,
653661
spv::StorageClass sc);

tools/clang/include/clang/SPIRV/SpirvModule.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ class SpirvModule {
137137
SpirvExtInstImport *getExtInstSet(llvm::StringRef name);
138138

139139
// Adds a variable to the module.
140-
void addVariable(SpirvInstruction *);
140+
void addVariable(SpirvVariableLike *);
141141

142142
// Adds a variable to the module immediately before `pos`.
143143
// If `pos` is not found, `var` is added at the end of the variable list.
144-
void addVariable(SpirvInstruction *var, SpirvInstruction *pos);
144+
void addVariable(SpirvVariableLike *var, SpirvInstruction *pos);
145145

146146
// Adds a decoration to the module.
147147
void addDecoration(SpirvDecoration *);
@@ -172,7 +172,7 @@ class SpirvModule {
172172
// Adds the given OpModuleProcessed to the module.
173173
void addModuleProcessed(SpirvModuleProcessed *);
174174

175-
llvm::ArrayRef<SpirvInstruction *> getVariables() const { return variables; }
175+
llvm::ArrayRef<SpirvVariableLike *> getVariables() const { return variables; }
176176

177177
llvm::ArrayRef<SpirvEntryPoint *> getEntryPoints() const {
178178
return entryPoints;
@@ -217,7 +217,7 @@ class SpirvModule {
217217

218218
std::vector<SpirvConstant *> constants;
219219
std::vector<SpirvUndef *> undefs;
220-
std::vector<SpirvInstruction *> variables;
220+
std::vector<SpirvVariableLike *> variables;
221221
// A vector of functions in the module in the order that they should be
222222
// emitted. The order starts with the entry-point function followed by a
223223
// depth-first discovery of functions reachable from the entry-point function.

tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ DeclResultIdMapper::createFileVar(const VarDecl *var,
11731173
return varInstr;
11741174
}
11751175

1176-
SpirvInstruction *
1176+
SpirvVariableLike *
11771177
DeclResultIdMapper::createResourceDescriptorHeap(const VarDecl *var) {
11781178
SpirvUntypedVariableKHR *HeapVar = nullptr;
11791179

@@ -1209,15 +1209,15 @@ DeclResultIdMapper::createResourceDescriptorHeap(const VarDecl *var) {
12091209
return HeapVar;
12101210
}
12111211

1212-
SpirvInstruction *
1212+
SpirvVariableLike *
12131213
DeclResultIdMapper::createEmulatedDescriptorHeap(const VarDecl *var,
12141214
QualType resourceType) {
12151215
QualType ResourceArrayType = astContext.getIncompleteArrayType(
12161216
resourceType, clang::ArrayType::Normal, 0);
12171217
return createExternVar(var, ResourceArrayType);
12181218
}
12191219

1220-
SpirvInstruction *
1220+
SpirvVariableLike *
12211221
DeclResultIdMapper::createResourceHeap(const VarDecl *var,
12221222
QualType resourceType) {
12231223
if (spirvOptions.useDescriptorHeap)
@@ -1999,9 +1999,9 @@ void DeclResultIdMapper::createFieldCounterVars(
19991999
}
20002000
}
20012001

2002-
std::vector<SpirvInstruction *>
2002+
std::vector<SpirvVariableLike *>
20032003
DeclResultIdMapper::collectStageVars(SpirvFunction *entryPoint) const {
2004-
std::vector<SpirvInstruction *> vars;
2004+
std::vector<SpirvVariableLike *> vars;
20052005

20062006
for (auto var : glPerVertex.getStageInVars())
20072007
vars.push_back(var);

tools/clang/lib/SPIRV/DeclResultIdMapper.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class DeclResultIdMapper {
291291

292292
/// Creates a global variable for resource heaps containing elements of type
293293
/// |type|.
294-
SpirvInstruction *createResourceHeap(const VarDecl *var, QualType type);
294+
SpirvVariableLike *createResourceHeap(const VarDecl *var, QualType type);
295295

296296
/// \brief Creates an external-visible variable and returns its instruction.
297297
SpirvVariable *createExternVar(const VarDecl *var);
@@ -510,7 +510,7 @@ class DeclResultIdMapper {
510510

511511
/// \brief Returns all defined stage (builtin/input/ouput) variables for the
512512
/// entry point function entryPoint in this mapper.
513-
std::vector<SpirvInstruction *>
513+
std::vector<SpirvVariableLike *>
514514
collectStageVars(SpirvFunction *entryPoint) const;
515515

516516
/// \brief Writes out the contents in the function parameter for the GS
@@ -1007,12 +1007,12 @@ class DeclResultIdMapper {
10071007
void registerVariableForDecl(const VarDecl *var, DeclSpirvInfo spirvInfo);
10081008

10091009
/// Creates a global variable for resource heaps using VK_EXT_descriptor_heap.
1010-
SpirvInstruction *createResourceDescriptorHeap(const VarDecl *var);
1010+
SpirvVariableLike *createResourceDescriptorHeap(const VarDecl *var);
10111011

10121012
/// Creates a global variable for resource heaps containing elements of type
10131013
/// |type| (emulated descriptor heaps).
1014-
SpirvInstruction *createEmulatedDescriptorHeap(const VarDecl *var,
1015-
QualType resourceType);
1014+
SpirvVariableLike *createEmulatedDescriptorHeap(const VarDecl *var,
1015+
QualType resourceType);
10161016

10171017
private:
10181018
SpirvBuilder &spvBuilder;

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,24 +696,20 @@ SpirvEmitter::SpirvEmitter(CompilerInstance &ci)
696696
}
697697
}
698698

699-
std::vector<SpirvInstruction *>
699+
std::vector<SpirvVariableLike *>
700700
SpirvEmitter::getInterfacesForEntryPoint(SpirvFunction *entryPoint) {
701701
auto stageVars = declIdMapper.collectStageVars(entryPoint);
702-
if (!featureManager.isTargetEnvVulkan1p1Spirv1p4OrAbove()) {
703-
std::vector<SpirvInstruction *> ifaces;
704-
for (auto *v : stageVars)
705-
ifaces.push_back(v);
706-
return ifaces;
707-
}
702+
if (!featureManager.isTargetEnvVulkan1p1Spirv1p4OrAbove())
703+
return stageVars;
708704

709705
// In SPIR-V 1.4 or above, we must include global variables in the 'Interface'
710706
// operands of OpEntryPoint. SpirvModule keeps all global variables, but some
711707
// of them can be duplicated with stage variables kept by declIdMapper. Since
712708
// declIdMapper keeps the mapping between variables with Input or Output
713709
// storage class and their storage class, we have to rely on
714710
// declIdMapper.collectStageVars() to collect them.
715-
llvm::SetVector<SpirvInstruction *> interfaces(stageVars.begin(),
716-
stageVars.end());
711+
llvm::SetVector<SpirvVariableLike *> interfaces(stageVars.begin(),
712+
stageVars.end());
717713

718714
for (auto *moduleVar : spvBuilder.getModule()->getVariables()) {
719715
if (moduleVar->getStorageClass() == spv::StorageClass::Input ||
@@ -733,7 +729,7 @@ SpirvEmitter::getInterfacesForEntryPoint(SpirvFunction *entryPoint) {
733729
}
734730
interfaces.insert(moduleVar);
735731
}
736-
std::vector<SpirvInstruction *> interfacesInVector;
732+
std::vector<SpirvVariableLike *> interfacesInVector;
737733
interfacesInVector.reserve(interfaces.size());
738734
for (auto *interface : interfaces) {
739735
interfacesInVector.push_back(interface);

tools/clang/lib/SPIRV/SpirvEmitter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ class SpirvEmitter : public ASTConsumer {
13201320

13211321
/// \brief Returns OpVariable to be used as 'Interface' operands of
13221322
/// OpEntryPoint. entryPoint is the SpirvFunction for the OpEntryPoint.
1323-
std::vector<SpirvInstruction *>
1323+
std::vector<SpirvVariableLike *>
13241324
getInterfacesForEntryPoint(SpirvFunction *entryPoint);
13251325

13261326
/// \brief Emits OpBeginInvocationInterlockEXT and add the appropriate

tools/clang/lib/SPIRV/SpirvInstruction.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ SpirvEntryPoint::SpirvEntryPoint(SourceLocation loc,
207207
spv::ExecutionModel executionModel,
208208
SpirvFunction *entryPointFn,
209209
llvm::StringRef nameStr,
210-
llvm::ArrayRef<SpirvInstruction *> iface)
210+
llvm::ArrayRef<SpirvVariableLike *> iface)
211211
: SpirvInstruction(IK_EntryPoint, spv::Op::OpEntryPoint, QualType(), loc),
212212
execModel(executionModel), entryPoint(entryPointFn), name(nameStr),
213213
interfaceVec(iface.begin(), iface.end()) {}
@@ -310,7 +310,7 @@ bool SpirvDecoration::operator==(const SpirvDecoration &that) const {
310310
SpirvVariable::SpirvVariable(QualType resultType, SourceLocation loc,
311311
spv::StorageClass sc, bool precise,
312312
bool isNointerp, SpirvInstruction *initializerInst)
313-
: SpirvInstruction(IK_Variable, spv::Op::OpVariable, resultType, loc),
313+
: SpirvVariableLike(IK_Variable, spv::Op::OpVariable, resultType, loc),
314314
initializer(initializerInst), descriptorSet(-1), binding(-1),
315315
hlslUserType("") {
316316
setStorageClass(sc);
@@ -321,7 +321,7 @@ SpirvVariable::SpirvVariable(QualType resultType, SourceLocation loc,
321321
SpirvVariable::SpirvVariable(const SpirvType *spvType, SourceLocation loc,
322322
spv::StorageClass sc, bool precise,
323323
bool isNointerp, SpirvInstruction *initializerInst)
324-
: SpirvInstruction(IK_Variable, spv::Op::OpVariable, QualType(), loc),
324+
: SpirvVariableLike(IK_Variable, spv::Op::OpVariable, QualType(), loc),
325325
initializer(initializerInst), descriptorSet(-1), binding(-1),
326326
hlslUserType("") {
327327
setResultType(spvType);
@@ -333,20 +333,25 @@ SpirvVariable::SpirvVariable(const SpirvType *spvType, SourceLocation loc,
333333
SpirvUntypedVariableKHR::SpirvUntypedVariableKHR(QualType resultType,
334334
SourceLocation loc,
335335
spv::StorageClass sc)
336-
: SpirvInstruction(IK_UntypedVariableKHR, spv::Op::OpUntypedVariableKHR,
337-
resultType, loc) {
336+
: SpirvVariableLike(IK_UntypedVariableKHR, spv::Op::OpUntypedVariableKHR,
337+
resultType, loc) {
338338
setStorageClass(sc);
339339
}
340340

341341
SpirvUntypedVariableKHR::SpirvUntypedVariableKHR(const SpirvType *spvType,
342342
SourceLocation loc,
343343
spv::StorageClass sc)
344-
: SpirvInstruction(IK_UntypedVariableKHR, spv::Op::OpUntypedVariableKHR,
345-
QualType(), loc) {
344+
: SpirvVariableLike(IK_UntypedVariableKHR, spv::Op::OpUntypedVariableKHR,
345+
QualType(), loc) {
346346
setResultType(spvType);
347347
setStorageClass(sc);
348348
}
349349

350+
SpirvVariableLike::SpirvVariableLike(Kind kind, spv::Op opcode,
351+
QualType astResultType, SourceLocation loc,
352+
SourceRange range)
353+
: SpirvInstruction(kind, opcode, astResultType, loc, range) {}
354+
350355
SpirvUntypedAccessChainKHR::SpirvUntypedAccessChainKHR(
351356
const SpirvType *resultType, SourceLocation loc, const SpirvType *baseType,
352357
SpirvInstruction *baseInst, llvm::ArrayRef<SpirvInstruction *> indexVec)

tools/clang/lib/SPIRV/SpirvModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,12 @@ SpirvExtInstImport *SpirvModule::getExtInstSet(llvm::StringRef name) {
336336
return nullptr;
337337
}
338338

339-
void SpirvModule::addVariable(SpirvInstruction *var) {
339+
void SpirvModule::addVariable(SpirvVariableLike *var) {
340340
assert(var && "cannot add null variable to the module");
341341
variables.push_back(var);
342342
}
343343

344-
void SpirvModule::addVariable(SpirvInstruction *var, SpirvInstruction *pos) {
344+
void SpirvModule::addVariable(SpirvVariableLike *var, SpirvInstruction *pos) {
345345
assert(var && "cannot add null variable to the module");
346346
auto location = std::find(variables.begin(), variables.end(), pos);
347347
variables.insert(location, var);

0 commit comments

Comments
 (0)