Skip to content

Commit ea11cbc

Browse files
Fix multiple void return points and fix val pointer copies (#24)
1 parent bd03617 commit ea11cbc

File tree

64 files changed

+439
-192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+439
-192
lines changed

nautilus/include/nautilus/val_ptr.hpp

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,18 @@ class base_ptr_val {
8383
base_ptr_val() : value() {};
8484
#ifdef ENABLE_TRACING
8585
base_ptr_val(ValuePtrType ptr) : state(tracing::traceConstant((void*) ptr)), value(ptr) {};
86-
#else
87-
base_ptr_val(ValuePtrType ptr) : value(ptr) {};
88-
#endif
8986
base_ptr_val(ValuePtrType ptr, tracing::value_ref tc) : state(tc), value(ptr) {};
90-
base_ptr_val(val<ValuePtrType>& otherValue) : state(otherValue.state), value(otherValue.value) {
91-
}
87+
base_ptr_val(ValuePtrType ptr, tracing::TypedValueRefHolder tc) : state(tc), value(ptr) {};
88+
9289
base_ptr_val(tracing::value_ref ref) : state(ref), value(nullptr) {
9390
}
91+
#else
92+
base_ptr_val(ValuePtrType ptr) : value(ptr) {};
93+
#endif
9494

95-
val<ValuePtrType>& operator=(const val<ValuePtrType>& other) {
9695
#ifdef ENABLE_TRACING
97-
if (tracing::inTracer()) {
98-
tracing::traceAssignment(this->state, other.state, tracing::to_type<ValuePtrType>());
99-
}
96+
tracing::TypedValueRefHolder state;
10097
#endif
101-
this->value = other.value;
102-
return *this;
103-
};
104-
105-
tracing::value_ref state;
10698
ValuePtrType value;
10799
};
108100

@@ -114,8 +106,31 @@ class val<ValuePtrType> : public base_ptr_val<ValuePtrType> {
114106
// enable cast to type T
115107
template <typename T>
116108
operator val<T*>() const {
109+
#ifdef ENABLE_TRACING
117110
return val<T*>((T*) this->value, this->state);
111+
#else
112+
return val<T*>((T*) this->value);
113+
#endif
118114
}
115+
116+
#ifdef ENABLE_TRACING
117+
val(const val<ValuePtrType>& otherValue) : base_ptr_val<ValuePtrType>(otherValue.value, tracing::traceCopy(otherValue.state)) {
118+
}
119+
120+
#else
121+
val(const val<ValuePtrType>& otherValue) : base_ptr_val<ValuePtrType>(otherValue.value) {
122+
}
123+
#endif
124+
125+
val<ValuePtrType>& operator=(const val<ValuePtrType>& other) {
126+
#ifdef ENABLE_TRACING
127+
if (tracing::inTracer()) {
128+
tracing::traceAssignment(this->state, other.state, tracing::to_type<ValuePtrType>());
129+
}
130+
#endif
131+
this->value = other.value;
132+
return *this;
133+
};
119134
};
120135

121136
template <is_arithmetic_ptr ValuePtrType>
@@ -124,6 +139,24 @@ class val<ValuePtrType> : public base_ptr_val<ValuePtrType> {
124139
using base_ptr_val<ValuePtrType>::base_ptr_val;
125140
using ValType = typename base_ptr_val<ValuePtrType>::ValType;
126141

142+
#ifdef ENABLE_TRACING
143+
val(const val<ValuePtrType>& otherValue) : base_ptr_val<ValuePtrType>(otherValue.value, tracing::traceCopy(otherValue.state)) {
144+
}
145+
#else
146+
val(const val<ValuePtrType>& otherValue) : base_ptr_val<ValuePtrType>(otherValue.value) {
147+
}
148+
#endif
149+
150+
val<ValuePtrType>& operator=(const val<ValuePtrType>& other) {
151+
#ifdef ENABLE_TRACING
152+
if (tracing::inTracer()) {
153+
tracing::traceAssignment(this->state, other.state, tracing::to_type<ValuePtrType>());
154+
}
155+
#endif
156+
this->value = other.value;
157+
return *this;
158+
};
159+
127160
val<ValType&> operator*() {
128161
#ifdef ENABLE_TRACING
129162
return val<ValType&>(*this, this->state);
@@ -166,6 +199,25 @@ template <is_void_ptr ValuePtrType>
166199
class val<ValuePtrType> : public base_ptr_val<ValuePtrType> {
167200
public:
168201
using base_ptr_val<ValuePtrType>::base_ptr_val;
202+
203+
#ifdef ENABLE_TRACING
204+
val(const val<ValuePtrType>& otherValue) : base_ptr_val<ValuePtrType>(otherValue.value, tracing::traceCopy(otherValue.state)) {
205+
}
206+
#else
207+
val(const val<ValuePtrType>& otherValue) : base_ptr_val<ValuePtrType>(otherValue.value) {
208+
}
209+
#endif
210+
211+
val<ValuePtrType>& operator=(const val<ValuePtrType>& other) {
212+
#ifdef ENABLE_TRACING
213+
if (tracing::inTracer()) {
214+
tracing::traceAssignment(this->state, other.state, tracing::to_type<ValuePtrType>());
215+
}
216+
#endif
217+
this->value = other.value;
218+
return *this;
219+
};
220+
169221
template <typename OtherType>
170222
requires std::is_pointer_v<OtherType>
171223
operator val<OtherType>() const {
@@ -205,7 +257,7 @@ auto inline operator==(val<ValueType> left, val<ValueType> right) {
205257

206258
#ifdef ENABLE_TRACING
207259
if (tracing::inTracer()) {
208-
auto tc = tracing::traceBinaryOp<tracing::EQ, ValueType>(left.state, right.state);
260+
auto tc = tracing::traceBinaryOp<tracing::EQ, bool>(left.state, right.state);
209261
return val<bool>(tc);
210262
}
211263
#endif
@@ -231,7 +283,7 @@ template <typename ValueType>
231283
auto inline operator<=(val<ValueType> left, val<ValueType> right) {
232284
#ifdef ENABLE_TRACING
233285
if (tracing::inTracer()) {
234-
auto tc = tracing::traceBinaryOp<tracing::LTE, ValueType>(left.state, right.state);
286+
auto tc = tracing::traceBinaryOp<tracing::LTE, bool>(left.state, right.state);
235287
return val<bool>(tc);
236288
}
237289
#endif
@@ -243,7 +295,7 @@ template <typename ValueType>
243295
auto inline operator<(val<ValueType> left, val<ValueType> right) {
244296
#ifdef ENABLE_TRACING
245297
if (tracing::inTracer()) {
246-
auto tc = tracing::traceBinaryOp<tracing::LT, ValueType>(left.state, right.state);
298+
auto tc = tracing::traceBinaryOp<tracing::LT, bool>(left.state, right.state);
247299
return val<bool>(tc);
248300
}
249301
#endif
@@ -255,7 +307,7 @@ template <typename ValueType>
255307
auto inline operator>(val<ValueType> left, val<ValueType> right) {
256308
#ifdef ENABLE_TRACING
257309
if (tracing::inTracer()) {
258-
auto tc = tracing::traceBinaryOp<tracing::GT, ValueType>(left.state, right.state);
310+
auto tc = tracing::traceBinaryOp<tracing::GT, bool>(left.state, right.state);
259311
return val<bool>(tc);
260312
}
261313
#endif
@@ -267,7 +319,7 @@ template <typename ValueType>
267319
auto inline operator>=(val<ValueType> left, val<ValueType> right) {
268320
#ifdef ENABLE_TRACING
269321
if (tracing::inTracer()) {
270-
auto tc = tracing::traceBinaryOp<tracing::GTE, ValueType>(left.state, right.state);
322+
auto tc = tracing::traceBinaryOp<tracing::GTE, bool>(left.state, right.state);
271323
return val<bool>(tc);
272324
}
273325
#endif
@@ -279,7 +331,7 @@ template <typename ValueType>
279331
auto inline operator!=(val<ValueType> left, val<ValueType> right) {
280332
#ifdef ENABLE_TRACING
281333
if (tracing::inTracer()) {
282-
auto tc = tracing::traceBinaryOp<tracing::NEQ, ValueType>(left.state, right.state);
334+
auto tc = tracing::traceBinaryOp<tracing::NEQ, bool>(left.state, right.state);
283335
return val<bool>(tc);
284336
}
285337
#endif
@@ -308,7 +360,7 @@ class val<bool&> {
308360
using ptrType = ref_less_type*;
309361

310362
#ifdef ENABLE_TRACING
311-
tracing::value_ref state;
363+
tracing::TypedValueRefHolder state;
312364
val(bool ref) : state(tracing::value_ref()), ptr(&ref) {};
313365
val(bool& ref, tracing::value_ref value_ref) : state(value_ref), ptr(&ref) {};
314366
val(val<ptrType> ptr, tracing::value_ref ref) : state(ref), ptr(ptr) {};

nautilus/src/nautilus/compiler/JITCompiler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ JITCompiler::~JITCompiler() = default;
2828
std::unique_ptr<Executable> JITCompiler::compile(JITCompiler::wrapper_function function) const {
2929
// derive trace from function
3030
auto executionTrace = tracing::TraceContext::trace(function);
31+
std::cout << executionTrace->toString() << std::endl;
3132
// create ssa
3233
auto ssaCreationPhase = tracing::SSACreationPhase();
3334
auto afterSSA = ssaCreationPhase.apply(std::move(executionTrace));
35+
std::cout << afterSSA->toString() << std::endl;
3436
// get nautilus ir from trace
3537
auto irGenerationPhase = tracing::TraceToIRConversionPhase();
3638
auto ir = irGenerationPhase.apply(std::move(afterSSA));

nautilus/src/nautilus/tracing/phases/SSACreationPhase.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ Block& SSACreationPhase::SSACreationPhaseContext::getReturnBlock() {
2828
// add return block
2929
auto& returnBlock = trace->getBlock(trace->createBlock());
3030
returnBlock.operations.emplace_back(defaultReturnOp);
31-
3231
for (auto returnOp : returns) {
3332
auto& returnOpBlock = trace->getBlock(returnOp.blockIndex);
3433
auto returnValue = returnOpBlock.operations[returnOp.operationIndex];
35-
auto snap = Snapshot();
36-
returnOpBlock.operations[returnOp.operationIndex] = TraceOperation(snap, ASSIGN, defaultReturnOp.resultType, defaultReturnOp.resultRef, {returnValue.resultRef});
34+
if (returnValue.resultType == Type::v) {
35+
returnOpBlock.operations.erase(returnOpBlock.operations.cbegin() + returnOp.operationIndex);
36+
} else {
37+
auto snap = Snapshot();
38+
returnOpBlock.operations[returnOp.operationIndex] = TraceOperation(snap, ASSIGN, defaultReturnOp.resultType, defaultReturnOp.resultRef, {returnValue.resultRef});
39+
}
3740
returnOpBlock.addOperation({Op::JMP, std::vector<InputVariant> {BlockRef(returnBlock.blockId)}});
3841
returnBlock.predecessors.emplace_back(returnOp.blockIndex);
3942
}
@@ -189,6 +192,11 @@ void SSACreationPhase::SSACreationPhaseContext::removeAssignOperations() {
189192
if (foundAssignment != assignmentMap.end()) {
190193
operation.resultRef.ref = foundAssignment->second;
191194
}
195+
} else if (operation.op == Op::STORE) {
196+
auto foundAssignment = assignmentMap.find(operation.resultRef.ref);
197+
if (foundAssignment != assignmentMap.end()) {
198+
operation.resultRef.ref = foundAssignment->second;
199+
}
192200
} else {
193201
for (auto& input : operation.input) {
194202
if (auto* valueRef = std::get_if<value_ref>(&input)) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
B0($1:i32)
2+
CONST $2 42 :i32
3+
EQ $3 $1 $2 :bool
4+
CMP $4 $3 B1() B2() :void
5+
B1()
6+
CONST $5 1 :i32
7+
JMP $0 B3($5) :void
8+
B2()
9+
CONST $7 120 :i32
10+
JMP $0 B3($7) :void
11+
B3($5:i32)
12+
RETURN $5 :i32
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
B0($1:ptr)
2+
LOAD $4 $1 :i32
3+
CONST $5 10 :i32
4+
GT $6 $4 $5 :bool
5+
CMP $7 $6 B1($1) B2($1) :void
6+
B1($1:ptr)
7+
CONST $10 1 :i32
8+
STORE $1 $10 :i32
9+
JMP $0 B3() :void
10+
B2($1:ptr)
11+
CONST $14 42 :i32
12+
STORE $1 $14 :i32
13+
JMP $0 B3() :void
14+
B3()
15+
RETURN $0 :void
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
NESIR {
2+
execute() {
3+
Block_0($1:i32):
4+
$2 = 42 :i32
5+
$3 = $1 == $2 :bool
6+
if $3 ? Block_1() : Block_2() :void
7+
8+
Block_1():
9+
$5 = 1 :i32
10+
br Block_3($5) :void
11+
12+
Block_3($5:i32):
13+
return ($5) :i32
14+
15+
Block_2():
16+
$7 = 120 :i32
17+
br Block_3($7) :void
18+
}
19+
} //NESIR
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
NESIR {
2+
execute() {
3+
Block_0($1:ptr):
4+
$4 = load($1) :i32
5+
$5 = 10 :i32
6+
$6 = $4 > $5 :bool
7+
if $6 ? Block_1($1) : Block_2($1) :void
8+
9+
Block_1($1:ptr):
10+
$10 = 1 :i32
11+
store($10, $1) :void
12+
br Block_3() :void
13+
14+
Block_3():
15+
return :void
16+
17+
Block_2($1:ptr):
18+
$14 = 42 :i32
19+
store($14, $1) :void
20+
br Block_3() :void
21+
}
22+
} //NESIR
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
B0($1:i32)
2+
CONST $2 42 :i32
3+
EQ $3 $1 $2 :bool
4+
CMP $4 $3 B1() B2() :void
5+
B1()
6+
CONST $5 1 :i32
7+
RETURN $5 :i32
8+
B2()
9+
CONST $7 120 :i32
10+
RETURN $7 :i32
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
B0($1:ptr)
2+
ASSIGN $2 $1 :ptr
3+
ASSIGN $3 $2 :ptr
4+
LOAD $4 $3 :i32
5+
CONST $5 10 :i32
6+
GT $6 $4 $5 :bool
7+
CMP $7 $6 B1() B2() :void
8+
B1()
9+
ASSIGN $8 $1 :ptr
10+
ASSIGN $9 $8 :ptr
11+
CONST $10 1 :i32
12+
STORE $1 $10 :i32
13+
RETURN $0 :void
14+
B2()
15+
ASSIGN $12 $1 :ptr
16+
ASSIGN $13 $12 :ptr
17+
CONST $14 42 :i32
18+
STORE $1 $14 :i32
19+
RETURN $0 :void
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
B0($1:ptr,$2:ptr)
22
CONST $3 0 :ui64
3-
CALL $6 ($2,$1,$3) :ptr
3+
CALL $10 ($2,$1,$3) :ptr
44
RETURN $0 :void
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
B0($1:ptr)
2-
CALL $2 ($1) :i32
3-
RETURN $2 :i32
2+
CALL $4 ($1) :i32
3+
RETURN $4 :i32
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
B0($1:ptr)
22
CONST $2 0 :i32
3-
CONST $5 2 :i32
4-
MUL $6 $2 $5 :i32
5-
ADD $7 $1 $6 :ptr
6-
LOAD $8 $7 :i16
7-
RETURN $8 :i16
3+
CONST $6 2 :i32
4+
MUL $7 $2 $6 :i32
5+
ADD $8 $1 $7 :ptr
6+
LOAD $11 $8 :i16
7+
RETURN $11 :i16
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
B0($1:ptr)
22
CONST $2 0 :i32
3-
CONST $5 4 :i32
4-
MUL $6 $2 $5 :i32
5-
ADD $7 $1 $6 :ptr
6-
LOAD $8 $7 :i32
7-
RETURN $8 :i32
3+
CONST $6 4 :i32
4+
MUL $7 $2 $6 :i32
5+
ADD $8 $1 $7 :ptr
6+
LOAD $11 $8 :i32
7+
RETURN $11 :i32
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
B0($1:ptr)
2-
CONST $2 :ptr
3-
EQ $3 $1 $2 :ptr
4-
RETURN $3 :bool
2+
CONST $3 :ptr
3+
EQ $6 $1 $3 :bool
4+
RETURN $6 :bool
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
B0($1:ptr,$2:i32)
2-
CONST $6 4 :i32
3-
MUL $7 $2 $6 :i32
4-
ADD $8 $1 $7 :ptr
5-
LOAD $9 $8 :i32
6-
RETURN $9 :i32
2+
CONST $7 4 :i32
3+
MUL $8 $2 $7 :i32
4+
ADD $9 $1 $8 :ptr
5+
LOAD $12 $9 :i32
6+
RETURN $12 :i32
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
B0()
22
CONST $1 :ptr
3-
LOAD $2 $1 :i32
4-
RETURN $2 :i32
3+
LOAD $4 $1 :i32
4+
RETURN $4 :i32
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
B0($1:ptr)
2-
CALL $2 ($1) :i32
3-
RETURN $2 :i32
2+
CALL $4 ($1) :i32
3+
RETURN $4 :i32

0 commit comments

Comments
 (0)