Skip to content

Commit ed5372c

Browse files
[SPIR-V] Add SPIRVGlobalInstrRegistry, don't emit OpEntryPoint in MF
1 parent d1a31fa commit ed5372c

8 files changed

Lines changed: 95 additions & 15 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===-- SPIRVGlobalInstrRegistry.h - SPIR-V Global Instr Registry -*- C++
2+
//-*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===------------------------------------------------------------------------===//
9+
//
10+
// The SPIRVGlobalInstrRegistry class is used to collect and contain information
11+
// required for emitting SPIR-V global instructions (instructions which do not
12+
// belong to any function and are emittied on a module level).
13+
//
14+
//===------------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALINSTRREGISTRY_H
17+
#define LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALINSTRREGISTRY_H
18+
19+
#include "MCTargetDesc/SPIRVBaseInfo.h"
20+
21+
#include "llvm/ADT/SmallVector.h"
22+
#include "llvm/IR/Function.h"
23+
24+
namespace llvm {
25+
namespace SPIRV {
26+
/// Struct representing a global OpEntryPoint instruction for later printing.
27+
struct OpEntryPointInst {
28+
const SPIRV::ExecutionModel::ExecutionModel ExecModel;
29+
const Function *const EntryFunction;
30+
31+
OpEntryPointInst(const SPIRV::ExecutionModel::ExecutionModel ExecModel,
32+
const Function *const EntryFunction)
33+
: ExecModel(ExecModel), EntryFunction(EntryFunction) {}
34+
OpEntryPointInst &operator=(const OpEntryPointInst &) = delete;
35+
};
36+
} // namespace SPIRV
37+
38+
/// SPIRVGlobalInstrRegistry collects and stores information required for
39+
/// emitting SPIR-V global instructions.
40+
class SPIRVGlobalInstrRegistry {
41+
private:
42+
SmallVector<SPIRV::OpEntryPointInst> OpEntryPoints;
43+
44+
public:
45+
SPIRVGlobalInstrRegistry() {}
46+
47+
/// Declare a new SPIR-V entry point.
48+
void addEntryPoint(const SPIRV::ExecutionModel::ExecutionModel ExecModel,
49+
const Function *const EntryFunction) {
50+
OpEntryPoints.push_back(SPIRV::OpEntryPointInst(ExecModel, EntryFunction));
51+
}
52+
53+
/// Get all already declared SPIR-V entry points.
54+
const SmallVector<SPIRV::OpEntryPointInst> &getAllEntryPoints() const {
55+
return OpEntryPoints;
56+
}
57+
};
58+
59+
} // namespace llvm
60+
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALINSTRREGISTRY_H

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class SPIRVAsmPrinter : public AsmPrinter {
8484

8585
void getAnalysisUsage(AnalysisUsage &AU) const override;
8686
SPIRV::ModuleAnalysisInfo *MAI;
87+
SPIRVGlobalInstrRegistry *GIR;
8788
};
8889
} // namespace
8990

@@ -308,10 +309,15 @@ void SPIRVAsmPrinter::outputEntryPoints() {
308309
}
309310

