Skip to content

Commit 1bdd66b

Browse files
Artem Gindinsonvladimirlaz
authored andcommitted
Improve parsing of FPGA memory decorations
- Use regex to avoid regular `find`s and substring assignments; - Make variable names more readable. Signed-off-by: Artem Gindinson <[email protected]>
1 parent d6140b0 commit 1bdd66b

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include <iostream>
8181
#include <memory>
8282
#include <queue>
83+
#include <regex>
8384
#include <set>
8485
#include <vector>
8586

@@ -1592,30 +1593,32 @@ std::vector<std::pair<Decoration, std::string>>
15921593
tryParseIntelFPGAAnnotationString(StringRef AnnotatedCode) {
15931594
std::vector<std::pair<Decoration, std::string>> Decorates;
15941595

1595-
size_t OpenBracketNum = AnnotatedCode.count('{');
1596-
size_t CloseBracketNum = AnnotatedCode.count('}');
1597-
if (OpenBracketNum != CloseBracketNum)
1598-
return {};
1599-
1600-
for (size_t I = 0; I < OpenBracketNum; ++I) {
1601-
size_t From = AnnotatedCode.find('{');
1602-
size_t To = AnnotatedCode.find('}', From);
1603-
StringRef AnnotatedDecoration = AnnotatedCode.substr(From + 1, To - 1);
1604-
std::pair<StringRef, StringRef> D = AnnotatedDecoration.split(':');
1605-
1606-
StringRef F = D.first, S = D.second;
1607-
StringRef Value;
1596+
// Intel FPGA decorations are separated into
1597+
// {word} OR {word:value,value,...} blocks
1598+
std::regex DecorationRegex("\\{[\\w:,]+\\}");
1599+
using RegexIterT = std::regex_iterator<StringRef::const_iterator>;
1600+
RegexIterT DecorationsIt(AnnotatedCode.begin(), AnnotatedCode.end(),
1601+
DecorationRegex);
1602+
RegexIterT DecorationsEnd;
1603+
for (; DecorationsIt != DecorationsEnd; ++DecorationsIt) {
1604+
// Drop the braces surrounding the actual decoration
1605+
const StringRef AnnotatedDecoration = AnnotatedCode.substr(
1606+
DecorationsIt->position() + 1, DecorationsIt->length() - 2);
1607+
std::pair<StringRef, StringRef> Split = AnnotatedDecoration.split(':');
1608+
StringRef Name = Split.first, ValueStr = Split.second;
1609+
1610+
StringRef Annotation;
16081611
Decoration Dec;
1609-
if (F == "pump") {
1610-
Dec = llvm::StringSwitch<Decoration>(S)
1612+
if (Name == "pump") {
1613+
Dec = llvm::StringSwitch<Decoration>(ValueStr)
16111614
.Case("1", DecorationSinglepumpINTEL)
16121615
.Case("2", DecorationDoublepumpINTEL);
1613-
} else if (F == "register") {
1616+
} else if (Name == "register") {
16141617
Dec = DecorationRegisterINTEL;
1615-
} else if (F == "simple_dual_port") {
1618+
} else if (Name == "simple_dual_port") {
16161619
Dec = DecorationSimpleDualPortINTEL;
16171620
} else {
1618-
Dec = llvm::StringSwitch<Decoration>(F)
1621+
Dec = llvm::StringSwitch<Decoration>(Name)
16191622
.Case("memory", DecorationMemoryINTEL)
16201623
.Case("numbanks", DecorationNumbanksINTEL)
16211624
.Case("bankwidth", DecorationBankwidthINTEL)
@@ -1626,13 +1629,12 @@ tryParseIntelFPGAAnnotationString(StringRef AnnotatedCode) {
16261629
.Case("force_pow2_depth", DecorationForcePow2DepthINTEL)
16271630
.Default(DecorationUserSemantic);
16281631
if (Dec == DecorationUserSemantic)
1629-
Value = AnnotatedCode.substr(From, To + 1);
1632+
Annotation = AnnotatedDecoration;
16301633
else
1631-
Value = S;
1634+
Annotation = ValueStr;
16321635
}
16331636

1634-
Decorates.emplace_back(Dec, Value.str());
1635-
AnnotatedCode = AnnotatedCode.drop_front(To + 1);
1637+
Decorates.emplace_back(Dec, Annotation.str());
16361638
}
16371639
return Decorates;
16381640
}

0 commit comments

Comments
 (0)