Skip to content

Commit 68c7513

Browse files
author
joaosaffran
committed
refactoring to remove use of map and string
1 parent d391727 commit 68c7513

File tree

2 files changed

+28
-55
lines changed

2 files changed

+28
-55
lines changed

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "llvm/ADT/IndexedMap.h"
910
#include "llvm/BinaryFormat/DXContainer.h"
1011
#include "llvm/Support/BinaryStreamWriter.h"
1112
#include "llvm/Support/raw_ostream.h"
12-
#include <map>
13-
#include <string>
1413

1514
namespace llvm {
1615

1716
class raw_ostream;
1817

1918
namespace mcdxbc {
2019

21-
class StreamOffsetHelper {
22-
private:
23-
std::map<std::string, std::pair<uint32_t, uint32_t>> OffsetsMaping;
24-
BinaryStreamWriter &Stream;
25-
26-
public:
27-
explicit StreamOffsetHelper(BinaryStreamWriter &Stream) : Stream(Stream) {}
28-
29-
Error addOffset(std::string Key);
30-
31-
void addRewriteValue(std::string Key);
32-
33-
Error rewrite();
34-
};
35-
3620
struct RootSignatureDesc {
3721
dxbc::RootSignatureHeader Header;
3822
SmallVector<dxbc::RootParameter> Parameters;

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,33 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/MC/DXContainerRootSignature.h"
10+
#include "llvm/ADT/SmallVector.h"
1011
#include "llvm/ADT/Twine.h"
1112
#include "llvm/Support/BinaryStreamWriter.h"
13+
#include <cstdint>
14+
#include <sys/types.h>
1215

1316
using namespace llvm;
1417
using namespace llvm::mcdxbc;
1518

16-
Error StreamOffsetHelper::addOffset(std::string Key) {
19+
Error setRewrite(BinaryStreamWriter &Stream, uint32_t &Offset) {
1720
const uint32_t DummyValue = std::numeric_limits<uint32_t>::max();
1821

19-
uint32_t Offset = Stream.getOffset();
20-
auto Value = std::make_pair(Offset, DummyValue);
21-
22-
OffsetsMaping.insert_or_assign(Key, Value);
22+
Offset = Stream.getOffset();
2323

2424
if (Error Err = Stream.writeInteger(DummyValue))
2525
return Err;
2626

2727
return Error::success();
2828
}
2929

30-
void StreamOffsetHelper::addRewriteValue(std::string Key) {
31-
auto It = OffsetsMaping.find(Key);
32-
assert(It != OffsetsMaping.end() && "Offset address was not found.");
33-
auto [Offset, _] = It->second;
34-
35-
uint32_t Value = Stream.getOffset();
36-
37-
std::pair<uint32_t, uint32_t> NewValue = std::make_pair(Offset, Value);
38-
OffsetsMaping.insert_or_assign(Key, NewValue);
39-
}
40-
41-
Error StreamOffsetHelper::rewrite() {
42-
for (auto &[Key, RewriteInfo] : OffsetsMaping) {
43-
auto [Position, Value] = RewriteInfo;
44-
assert(Value != std::numeric_limits<uint32_t>::max());
30+
Error rewriteOffset(BinaryStreamWriter &Stream, uint32_t Offset) {
31+
uint64_t Value = Stream.getOffset();
32+
Stream.setOffset(Offset);
33+
if (Error Err = Stream.writeInteger((uint32_t)Value))
34+
return Err;
4535

46-
Stream.setOffset(Position);
47-
if (Error Err = Stream.writeInteger(Value))
48-
return Err;
49-
}
36+
Stream.setOffset(Value);
5037

5138
return Error::success();
5239
}
@@ -55,8 +42,6 @@ Error RootSignatureDesc::write(raw_ostream &OS) const {
5542
std::vector<uint8_t> Buffer(getSizeInBytes());
5643
BinaryStreamWriter Writer(Buffer, llvm::endianness::little);
5744

58-
StreamOffsetHelper OffsetMap(Writer);
59-
6045
const uint32_t NumParameters = Parameters.size();
6146
const uint32_t Zero = 0;
6247

@@ -66,7 +51,8 @@ Error RootSignatureDesc::write(raw_ostream &OS) const {
6651
if (Error Err = Writer.writeInteger(NumParameters))
6752
return Err;
6853

69-
if (Error Err = OffsetMap.addOffset("header"))
54+
uint32_t HeaderPoint;
55+
if (Error Err = setRewrite(Writer, HeaderPoint))
7056
return Err;
7157

7258
// Static samplers still not implemented
@@ -79,27 +65,31 @@ Error RootSignatureDesc::write(raw_ostream &OS) const {
7965
if (Error Err = Writer.writeInteger(Header.Flags))
8066
return Err;
8167

82-
OffsetMap.addRewriteValue("header");
68+
if (Error Err = rewriteOffset(Writer, HeaderPoint))
69+
return Err;
8370

84-
for (size_t It = 0; It < Parameters.size(); It++) {
85-
const auto &P = Parameters[It];
71+
SmallVector<uint32_t> ParamsOffset;
72+
for (const auto &P : Parameters) {
8673

8774
if (Error Err = Writer.writeEnum(P.ParameterType))
8875
return Err;
8976

9077
if (Error Err = Writer.writeEnum(P.ShaderVisibility))
9178
return Err;
9279

93-
std::string Key = ("parameters" + Twine(It)).str();
94-
if (Error Err = OffsetMap.addOffset(Key))
80+
uint32_t Offset;
81+
if (Error Err = setRewrite(Writer, Offset))
9582
return Err;
83+
ParamsOffset.push_back(Offset);
9684
}
9785

98-
for (size_t It = 0; It < Parameters.size(); It++) {
99-
const auto &P = Parameters[It];
86+
size_t It = 0;
87+
for (const auto &P : Parameters) {
10088

101-
std::string Key = ("parameters" + Twine(It)).str();
102-
OffsetMap.addRewriteValue(Key);
89+
auto Offset = ParamsOffset[It];
90+
if (Error Err = rewriteOffset(Writer, Offset))
91+
return Err;
92+
It++;
10393

10494
switch (P.ParameterType) {
10595
case dxbc::RootParameterType::Constants32Bit: {
@@ -115,8 +105,7 @@ Error RootSignatureDesc::write(raw_ostream &OS) const {
115105
}
116106
}
117107

118-
if (Error Err = OffsetMap.rewrite())
119-
return Err;
108+
assert(It == NumParameters);
120109

121110
llvm::ArrayRef<char> BufferRef(reinterpret_cast<char *>(Buffer.data()),
122111
Buffer.size());

0 commit comments

Comments
 (0)