Skip to content

Sync with SPIRV-LLVM-Translator 55f2041f2c #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions llvm-spirv/include/LLVMSPIRVLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool isSpirvBinary(std::string &Img);
bool convertSpirv(std::istream &IS, std::ostream &OS, std::string &ErrMsg,
bool FromText, bool ToText);

/// \brief Convert SPIR-V between binary and internel text formats.
/// \brief Convert SPIR-V between binary and internal text formats.
/// This function is not thread safe and should not be used in multi-thread
/// applications unless guarded by a critical section.
bool convertSpirv(std::string &Input, std::string &Out, std::string &ErrMsg,
Expand All @@ -93,11 +93,11 @@ bool isSpirvText(std::string &Img);

namespace llvm {

/// \brief Translate LLVM module to SPIRV and write to ostream.
/// \brief Translate LLVM module to SPIR-V and write to ostream.
/// \returns true if succeeds.
bool writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg);

/// \brief Load SPIRV from istream and translate to LLVM module.
/// \brief Load SPIR-V from istream and translate to LLVM module.
/// \returns true if succeeds.
bool readSpirv(LLVMContext &C, std::istream &IS, Module *&M,
std::string &ErrMsg);
Expand Down
17 changes: 9 additions & 8 deletions llvm-spirv/lib/SPIRV/OCL20ToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ void OCL20ToSPIRV::visitCallReadImageWithSampler(
assert(0 && "read_image* with unhandled number of args!");
}

// SPIR-V intruction always returns 4-element vector
// SPIR-V instruction always returns 4-element vector
if (IsRetScalar)
Ret = VectorType::get(Ret, 4);
return getSPIRVFuncName(OpImageSampleExplicitLod,
Expand Down Expand Up @@ -1687,13 +1687,14 @@ void OCL20ToSPIRV::visitSubgroupImageMediaBlockINTEL(
spv::Op OpCode = DemangledName.rfind("read") != std::string::npos
? spv::OpSubgroupImageMediaBlockReadINTEL
: spv::OpSubgroupImageMediaBlockWriteINTEL;
mutateCallInstSPIRV(M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
// Moving the last argument to the begining.
std::rotate(Args.begin(), Args.end() - 1, Args.end());
return getSPIRVFuncName(OpCode, CI->getType());
},
&Attrs);
mutateCallInstSPIRV(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
// Moving the last argument to the beginning.
std::rotate(Args.begin(), Args.end() - 1, Args.end());
return getSPIRVFuncName(OpCode, CI->getType());
},
&Attrs);
}

