Skip to content

Commit

Permalink
Fix wrong mangled symbol (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: lukas schwerdtfeger <lukas.schwerdtfeger@gmail.com>
  • Loading branch information
PhilippGrulich and ls-1801 authored Sep 15, 2024
1 parent ae705b6 commit cca6048
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion nautilus/include/nautilus/common/traceing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void traceValueDestruction(value_ref ref);

value_ref traceCast(value_ref state, Type resultType);

value_ref traceCall(void* fptn, const std::type_info& ti, Type resultType, const std::vector<tracing::value_ref>& arguments);
value_ref traceCall(void* fptn, Type resultType, const std::vector<tracing::value_ref>& arguments);

std::ostream& operator<<(std::ostream& os, const Op& operation);

Expand Down
6 changes: 2 additions & 4 deletions nautilus/include/nautilus/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class CallableRuntimeFunction {
auto operator()(FunctionArgumentsRaw&&... args) {
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
const std::type_info &ti = typeid(fnptr);
auto functionArgumentReferences = getArgumentReferences(std::forward<FunctionArgumentsRaw>(args)...);
auto resultRef = tracing::traceCall(reinterpret_cast<void*>(fnptr), ti, tracing::to_type<R>(), functionArgumentReferences);
auto resultRef = tracing::traceCall(reinterpret_cast<void*>(fnptr), tracing::to_type<R>(), functionArgumentReferences);
return val<R>(resultRef);
}
#endif
Expand All @@ -45,9 +44,8 @@ class CallableRuntimeFunction {
void operator()(FunctionArgumentsRaw&&... args) {
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
const std::type_info &ti = typeid(fnptr);
auto functionArgumentReferences = getArgumentReferences(std::forward<FunctionArgumentsRaw>(args)...);
tracing::traceCall(reinterpret_cast<void*>(fnptr), ti, Type::v, functionArgumentReferences);
tracing::traceCall(reinterpret_cast<void*>(fnptr), Type::v, functionArgumentReferences);
return;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ void CPPLoweringProvider::LoweringContext::process(ir::ProxyCallOperation* opt,
argTypes << getType(arg->getStamp());
}
if (!functionNames.contains(opt->getFunctionSymbol())) {
functions << "auto " << opt->getFunctionSymbol() << " = "
functions << "auto f_" << opt->getFunctionSymbol() << " = "
<< "(" << returnType << "(*)(" << argTypes.str() << "))" << opt->getFunctionPtr() << ";\n";
functionNames.emplace(opt->getFunctionSymbol());
}
Expand All @@ -383,7 +383,7 @@ void CPPLoweringProvider::LoweringContext::process(ir::ProxyCallOperation* opt,
frame.setValue(opt->getIdentifier(), resultVar);
blocks[blockIndex] << resultVar << " = ";
}
blocks[blockIndex] << opt->getFunctionSymbol() << "(" << args.str() << ");\n";
blocks[blockIndex] << "f_" << opt->getFunctionSymbol() << "(" << args.str() << ");\n";
}

void CPPLoweringProvider::LoweringContext::process(ir::NegateOperation* negateOperation, short blockIndex, RegisterFrame& frame) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::function<llvm::Error(llvm::Module*)> LLVMIROptimizer::getLLVMOptimizerPipel
auto optPipeline = ::mlir::makeOptimizingTransformer(getOptimizationLevel(options), SIZE_LEVEL, targetMachinePtr);
auto optimizedModule = optPipeline(llvmIRModule);

handler.dump("llvm", "ll", [&]() {
handler.dump("after_llvm_generation", "ll", [&]() {
std::string llvmIRString;
llvm::raw_string_ostream llvmStringStream(llvmIRString);
llvmIRModule->print(llvmStringStream, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,7 @@ void MLIRLoweringProvider::generateMLIR(ir::FunctionOperation* functionOp, Value
}

// Generate MLIR for operations in function body (BasicBlock).
generateMLIR(functionOp->

getFunctionBasicBlock(),
frame

);
generateMLIR(functionOp->getFunctionBasicBlock(), frame);

theModule.push_back(mlirFunction);
}
Expand Down
12 changes: 6 additions & 6 deletions nautilus/src/nautilus/tracing/TracingUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
#include <sstream>
namespace nautilus::tracing {

std::array<const char*, 4> lookup = {{"The demangling operation succeeded", "A memory allocation failure occurred", "mangled_name is not a valid name under the C++ ABI mangling rules", "One of the arguments is invalid"}};

std::string getMangledName(void* fnptr, const std::type_info& ti) {
std::string getMangledName(void* fnptr) {
Dl_info info;
dladdr(reinterpret_cast<void*>(fnptr), &info);
if (info.dli_sname != nullptr) {
return info.dli_sname;
}
return ti.name();
std::stringstream ss;
ss << fnptr;
return ss.str();
}

std::string getFunctionName(const std::string& mangledName) {
Expand Down Expand Up @@ -89,8 +89,8 @@ void freeValRef(ValueRef ref) {
return TraceContext::get() != nullptr;
}

value_ref traceCall(void* fptn, const std::type_info& ti, Type resultType, const std::vector<tracing::value_ref>& arguments) {
auto mangledName = getMangledName(fptn, ti);
value_ref traceCall(void* fptn, Type resultType, const std::vector<tracing::value_ref>& arguments) {
auto mangledName = getMangledName(fptn);
auto functionName = getFunctionName(mangledName);
return TraceContext::get()->traceCall(functionName, mangledName, fptn, resultType, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
B0($1:i32,$2:i32)
CALL $3 nautilus::engine::add(int, int)($1,$2) :i32
CALL $4 nautilus::engine::sub(int, int)($1,$2) :i32
ADD $5 $3 $4 :i32
RETURN $5 :i32
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
NESIR {
execute() {
Block_0($1:i32, $2:i32):
$3 = nautilus::engine::add(int, int)($1, $2) :i32
$4 = nautilus::engine::sub(int, int)($1, $2) :i32
$5 = $3 + $4 :i32
return ($5) :i32
}
} //NESIR
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
B0($1:i32,$2:i32)
CALL $3 nautilus::engine::add(int, int)($1,$2) :i32
CALL $4 nautilus::engine::sub(int, int)($1,$2) :i32
ADD $5 $3 $4 :i32
RETURN $5 :i32
7 changes: 7 additions & 0 deletions nautilus/test/execution-tests/ExecutionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,13 @@ void functionCallExecutionTest(engine::NautilusEngine& engine) {
REQUIRE(f(10, 10) == 20);
REQUIRE(f(0, 1) == 1);
}

SECTION("twoDistinctFunctionCalls") {
auto f = engine.registerFunction(callTwoFunctions);
REQUIRE(f(10, 10) == 20); // 10 + 10 + (10 - 10)
REQUIRE(f(0, 1) == 0); // (0 + 1) + (0 - 1)
}

SECTION("loopDirectCall") {
auto f = engine.registerFunction(loopDirectCall);
REQUIRE(f(10, 10) == 100);
Expand Down
8 changes: 8 additions & 0 deletions nautilus/test/execution-tests/RunctimeCallFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ int32_t add(int32_t x, int32_t y) {
return x + y;
}

int32_t sub(int32_t x, int32_t y) {
return x - y;
}

val<int32_t> simpleDirectCall(val<int32_t> x, val<int32_t> y) {
return invoke<>(add, x, y);
}

val<int32_t> callTwoFunctions(val<int32_t> x, val<int32_t> y) {
return invoke<>(add, x, y) + invoke<>(sub, x, y);
}

val<int32_t> loopDirectCall(val<int32_t> c, val<int32_t> x) {
val<int32_t> sum = 0;
for (val<int32_t> i = 0; i < c; i = i + 1) {
Expand Down
3 changes: 2 additions & 1 deletion nautilus/test/execution-tests/TracingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ TEST_CASE("Runtime Call Trace Test") {
{"lambdaRuntimeFunction", details::createFunctionWrapper(lambdaRuntimeFunction)},
{"nestedLambdaRuntimeFunction", details::createFunctionWrapper(nestedLambdaRuntimeFunction)},
{"callSameFunction", details::createFunctionWrapper(callSameFunction)},
{"voidFuncCall", details::createFunctionWrapper(voidFuncCall)}};
{"voidFuncCall", details::createFunctionWrapper(voidFuncCall)},
{"callTwoFunctions", details::createFunctionWrapper(callTwoFunctions)}};
runTraceTests("runtime-call-tests", tests);
}

Expand Down

0 comments on commit cca6048

Please sign in to comment.