Skip to content

Commit

Permalink
Fixed a tracing bug with operands less than 32-bit (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Nils Schubert <[email protected]>
Co-authored-by: Philipp Grulich <[email protected]>
  • Loading branch information
keyseven123 and PhilippGrulich authored Oct 8, 2024
1 parent 0d38c25 commit 5faf0bc
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 21 deletions.
30 changes: 15 additions & 15 deletions nautilus/include/nautilus/val.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ template <typename LHS>
LHS getRawValue(const val<LHS>& val);

#define COMMON_RETURN_TYPE \
val<typename std::common_type<typename std::remove_cvref_t<LHS>::basic_type, \
typename std::remove_cvref_t<RHS>::basic_type>::type>
typename std::common_type_t<typename std::remove_cvref_t<LHS>::basic_type, \
typename std::remove_cvref_t<RHS>::basic_type>

#define DEDUCT_RETURN_TYPE(OP) val<decltype(getRawValue(left) OP getRawValue(right))>
#define DEDUCT_RETURN_TYPE(OP) decltype(getRawValue(left) OP getRawValue(right))

template <typename T>
tracing::TypedValueRef getState(T&& value) {
Expand Down Expand Up @@ -245,17 +245,17 @@ namespace details {
#define DEFINE_BINARY_OPERATOR_HELPER(OP, OP_NAME, OP_TRACE, RES_TYPE) \
template <typename LHS, typename RHS> \
auto inline OP_NAME(LHS&& left, RHS&& right) { \
typedef typename std::common_type<typename std::remove_reference_t<LHS>::basic_type, \
typename std::remove_reference_t<RHS>::basic_type>::type commonType; \
typedef std::common_type_t<typename std::remove_reference_t<LHS>::basic_type, \
typename std::remove_reference_t<RHS>::basic_type> \
commonType; \
auto&& lValue = cast_value<LHS, commonType>(std::forward<LHS>(left)); \
auto&& rValue = cast_value<RHS, commonType>(std::forward<RHS>(right)); \
using resultType = decltype(getRawValue(lValue) OP getRawValue(rValue)); \
if SHOULD_TRACE () { \
auto tc = tracing::traceBinaryOp(tracing::OP_TRACE, tracing::to_type<resultType>(), \
auto tc = tracing::traceBinaryOp(tracing::OP_TRACE, tracing::to_type<RES_TYPE>(), \
details::getState(lValue), details::getState(rValue)); \
return RES_TYPE(tc); \
return val<RES_TYPE>(tc); \
} \
return RES_TYPE(getRawValue(lValue) OP getRawValue(rValue)); \
return val<RES_TYPE>(getRawValue(lValue) OP getRawValue(rValue)); \
}

DEFINE_BINARY_OPERATOR_HELPER(+, add, ADD, COMMON_RETURN_TYPE)
Expand All @@ -268,17 +268,17 @@ DEFINE_BINARY_OPERATOR_HELPER(/, div, DIV, COMMON_RETURN_TYPE)

DEFINE_BINARY_OPERATOR_HELPER(%, mod, MOD, COMMON_RETURN_TYPE)

DEFINE_BINARY_OPERATOR_HELPER(==, eq, EQ, val<bool>)
DEFINE_BINARY_OPERATOR_HELPER(==, eq, EQ, bool)

DEFINE_BINARY_OPERATOR_HELPER(!=, neq, NEQ, val<bool>)
DEFINE_BINARY_OPERATOR_HELPER(!=, neq, NEQ, bool)

DEFINE_BINARY_OPERATOR_HELPER(<, lt, LT, val<bool>)
DEFINE_BINARY_OPERATOR_HELPER(<, lt, LT, bool)

DEFINE_BINARY_OPERATOR_HELPER(<=, lte, LTE, val<bool>)
DEFINE_BINARY_OPERATOR_HELPER(<=, lte, LTE, bool)

DEFINE_BINARY_OPERATOR_HELPER(>, gt, GT, val<bool>)
DEFINE_BINARY_OPERATOR_HELPER(>, gt, GT, bool)

DEFINE_BINARY_OPERATOR_HELPER(>=, gte, GTE, val<bool>)
DEFINE_BINARY_OPERATOR_HELPER(>=, gte, GTE, bool)

DEFINE_BINARY_OPERATOR_HELPER(>>, shr, RSH, DEDUCT_RETURN_TYPE(>>))

Expand Down
8 changes: 8 additions & 0 deletions nautilus/test/common/ExpressionFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ val<double> doubleAddExpression(val<double> x) {
return x + y;
}

auto subInt8AndInt8(val<int8_t> x, val<int8_t> y) {
const auto result = x - y;
return result;
}

auto addInt8AndInt32(val<int8_t> x, val<int32_t> y) {
const auto result = x + y;
return result;
}

val<double> castFloatToDoubleAddExpression(val<float> x) {
val<double> y = 7.0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
B0($1:i8,$2:i32)
CAST $3 $1 :i32
ADD $4 $3 $2 :i32
RETURN $0 $4 :i32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:i16)
CONST $2 5 :i16
ADD $3 $1 $2 :i32
ADD $3 $1 $2 :i16
RETURN $0 $3 :i16
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:i8)
CONST $2 2 :i8
ADD $3 $2 $1 :i32
ADD $3 $2 $1 :i8
RETURN $0 $3 :i8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
B0($1:i8,$2:i8)
SUB $3 $1 $2 :i8
RETURN $0 $3 :i8
8 changes: 8 additions & 0 deletions nautilus/test/data/expression-tests/ir/addInt8AndInt32.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NautilusIr {
execute() {
Block_0($1:i8, $2:i32):
$3 = $1 cast_to i32 :i32
$4 = $3 + $2 :i32
return ($4) :i32
}
} //NESIR
7 changes: 7 additions & 0 deletions nautilus/test/data/expression-tests/ir/subInt8AndInt8.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NautilusIr {
execute() {
Block_0($1:i8, $2:i8):
$3 = $1 - $2 :i8
return ($3) :i8
}
} //NESIR
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
B0($1:i8,$2:i32)
CAST $3 $1 :i32
ADD $4 $3 $2 :i32
RETURN $0 $4 :i32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:i16)
CONST $2 5 :i16
ADD $3 $1 $2 :i32
ADD $3 $1 $2 :i16
RETURN $0 $3 :i16
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:i8)
CONST $2 2 :i8
ADD $3 $2 $1 :i32
ADD $3 $2 $1 :i8
RETURN $0 $3 :i8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
B0($1:i8,$2:i8)
SUB $3 $1 $2 :i8
RETURN $0 $3 :i8
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ B1($3:i8,$4:i8,$1:ptr,$2:ptr)
MUL $24 $23 $22 :ui64
ADD $25 $2 $24 :ptr
LOAD $28 $25 :i8
ADD $29 $17 $28 :i32
ADD $29 $17 $28 :i8
CAST $30 $4 :i32
CONST $34 1 :ui64
CAST $35 $30 :ui64
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/pointer-tests/tracing/addArray_i8.trace
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ B1()
ASSIGN $26 $25 :ptr
ASSIGN $27 $26 :ptr
LOAD $28 $27 :i8
ADD $29 $17 $28 :i32
ADD $29 $17 $28 :i8
CAST $30 $4 :i32
ASSIGN $31 $1 :ptr
ASSIGN $32 $30 :i32
Expand Down
16 changes: 16 additions & 0 deletions nautilus/test/execution-tests/ExecutionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ void expressionTests(engine::NautilusEngine& engine) {
REQUIRE(f((double) -7) == 0);
REQUIRE(f((double) -14) == -7);
}

SECTION("subInt8AndInt8") {
auto f = engine.registerFunction(subInt8AndInt8);
REQUIRE(f((int8_t) 1, (int8_t) 2) == -1);
REQUIRE(f((int8_t) 123, (int8_t) 123) == 0);
REQUIRE(f((int8_t) 78, (int8_t) 70) == 8);
REQUIRE(f((int8_t) -128, (int8_t) 1) == 127);
}

SECTION("addInt8AndInt32") {
auto f = engine.registerFunction(addInt8AndInt32);
REQUIRE(f((int8_t) 1, (int32_t) 2) == 3);
REQUIRE(f((int8_t) 123, (int32_t) 123) == 246);
REQUIRE(f((int8_t) 78, (int32_t) 70) == 148);
REQUIRE(f((int8_t) -128, (int32_t) -1) == -129);
}
}

void controlFlowTest(engine::NautilusEngine& engine) {
Expand Down
2 changes: 2 additions & 0 deletions nautilus/test/execution-tests/TracingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ TEST_CASE("Expression Trace Test") {
{"shiftRight_i8", details::createFunctionWrapper(shiftRight<int8_t>)},
{"negate_i8", details::createFunctionWrapper(negate<int8_t>)},
{"logicalNot_bool", details::createFunctionWrapper(logicalNot<bool>)},
{"subInt8AndInt8", details::createFunctionWrapper(subInt8AndInt8)},
{"addInt8AndInt32", details::createFunctionWrapper(addInt8AndInt32)},
};
runTraceTests("expression-tests", tests);
}
Expand Down

0 comments on commit 5faf0bc

Please sign in to comment.