static const char *getSubgroupAVCIntelOpKind(const std::string &Name) {
Expand Down
1 change: 1 addition & 0 deletions llvm-spirv/lib/SPIRV/SPIRVInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ enum SPIRAddressSpace {
SPIRAS_Local,
SPIRAS_Generic,
SPIRAS_Input,
SPIRAS_Output,
SPIRAS_Count,
};

Expand Down
9 changes: 9 additions & 0 deletions llvm-spirv/lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,10 @@ void generateIntelFPGAAnnotation(const SPIRVEntry *E,
Out << "{numbanks:" << Result << '}';
if (E->hasDecorate(DecorationMaxconcurrencyINTEL, 0, &Result))
Out << "{max_concurrency:" << Result << '}';
if (E->hasDecorate(DecorationSinglepumpINTEL))
Out << "{pump:1}";
if (E->hasDecorate(DecorationDoublepumpINTEL))
Out << "{pump:2}";
}

void generateIntelFPGAAnnotationForStructMember(
Expand All @@ -2224,6 +2228,10 @@ void generateIntelFPGAAnnotationForStructMember(
if (E->hasMemberDecorate(DecorationMaxconcurrencyINTEL, 0, MemberNumber,
&Result))
Out << "{max_concurrency:" << Result << '}';
if (E->hasMemberDecorate(DecorationSinglepumpINTEL, 0, MemberNumber))
Out << "{pump:1}";
if (E->hasMemberDecorate(DecorationDoublepumpINTEL, 0, MemberNumber))
Out << "{pump:2}";
}

void SPIRVToLLVM::transIntelFPGADecorations(SPIRVValue *BV, Value *V) {
Expand Down Expand Up @@ -2904,6 +2912,7 @@ bool llvm::readSpirv(LLVMContext &C, std::istream &IS, Module *&M,

IS >> *BM;
if (!BM->isModuleValid()) {
BM->getError(ErrMsg);
M = nullptr;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm-spirv/lib/SPIRV/SPIRVToOCL20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SPIRVToOCL20 : public ModulePass, public InstVisitor<SPIRVToOCL20> {
// appropriate calls to conversion built-ins defined by the standards.
void visitCastInst(CastInst &CI);

/// Transform __spirv_ImageQuerySize[Lod] into vector of the same lenght
/// Transform __spirv_ImageQuerySize[Lod] into vector of the same length
/// containing {[get_image_width | get_image_dim], get_image_array_size}
/// for all images except image1d_t which is always converted into
/// get_image_width returning scalar result.
Expand Down
2 changes: 1 addition & 1 deletion llvm-spirv/lib/SPIRV/SPIRVUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ Type *getSPIRVTypeByChangeBaseTypeName(Module *M, Type *T, StringRef OldName,
if (isSPIRVType(T, OldName, &Postfixes))
return getOrCreateOpaquePtrType(M, getSPIRVTypeName(NewName, Postfixes));
LLVM_DEBUG(dbgs() << " Invalid SPIR-V type " << *T << '\n');
llvm_unreachable("Invalid SPIRV-V type");
llvm_unreachable("Invalid SPIR-V type");
return nullptr;
}

Expand Down
129 changes: 113 additions & 16 deletions llvm-spirv/lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ LLVMToSPIRV::LLVMToSPIRV(SPIRVModule *SMod)

bool LLVMToSPIRV::runOnModule(Module &Mod) {
M = &Mod;
CG = make_unique<CallGraph>(Mod);
Ctx = &M->getContext();
DbgTran->setModule(M);
assert(BM && "SPIR-V module not initialized");
Expand Down Expand Up @@ -1127,16 +1128,25 @@ parseAnnotations(StringRef AnnotatedCode) {
StringRef AnnotatedDecoration = AnnotatedCode.substr(From + 1, To - 1);
std::pair<StringRef, StringRef> D = AnnotatedDecoration.split(':');

StringRef F = D.first;
Decoration Dec =
llvm::StringSwitch<Decoration>(F)
.Case("memory", DecorationMemoryINTEL)
.Case("register", DecorationRegisterINTEL)
.Case("numbanks", DecorationNumbanksINTEL)
.Case("bankwidth", DecorationBankwidthINTEL)
.Case("max_concurrency", DecorationMaxconcurrencyINTEL);
StringRef F = D.first, S = D.second;
StringRef Value;
Decoration Dec;
if (F == "pump") {
Dec = llvm::StringSwitch<Decoration>(S)
.Case("1", DecorationSinglepumpINTEL)
.Case("2", DecorationDoublepumpINTEL);
} else if (F == "register") {
Dec = DecorationRegisterINTEL;
} else {
Dec = llvm::StringSwitch<Decoration>(F)
.Case("memory", DecorationMemoryINTEL)
.Case("numbanks", DecorationNumbanksINTEL)
.Case("bankwidth", DecorationBankwidthINTEL)
.Case("max_concurrency", DecorationMaxconcurrencyINTEL);
Value = S;
}

Decorates.push_back({Dec, D.second});
Decorates.push_back({Dec, Value});
AnnotatedCode = AnnotatedCode.drop_front(To + 1);
}
return Decorates;
Expand All @@ -1146,12 +1156,25 @@ void addIntelFPGADecorations(
SPIRVEntry *E,
std::vector<std::pair<Decoration, std::string>> &Decorations) {
for (const auto &I : Decorations) {
if (I.first == DecorationMemoryINTEL)
switch (I.first) {
case DecorationMemoryINTEL:
E->addDecorate(new SPIRVDecorateMemoryINTELAttr(E, I.second));
else {
break;
case DecorationRegisterINTEL:
case DecorationSinglepumpINTEL:
case DecorationDoublepumpINTEL:
assert(I.second.empty());
E->addDecorate(I.first);
break;
// The rest of IntelFPGA decorations:
// DecorationNumbanksINTEL
// DecorationBankwidthINTEL
// DecorationMaxconcurrencyINTEL
default:
SPIRVWord Result = 0;
StringRef(I.second).getAsInteger(10, Result);
E->addDecorate(I.first, Result);
break;
}
}
}
Expand All @@ -1160,13 +1183,26 @@ void addIntelFPGADecorationsForStructMember(
SPIRVEntry *E, SPIRVWord MemberNumber,
std::vector<std::pair<Decoration, std::string>> &Decorations) {
for (const auto &I : Decorations) {
if (I.first == DecorationMemoryINTEL)
switch (I.first) {
case DecorationMemoryINTEL:
E->addMemberDecorate(
new SPIRVMemberDecorateMemoryINTELAttr(E, MemberNumber, I.second));
else {
break;
case DecorationRegisterINTEL:
case DecorationSinglepumpINTEL:
case DecorationDoublepumpINTEL:
assert(I.second.empty());
E->addMemberDecorate(MemberNumber, I.first);
break;
// The rest of IntelFPGA decorations:
// DecorationNumbanksINTEL
// DecorationBankwidthINTEL
// DecorationMaxconcurrencyINTEL
default:
SPIRVWord Result = 0;
StringRef(I.second).getAsInteger(10, Result);
E->addMemberDecorate(MemberNumber, I.first, Result);
break;
}
}
}
Expand All @@ -1177,6 +1213,14 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
std::vector<SPIRVWord> MemoryAccess(1, MemoryAccessMaskNone);
if (SPIRVWord AlignVal = MI->getDestAlignment()) {
MemoryAccess[0] |= MemoryAccessAlignedMask;
if (auto MTI = dyn_cast<MemTransferInst>(MI)) {
SPIRVWord SourceAlignVal = MTI->getSourceAlignment();
assert(SourceAlignVal && "Missed Source alignment!");

// In a case when alignment of source differs from dest one
// least value is guaranteed anyway.
AlignVal = std::min(AlignVal, SourceAlignVal);
}
MemoryAccess.push_back(AlignVal);
}
if (MI->isVolatile())
Expand Down Expand Up @@ -1233,9 +1277,6 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
GetMemoryAccess(MSI), BB);
} break;
case Intrinsic::memcpy:
assert(cast<MemCpyInst>(II)->getSourceAlignment() ==
cast<MemCpyInst>(II)->getDestAlignment() &&
"Alignment mismatch!");
return BM->addCopyMemorySizedInst(
transValue(II->getOperand(0), BB), transValue(II->getOperand(1), BB),
transValue(II->getOperand(2), BB),
Expand Down Expand Up @@ -1417,6 +1458,59 @@ bool LLVMToSPIRV::transGlobalVariables() {
return true;
}

bool LLVMToSPIRV::isAnyFunctionReachableFromFunction(
const Function *FS,
const std::unordered_set<const Function *> Funcs) const {
std::unordered_set<const Function *> Done;
std::unordered_set<const Function *> ToDo;
ToDo.insert(FS);

while (!ToDo.empty()) {
auto It = ToDo.begin();
const Function *F = *It;

if (Funcs.find(F) != Funcs.end())
return true;

ToDo.erase(It);
Done.insert(F);

const CallGraphNode *FN = (*CG)[F];
for (unsigned I = 0; I < FN->size(); ++I) {
const CallGraphNode *NN = (*FN)[I];
const Function *NNF = NN->getFunction();
if (!NNF)
continue;
if (Done.find(NNF) == Done.end()) {
ToDo.insert(NNF);
}
}
}

return false;
}

void LLVMToSPIRV::collectInputOutputVariables(SPIRVFunction *SF, Function *F) {
for (auto &GV : M->globals()) {
const auto AS = GV.getAddressSpace();
if (AS != SPIRAS_Input && AS != SPIRAS_Output)
continue;

std::unordered_set<const Function *> Funcs;

for (const auto &U : GV.uses()) {
const Instruction *Inst = dyn_cast<Instruction>(U.getUser());
if (!Inst)
continue;
Funcs.insert(Inst->getFunction());
}

if (isAnyFunctionReachableFromFunction(F, Funcs)) {
SF->addVariable(ValueMap[&GV]);
}
}
}

void LLVMToSPIRV::mutateFuncArgType(
const std::map<unsigned, Type *> &ChangedType, Function *F) {
for (auto &I : ChangedType) {
Expand Down Expand Up @@ -1458,6 +1552,9 @@ void LLVMToSPIRV::transFunction(Function *I) {
BF->addExecutionMode(BF->getModule()->add(
new SPIRVExecutionMode(BF, spv::ExecutionModeContractionOff)));
}
if (BF->getModule()->isEntryPoint(spv::ExecutionModelKernel, BF->getId())) {
collectInputOutputVariables(BF, I);
}
}

bool LLVMToSPIRV::translate() {
Expand Down
7 changes: 7 additions & 0 deletions llvm-spirv/lib/SPIRV/SPIRVWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "SPIRVUtil.h"
#include "SPIRVValue.h"

#include "llvm/Analysis/CallGraph.h"
#include "llvm/IR/IntrinsicInst.h"

#include <memory>
Expand Down Expand Up @@ -128,6 +129,7 @@ class LLVMToSPIRV : public ModulePass {
SPIRVWord SrcLang;
SPIRVWord SrcLangVer;
std::unique_ptr<LLVMToSPIRVDbgTran> DbgTran;
std::unique_ptr<CallGraph> CG;

SPIRVType *mapType(Type *T, SPIRVType *BT);
SPIRVValue *mapValue(Value *V, SPIRVValue *BV);
Expand Down Expand Up @@ -178,6 +180,11 @@ class LLVMToSPIRV : public ModulePass {
SPIRVId addInt32(int);
void transFunction(Function *I);
SPIRV::SPIRVLinkageTypeKind transLinkageType(const GlobalValue *GV);

bool isAnyFunctionReachableFromFunction(
const Function *FS,
const std::unordered_set<const Function *> Funcs) const;
void collectInputOutputVariables(SPIRVFunction *SF, Function *F);
};

} // namespace SPIRV
Expand Down
10 changes: 10 additions & 0 deletions llvm-spirv/lib/SPIRV/libSPIRV/SPIRVDecorate.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ class SPIRVDecorate : public SPIRVDecorateGeneric {
// Incomplete constructor
SPIRVDecorate() : SPIRVDecorateGeneric(OC) {}

SPIRVExtSet getRequiredExtensions() const override {
switch (Dec) {
case DecorationNoSignedWrap:
case DecorationNoUnsignedWrap:
return getSet(SPV_KHR_no_integer_wrap_decoration);
default:
return SPIRVExtSet();
}
}

_SPIRV_DCL_ENCDEC
void setWordCount(SPIRVWord) override;
void validate() const override {
Expand Down
11 changes: 6 additions & 5 deletions llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,17 +438,18 @@ std::istream &operator>>(std::istream &I, SPIRVEntry &E) {

SPIRVEntryPoint::SPIRVEntryPoint(SPIRVModule *TheModule,
SPIRVExecutionModelKind TheExecModel,
SPIRVId TheId, const std::string &TheName)
SPIRVId TheId, const std::string &TheName,
std::vector<SPIRVId> Variables)
: SPIRVAnnotation(TheModule->get<SPIRVFunction>(TheId),
getSizeInWords(TheName) + 3),
ExecModel(TheExecModel), Name(TheName) {}
getSizeInWords(TheName) + Variables.size() + 3),
ExecModel(TheExecModel), Name(TheName), Variables(Variables) {}

void SPIRVEntryPoint::encode(spv_ostream &O) const {
getEncoder(O) << ExecModel << Target << Name;
getEncoder(O) << ExecModel << Target << Name << Variables;
}

void SPIRVEntryPoint::decode(std::istream &I) {
getDecoder(I) >> ExecModel >> Target >> Name;
getDecoder(I) >> ExecModel >> Target >> Name >> Variables;
Module->setName(getOrCreateTarget(), Name);
Module->addEntryPoint(ExecModel, Target);
}
Expand Down
7 changes: 6 additions & 1 deletion llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class SPIRVEntry {
Op getOpCode() const { return OpCode; }
SPIRVModule *getModule() const { return Module; }
virtual SPIRVCapVec getRequiredCapability() const { return SPIRVCapVec(); }
virtual SPIRVExtSet getRequiredExtensions() const { return SPIRVExtSet(); }
const std::string &getName() const { return Name; }
bool hasDecorate(Decoration Kind, size_t Index = 0,
SPIRVWord *Result = 0) const;
Expand Down Expand Up @@ -478,12 +479,16 @@ template <Op OC> class SPIRVAnnotation : public SPIRVAnnotationGeneric {
class SPIRVEntryPoint : public SPIRVAnnotation<OpEntryPoint> {
public:
SPIRVEntryPoint(SPIRVModule *TheModule, SPIRVExecutionModelKind,
SPIRVId TheId, const std::string &TheName);
SPIRVId TheId, const std::string &TheName,
std::vector<SPIRVId> Variables);
SPIRVEntryPoint() : ExecModel(ExecutionModelKernel) {}
_SPIRV_DCL_ENCDEC
protected:
SPIRVExecutionModelKind ExecModel;
std::string Name;

private:
std::vector<SPIRVId> Variables;
};

class SPIRVName : public SPIRVAnnotation<OpName> {
Expand Down
Loading