Skip to content

Commit f1926cc

Browse files
committed
TypeGraph: Add "--tree-builder-v2" flag
This will eventually be used to enable running with Tree Builder v2. For now, when it is disabled it puts CodeGen v2 into compatibility mode, disabling features which weren't present in CodeGen v1 so that its output can be understood by Tree Builder v1.
1 parent 15f7423 commit f1926cc

File tree

9 files changed

+80
-17
lines changed

9 files changed

+80
-17
lines changed

oi/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ void CodeGen::transform(type_graph::TypeGraph& typeGraph) {
725725
type_graph::TypeIdentifier::createPass(config_.passThroughTypes));
726726
}
727727
pm.addPass(type_graph::RemoveIgnored::createPass(config_.membersToStub));
728-
pm.addPass(type_graph::AddPadding::createPass());
728+
pm.addPass(type_graph::AddPadding::createPass(config_.features));
729729
pm.addPass(type_graph::NameGen::createPass());
730730
pm.addPass(type_graph::AlignmentCalc::createPass());
731731
pm.addPass(type_graph::RemoveTopLevelPointer::createPass());

oi/Features.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ std::string_view featureHelp(Feature f) {
3333
case Feature::CaptureThriftIsset:
3434
return "Capture isset data for Thrift object.";
3535
case Feature::TypeGraph:
36-
return "Use Type Graph for code generation (CodeGen V2).";
36+
return "Use Type Graph for code generation (CodeGen v2).";
3737
case Feature::TypedDataSegment:
3838
return "Use Typed Data Segment in generated code.";
39+
case Feature::TreeBuilderV2:
40+
return "Use Tree Builder v2 for reading the data segment";
3941
case Feature::GenJitDebug:
4042
return "Generate debug information for the JIT object.";
4143
case Feature::JitLogging:

oi/Features.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
X(CaptureThriftIsset, "capture-thrift-isset") \
2929
X(TypeGraph, "type-graph") \
3030
X(TypedDataSegment, "typed-data-segment") \
31+
X(TreeBuilderV2, "tree-builder-v2") \
3132
X(GenJitDebug, "gen-jit-debug") \
3233
X(JitLogging, "jit-logging") \
3334
X(JitTiming, "jit-timing") \

oi/type_graph/AddPadding.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717

1818
#include <cassert>
1919

20+
#include "Flattener.h"
2021
#include "TypeGraph.h"
2122

2223
template <typename T>
2324
using ref = std::reference_wrapper<T>;
2425

26+
using ObjectIntrospection::Feature;
27+
using ObjectIntrospection::FeatureSet;
28+
2529
namespace type_graph {
2630

27-
Pass AddPadding::createPass() {
28-
auto fn = [](TypeGraph& typeGraph) {
29-
AddPadding pass(typeGraph);
31+
Pass AddPadding::createPass(FeatureSet features) {
32+
auto fn = [features](TypeGraph& typeGraph) {
33+
AddPadding pass(typeGraph, features);
3034
for (auto& type : typeGraph.rootTypes()) {
3135
pass.visit(type);
3236
}
@@ -67,6 +71,19 @@ void AddPadding::visit(Class& c) {
6771
if (i >= 1) {
6872
addPadding(c.members[i - 1], c.members[i].bitOffset, paddedMembers);
6973
}
74+
75+
if (!features_[Feature::TreeBuilderV2] &&
76+
c.members[i].name.starts_with(Flattener::ParentPrefix)) {
77+
// CodeGen v1 can't handle parent containers. It replaces them with
78+
// padding.
79+
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int8);
80+
auto& paddingArray =
81+
typeGraph_.makeType<Array>(primitive, c.members[i].type().size());
82+
paddedMembers.emplace_back(paddingArray, MemberPrefix,
83+
c.members[i].bitOffset);
84+
continue;
85+
}
86+
7087
paddedMembers.push_back(c.members[i]);
7188
}
7289

oi/type_graph/AddPadding.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
#pragma once
1717

18-
#include <functional>
18+
#include <string>
1919
#include <unordered_set>
2020

2121
#include "PassManager.h"
2222
#include "Types.h"
2323
#include "Visitor.h"
24+
#include "oi/Features.h"
2425

2526
namespace type_graph {
2627

@@ -35,9 +36,11 @@ class TypeGraph;
3536
*/
3637
class AddPadding final : public RecursiveVisitor {
3738
public:
38-
static Pass createPass();
39+
static Pass createPass(ObjectIntrospection::FeatureSet features);
3940

40-
explicit AddPadding(TypeGraph& typeGraph) : typeGraph_(typeGraph) {
41+
explicit AddPadding(TypeGraph& typeGraph,
42+
ObjectIntrospection::FeatureSet features)
43+
: typeGraph_(typeGraph), features_(features) {
4144
}
4245

4346
using RecursiveVisitor::visit;
@@ -50,6 +53,7 @@ class AddPadding final : public RecursiveVisitor {
5053
private:
5154
std::unordered_set<Type*> visited_;
5255
TypeGraph& typeGraph_;
56+
ObjectIntrospection::FeatureSet features_;
5357

5458
void addPadding(const Member& prevMember,
5559
uint64_t paddingEndBits,

oi/type_graph/Flattener.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void flattenParent(const Parent& parent,
7171
} else if (Container* parentContainer =
7272
dynamic_cast<Container*>(&parentType)) {
7373
// Create a new member to represent this parent container
74-
flattenedMembers.emplace_back(*parentContainer, "__parent",
74+
flattenedMembers.emplace_back(*parentContainer, Flattener::ParentPrefix,
7575
parent.bitOffset);
7676
} else {
7777
throw std::runtime_error("Invalid type for parent");

oi/type_graph/Flattener.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#pragma once
1717

18+
#include <string>
1819
#include <unordered_set>
1920
#include <vector>
2021

@@ -42,6 +43,8 @@ class Flattener : public RecursiveVisitor {
4243
void visit(Class& c) override;
4344
void visit(Container& c) override;
4445

46+
static const inline std::string ParentPrefix = "__oi_parent";
47+
4548
private:
4649
std::unordered_set<Type*> visited_;
4750
std::vector<Member> flattened_members_;

test/test_add_padding.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
#include "test/type_graph_utils.h"
66

77
using namespace type_graph;
8+
using ObjectIntrospection::Feature;
9+
using ObjectIntrospection::FeatureSet;
10+
11+
namespace {
12+
void test(std::vector<std::reference_wrapper<type_graph::Type>> rootTypes,
13+
std::string_view expectedAfter) {
14+
FeatureSet features;
15+
features[Feature::TreeBuilderV2] = true;
16+
::test(AddPadding::createPass({}), rootTypes, expectedAfter);
17+
}
18+
} // namespace
819

920
TEST(AddPaddingTest, BetweenMembers) {
1021
auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
@@ -13,7 +24,7 @@ TEST(AddPaddingTest, BetweenMembers) {
1324
myclass.members.push_back(Member{myint8, "n1", 0});
1425
myclass.members.push_back(Member{myint64, "n2", 8 * 8});
1526

16-
test(AddPadding::createPass(), {myclass}, R"(
27+
test({myclass}, R"(
1728
[0] Class: MyClass (size: 16)
1829
Member: n1 (offset: 0)
1930
Primitive: int8_t
@@ -32,7 +43,7 @@ TEST(AddPaddingTest, AtEnd) {
3243
myclass.members.push_back(Member{myint64, "n1", 0});
3344
myclass.members.push_back(Member{myint8, "n2", 8 * 8});
3445

35-
test(AddPadding::createPass(), {myclass}, R"(
46+
test({myclass}, R"(
3647
[0] Struct: MyStruct (size: 16)
3748
Member: n1 (offset: 0)
3849
Primitive: int64_t
@@ -51,7 +62,7 @@ TEST(AddPaddingTest, UnionNotPadded) {
5162
myclass.members.push_back(Member{myint64, "n1", 0});
5263
myclass.members.push_back(Member{myint8, "n2", 0});
5364

54-
test(AddPadding::createPass(), {myclass}, R"(
65+
test({myclass}, R"(
5566
[0] Union: MyUnion (size: 8)
5667
Member: n1 (offset: 0)
5768
Primitive: int64_t
@@ -86,7 +97,7 @@ TEST(AddPaddingTest, Bitfields) {
8697
myclass.members.push_back(b4);
8798
myclass.members.push_back(n);
8899

89-
test(AddPadding::createPass(), {myclass}, R"(
100+
test({myclass}, R"(
90101
[0] Class: MyClass (size: 16)
91102
Member: b1 (offset: 0, bitsize: 3)
92103
Primitive: int64_t
@@ -111,4 +122,29 @@ TEST(AddPaddingTest, Bitfields) {
111122
)");
112123
}
113124

125+
TEST(AddPaddingTest, CodeGenCompatibility) {
126+
auto myint = Primitive{Primitive::Kind::Int32};
127+
auto vector = getVector(1);
128+
vector.templateParams.push_back(TemplateParam{myint});
129+
130+
auto myclass = Class{0, Class::Kind::Class, "MyClass", 24};
131+
myclass.members.push_back(Member{vector, "__oi_parent", 0});
132+
133+
FeatureSet features;
134+
features[Feature::TreeBuilderV2] = false;
135+
test(AddPadding::createPass(features), {myclass}, R"(
136+
[0] Class: MyClass (size: 24)
137+
Member: __oi_parent (offset: 0)
138+
[1] Container: std::vector (size: 24)
139+
Param
140+
Primitive: int32_t
141+
)",
142+
R"(
143+
[0] Class: MyClass (size: 24)
144+
Member: __oi_padding (offset: 0)
145+
[1] Array: (length: 24)
146+
Primitive: int8_t
147+
)");
148+
}
149+
114150
// TODO test we follow class members, templates, children

test/test_flattener.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ TEST(FlattenerTest, ParentContainer) {
705705
)",
706706
R"(
707707
[0] Class: ClassA (size: 32)
708-
Member: __parent (offset: 0)
708+
Member: __oi_parent (offset: 0)
709709
[1] Container: std::vector (size: 24)
710710
Param
711711
Primitive: int32_t
@@ -735,11 +735,11 @@ TEST(FlattenerTest, ParentTwoContainers) {
735735
)",
736736
R"(
737737
[0] Class: ClassA (size: 48)
738-
Member: __parent (offset: 0)
738+
Member: __oi_parent (offset: 0)
739739
[1] Container: std::vector (size: 24)
740740
Param
741741
Primitive: int32_t
742-
Member: __parent (offset: 24)
742+
Member: __oi_parent (offset: 24)
743743
[1]
744744
)");
745745
}
@@ -772,7 +772,7 @@ TEST(FlattenerTest, ParentClassAndContainer) {
772772
[0] Class: ClassA (size: 32)
773773
Member: b (offset: 0)
774774
Primitive: int32_t
775-
Member: __parent (offset: 8)
775+
Member: __oi_parent (offset: 8)
776776
[1] Container: std::vector (size: 24)
777777
Param
778778
Primitive: int32_t

0 commit comments

Comments
 (0)