Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export symbol from DLL in test_jit #2861

Merged
merged 18 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/dynamic_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <migraphx/errors.hpp>
#include <migraphx/file_buffer.hpp>
#include <migraphx/tmp_dir.hpp>
#include <migraphx/stringutils.hpp>
#include <utility>

#ifdef _WIN32
Expand Down Expand Up @@ -182,17 +183,17 @@ dynamic_loader::dynamic_loader(const std::vector<char>& buffer)
{
}

std::shared_ptr<void> dynamic_loader::get_symbol(const std::string& name) const
std::shared_ptr<void> dynamic_loader::get_symbol(std::string_view name) const
{
#ifndef _WIN32
// flush any previous error messages
check_load_error(true);
void* symbol = dlsym(impl->handle.get(), name.c_str());
void* symbol = dlsym(impl->handle.get(), name.data());
if(symbol == nullptr)
check_load_error();
return {impl, symbol};
#else
FARPROC addr = GetProcAddress(impl->handle, name.c_str());
FARPROC addr = GetProcAddress(impl->handle, name.data());
if(addr == nullptr)
MIGRAPHX_THROW("Symbol not found: " + name + " (" + std::to_string(GetLastError()) + ")");
return {impl, reinterpret_cast<void*>(addr)};
Expand Down
6 changes: 3 additions & 3 deletions src/include/migraphx/dynamic_loader.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -54,10 +54,10 @@ struct MIGRAPHX_EXPORT dynamic_loader

dynamic_loader(const std::vector<char>& buffer);

std::shared_ptr<void> get_symbol(const std::string& name) const;
std::shared_ptr<void> get_symbol(std::string_view name) const;

template <class F>
std::function<F> get_function(const std::string& name) const
std::function<F> get_function(std::string_view name) const
{
auto s = get_symbol(name);
return [=](auto&&... xs) -> decltype(auto) {
Expand Down
6 changes: 6 additions & 0 deletions src/include/migraphx/stringutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sstream>
#include <unordered_map>
#include <vector>
#include <string_view>
#include <migraphx/config.hpp>

namespace migraphx {
Expand All @@ -38,6 +39,11 @@ inline namespace MIGRAPHX_INLINE_NS {
#define MIGRAPHX_STRINGIZE_1(...) #__VA_ARGS__
#define MIGRAPHX_STRINGIZE(...) MIGRAPHX_STRINGIZE_1(__VA_ARGS__)

inline std::string operator+(std::string_view left, std::string_view right)
{
return std::string{left}.append(right);
}

template <class F>
auto with_char(F f)
{
Expand Down
14 changes: 10 additions & 4 deletions test/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
#include <migraphx/make_op.hpp>
#include <test.hpp>

#ifdef _WIN32
#define EXPORT_SYMBOL R"migraphx(__declspec(dllexport) )migraphx"
#else
#define EXPORT_SYMBOL

Check warning on line 35 in test/jit.cpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for macro definition 'EXPORT_SYMBOL' [readability-identifier-naming,-warnings-as-errors]
#endif

// NOLINTNEXTLINE
const std::string_view add_42_src = R"migraphx(
const std::string_view add_42_src = EXPORT_SYMBOL R"migraphx(
Copy link
Collaborator

@pfultz2 pfultz2 Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, I would add a -DEXPORT=... define when compiling the test(they all use the compile_function so can be added there. And then in the string you can just do EXPORT extern.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

extern "C" int add(int x)
{
return x+42;
Expand All @@ -54,17 +60,17 @@
compiler.output = migraphx::make_shared_object_filename("simple");
migraphx::src_file f{"main.cpp", src};
auto image = compiler.compile({f});
return migraphx::dynamic_loader{image}.get_function<F>(std::string{symbol_name});
return migraphx::dynamic_loader{image}.get_function<F>(symbol_name);
}

template <class F>
std::function<F> compile_module(const migraphx::module& m)
{
migraphx::cpp_generator g;
g.fmap([](auto&& name) { return "std::" + name; });
g.create_function(g.generate_module(m).set_attributes({"extern \"C\""}));
g.create_function(g.generate_module(m).set_attributes({EXPORT_SYMBOL "extern \"C\""}));

return compile_function<F>(preamble.data() + g.str(), m.name());
return compile_function<F>(g.str().insert(0, preamble), m.name());
}

TEST_CASE(simple_run)
Expand Down
Loading