Skip to content

Commit 2183d13

Browse files
committed
[Offload] Add olGetGlobalVariable
Looks up a global variable as a symbol
1 parent 466357a commit 2183d13

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

offload/liboffload/API/Symbol.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,20 @@ def : Enum {
1515
let desc = "The kind of a symbol";
1616
let etors =[
1717
Etor<"KERNEL", "a kernel object">,
18+
Etor<"GLOBAL_VARIABLE", "a global variable">,
1819
];
1920
}
21+
22+
def : Function {
23+
let name = "olGetGlobalVariable";
24+
let desc = "Get a global variable named `GlobalName` in the given program.";
25+
let details = [
26+
"Symbol handles are owned by the program, so do not need to be destroyed."
27+
];
28+
let params = [
29+
Param<"ol_program_handle_t", "Program", "handle of the program", PARAM_IN>,
30+
Param<"const char*", "GlobalName", "name of the global variable in the program", PARAM_IN>,
31+
Param<"ol_symbol_handle_t*", "Global", "output pointer for the fetched global", PARAM_OUT>
32+
];
33+
let returns = [];
34+
}

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ struct ol_program_impl_t {
9191
struct ol_symbol_impl_t {
9292
ol_symbol_impl_t(GenericKernelTy *Kernel)
9393
: PluginImpl(Kernel), Kind(OL_SYMBOL_KIND_KERNEL) {}
94-
std::variant<GenericKernelTy *> PluginImpl;
94+
ol_symbol_impl_t(GlobalTy &&Global)
95+
: PluginImpl(Global), Kind(OL_SYMBOL_KIND_GLOBAL_VARIABLE) {}
96+
std::variant<GenericKernelTy *, GlobalTy> PluginImpl;
9597
ol_symbol_kind_t Kind;
9698
};
9799

@@ -726,5 +728,23 @@ Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
726728
return Error::success();
727729
}
728730

731+
Error olGetGlobalVariable_impl(ol_program_handle_t Program,
732+
const char *GlobalName,
733+
ol_symbol_handle_t *Global) {
734+
auto &Device = Program->Image->getDevice();
735+
736+
GlobalTy GlobalObj{GlobalName};
737+
if (auto Res = Device.Plugin.getGlobalHandler().getGlobalMetadataFromDevice(
738+
Device, *Program->Image, GlobalObj))
739+
return Res;
740+
741+
*Global = Program->Symbols
742+
.emplace_back(
743+
std::make_unique<ol_symbol_impl_t>(std::move(GlobalObj)))
744+
.get();
745+
746+
return Error::success();
747+
}
748+
729749
} // namespace offload
730750
} // namespace llvm

offload/unittests/OffloadAPI/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ add_offload_unittest("queue"
4141
queue/olDestroyQueue.cpp
4242
queue/olGetQueueInfo.cpp
4343
queue/olGetQueueInfoSize.cpp)
44+
45+
add_offload_unittest("symbol"
46+
symbol/olGetGlobalVariable.cpp)

offload/unittests/OffloadAPI/device_code/global.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <gpuintrin.h>
22
#include <stdint.h>
33

4+
[[gnu::visibility("default")]]
45
uint32_t global[64];
56

67
__gpu_kernel void write() {

offload/unittests/OffloadAPI/kernel/olLaunchKernel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ TEST_P(olLaunchKernelGlobalTest, Success) {
223223
ASSERT_SUCCESS(olMemFree(Mem));
224224
}
225225

226+
TEST_P(olLaunchKernelGlobalTest, InvalidNotAKernel) {
227+
ol_symbol_handle_t Global = nullptr;
228+
ASSERT_SUCCESS(olGetGlobalVariable(Program, "global", &Global));
229+
ASSERT_ERROR(
230+
OL_ERRC_SYMBOL_KIND,
231+
olLaunchKernel(Queue, Device, Global, nullptr, 0, &LaunchArgs, nullptr));
232+
}
233+
226234
TEST_P(olLaunchKernelGlobalCtorTest, Success) {
227235
void *Mem;
228236
ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===------- Offload API tests - olGetGlobalVariable ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "../common/Fixtures.hpp"
10+
#include <OffloadAPI.h>
11+
#include <gtest/gtest.h>
12+
13+
struct olGetGlobalVariableTest : OffloadQueueTest {
14+
void SetUp() override {
15+
RETURN_ON_FATAL_FAILURE(OffloadQueueTest::SetUp());
16+
ASSERT_TRUE(TestEnvironment::loadDeviceBinary("global", Device, DeviceBin));
17+
ASSERT_GE(DeviceBin->getBufferSize(), 0lu);
18+
ASSERT_SUCCESS(olCreateProgram(Device, DeviceBin->getBufferStart(),
19+
DeviceBin->getBufferSize(), &Program));
20+
}
21+
22+
void TearDown() override {
23+
if (Program) {
24+
olDestroyProgram(Program);
25+
}
26+
RETURN_ON_FATAL_FAILURE(OffloadQueueTest::TearDown());
27+
}
28+
29+
std::unique_ptr<llvm::MemoryBuffer> DeviceBin;
30+
ol_program_handle_t Program = nullptr;
31+
ol_kernel_launch_size_args_t LaunchArgs{};
32+
};
33+
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olGetGlobalVariableTest);
34+
35+
TEST_P(olGetGlobalVariableTest, Success) {
36+
ol_symbol_handle_t Global = nullptr;
37+
ASSERT_SUCCESS(olGetGlobalVariable(Program, "global", &Global));
38+
ASSERT_NE(Global, nullptr);
39+
}
40+
41+
TEST_P(olGetGlobalVariableTest, InvalidNullProgram) {
42+
ol_symbol_handle_t Global = nullptr;
43+
ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE,
44+
olGetGlobalVariable(nullptr, "global", &Global));
45+
}
46+
47+
TEST_P(olGetGlobalVariableTest, InvalidNullGlobalPointer) {
48+
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,
49+
olGetGlobalVariable(Program, "global", nullptr));
50+
}
51+
52+
TEST_P(olGetGlobalVariableTest, InvalidGlobalName) {
53+
ol_symbol_handle_t Global = nullptr;
54+
ASSERT_ERROR(OL_ERRC_NOT_FOUND,
55+
olGetGlobalVariable(Program, "invalid_global", &Global));
56+
}

0 commit comments

Comments
 (0)