Skip to content

Commit f8b112a

Browse files
Artem GindinsonAlexeySotkin
authored andcommitted
[NFC] 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 0420de4 commit f8b112a

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

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

@@ -1584,30 +1585,32 @@ std::vector<std::pair<Decoration, std::string>>
15841585
tryParseIntelFPGAAnnotationString(StringRef AnnotatedCode) {
15851586
std::vector<std::pair<Decoration, std::string>> Decorates;
15861587

1587-
size_t OpenBracketNum = AnnotatedCode.count('{');
1588-
size_t CloseBracketNum = AnnotatedCode.count('}');
1589-
if (OpenBracketNum != CloseBracketNum)
1590-
return {};
1591-
1592-
for (size_t I = 0; I < OpenBracketNum; ++I) {
1593-
size_t From = AnnotatedCode.find('{');
1594-
size_t To = AnnotatedCode.find('}', From);
1595-
StringRef AnnotatedDecoration = AnnotatedCode.substr(From + 1, To - 1);
1596-
std::pair<StringRef, StringRef> D = AnnotatedDecoration.split(':');
1597-
1598-
StringRef F = D.first, S = D.second;
1599-
StringRef Value;
1588+
// Intel FPGA decorations are separated into
1589+
// {word} OR {word:value,value,...} blocks
1590+
std::regex DecorationRegex("\\{[\\w:,]+\\}");
1591+
using RegexIterT = std::regex_iterator<StringRef::const_iterator>;
1592+
RegexIterT DecorationsIt(AnnotatedCode.begin(), AnnotatedCode.end(),
1593+
DecorationRegex);
1594+
RegexIterT DecorationsEnd;
1595+
for (; DecorationsIt != DecorationsEnd; ++DecorationsIt) {
1596+
// Drop the braces surrounding the actual decoration
1597+
const StringRef AnnotatedDecoration = AnnotatedCode.substr(
1598+
DecorationsIt->position() + 1, DecorationsIt->length() - 2);
1599+
std::pair<StringRef, StringRef> Split = AnnotatedDecoration.split(':');
1600+
StringRef Name = Split.first, ValueStr = Split.second;
1601+
1602+
StringRef Annotation;
16001603
Decoration Dec;
1601-
if (F == "pump") {
1602-
Dec = llvm::StringSwitch<Decoration>(S)
1604+
if (Name == "pump") {
1605+
Dec = llvm::StringSwitch<Decoration>(ValueStr)
16031606
.Case("1", DecorationSinglepumpINTEL)
16041607
.Case("2", DecorationDoublepumpINTEL);
1605-
} else if (F == "register") {
1608+
} else if (Name == "register") {
16061609
Dec = DecorationRegisterINTEL;
1607-
} else if (F == "simple_dual_port") {
1610+
} else if (Name == "simple_dual_port") {
16081611
Dec = DecorationSimpleDualPortINTEL;
16091612
} else {
1610-
Dec = llvm::StringSwitch<Decoration>(F)
1613+
Dec = llvm::StringSwitch<Decoration>(Name)
16111614
.Case("memory", DecorationMemoryINTEL)
16121615
.Case("numbanks", DecorationNumbanksINTEL)
16131616
.Case("bankwidth", DecorationBankwidthINTEL)
@@ -1618,13 +1621,12 @@ tryParseIntelFPGAAnnotationString(StringRef AnnotatedCode) {
16181621
.Case("force_pow2_depth", DecorationForcePow2DepthINTEL)
16191622
.Default(DecorationUserSemantic);
16201623
if (Dec == DecorationUserSemantic)
1621-
Value = AnnotatedCode.substr(From, To + 1);
1624+
Annotation = AnnotatedDecoration;
16221625
else
1623-
Value = S;
1626+
Annotation = ValueStr;
16241627
}
16251628

1626-
Decorates.emplace_back(Dec, Value.str());
1627-
AnnotatedCode = AnnotatedCode.drop_front(To + 1);
1629+
Decorates.emplace_back(Dec, Annotation.str());
16281630
}
16291631
return Decorates;
16301632
}

0 commit comments

Comments
 (0)