aboutsummaryrefslogtreecommitdiff
path: root/offload/test/libc/rpc_callback.c
blob: 223b54eddd81a2a6931cf22d16462ea64721db06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// RUN: %libomptarget-compilexx-run-and-check-generic

// REQUIRES: libc
// REQUIRES: gpu

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

// CHECK: PASS

// This should be present in-tree relative to the test directory. If someone is
// using a partial tree just pass the test.
#if !__has_include(<../../libc/shared/rpc.h>)
int main() { printf("PASS\n"); }
#else
#include <../../libc/shared/rpc.h>

extern "C" void __tgt_register_rpc_callback(unsigned (*Callback)(void *,
                                                                 unsigned));
constexpr uint32_t RPC_TEST_OPCODE = 1;

template<uint32_t NumLanes> rpc::Status handleOpcodes(rpc::Server::Port &Port) {
  switch (Port.get_opcode()) {
  case RPC_TEST_OPCODE: {
    Port.recv(
        [&](rpc::Buffer *Buffer, uint32_t) { assert(Buffer->data[0] == 42); });
    Port.send([&](rpc::Buffer *, uint32_t) {});
    break;
  }
  default:
    return rpc::RPC_UNHANDLED_OPCODE;
    break;
  }
  return rpc::RPC_SUCCESS;
}

static uint32_t handleOffloadOpcodes(void *Raw, uint32_t NumLanes) {
  rpc::Server::Port &Port = *reinterpret_cast<rpc::Server::Port *>(Raw);
  if (NumLanes == 1)
    return handleOpcodes<1>(Port);
  else if (NumLanes == 32)
    return handleOpcodes<32>(Port);
  else if (NumLanes == 64)
    return handleOpcodes<64>(Port);
  else
    return rpc::RPC_ERROR;
}

[[gnu::weak]] rpc::Client client asm("__llvm_rpc_client");
#pragma omp declare target to(client) device_type(nohost)

void __tgt_register_rpc_callback(unsigned (*Callback)(void *, unsigned));

int main() {
  __tgt_register_rpc_callback(&handleOffloadOpcodes);
#pragma omp target
  {
    rpc::Client::Port Port = client.open<RPC_TEST_OPCODE>();
    Port.send([=](rpc::Buffer *buffer, uint32_t) { buffer->data[0] = 42; });
    Port.recv([](rpc::Buffer *, uint32_t) {});
    Port.close();
  }
  printf("PASS\n");
}
#endif