310311
// Output OpEntryPoints adding interface args to all of them.
311-
for (MachineInstr *MI : MAI->getMSInstrs(SPIRV::MB_EntryPoints)) {
312-
SPIRVMCInstLower MCInstLowering;
312+
for (const SPIRV::OpEntryPointInst &EP : GIR->getAllEntryPoints()) {
313313
MCInst TmpInst;
314-
MCInstLowering.lower(MI, TmpInst, MAI);
314+
TmpInst.setOpcode(SPIRV::OpEntryPoint);
315+
TmpInst.addOperand(
316+
MCOperand::createImm(static_cast<unsigned>(EP.ExecModel)));
317+
Register EntryFuncReg = MAI->getFuncReg(EP.EntryFunction);
318+
TmpInst.addOperand(MCOperand::createReg(EntryFuncReg));
319+
addStringImm(EP.EntryFunction->getName(), TmpInst);
320+
315321
for (Register Reg : InterfaceIDs) {
316322
assert(Reg.isValid());
317323
TmpInst.addOperand(MCOperand::createReg(Reg));
@@ -500,6 +506,7 @@ void SPIRVAsmPrinter::outputModuleSections() {
500506
ST = static_cast<const SPIRVTargetMachine &>(TM).getSubtargetImpl();
501507
TII = ST->getInstrInfo();
502508
MAI = &SPIRVModuleAnalysis::MAI;
509+
GIR = ST->getSPIRVGlobalInstrRegistry();
503510
assert(ST && TII && MAI && M && "Module analysis is required");
504511
// Output instructions according to the Logical Layout of a Module:
505512
// 1,2. All OpCapability instructions, then optional OpExtension instructions.

llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "SPIRV.h"
1717
#include "SPIRVBuiltins.h"
1818
#include "Registries/SPIRVGlobalTypeRegistry.h"
19+
#include "Registries/SPIRVGlobalInstrRegistry.h"
1920
#include "SPIRVISelLowering.h"
2021
#include "SPIRVRegisterInfo.h"
2122
#include "SPIRVSubtarget.h"
@@ -26,8 +27,9 @@
2627
using namespace llvm;
2728

2829
SPIRVCallLowering::SPIRVCallLowering(const SPIRVTargetLowering &TLI,
29-
SPIRVGlobalTypeRegistry *GTR)
30-
: CallLowering(&TLI), GTR(GTR) {}
30+
SPIRVGlobalTypeRegistry *GTR,
31+
SPIRVGlobalInstrRegistry *GIR)
32+
: CallLowering(&TLI), GTR(GTR), GIR(GIR) {}
3133

3234
bool SPIRVCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
3335
const Value *Val, ArrayRef<Register> VRegs,
@@ -339,10 +341,7 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
339341

340342
// Handle entry points and function linkage.
341343
if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
342-
auto MIB = MIRBuilder.buildInstr(SPIRV::OpEntryPoint)
343-
.addImm(static_cast<uint32_t>(SPIRV::ExecutionModel::Kernel))
344-
.addUse(FuncVReg);
345-
addStringImm(F.getName(), MIB);
344+
GIR->addEntryPoint(SPIRV::ExecutionModel::Kernel, &F);
346345
} else if (F.getLinkage() == GlobalValue::LinkageTypes::ExternalLinkage ||
347346
F.getLinkage() == GlobalValue::LinkOnceODRLinkage) {
348347
auto LnkTy = F.isDeclaration() ? SPIRV::LinkageType::Import

llvm/lib/Target/SPIRV/SPIRVCallLowering.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_LIB_TARGET_SPIRV_SPIRVCALLLOWERING_H
1515

1616
#include "Registries/SPIRVGlobalTypeRegistry.h"
17+
#include "Registries/SPIRVGlobalInstrRegistry.h"
1718
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
1819

1920
namespace llvm {
@@ -26,9 +27,13 @@ class SPIRVCallLowering : public CallLowering {
2627
// Used to create and assign function, argument, and return type information.
2728
SPIRVGlobalTypeRegistry *GTR;
2829

30+
// Used to create global instructions which do not belong to any function.
31+
SPIRVGlobalInstrRegistry *GIR;
32+
2933
public:
3034
SPIRVCallLowering(const SPIRVTargetLowering &TLI,
31-
SPIRVGlobalTypeRegistry *GTR);
35+
SPIRVGlobalTypeRegistry *GTR,
36+
SPIRVGlobalInstrRegistry *GIR);
3237

3338
// Built OpReturn or OpReturnValue.
3439
bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val,

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ void SPIRVModuleAnalysis::processOtherInstrs(const Module &M) {
338338
const unsigned OpCode = MI.getOpcode();
339339
if (OpCode == SPIRV::OpName || OpCode == SPIRV::OpMemberName) {
340340
collectOtherInstr(MI, MAI, SPIRV::MB_DebugNames);
341-
} else if (OpCode == SPIRV::OpEntryPoint) {
342-
collectOtherInstr(MI, MAI, SPIRV::MB_EntryPoints);
343341
} else if (TII->isDecorationInstr(MI)) {
344342
collectOtherInstr(MI, MAI, SPIRV::MB_Annotations);
345343
collectFuncNames(MI, &*F);
@@ -969,6 +967,7 @@ bool SPIRVModuleAnalysis::runOnModule(Module &M) {
969967
getAnalysis<TargetPassConfig>().getTM<SPIRVTargetMachine>();
970968
ST = TM.getSubtargetImpl();
971969
GTR = ST->getSPIRVGlobalTypeRegistry();
970+
GIR = ST->getSPIRVGlobalInstrRegistry();
972971
TII = ST->getInstrInfo();
973972

974973
MMI = &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
@@ -990,7 +989,7 @@ bool SPIRVModuleAnalysis::runOnModule(Module &M) {
990989
processOtherInstrs(M);
991990

992991
// If there are no entry points, we need the Linkage capability.
993-
if (MAI.MS[SPIRV::MB_EntryPoints].empty())
992+
if (GIR->getAllEntryPoints().empty())
994993
MAI.Reqs.addCapability(SPIRV::Capability::Linkage);
995994

996995
return false;

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "MCTargetDesc/SPIRVBaseInfo.h"
1818
#include "Registries/SPIRVGlobalTypeRegistry.h"
19+
#include "Registries/SPIRVGlobalInstrRegistry.h"
1920
#include "SPIRVUtils.h"
2021
#include "llvm/ADT/DenseMap.h"
2122
#include "llvm/ADT/SmallSet.h"
@@ -31,7 +32,6 @@ namespace SPIRV {
3132
// The enum contains logical module sections for the instruction collection.
3233
enum ModuleSectionType {
3334
// MB_Capabilities, MB_Extensions, MB_ExtInstImports, MB_MemoryModel,
34-
MB_EntryPoints, // All OpEntryPoint instructions (if any).
3535
// MB_ExecutionModes, MB_DebugSourceAndStrings,
3636
MB_DebugNames, // All OpName and OpMemberName intrs.
3737
MB_DebugModuleProcessed, // All OpModuleProcessed instructions.
@@ -214,6 +214,7 @@ struct SPIRVModuleAnalysis : public ModulePass {
214214

215215
const SPIRVSubtarget *ST;
216216
SPIRVGlobalTypeRegistry *GTR;
217+
SPIRVGlobalInstrRegistry *GIR;
217218
const SPIRVInstrInfo *TII;
218219
MachineModuleInfo *MMI;
219220
};

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
5151
initAvailableExtInstSets();
5252

5353
GTR = std::make_unique<SPIRVGlobalTypeRegistry>(PointerSize);
54-
CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GTR.get());
54+
GIR = std::make_unique<SPIRVGlobalInstrRegistry>();
55+
CallLoweringInfo =
56+
std::make_unique<SPIRVCallLowering>(TLInfo, GTR.get(), GIR.get());
5557
Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
5658
RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
5759
InstSelector.reset(

llvm/lib/Target/SPIRV/SPIRVSubtarget.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "SPIRVFrameLowering.h"
1818
#include "SPIRVISelLowering.h"
1919
#include "SPIRVInstrInfo.h"
20+
#include "Registries/SPIRVGlobalTypeRegistry.h"
21+
#include "Registries/SPIRVGlobalInstrRegistry.h"
2022
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
2123
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
2224
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
@@ -41,6 +43,7 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
4143
SmallSet<SPIRV::Extension::Extension, 4> AvailableExtensions;
4244
SmallSet<SPIRV::InstructionSet::InstructionSet, 4> AvailableExtInstSets;
4345
std::unique_ptr<SPIRVGlobalTypeRegistry> GTR;
46+
std::unique_ptr<SPIRVGlobalInstrRegistry> GIR;
4447

4548
SPIRVInstrInfo InstrInfo;
4649
SPIRVFrameLowering FrameLowering;
@@ -86,6 +89,10 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
8689
return GTR.get();
8790
}
8891

92+
SPIRVGlobalInstrRegistry *getSPIRVGlobalInstrRegistry() const {
93+
return GIR.get();
94+
}
95+
8996
const CallLowering *getCallLowering() const override {
9097
return CallLoweringInfo.get();
9198
}

0 commit comments

Comments
 (0)