Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Fixing encoding/decoding of OpLine instruction (#184)
Browse files Browse the repository at this point in the history
Changed wordcount to 4. Changing layout of OpLine in module
according to the specification requirements.
  • Loading branch information
AlexeySotkin authored and yxsamliu committed Sep 23, 2016
1 parent 8a95239 commit c370052
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 63 deletions.
11 changes: 6 additions & 5 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ SPIRVToLLVM::transOCLSampledImageTypeName(SPIRV::SPIRVTypeSampledImage* ST) {
}

std::string
SPIRVToLLVM::transOCLPipeTypeName(SPIRV::SPIRVTypePipe* PT,
SPIRVToLLVM::transOCLPipeTypeName(SPIRV::SPIRVTypePipe* PT,
bool UseSPIRVFriendlyFormat, int PipeAccess){
if (!UseSPIRVFriendlyFormat)
return kSPR2TypeName::Pipe;
Expand Down Expand Up @@ -744,7 +744,7 @@ SPIRVToLLVM::transType(SPIRVType *T, bool IsClassMember) {
return mapType(T, getOrCreateOpaquePtrType(M,
transOCLPipeTypeName(PT, IsClassMember, PT->getAccessQualifier()),
getOCLOpaqueTypeAddrSpace(T->getOpCode())));


}
case OpTypePipeStorage: {
auto PST = static_cast<SPIRVTypePipeStorage *>(T);
Expand Down Expand Up @@ -1281,13 +1281,13 @@ SPIRVToLLVM::oclTransConstantSampler(SPIRV::SPIRVConstantSampler* BCS) {
Value *
SPIRVToLLVM::oclTransConstantPipeStorage(
SPIRV::SPIRVConstantPipeStorage* BCPS) {


string CPSName = string(kSPIRVTypeName::PrefixAndDelim)
+ kSPIRVTypeName::ConstantPipeStorage;

auto Int32Ty = IntegerType::getInt32Ty(*Context);
auto CPSTy = M->getTypeByName(CPSName);
if (!CPSTy) {
if (!CPSTy) {
Type* CPSElemsTy[] = { Int32Ty, Int32Ty, Int32Ty };
CPSTy = StructType::create(*Context, CPSElemsTy, CPSName);
}
Expand Down Expand Up @@ -1396,7 +1396,7 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
CV[i] = ConstantExpr::getBitCast(CV[i], BCCTy->getElementType(i));
}
}


return mapValue(BV, ConstantStruct::get(
dyn_cast<StructType>(transType(BCC->getType())), CV));
}
Expand Down Expand Up @@ -1613,6 +1613,7 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
BV->getName(), BB));
}

case OpLine:
case OpSelectionMerge: {
// OpenCL Compiler does not use this instruction
return nullptr;
Expand Down
8 changes: 2 additions & 6 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,12 @@ class LLVMToSPIRVDbgTran {
if (!DL.isUnknown()) {
DILocation DIL(DL.getAsMDNode());
auto File = BM->getString(DIL.getFilename().str());
// ToDo: SPIR-V rev.31 cannot add debug info for instructions without ids.
// This limitation needs to be addressed.
if (!BV->hasId())
return;
BM->addLine(BV, File, DIL.getLineNumber(), DIL.getColumnNumber());
BM->addLine(BV, File->getId(), DIL.getLineNumber(), DIL.getColumnNumber());
}
} else if (auto F = dyn_cast<Function>(V)) {
if (auto DIS = getDISubprogram(F)) {
auto File = BM->getString(DIS.getFilename().str());
BM->addLine(BV, File, DIS.getLineNumber(), 0);
BM->addLine(BV, File->getId(), DIS.getLineNumber(), 0);
}
}
}
Expand Down
53 changes: 36 additions & 17 deletions lib/SPIRV/libSPIRV/SPIRVEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,37 @@ SPIRVEntry::encodeName(spv_ostream &O) const {
O << SPIRVName(this, Name);
}

bool SPIRVEntry::isEndOfBlock() const {
switch (OpCode) {
case OpBranch:
case OpBranchConditional:
case OpSwitch:
case OpKill:
case OpReturn:
case OpReturnValue:
case OpUnreachable:
return true;
default:
return false;
}
}

void
SPIRVEntry::encodeLine(spv_ostream &O) const {
if (!Module)
return;
const std::shared_ptr<const SPIRVLine> &CurrLine = Module->getCurrentLine();
if (Line && ((CurrLine && *Line != *CurrLine) || !CurrLine)) {
O << *Line;
Module->setCurrentLine(Line);
}
if (isEndOfBlock() || OpCode == OpNoLine)
Module->setCurrentLine(nullptr);
}

void
SPIRVEntry::encodeAll(spv_ostream &O) const {
encodeLine(O);
encodeWordCountOpCode(O);
encode(O);
encodeChildren(O);
Expand Down Expand Up @@ -286,21 +315,11 @@ SPIRVEntry::takeDecorates(SPIRVEntry *E){
}

void
SPIRVEntry::setLine(SPIRVLine *L){
SPIRVEntry::setLine(const std::shared_ptr<const SPIRVLine>& L){
Line = L;
L->setTargetId(Id);
SPIRVDBG(spvdbgs() << "[setLine] " << *L << '\n';)
}

void
SPIRVEntry::takeLine(SPIRVEntry *E){
Line = E->Line;
if (Line == nullptr)
return;
Line->setTargetId(Id);
E->Line = nullptr;
}

void
SPIRVEntry::addMemberDecorate(const SPIRVMemberDecorate *Dec){
assert(canHaveMemberDecorates() && MemberDecorates.find(Dec->getPair()) ==
Expand Down Expand Up @@ -337,7 +356,6 @@ SPIRVEntry::takeAnnotations(SPIRVForward *E){
Module->setName(this, E->getName());
takeDecorates(E);
takeMemberDecorates(E);
takeLine(E);
if (OpCode == OpFunction)
static_cast<SPIRVFunction *>(this)->takeExecutionModes(E);
}
Expand Down Expand Up @@ -496,23 +514,24 @@ _SPIRV_IMP_ENCDEC3(SPIRVMemberName, Target, MemberNumber, Str)

void
SPIRVLine::encode(spv_ostream &O) const {
getEncoder(O) << Target << FileName << Line << Column;
getEncoder(O) << FileName << Line << Column;
}

void
SPIRVLine::decode(std::istream &I) {
getDecoder(I) >> Target >> FileName >> Line >> Column;
Module->addLine(getOrCreateTarget(), get<SPIRVString>(FileName), Line, Column);
getDecoder(I) >> FileName >> Line >> Column;
std::shared_ptr<const SPIRVLine> L(this);
Module->setCurrentLine(L);
}

void
SPIRVLine::validate() const {
assert(OpCode == OpLine);
assert(WordCount == 5);
assert(get<SPIRVEntry>(Target));
assert(WordCount == 4);
assert(get<SPIRVEntry>(FileName)->getOpCode() == OpString);
assert(Line != SPIRVWORD_MAX);
assert(Column != SPIRVWORD_MAX);
assert(!hasId());
}

void
Expand Down
46 changes: 31 additions & 15 deletions lib/SPIRV/libSPIRV/SPIRVEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class SPIRVEntry {
virtual SPIRVEncoder getEncoder(spv_ostream &)const;
SPIRVErrorLog &getErrorLog()const;
SPIRVId getId() const { assert(hasId()); return Id;}
SPIRVLine *getLine() const { return Line;}
std::shared_ptr<const SPIRVLine> getLine() const { return Line;}
SPIRVLinkageTypeKind getLinkageType() const;
Op getOpCode() const { return OpCode;}
SPIRVModule *getModule() const { return Module;}
Expand All @@ -266,6 +266,7 @@ class SPIRVEntry {
bool isControlBarrier() const { return OpCode == OpControlBarrier;}
bool isMemoryBarrier() const { return OpCode == OpMemoryBarrier;}
bool isVariable() const { return OpCode == OpVariable;}
bool isEndOfBlock() const;
virtual bool isInst() const { return false;}
virtual bool isOperandLiteral(unsigned Index) const {
assert(0 && "not implemented");
Expand All @@ -283,15 +284,14 @@ class SPIRVEntry {
void eraseMemberDecorate(SPIRVWord MemberNumber, Decoration Kind);
void setHasNoId() { Attrib |= SPIRVEA_NOID;}
void setId(SPIRVId TheId) { Id = TheId;}
void setLine(SPIRVLine*);
void setLine(const std::shared_ptr<const SPIRVLine>& L);
void setLinkageType(SPIRVLinkageTypeKind);
void setModule(SPIRVModule *TheModule);
void setName(const std::string& TheName);
virtual void setScope(SPIRVEntry *Scope){};
void takeAnnotations(SPIRVForward *);
void takeDecorates(SPIRVEntry *);
void takeMemberDecorates(SPIRVEntry *);
void takeLine(SPIRVEntry *);

/// After a SPIRV entry is created during reading SPIRV binary by default
/// constructor, this function is called to allow the SPIRV entry to resize
Expand All @@ -310,6 +310,7 @@ class SPIRVEntry {

friend spv_ostream &operator<<(spv_ostream &O, const SPIRVEntry &E);
friend std::istream &operator>>(std::istream &I, SPIRVEntry &E);
virtual void encodeLine(spv_ostream &O) const;
virtual void encodeAll(spv_ostream &O) const;
virtual void encodeName(spv_ostream &O) const;
virtual void encodeChildren(spv_ostream &O)const;
Expand Down Expand Up @@ -363,7 +364,7 @@ class SPIRVEntry {

DecorateMapType Decorates;
MemberDecorateMapType MemberDecorates;
SPIRVLine *Line;
std::shared_ptr<const SPIRVLine> Line;
};

class SPIRVEntryNoIdGeneric:public SPIRVEntry {
Expand Down Expand Up @@ -488,25 +489,30 @@ class SPIRVString:public SPIRVEntry {
std::string Str;
};

class SPIRVLine:public SPIRVAnnotation<OpLine> {
class SPIRVLine: public SPIRVEntry {
public:
static const SPIRVWord WC = 5;
static const SPIRVWord WC = 4;
// Complete constructor
SPIRVLine(const SPIRVEntry *TheTarget, SPIRVId TheFileName, SPIRVWord TheLine,
SPIRVWord TheColumn)
:SPIRVAnnotation(TheTarget, WC), FileName(TheFileName), Line(TheLine),
Column(TheColumn){
SPIRVLine(SPIRVModule *M, SPIRVId TheFileName, SPIRVWord TheLine,
SPIRVWord TheColumn)
:SPIRVEntry(M, WC, OpLine),
FileName(TheFileName),
Line(TheLine),
Column(TheColumn) {
Attrib = SPIRVEA_NOID | SPIRVEA_NOTYPE;
validate();
}
// Incomplete constructor
SPIRVLine():FileName(SPIRVID_INVALID), Line(SPIRVWORD_MAX),
Column(SPIRVWORD_MAX){}
SPIRVLine():SPIRVEntry(OpLine), FileName(SPIRVID_INVALID), Line(SPIRVWORD_MAX),
Column(SPIRVWORD_MAX) {
Attrib = SPIRVEA_NOID | SPIRVEA_NOTYPE;
}

SPIRVWord getColumn() const {
return Column;
}

void setColumn(SPIRVWord column) {
void setColumn(const SPIRVWord column) {
Column = column;
}

Expand All @@ -518,18 +524,28 @@ class SPIRVLine:public SPIRVAnnotation<OpLine> {
return get<SPIRVString>(FileName)->getStr();
}

void setFileName(SPIRVId fileName) {
void setFileName(const SPIRVId fileName) {
FileName = fileName;
}

SPIRVWord getLine() const {
return Line;
}

void setLine(SPIRVWord line) {
void setLine(const SPIRVWord line) {
Line = line;
}

bool operator!=(const SPIRVLine &O) const {
return !equals(O.FileName, O.Line, O.Column);
}

bool equals(const SPIRVId TheFileName,
const SPIRVWord TheLine,
const SPIRVWord TheColumn) const {
return FileName == TheFileName && Line == TheLine && Column == TheColumn;
}

protected:
_SPIRV_DCL_ENCDEC
void validate() const;
Expand Down
5 changes: 2 additions & 3 deletions lib/SPIRV/libSPIRV/SPIRVFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===- SPIRVFunction.cpp Class to represent a SPIR-V Function --*- C++ -*-===//
//===- SPIRVFunction.cpp - Class to represent a SPIR-V Function --*- C++ -*-===//
//
// The LLVM/SPIRV Translator
//
Expand Down Expand Up @@ -142,8 +142,7 @@ SPIRVFunction::decodeBB(SPIRVDecoder &Decoder) {
break;
}

if (Decoder.OpCode == OpName ||
Decoder.OpCode == OpDecorate) {
if (Decoder.OpCode == OpLine) {
Decoder.getEntry();
continue;
}
Expand Down
Loading

0 comments on commit c370052

Please sign in to comment.