Skip to content

Commit 5d28c93

Browse files
authored
[llvm-spirv] Cherry pick Khronos changes to expand collection of entry point interfaces (PR #1334) (#10623)
This PR pulls in the following PR from upstream Khronos SPIRV-LLVM-Translator repo: KhronosGroup/SPIRV-LLVM-Translator#1334 ` This is a patch to expand the collection of entry point interfaces. In SPIR-V 1.4 and later OpEntryPoint must list all global variables in the interface. ` In addition, a couple of minor changes have been added to sync with latest code. This patch addresses #9958 Updated the following tests to sync with upstream as well: llvm-spirv/test/extensions/INTEL/SPV_INTEL_inline_assembly/inline_asm_clobbers.cl llvm-spirv/test/extensions/INTEL/SPV_INTEL_inline_assembly/inline_asm_constraints.cl Thanks --------- Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent dd00ff6 commit 5d28c93

File tree

15 files changed

+107
-62
lines changed

15 files changed

+107
-62
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,7 @@ SPIRVFunction *LLVMToSPIRVBase::transFunctionDecl(Function *F) {
874874

875875
BM->setName(BF, F->getName().str());
876876
}
877-
if (isKernel(F))
878-
BM->addEntryPoint(ExecutionModelKernel, BF->getId());
879-
else if (F->getLinkage() != GlobalValue::InternalLinkage)
877+
if (!isKernel(F) && F->getLinkage() != GlobalValue::InternalLinkage)
880878
BF->setLinkageType(transLinkageType(F));
881879

882880
// Translate OpenCL/SYCL buffer_location metadata if it's attached to the
@@ -4898,12 +4896,15 @@ bool LLVMToSPIRVBase::isAnyFunctionReachableFromFunction(
48984896
return false;
48994897
}
49004898

4901-
void LLVMToSPIRVBase::collectInputOutputVariables(SPIRVFunction *SF,
4902-
Function *F) {
4899+
std::vector<SPIRVId>
4900+
LLVMToSPIRVBase::collectEntryPointInterfaces(SPIRVFunction *SF, Function *F) {
4901+
std::vector<SPIRVId> Interface;
49034902
for (auto &GV : M->globals()) {
49044903
const auto AS = GV.getAddressSpace();
4905-
if (AS != SPIRAS_Input && AS != SPIRAS_Output)
4906-
continue;
4904+
SPIRVModule *BM = SF->getModule();
4905+
if (!BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4))
4906+
if (AS != SPIRAS_Input && AS != SPIRAS_Output)
4907+
continue;
49074908

49084909
std::unordered_set<const Function *> Funcs;
49094910

@@ -4915,9 +4916,14 @@ void LLVMToSPIRVBase::collectInputOutputVariables(SPIRVFunction *SF,
49154916
}
49164917

49174918
if (isAnyFunctionReachableFromFunction(F, Funcs)) {
4918-
SF->addVariable(ValueMap[&GV]);
4919+
SPIRVWord ModuleVersion = static_cast<SPIRVWord>(BM->getSPIRVVersion());
4920+
if (AS != SPIRAS_Input && AS != SPIRAS_Output &&
4921+
ModuleVersion < static_cast<SPIRVWord>(VersionNumber::SPIRV_1_4))
4922+
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
4923+
Interface.push_back(ValueMap[&GV]->getId());
49194924
}
49204925
}
4926+
return Interface;
49214927
}
49224928

49234929
void LLVMToSPIRVBase::mutateFuncArgType(
@@ -5118,10 +5124,10 @@ void LLVMToSPIRVBase::transFunction(Function *I) {
51185124
joinFPContract(I, FPContract::ENABLED);
51195125
fpContractUpdateRecursive(I, getFPContract(I));
51205126

5121-
bool IsKernelEntryPoint = isKernel(I);
5122-
5123-
if (IsKernelEntryPoint) {
5124-
collectInputOutputVariables(BF, I);
5127+
if (isKernel(I)) {
5128+
auto Interface = collectEntryPointInterfaces(BF, I);
5129+
BM->addEntryPoint(ExecutionModelKernel, BF->getId(), I->getName().str(),
5130+
Interface);
51255131
}
51265132
}
51275133

llvm-spirv/lib/SPIRV/SPIRVWriter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ class LLVMToSPIRVBase : protected BuiltinCallHelper {
248248
const Function *FS,
249249
const std::unordered_set<const Function *> Funcs) const;
250250
void collectInputOutputVariables(SPIRVFunction *SF, Function *F);
251+
std::vector<SPIRVId> collectEntryPointInterfaces(SPIRVFunction *BF,
252+
Function *F);
251253
};
252254

253255
class LLVMToSPIRVPass : public PassInfoMixin<LLVMToSPIRVPass> {

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVDecorate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class SPIRVDecorateLinkageAttr : public SPIRVDecorate {
268268
#ifdef _SPIRV_SUPPORT_TEXT_FMT
269269
if (SPIRVUseTextFormat) {
270270
Encoder << getString(Literals.cbegin(), Literals.cend() - 1);
271-
Encoder.OS << " ";
272271
Encoder << (SPIRVLinkageTypeKind)Literals.back();
273272
} else
274273
#endif

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,11 @@ void SPIRVEntryPoint::encode(spv_ostream &O) const {
621621
}
622622

623623
void SPIRVEntryPoint::decode(std::istream &I) {
624-
getDecoder(I) >> ExecModel >> Target >> Name >> Variables;
624+
getDecoder(I) >> ExecModel >> Target >> Name;
625+
Variables.resize(WordCount - FixedWC - getSizeInWords(Name) + 1);
626+
getDecoder(I) >> Variables;
625627
Module->setName(getOrCreateTarget(), Name);
626-
Module->addEntryPoint(ExecModel, Target);
628+
Module->addEntryPoint(ExecModel, Target, Name, Variables);
627629
}
628630

629631
void SPIRVExecutionMode::encode(spv_ostream &O) const {

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ template <Op OC> class SPIRVAnnotation : public SPIRVAnnotationGeneric {
533533

534534
class SPIRVEntryPoint : public SPIRVAnnotation<OpEntryPoint> {
535535
public:
536+
static const SPIRVWord FixedWC = 4;
536537
SPIRVEntryPoint(SPIRVModule *TheModule, SPIRVExecutionModelKind,
537538
SPIRVId TheId, const std::string &TheName,
538539
std::vector<SPIRVId> Variables);

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,6 @@ class SPIRVModuleImpl : public SPIRVModule {
128128
getValueTypes(const std::vector<SPIRVId> &) const override;
129129
SPIRVMemoryModelKind getMemoryModel() const override { return MemoryModel; }
130130
SPIRVConstant *getLiteralAsConstant(unsigned Literal) override;
131-
unsigned getNumEntryPoints(SPIRVExecutionModelKind EM) const override {
132-
auto Loc = EntryPointVec.find(EM);
133-
if (Loc == EntryPointVec.end())
134-
return 0;
135-
return Loc->second.size();
136-
}
137-
SPIRVFunction *getEntryPoint(SPIRVExecutionModelKind EM,
138-
unsigned I) const override {
139-
auto Loc = EntryPointVec.find(EM);
140-
if (Loc == EntryPointVec.end())
141-
return nullptr;
142-
assert(I < Loc->second.size());
143-
return get<SPIRVFunction>(Loc->second[I]);
144-
}
145131
unsigned getNumFunctions() const override { return FuncVec.size(); }
146132
unsigned getNumVariables() const override { return VariableVec.size(); }
147133
SourceLanguage getSourceLanguage(SPIRVWord *Ver = nullptr) const override {
@@ -225,8 +211,9 @@ class SPIRVModuleImpl : public SPIRVModule {
225211
SPIRVGroupMemberDecorate *
226212
addGroupMemberDecorate(SPIRVDecorationGroup *Group,
227213
const std::vector<SPIRVEntry *> &Targets) override;
228-
void addEntryPoint(SPIRVExecutionModelKind ExecModel,
229-
SPIRVId EntryPoint) override;
214+
void addEntryPoint(SPIRVExecutionModelKind ExecModel, SPIRVId EntryPoint,
215+
const std::string &Name,
216+
const std::vector<SPIRVId> &Variables) override;
230217
SPIRVForward *addForward(SPIRVType *Ty) override;
231218
SPIRVForward *addForward(SPIRVId, SPIRVType *Ty) override;
232219
SPIRVFunction *addFunction(SPIRVFunction *) override;
@@ -508,11 +495,11 @@ class SPIRVModuleImpl : public SPIRVModule {
508495
typedef std::vector<SPIRVGroupDecorateGeneric *> SPIRVGroupDecVec;
509496
typedef std::vector<SPIRVAsmTargetINTEL *> SPIRVAsmTargetVector;
510497
typedef std::vector<SPIRVAsmINTEL *> SPIRVAsmVector;
498+
typedef std::vector<SPIRVEntryPoint *> SPIRVEntryPointVec;
511499
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToInstructionSetMap;
512500
std::map<SPIRVExtInstSetKind, SPIRVId> ExtInstSetIds;
513501
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToBuiltinSetMap;
514502
typedef std::map<SPIRVExecutionModelKind, SPIRVIdSet> SPIRVExecModelIdSetMap;
515-
typedef std::map<SPIRVExecutionModelKind, SPIRVIdVec> SPIRVExecModelIdVecMap;
516503
typedef std::unordered_map<std::string, SPIRVString *> SPIRVStringMap;
517504
typedef std::map<SPIRVTypeStruct *, std::vector<std::pair<unsigned, SPIRVId>>>
518505
SPIRVUnknownStructFieldMap;
@@ -540,7 +527,7 @@ class SPIRVModuleImpl : public SPIRVModule {
540527
SPIRVAsmTargetVector AsmTargetVec;
541528
SPIRVAsmVector AsmVec;
542529
SPIRVExecModelIdSetMap EntryPointSet;
543-
SPIRVExecModelIdVecMap EntryPointVec;
530+
SPIRVEntryPointVec EntryPointVec;
544531
SPIRVStringMap StrMap;
545532
SPIRVCapMap CapMap;
546533
SPIRVUnknownStructFieldMap UnknownStructFieldMap;
@@ -1086,11 +1073,14 @@ SPIRVModuleImpl::addDecorate(SPIRVDecorateGeneric *Dec) {
10861073
}
10871074

10881075
void SPIRVModuleImpl::addEntryPoint(SPIRVExecutionModelKind ExecModel,
1089-
SPIRVId EntryPoint) {
1076+
SPIRVId EntryPoint, const std::string &Name,
1077+
const std::vector<SPIRVId> &Variables) {
10901078
assert(isValid(ExecModel) && "Invalid execution model");
10911079
assert(EntryPoint != SPIRVID_INVALID && "Invalid entry point");
1080+
auto *EP =
1081+
add(new SPIRVEntryPoint(this, ExecModel, EntryPoint, Name, Variables));
1082+
EntryPointVec.push_back(EP);
10921083
EntryPointSet[ExecModel].insert(EntryPoint);
1093-
EntryPointVec[ExecModel].push_back(EntryPoint);
10941084
addCapabilities(SPIRV::getCapability(ExecModel));
10951085
}
10961086

@@ -1937,14 +1927,10 @@ spv_ostream &operator<<(spv_ostream &O, SPIRVModule &M) {
19371927

19381928
O << SPIRVMemoryModel(&M);
19391929

1940-
for (auto &I : MI.EntryPointVec)
1941-
for (auto &II : I.second)
1942-
O << SPIRVEntryPoint(&M, I.first, II, M.get<SPIRVFunction>(II)->getName(),
1943-
M.get<SPIRVFunction>(II)->getVariables());
1930+
O << MI.EntryPointVec;
19441931

19451932
for (auto &I : MI.EntryPointVec)
1946-
for (auto &II : I.second)
1947-
MI.get<SPIRVFunction>(II)->encodeExecutionModes(O);
1933+
MI.get<SPIRVFunction>(I->getTargetId())->encodeExecutionModes(O);
19481934

19491935
O << MI.StringVec;
19501936

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,11 @@ class SPIRVModule {
133133
virtual const SPIRVCapMap &getCapability() const = 0;
134134
virtual bool hasCapability(SPIRVCapabilityKind) const = 0;
135135
virtual SPIRVExtInstSetKind getBuiltinSet(SPIRVId) const = 0;
136-
virtual SPIRVFunction *getEntryPoint(SPIRVExecutionModelKind,
137-
unsigned) const = 0;
138136
virtual std::set<std::string> &getExtension() = 0;
139137
virtual SPIRVFunction *getFunction(unsigned) const = 0;
140138
virtual SPIRVVariable *getVariable(unsigned) const = 0;
141139
virtual SPIRVMemoryModelKind getMemoryModel() const = 0;
142140
virtual unsigned getNumFunctions() const = 0;
143-
virtual unsigned getNumEntryPoints(SPIRVExecutionModelKind) const = 0;
144141
virtual unsigned getNumVariables() const = 0;
145142
virtual SourceLanguage getSourceLanguage(SPIRVWord *) const = 0;
146143
virtual std::set<std::string> &getSourceExtension() = 0;
@@ -223,7 +220,9 @@ class SPIRVModule {
223220
const std::vector<SPIRVEntry *> &Targets) = 0;
224221
virtual SPIRVGroupDecorateGeneric *
225222
addGroupDecorateGeneric(SPIRVGroupDecorateGeneric *GDec) = 0;
226-
virtual void addEntryPoint(SPIRVExecutionModelKind, SPIRVId) = 0;
223+
virtual void addEntryPoint(SPIRVExecutionModelKind, SPIRVId,
224+
const std::string &,
225+
const std::vector<SPIRVId> &) = 0;
227226
virtual SPIRVForward *addForward(SPIRVType *Ty) = 0;
228227
virtual SPIRVForward *addForward(SPIRVId, SPIRVType *Ty) = 0;
229228
virtual SPIRVFunction *addFunction(SPIRVFunction *) = 0;

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVStream.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const SPIRVEncoder &operator<<(const SPIRVEncoder &O, const std::string &Str) {
180180
#ifdef _SPIRV_SUPPORT_TEXT_FMT
181181
if (SPIRVUseTextFormat) {
182182
writeQuotedString(O.OS, Str);
183+
O.OS << " ";
183184
return O;
184185
}
185186
#endif

llvm-spirv/test/ExecutionMode.ll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
; RUN: llvm-as < %s | llvm-spirv -spirv-text -o %t
22
; RUN: FileCheck < %t %s
33

4-
; check for magic number followed by version 1.1
5-
; CHECK: 119734787 65792
6-
74
; CHECK-DAG: TypeVoid [[VOID:[0-9]+]]
85

96
; CHECK-DAG: EntryPoint 6 [[WORKER:[0-9]+]] "worker"

llvm-spirv/test/copy_object.spt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
2 Capability Int64
66
2 Capability Int8
77
3 MemoryModel 2 2
8-
8 EntryPoint 6 1 "copy_object"
8+
6 EntryPoint 6 1 "copy_object"
99
3 Source 3 102000
1010
3 Name 2 "in"
1111
4 Decorate 3 BuiltIn 28

0 commit comments

Comments
 (0)